Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooMultiCatIter.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooMultiCatIter.cxx
19 \class RooMultiCatIter
20 \ingroup Roofitcore
21 
22 RooMultiCatIter iterators over all state permutations of a list of categories.
23 It serves as the state iterator for a RooSuperCategory or a RooMultiCategory.
24 Since this iterator only constructs state labels and does not change the value
25 of its input categories, it is not required that its inputs are LValues.
26 For cases where all inputs are LValues (such as for RooSuperCategory) the
27 values of the input can be changes by assigning the super category the
28 string label generated by this iterator
29 **/
30 
31 #include "RooFit.h"
32 
33 #include "RooAbsCategoryLValue.h"
34 #include "RooAbsCategoryLValue.h"
35 #include "RooMultiCatIter.h"
36 
37 using namespace std;
38 
39 ClassImp(RooMultiCatIter);
40 ;
41 
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Construct iterator over all permutations of states of categories in catList.
46 /// If rangeName is not null, iteration is restricted to states that are selected
47 /// in the given range name
48 
49 RooMultiCatIter::RooMultiCatIter(const RooArgSet& catList, const char* rangeName) : _catList("catList")
50 {
51  if (rangeName) {
52  _rangeName = rangeName ;
53  }
54  initialize(catList) ;
55 }
56 
57 
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Copy constructor
61 
62 RooMultiCatIter::RooMultiCatIter(const RooMultiCatIter& other) : TIterator(other), _catList("catList")
63 {
64  initialize(other._catList) ;
65 }
66 
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Build iterator array for given catList
71 
72 void RooMultiCatIter::initialize(const RooArgSet& catList)
73 {
74  // Copy RooCategory list into internal argset
75  TIterator* catIter = catList.createIterator() ;
76  TObject* obj ;
77  while ((obj = catIter->Next())) {
78  _catList.add((RooAbsArg&)*obj) ;
79  }
80  delete catIter ;
81 
82  // Allocate storage for component iterators
83  _nIter = catList.getSize() ;
84  _iterList = new pTIterator[_nIter] ;
85  _catPtrList = new pRooCategory[_nIter] ;
86  _curTypeList = new RooCatType[_nIter] ;
87 
88  // Construct component iterators
89  _curIter = 0 ;
90  _curItem = 0 ;
91  TIterator* cIter = _catList.createIterator() ;
92  RooAbsCategoryLValue* cat ;
93  while((cat=(RooAbsCategoryLValue*)cIter->Next())) {
94  _catPtrList[_curIter] = cat ;
95  _iterList[_curIter] = cat->typeIterator() ;
96  _iterList[_curIter]->Next() ;
97 // _curTypeList[_curIter] = *first ;
98 // _curTypeList[_curIter].SetName(first->GetName()) ;
99 // cout << "init: _curTypeList[" << _curIter << "] set to " << first->GetName() << endl ;
100 // _iterList[_curIter]->Reset() ;
101  _curIter++ ;
102  }
103  delete cIter ;
104 
105  Reset() ;
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Destructor
112 
113 RooMultiCatIter::~RooMultiCatIter()
114 {
115  for (_curIter=0 ; _curIter<_nIter ; _curIter++) {
116  delete _iterList[_curIter] ;
117  }
118  delete[] _iterList ;
119  delete[] _catPtrList ;
120  delete[] _curTypeList ;
121 }
122 
123 
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Dummy implementation, always returns zero
127 
128 const TCollection* RooMultiCatIter::GetCollection() const
129 {
130  //return &_catList.getCollection() ;
131  return 0 ;
132 }
133 
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Construct string with composite object
138 /// label corresponding to the state name
139 /// of a RooMultiCategory or RooSuperCategory
140 /// constructed from this set of input categories
141 
142 TObjString* RooMultiCatIter::compositeLabel()
143 {
144  TString& str = _compositeLabel.String() ;
145 
146  str = "{" ;
147  Int_t i ;
148  for (i=0 ; i<_nIter ; i++) {
149  if (i>0) str.Append(";") ;
150  str.Append(_curTypeList[i].GetName()) ;
151  }
152  str.Append("}") ;
153 
154  return &_compositeLabel ;
155 }
156 
157 
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Iterator increment operator
161 
162 TObject* RooMultiCatIter::Next()
163 {
164  // Check for end
165  if (_curIter==_nIter) {
166  _curItem = 0;
167  return 0 ;
168  }
169 
170  RooCatType* next = (RooCatType*) _iterList[_curIter]->Next() ;
171  if (next) {
172 
173  // Increment current iterator
174  _curTypeList[_curIter] = *next ;
175  _curTypeList[_curIter].SetName(next->GetName()) ;
176 
177  // If higher order increment was successful, reset master iterator
178  if (_curIter>0) _curIter=0 ;
179 
180  _curItem = compositeLabel() ;
181  return _curItem ;
182  } else {
183 
184  // Reset current iterator
185  _iterList[_curIter]->Reset() ;
186  next = (RooCatType*) _iterList[_curIter]->Next() ;
187  if (next) {
188  _curTypeList[_curIter] = *next ;
189  _curTypeList[_curIter].SetName(next->GetName()) ;
190  }
191  //if (next) _catPtrList[_curIter]->setIndex(next->getVal()) ;
192 
193  // Increment next iterator
194  _curIter++ ;
195  _curItem = Next() ;
196  return _curItem ;
197  }
198 }
199 
200 
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Rewind master iterator
204 
205 void RooMultiCatIter::Reset()
206 {
207  for (_curIter=0 ; _curIter<_nIter ; _curIter++) {
208  TIterator* cIter = _iterList[_curIter] ;
209  cIter->Reset() ;
210  RooCatType* first = (RooCatType*) cIter->Next() ;
211  if (first) {
212  if (_curIter==0) cIter->Reset() ;
213  _curTypeList[_curIter] = *first ;
214  _curTypeList[_curIter].SetName(first->GetName()) ;
215  }
216  }
217  _curIter=0 ;
218 }
219 
220 
221 ////////////////////////////////////////////////////////////////////////////////
222 /// Return current item (dummy)
223 
224 TObject *RooMultiCatIter::operator*() const
225 {
226  return _curItem ;
227 }
228 
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Comparison operator to other iterator
232 /// Returns true if both iterator iterate over the
233 /// same set of input categories and are not at the
234 /// same sequential position
235 
236 bool RooMultiCatIter::operator!=(const TIterator &aIter) const
237 {
238  if ((aIter.IsA() == RooMultiCatIter::Class())) {
239  const RooMultiCatIter &iter(dynamic_cast<const RooMultiCatIter &>(aIter));
240  return (_curItem != iter._curItem);
241  }
242 
243  return false;
244 }
245