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
22Meta 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 <cmath>
36
37#include "RooChangeTracker.h"
38#include "RooAbsReal.h"
39#include "RooAbsCategory.h"
40#include "RooArgSet.h"
41#include "RooMsgService.h"
42
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor. The set trackSet contains the observables to be
47/// tracked for changes. If checkValues is true an additional
48/// validation step is activated where the numeric values of the
49/// tracked arguments are compared with reference values ensuring
50/// that values have actually changed.
51
52RooChangeTracker::RooChangeTracker(const char* name, const char* title, const RooArgSet& trackSet, bool checkValues) :
53 RooAbsReal(name, title),
54 _realSet("realSet","Set of real-valued components to be tracked",this),
55 _catSet("catSet","Set of discrete-valued components to be tracked",this),
56 _realRef(trackSet.size()),
57 _catRef(trackSet.size()),
58 _checkVal(checkValues)
59{
60for (const auto arg : trackSet) {
61 if (dynamic_cast<const RooAbsReal*>(arg)) {
62 _realSet.add(*arg) ;
63 }
64 if (dynamic_cast<const RooAbsCategory*>(arg)) {
65 _catSet.add(*arg) ;
66 }
67 }
68
69 if (_checkVal) {
70 for (unsigned int i=0; i < _realSet.size(); ++i) {
71 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
72 _realRef[i++] = real->getVal() ;
73 }
74
75 for (unsigned int i=0; i < _catSet.size(); ++i) {
76 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
77 _catRef[i++] = cat->getCurrentIndex() ;
78 }
79 }
80
81}
82
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Copy constructor
87
89 RooAbsReal(other, name),
90 _realSet("realSet",this,other._realSet),
91 _catSet("catSet",this,other._catSet),
92 _realRef(other._realRef),
93 _catRef(other._catRef),
94 _checkVal(other._checkVal)
95{
96}
97
98
99
100////////////////////////////////////////////////////////////////////////////////
101/// Returns true if state has changed since last call with clearState=true.
102/// If clearState is true, changeState flag will be cleared.
103
104bool RooChangeTracker::hasChanged(bool clearState)
105{
106
107 // If dirty flag did not change, object has not changed in any case
108 if (!isValueDirty()) {
109 return false ;
110 }
111
112 // If no value checking is required and dirty flag has changed, return true
113 if (!_checkVal) {
114
115 if (clearState) {
116 // Clear dirty flag by calling getVal()
117 //cout << "RooChangeTracker(" << GetName() << ") clearing isValueDirty" << endl ;
119 }
120
121 //cout << "RooChangeTracker(" << GetName() << ") isValueDirty = true, returning true" << endl ;
122
123 return true ;
124 }
125
126 // Compare values against reference
127 if (clearState) {
128
129 bool valuesChanged(false) ;
130
131 // Check if any of the real values changed
132 for (unsigned int i=0; i < _realSet.size(); ++i) {
133 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
134 if (real->getVal() != _realRef[i]) {
135 // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << real->GetName() << " has changed from " << _realRef[i] << " to " << real->getVal() << " clearState = " << (clearState?"T":"F") << endl ;
136 valuesChanged = true ;
137 _realRef[i] = real->getVal() ;
138 }
139 }
140 // Check if any of the categories changed
141 for (unsigned int i=0; i < _catSet.size(); ++i) {
142 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
143 if (cat->getCurrentIndex() != _catRef[i]) {
144 // cout << "RooChangeTracker(" << this << "," << GetName() << ") value of " << cat->GetName() << " has changed from " << _catRef[i-1] << " to " << cat->getIndex() << endl ;
145 valuesChanged = true ;
146 _catRef[i] = cat->getCurrentIndex() ;
147 }
148 }
149
151
152
153 if (!_init) {
154 valuesChanged=true ;
155 _init = true ;
156 }
157
158 // cout << "RooChangeTracker(" << GetName() << ") returning " << (valuesChanged?"T":"F") << endl ;
159
160 return valuesChanged ;
161
162 } else {
163
164 // Return true as soon as any input has changed
165
166 // Check if any of the real values changed
167 for (unsigned int i=0; i < _realSet.size(); ++i) {
168 auto real = static_cast<const RooAbsReal*>(_realSet.at(i));
169 if (real->getVal() != _realRef[i]) {
170 return true ;
171 }
172 }
173 // Check if any of the categories changed
174 for (unsigned int i=0; i < _catSet.size(); ++i) {
175 auto cat = static_cast<const RooAbsCategory*>(_catSet.at(i));
176 if (cat->getCurrentIndex() != _catRef[i]) {
177 return true ;
178 }
179 }
180
181 }
182
183 return false ;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187
189{
190 RooArgSet ret ;
191 ret.add(_realSet) ;
192 ret.add(_catSet) ;
193 return ret ;
194}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define ClassImp(name)
Definition Rtypes.h:382
char name[80]
Definition TGX11.cxx:110
void clearValueDirty() const
Definition RooAbsArg.h:576
bool isValueDirty() const
Definition RooAbsArg.h:393
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
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
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:24
Meta object that tracks value changes in a given set of RooAbsArgs by registering itself as value cli...
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
std::vector< Int_t > _catRef
Reference values for categories.
RooArgSet parameters() const
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...