Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22RooChangeTracker is a meta object that tracks value
23changes in a given set of RooAbsArgs by registering itself as value
24client of these objects. The change tracker can perform an
25additional validation step where it also compares the numeric
26values of the tracked arguments with reference values to ensure
27that values have actually changed. This may be useful in case some
28of the tracked observables are in binned datasets where each
29observable propagates a valueDirty flag when an event is loaded even
30though usually only one observable actually changes.
31**/
32
33
34#include "Riostream.h"
35#include <math.h>
36
37#include "RooChangeTracker.h"
38#include "RooAbsReal.h"
39#include "RooAbsCategory.h"
40#include "RooArgSet.h"
41#include "RooMsgService.h"
42
43using namespace std ;
44
46;
47
48////////////////////////////////////////////////////////////////////////////////
49/// Default constructor
50
51RooChangeTracker::RooChangeTracker() : _checkVal(false), _init(false)
52{
53}
54
55
56
57////////////////////////////////////////////////////////////////////////////////
58/// Constructor. The set trackSet contains the observables to be
59/// tracked for changes. If checkValues is true an additional
60/// validation step is activated where the numeric values of the
61/// tracked arguments are compared with reference values ensuring
62/// that values have actually changed.
63
64RooChangeTracker::RooChangeTracker(const char* name, const char* title, const RooArgSet& trackSet, bool checkValues) :
65 RooAbsReal(name, title),
66 _realSet("realSet","Set of real-valued components to be tracked",this),
67 _catSet("catSet","Set of discrete-valued components to be tracked",this),
68 _realRef(trackSet.getSize()),
69 _catRef(trackSet.getSize()),
70 _checkVal(checkValues),
71 _init(false)
72{
73for (const auto arg : trackSet) {
74 if (dynamic_cast<const RooAbsReal*>(arg)) {
75 _realSet.add(*arg) ;
76 }
77 if (dynamic_cast<const RooAbsCategory*>(arg)) {
78 _catSet.add(*arg) ;
79 }
80 }
81
82 if (_checkVal) {
83 for (unsigned int i=0; i < _realSet.size(); ++i) {
84 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
85 _realRef[i++] = real->getVal() ;
86 }
87
88 for (unsigned int i=0; i < _catSet.size(); ++i) {
89 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
90 _catRef[i++] = cat->getCurrentIndex() ;
91 }
92 }
93
94}
95
96
97
98////////////////////////////////////////////////////////////////////////////////
99/// Copy constructor
100
102 RooAbsReal(other, name),
103 _realSet("realSet",this,other._realSet),
104 _catSet("catSet",this,other._catSet),
105 _realRef(other._realRef),
106 _catRef(other._catRef),
107 _checkVal(other._checkVal),
108 _init(false)
109{
110}
111
112
113
114////////////////////////////////////////////////////////////////////////////////
115/// Returns true if state has changed since last call with clearState=true.
116/// If clearState is true, changeState flag will be cleared.
117
118bool RooChangeTracker::hasChanged(bool clearState)
119{
120
121 // If dirty flag did not change, object has not changed in any case
122 if (!isValueDirty()) {
123 return false ;
124 }
125
126 // If no value checking is required and dirty flag has changed, return true
127 if (!_checkVal) {
128
129 if (clearState) {
130 // Clear dirty flag by calling getVal()
131 //cout << "RooChangeTracker(" << GetName() << ") clearing isValueDirty" << endl ;
133 }
134
135 //cout << "RooChangeTracker(" << GetName() << ") isValueDirty = true, returning true" << endl ;
136
137 return true ;
138 }
139
140 // Compare values against reference
141 if (clearState) {
142
143 bool valuesChanged(false) ;
144
145 // Check if any of the real values changed
146 for (unsigned int i=0; i < _realSet.size(); ++i) {
147 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
148 if (real->getVal() != _realRef[i]) {
149 // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << real->GetName() << " has changed from " << _realRef[i] << " to " << real->getVal() << " clearState = " << (clearState?"T":"F") << endl ;
150 valuesChanged = true ;
151 _realRef[i] = real->getVal() ;
152 }
153 }
154 // Check if any of the categories changed
155 for (unsigned int i=0; i < _catSet.size(); ++i) {
156 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
157 if (cat->getCurrentIndex() != _catRef[i]) {
158 // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << cat->GetName() << " has changed from " << _catRef[i-1] << " to " << cat->getIndex() << endl ;
159 valuesChanged = true ;
160 _catRef[i] = cat->getCurrentIndex() ;
161 }
162 }
163
165
166
167 if (!_init) {
168 valuesChanged=true ;
169 _init = true ;
170 }
171
172 // cout << "RooChangeTracker(" << GetName() << ") returning " << (valuesChanged?"T":"F") << endl ;
173
174 return valuesChanged ;
175
176 } else {
177
178 // Return true as soon as any input has changed
179
180 // Check if any of the real values changed
181 for (unsigned int i=0; i < _realSet.size(); ++i) {
182 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
183 if (real->getVal() != _realRef[i]) {
184 return true ;
185 }
186 }
187 // Check if any of the categories changed
188 for (unsigned int i=0; i < _catSet.size(); ++i) {
189 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
190 if (cat->getCurrentIndex() != _catRef[i]) {
191 return true ;
192 }
193 }
194
195 }
196
197 return false ;
198}
199
200
201
202////////////////////////////////////////////////////////////////////////////////
203/// Destructor
204
206{
207
208}
209
210
211
212////////////////////////////////////////////////////////////////////////////////
213
215{
216 RooArgSet ret ;
217 ret.add(_realSet) ;
218 ret.add(_catSet) ;
219 return ret ;
220}
221
222
223
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
void clearValueDirty() const
Definition RooAbsArg.h:599
bool isValueDirty() const
Definition RooAbsArg.h:418
A space to attach TBranches.
virtual value_type getCurrentIndex() const
Return index number of current state.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Storage_t::size_type size() const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooChangeTracker is a meta object that tracks value changes in a given set of RooAbsArgs by registeri...
bool hasChanged(bool clearState)
Returns true if state has changed since last call with clearState=true.
RooListProxy _catSet
List of categories to check.
bool _checkVal
Check contents as well if true.
RooChangeTracker()
Default constructor.
std::vector< Int_t > _catRef
Reference values for categories.
RooArgSet parameters() const
~RooChangeTracker() override
Destructor.
std::vector< double > _realRef
Reference values for reals.
RooListProxy _realSet
List of reals to track.
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...