Logo ROOT   6.10/09
Reference Guide
RooChangeTracker.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 RooChangeTracker.cxx
19 \class RooChangeTracker
20 \ingroup Roofitcore
21 
22 RooChangeTracker is a meta object that tracks value
23 changes in a given set of RooAbsArgs by registering itself as value
24 client of these objects. The change tracker can perform an
25 additional validation step where it also compares the numeric
26 values of the tracked arguments with reference values to ensure
27 that values have actually changed. This may be useful in case some
28 of the tracked observables are in binned datasets where each
29 observable propates a valueDirty flag when an event is loaded even
30 though usually only one observable actually changes.
31 **/
32 
33 
34 #include "RooFit.h"
35 
36 #include "Riostream.h"
37 #include <math.h>
38 
39 #include "RooChangeTracker.h"
40 #include "RooAbsReal.h"
41 #include "RooAbsCategory.h"
42 #include "RooArgSet.h"
43 #include "RooMsgService.h"
44 
45 using namespace std ;
46 
48 ;
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Default constructor
52 
54 {
57 }
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Constructor. The set trackSet contains the observables to be
63 /// tracked for changes. If checkValues is true an additional
64 /// validation step is activated where the numeric values of the
65 /// tracked arguments are compared with reference values ensuring
66 /// that values have actually changed.
67 
68 RooChangeTracker::RooChangeTracker(const char* name, const char* title, const RooArgSet& trackSet, Bool_t checkValues) :
69  RooAbsReal(name, title),
70  _realSet("realSet","Set of real-valued components to be tracked",this),
71  _catSet("catSet","Set of discrete-valued components to be tracked",this),
72  _realRef(trackSet.getSize()),
73  _catRef(trackSet.getSize()),
74  _checkVal(checkValues),
75  _init(kFALSE)
76 {
79 
80  TIterator* iter = trackSet.createIterator() ;
81  RooAbsArg* arg ;
82  while((arg=(RooAbsArg*)iter->Next())) {
83  if (dynamic_cast<RooAbsReal*>(arg)) {
84  _realSet.add(*arg) ;
85  }
86  if (dynamic_cast<RooAbsCategory*>(arg)) {
87  _catSet.add(*arg) ;
88  }
89  }
90  delete iter ;
91 
92  if (_checkVal) {
93  RooAbsReal* real ;
94  RooAbsCategory* cat ;
95  Int_t i(0) ;
96  _realSetIter->Reset() ;
97  _catSetIter->Reset() ;
98  while((real=(RooAbsReal*)_realSetIter->Next())) {
99  _realRef[i++] = real->getVal() ;
100  }
101  i=0 ;
102  while((cat=(RooAbsCategory*)_catSetIter->Next())) {
103  _catRef[i++] = cat->getIndex() ;
104  }
105  }
106 
107 }
108 
109 
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// Copy constructor
113 
115  RooAbsReal(other, name),
116  _realSet("realSet",this,other._realSet),
117  _catSet("catSet",this,other._catSet),
118  _realRef(other._realRef),
119  _catRef(other._catRef),
120  _checkVal(other._checkVal),
121  _init(kFALSE)
122 {
125 
126 // _realSet.add(other._realSet) ;
127 // _catSet.add(other._catSet) ;
128 
129 }
130 
131 
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Returns true if state has changes since last call with clearState=kTRUE
135 /// If clearState is true, changeState flag will be cleared.
136 
138 {
139 
140  // If dirty flag did not change, object has not changed in any case
141  if (!isValueDirty()) {
142  return kFALSE ;
143  }
144 
145  // If no value checking is required and dirty flag has changed, return true
146  if (!_checkVal) {
147 
148  if (clearState) {
149  // Clear dirty flag by calling getVal()
150  //cout << "RooChangeTracker(" << GetName() << ") clearing isValueDirty" << endl ;
151  clearValueDirty() ;
152  }
153 
154  //cout << "RooChangeTracker(" << GetName() << ") isValueDirty = kTRUE, returning kTRUE" << endl ;
155 
156  return kTRUE ;
157  }
158 
159  // Compare values against reference
160  _realSetIter->Reset() ;
161  _catSetIter->Reset() ;
162  RooAbsReal* real ;
163  RooAbsCategory* cat ;
164  Int_t i(0) ;
165 
166  if (clearState) {
167 
168  Bool_t valuesChanged(kFALSE) ;
169 
170  // Check if any of the real values changed
171  while ((real=(RooAbsReal*)_realSetIter->Next())) {
172  if (real->getVal() != _realRef[i]) {
173  // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << real->GetName() << " has changed from " << _realRef[i] << " to " << real->getVal() << " clearState = " << (clearState?"T":"F") << endl ;
174  valuesChanged = kTRUE ;
175  _realRef[i] = real->getVal() ;
176  }
177  i++ ;
178  }
179  // Check if any of the categories changed
180  i=0 ;
181  while ((cat=(RooAbsCategory*)_catSetIter->Next())) {
182  if (cat->getIndex() != _catRef[i++]) {
183  // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << cat->GetName() << " has changed from " << _catRef[i-1] << " to " << cat->getIndex() << endl ;
184  valuesChanged = kTRUE ;
185  _catRef[i-1] = cat->getIndex() ;
186  }
187  }
188 
189  clearValueDirty() ;
190 
191 
192  if (!_init) {
193  valuesChanged=kTRUE ;
194  _init = kTRUE ;
195  }
196 
197  // cout << "RooChangeTracker(" << GetName() << ") returning " << (valuesChanged?"T":"F") << endl ;
198 
199  return valuesChanged ;
200 
201  } else {
202 
203  // Return true as soon as any input has changed
204 
205  // Check if any of the real values changed
206  while ((real=(RooAbsReal*)_realSetIter->Next())) {
207  if (real->getVal() != _realRef[i++]) {
208  return kTRUE ;
209  }
210  }
211  // Check if any of the categories changed
212  i=0 ;
213  while ((cat=(RooAbsCategory*)_catSetIter->Next())) {
214  if (cat->getIndex() != _catRef[i++]) {
215  return kTRUE ;
216  }
217  }
218 
219  }
220 
221  return kFALSE ;
222 }
223 
224 
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Destructor
228 
230 {
231  if (_realSetIter) delete _realSetIter ;
232  if (_catSetIter) delete _catSetIter ;
233 }
234 
235 
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 
240 {
241  RooArgSet ret ;
242  ret.add(_realSet) ;
243  ret.add(_catSet) ;
244  return ret ;
245 }
246 
247 
248 
void clearValueDirty() const
Definition: RooAbsArg.h:447
TIterator * createIterator(Bool_t dir=kIterForward) const
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:86
virtual void Reset()=0
virtual ~RooChangeTracker()
Destructor.
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
Bool_t hasChanged(Bool_t clearState)
Returns true if state has changes since last call with clearState=kTRUE If clearState is true...
RooListProxy _realSet
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual Int_t getIndex() const
Return index number of current state.
STL namespace.
std::vector< Int_t > _catRef
Iterator abstract base class.
Definition: TIterator.h:30
RooChangeTracker()
Default constructor.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
Bool_t _init
do not persist
RooArgSet parameters() const
const Bool_t kFALSE
Definition: RtypesCore.h:92
#define ClassImp(name)
Definition: Rtypes.h:336
TIterator * _catSetIter
do not persist
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
RooChangeTracker is a meta object that tracks value changes in a given set of RooAbsArgs by registeri...
Bool_t isValueDirty() const
Definition: RooAbsArg.h:336
std::vector< Double_t > _realRef
virtual TObject * Next()=0
RooAbsCategory is the common abstract base class for objects that represent a discrete value with a f...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
TIterator * _realSetIter
RooListProxy _catSet
const Bool_t kTRUE
Definition: RtypesCore.h:91