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 "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
45using namespace std ;
46
48;
49
50////////////////////////////////////////////////////////////////////////////////
51/// Default constructor
52
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
66RooChangeTracker::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{
75for (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->getCurrentIndex() ;
93 }
94 }
95
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Copy constructor
102
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
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 ;
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->getCurrentIndex() != _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->getCurrentIndex() ;
163 }
164 }
165
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->getCurrentIndex() != _catRef[i]) {
193 return kTRUE ;
194 }
195 }
196
197 }
198
199 return kFALSE ;
200}
201
202
203
204////////////////////////////////////////////////////////////////////////////////
205/// Destructor
206
208{
209
210}
211
212
213
214////////////////////////////////////////////////////////////////////////////////
215
217{
218 RooArgSet ret ;
219 ret.add(_realSet) ;
220 ret.add(_catSet) ;
221 return ret ;
222}
223
224
225
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
Bool_t isValueDirty() const
Definition RooAbsArg.h:436
void clearValueDirty() const
Definition RooAbsArg.h:616
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
virtual value_type getCurrentIndex() const
Return index number of current state.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
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:64
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:94
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:35
RooChangeTracker is a meta object that tracks value changes in a given set of RooAbsArgs by registeri...
RooListProxy _catSet
std::vector< Double_t > _realRef
RooChangeTracker()
Default constructor.
Bool_t hasChanged(Bool_t clearState)
Returns true if state has changed since last call with clearState=kTRUE.
std::vector< Int_t > _catRef
virtual ~RooChangeTracker()
Destructor.
RooArgSet parameters() const
RooListProxy _realSet
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Reimplementation of standard RooArgList::add()