Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 propagates 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 
47 ClassImp(RooChangeTracker);
48 ;
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Default constructor
52 
53 RooChangeTracker::RooChangeTracker() : _checkVal(kFALSE), _init(kFALSE)
54 {
55 }
56 
57 
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Constructor. The set trackSet contains the observables to be
61 /// tracked for changes. If checkValues is true an additional
62 /// validation step is activated where the numeric values of the
63 /// tracked arguments are compared with reference values ensuring
64 /// that values have actually changed.
65 
66 RooChangeTracker::RooChangeTracker(const char* name, const char* title, const RooArgSet& trackSet, Bool_t checkValues) :
67  RooAbsReal(name, title),
68  _realSet("realSet","Set of real-valued components to be tracked",this),
69  _catSet("catSet","Set of discrete-valued components to be tracked",this),
70  _realRef(trackSet.getSize()),
71  _catRef(trackSet.getSize()),
72  _checkVal(checkValues),
73  _init(kFALSE)
74 {
75 for (const auto arg : trackSet) {
76  if (dynamic_cast<const RooAbsReal*>(arg)) {
77  _realSet.add(*arg) ;
78  }
79  if (dynamic_cast<const RooAbsCategory*>(arg)) {
80  _catSet.add(*arg) ;
81  }
82  }
83 
84  if (_checkVal) {
85  for (unsigned int i=0; i < _realSet.size(); ++i) {
86  auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
87  _realRef[i++] = real->getVal() ;
88  }
89 
90  for (unsigned int i=0; i < _catSet.size(); ++i) {
91  auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
92  _catRef[i++] = cat->getIndex() ;
93  }
94  }
95 
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Copy constructor
102 
103 RooChangeTracker::RooChangeTracker(const RooChangeTracker& other, const char* name) :
104  RooAbsReal(other, name),
105  _realSet("realSet",this,other._realSet),
106  _catSet("catSet",this,other._catSet),
107  _realRef(other._realRef),
108  _catRef(other._catRef),
109  _checkVal(other._checkVal),
110  _init(kFALSE)
111 {
112 }
113 
114 
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// Returns true if state has changed since last call with clearState=kTRUE.
118 /// If clearState is true, changeState flag will be cleared.
119 
120 Bool_t RooChangeTracker::hasChanged(Bool_t clearState)
121 {
122 
123  // If dirty flag did not change, object has not changed in any case
124  if (!isValueDirty()) {
125  return kFALSE ;
126  }
127 
128  // If no value checking is required and dirty flag has changed, return true
129  if (!_checkVal) {
130 
131  if (clearState) {
132  // Clear dirty flag by calling getVal()
133  //cout << "RooChangeTracker(" << GetName() << ") clearing isValueDirty" << endl ;
134  clearValueDirty() ;
135  }
136 
137  //cout << "RooChangeTracker(" << GetName() << ") isValueDirty = kTRUE, returning kTRUE" << endl ;
138 
139  return kTRUE ;
140  }
141 
142  // Compare values against reference
143  if (clearState) {
144 
145  Bool_t valuesChanged(kFALSE) ;
146 
147  // Check if any of the real values changed
148  for (unsigned int i=0; i < _realSet.size(); ++i) {
149  auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
150  if (real->getVal() != _realRef[i]) {
151  // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << real->GetName() << " has changed from " << _realRef[i] << " to " << real->getVal() << " clearState = " << (clearState?"T":"F") << endl ;
152  valuesChanged = kTRUE ;
153  _realRef[i] = real->getVal() ;
154  }
155  }
156  // Check if any of the categories changed
157  for (unsigned int i=0; i < _catSet.size(); ++i) {
158  auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
159  if (cat->getIndex() != _catRef[i]) {
160  // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << cat->GetName() << " has changed from " << _catRef[i-1] << " to " << cat->getIndex() << endl ;
161  valuesChanged = kTRUE ;
162  _catRef[i] = cat->getIndex() ;
163  }
164  }
165 
166  clearValueDirty() ;
167 
168 
169  if (!_init) {
170  valuesChanged=kTRUE ;
171  _init = kTRUE ;
172  }
173 
174  // cout << "RooChangeTracker(" << GetName() << ") returning " << (valuesChanged?"T":"F") << endl ;
175 
176  return valuesChanged ;
177 
178  } else {
179 
180  // Return true as soon as any input has changed
181 
182  // Check if any of the real values changed
183  for (unsigned int i=0; i < _realSet.size(); ++i) {
184  auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
185  if (real->getVal() != _realRef[i]) {
186  return kTRUE ;
187  }
188  }
189  // Check if any of the categories changed
190  for (unsigned int i=0; i < _catSet.size(); ++i) {
191  auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
192  if (cat->getIndex() != _catRef[i]) {
193  return kTRUE ;
194  }
195  }
196 
197  }
198 
199  return kFALSE ;
200 }
201 
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Destructor
206 
207 RooChangeTracker::~RooChangeTracker()
208 {
209 
210 }
211 
212 
213 
214 ////////////////////////////////////////////////////////////////////////////////
215 
216 RooArgSet RooChangeTracker::parameters() const
217 {
218  RooArgSet ret ;
219  ret.add(_realSet) ;
220  ret.add(_catSet) ;
221  return ret ;
222 }
223 
224 
225