ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id: RooChangeTracker.cxx 26174 2008-11-13 08:25:25Z wouter $
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
// 
// BEGIN_HTML 
// RooChangeTracker is a meta object that tracks value
// changes in a given set of RooAbsArgs by registering itself as value
// client of these objects. The change tracker can perform an
// additional validation step where it also compares the numeric
// values of the tracked arguments with reference values to ensure
// that values have actually changed. This may be useful in case some
// of the tracked observables are in binned datasets where each
// observable propates a valueDirty flag when an event is loaded even
// though usually only one observable actually changes.
// END_HTML
//


#include "RooFit.h"

#include "Riostream.h"
#include <math.h>

#include "RooChangeTracker.h"
#include "RooAbsReal.h"
#include "RooAbsCategory.h"
#include "RooArgSet.h"
#include "RooMsgService.h"

using namespace std ;

ClassImp(RooChangeTracker)
;

//_____________________________________________________________________________
RooChangeTracker::RooChangeTracker()
{
  // Deault constructor

  _realSetIter = _realSet.createIterator() ;
  _catSetIter = _catSet.createIterator() ;
}



//_____________________________________________________________________________
RooChangeTracker::RooChangeTracker(const char* name, const char* title, const RooArgSet& trackSet, Bool_t checkValues) :
  RooAbsReal(name, title),
  _realSet("realSet","Set of real-valued components to be tracked",this),
  _catSet("catSet","Set of discrete-valued components to be tracked",this),
  _realRef(trackSet.getSize()),
  _catRef(trackSet.getSize()),
  _checkVal(checkValues)
{
  // Constructor. The set trackSet contains the observables to be
  // tracked for changes. If checkValues is true an additional
  // validation step is activated where the numeric values of the
  // tracked arguments are compared with reference values ensuring
  // that values have actually changed.

  _realSetIter = _realSet.createIterator() ;
  _catSetIter = _catSet.createIterator() ;

  TIterator* iter = trackSet.createIterator() ;
  RooAbsArg* arg ;
  while((arg=(RooAbsArg*)iter->Next())) {
    if (dynamic_cast<RooAbsReal*>(arg)) {
      _realSet.add(*arg) ;      
    }
    if (dynamic_cast<RooAbsCategory*>(arg)) {
      _catSet.add(*arg) ;      
    }
  }
  delete iter ;
  
  if (_checkVal) {
    RooAbsReal* real  ;
    RooAbsCategory* cat  ;
    Int_t i(0) ;
    while((real=(RooAbsReal*)_realSetIter->Next())) {
      _realRef[i++] = real->getVal() ;
    }
    i=0 ;
    while((cat=(RooAbsCategory*)_catSetIter->Next())) {
      _catRef[i++] = cat->getIndex() ;
    }
  }

}



//_____________________________________________________________________________
RooChangeTracker::RooChangeTracker(const RooChangeTracker& other, const char* name) :
  RooAbsReal(other, name), 
  _realSet("realSet",this,other._realSet),
  _catSet("catSet",this,other._catSet),
  _realRef(other._realRef),
  _catRef(other._catRef),
  _checkVal(other._checkVal)
{
  // Copy constructor

  _realSetIter = _realSet.createIterator() ;
  _catSetIter = _catSet.createIterator() ;

  _realSet.add(other._realSet) ;
  _catSet.add(other._catSet) ;

}



//_____________________________________________________________________________
Bool_t RooChangeTracker::hasChanged(Bool_t clearState) 
{
  // Returns true if state has changes since last call with clearState=kTRUE
  // If clearState is true, changeState flag will be cleared.


  // If dirty flag did not change, object has not changed in any case
  if (!isValueDirty()) {
    return kFALSE ;
  }

  // If no value checking is required and dirty flag has changed, return true
  if (!_checkVal) {

    if (clearState) {
      // Clear dirty flag by calling getVal()
      //cout << "RooChangeTracker(" << GetName() << ") clearing isValueDirty" << endl ;
      clearValueDirty() ;
    }

    //cout << "RooChangeTracker(" << GetName() << ") isValueDirty = kTRUE, returning kTRUE" << endl ;

    return kTRUE ;
  }
  
  // Compare values against reference
  _realSetIter->Reset() ;  
  _catSetIter->Reset() ;
  RooAbsReal* real ;
  RooAbsCategory* cat ;
  Int_t i(0) ;

  if (clearState) {

    Bool_t valuesChanged(kFALSE) ;

    // Check if any of the real values changed
    while ((real=(RooAbsReal*)_realSetIter->Next())) {
      if (real->getVal() != _realRef[i++]) {
	//cout << "RooChangeTracker(" << GetName() << ") value of " << real->GetName() << " has changed from " << _realRef[i-1] << " to " << real->getVal() << endl ;
	valuesChanged = kTRUE ;
	_realRef[i-1] = real->getVal() ;
      }
    }
    // Check if any of the categories changed
    i=0 ;
    while ((cat=(RooAbsCategory*)_catSetIter->Next())) {
      if (cat->getIndex() != _catRef[i++]) {
	//cout << "RooChangeTracker(" << GetName() << ") value of " << cat->GetName() << " has changed from " << _catRef[i-1] << " to " << cat->getIndex() << endl ;
	valuesChanged = kTRUE ;
	_catRef[i-1] = cat->getIndex() ;
      }
    }

    clearValueDirty() ;

    return valuesChanged ;

  } else {
    
    // Return true as soon as any input has changed

    // Check if any of the real values changed
    while ((real=(RooAbsReal*)_realSetIter->Next())) {
      if (real->getVal() != _realRef[i++]) {
	return kTRUE ;
      }
    }
    // Check if any of the categories changed
    i=0 ;
    while ((cat=(RooAbsCategory*)_catSetIter->Next())) {
      if (cat->getIndex() != _catRef[i++]) {
	return kTRUE ;
      }
    }
   
  }
  
  return kFALSE ;
}



//_____________________________________________________________________________
RooChangeTracker::~RooChangeTracker() 
{
  // Destructor
  if (_realSetIter) delete _realSetIter ;
  if (_catSetIter) delete _catSetIter ;
}



//_____________________________________________________________________________
RooArgSet RooChangeTracker::parameters() const 
{
  RooArgSet ret ;
  ret.add(_realSet) ;
  ret.add(_catSet) ;
  return ret ;
}



 RooChangeTracker.cxx:1
 RooChangeTracker.cxx:2
 RooChangeTracker.cxx:3
 RooChangeTracker.cxx:4
 RooChangeTracker.cxx:5
 RooChangeTracker.cxx:6
 RooChangeTracker.cxx:7
 RooChangeTracker.cxx:8
 RooChangeTracker.cxx:9
 RooChangeTracker.cxx:10
 RooChangeTracker.cxx:11
 RooChangeTracker.cxx:12
 RooChangeTracker.cxx:13
 RooChangeTracker.cxx:14
 RooChangeTracker.cxx:15
 RooChangeTracker.cxx:16
 RooChangeTracker.cxx:17
 RooChangeTracker.cxx:18
 RooChangeTracker.cxx:19
 RooChangeTracker.cxx:20
 RooChangeTracker.cxx:21
 RooChangeTracker.cxx:22
 RooChangeTracker.cxx:23
 RooChangeTracker.cxx:24
 RooChangeTracker.cxx:25
 RooChangeTracker.cxx:26
 RooChangeTracker.cxx:27
 RooChangeTracker.cxx:28
 RooChangeTracker.cxx:29
 RooChangeTracker.cxx:30
 RooChangeTracker.cxx:31
 RooChangeTracker.cxx:32
 RooChangeTracker.cxx:33
 RooChangeTracker.cxx:34
 RooChangeTracker.cxx:35
 RooChangeTracker.cxx:36
 RooChangeTracker.cxx:37
 RooChangeTracker.cxx:38
 RooChangeTracker.cxx:39
 RooChangeTracker.cxx:40
 RooChangeTracker.cxx:41
 RooChangeTracker.cxx:42
 RooChangeTracker.cxx:43
 RooChangeTracker.cxx:44
 RooChangeTracker.cxx:45
 RooChangeTracker.cxx:46
 RooChangeTracker.cxx:47
 RooChangeTracker.cxx:48
 RooChangeTracker.cxx:49
 RooChangeTracker.cxx:50
 RooChangeTracker.cxx:51
 RooChangeTracker.cxx:52
 RooChangeTracker.cxx:53
 RooChangeTracker.cxx:54
 RooChangeTracker.cxx:55
 RooChangeTracker.cxx:56
 RooChangeTracker.cxx:57
 RooChangeTracker.cxx:58
 RooChangeTracker.cxx:59
 RooChangeTracker.cxx:60
 RooChangeTracker.cxx:61
 RooChangeTracker.cxx:62
 RooChangeTracker.cxx:63
 RooChangeTracker.cxx:64
 RooChangeTracker.cxx:65
 RooChangeTracker.cxx:66
 RooChangeTracker.cxx:67
 RooChangeTracker.cxx:68
 RooChangeTracker.cxx:69
 RooChangeTracker.cxx:70
 RooChangeTracker.cxx:71
 RooChangeTracker.cxx:72
 RooChangeTracker.cxx:73
 RooChangeTracker.cxx:74
 RooChangeTracker.cxx:75
 RooChangeTracker.cxx:76
 RooChangeTracker.cxx:77
 RooChangeTracker.cxx:78
 RooChangeTracker.cxx:79
 RooChangeTracker.cxx:80
 RooChangeTracker.cxx:81
 RooChangeTracker.cxx:82
 RooChangeTracker.cxx:83
 RooChangeTracker.cxx:84
 RooChangeTracker.cxx:85
 RooChangeTracker.cxx:86
 RooChangeTracker.cxx:87
 RooChangeTracker.cxx:88
 RooChangeTracker.cxx:89
 RooChangeTracker.cxx:90
 RooChangeTracker.cxx:91
 RooChangeTracker.cxx:92
 RooChangeTracker.cxx:93
 RooChangeTracker.cxx:94
 RooChangeTracker.cxx:95
 RooChangeTracker.cxx:96
 RooChangeTracker.cxx:97
 RooChangeTracker.cxx:98
 RooChangeTracker.cxx:99
 RooChangeTracker.cxx:100
 RooChangeTracker.cxx:101
 RooChangeTracker.cxx:102
 RooChangeTracker.cxx:103
 RooChangeTracker.cxx:104
 RooChangeTracker.cxx:105
 RooChangeTracker.cxx:106
 RooChangeTracker.cxx:107
 RooChangeTracker.cxx:108
 RooChangeTracker.cxx:109
 RooChangeTracker.cxx:110
 RooChangeTracker.cxx:111
 RooChangeTracker.cxx:112
 RooChangeTracker.cxx:113
 RooChangeTracker.cxx:114
 RooChangeTracker.cxx:115
 RooChangeTracker.cxx:116
 RooChangeTracker.cxx:117
 RooChangeTracker.cxx:118
 RooChangeTracker.cxx:119
 RooChangeTracker.cxx:120
 RooChangeTracker.cxx:121
 RooChangeTracker.cxx:122
 RooChangeTracker.cxx:123
 RooChangeTracker.cxx:124
 RooChangeTracker.cxx:125
 RooChangeTracker.cxx:126
 RooChangeTracker.cxx:127
 RooChangeTracker.cxx:128
 RooChangeTracker.cxx:129
 RooChangeTracker.cxx:130
 RooChangeTracker.cxx:131
 RooChangeTracker.cxx:132
 RooChangeTracker.cxx:133
 RooChangeTracker.cxx:134
 RooChangeTracker.cxx:135
 RooChangeTracker.cxx:136
 RooChangeTracker.cxx:137
 RooChangeTracker.cxx:138
 RooChangeTracker.cxx:139
 RooChangeTracker.cxx:140
 RooChangeTracker.cxx:141
 RooChangeTracker.cxx:142
 RooChangeTracker.cxx:143
 RooChangeTracker.cxx:144
 RooChangeTracker.cxx:145
 RooChangeTracker.cxx:146
 RooChangeTracker.cxx:147
 RooChangeTracker.cxx:148
 RooChangeTracker.cxx:149
 RooChangeTracker.cxx:150
 RooChangeTracker.cxx:151
 RooChangeTracker.cxx:152
 RooChangeTracker.cxx:153
 RooChangeTracker.cxx:154
 RooChangeTracker.cxx:155
 RooChangeTracker.cxx:156
 RooChangeTracker.cxx:157
 RooChangeTracker.cxx:158
 RooChangeTracker.cxx:159
 RooChangeTracker.cxx:160
 RooChangeTracker.cxx:161
 RooChangeTracker.cxx:162
 RooChangeTracker.cxx:163
 RooChangeTracker.cxx:164
 RooChangeTracker.cxx:165
 RooChangeTracker.cxx:166
 RooChangeTracker.cxx:167
 RooChangeTracker.cxx:168
 RooChangeTracker.cxx:169
 RooChangeTracker.cxx:170
 RooChangeTracker.cxx:171
 RooChangeTracker.cxx:172
 RooChangeTracker.cxx:173
 RooChangeTracker.cxx:174
 RooChangeTracker.cxx:175
 RooChangeTracker.cxx:176
 RooChangeTracker.cxx:177
 RooChangeTracker.cxx:178
 RooChangeTracker.cxx:179
 RooChangeTracker.cxx:180
 RooChangeTracker.cxx:181
 RooChangeTracker.cxx:182
 RooChangeTracker.cxx:183
 RooChangeTracker.cxx:184
 RooChangeTracker.cxx:185
 RooChangeTracker.cxx:186
 RooChangeTracker.cxx:187
 RooChangeTracker.cxx:188
 RooChangeTracker.cxx:189
 RooChangeTracker.cxx:190
 RooChangeTracker.cxx:191
 RooChangeTracker.cxx:192
 RooChangeTracker.cxx:193
 RooChangeTracker.cxx:194
 RooChangeTracker.cxx:195
 RooChangeTracker.cxx:196
 RooChangeTracker.cxx:197
 RooChangeTracker.cxx:198
 RooChangeTracker.cxx:199
 RooChangeTracker.cxx:200
 RooChangeTracker.cxx:201
 RooChangeTracker.cxx:202
 RooChangeTracker.cxx:203
 RooChangeTracker.cxx:204
 RooChangeTracker.cxx:205
 RooChangeTracker.cxx:206
 RooChangeTracker.cxx:207
 RooChangeTracker.cxx:208
 RooChangeTracker.cxx:209
 RooChangeTracker.cxx:210
 RooChangeTracker.cxx:211
 RooChangeTracker.cxx:212
 RooChangeTracker.cxx:213
 RooChangeTracker.cxx:214
 RooChangeTracker.cxx:215
 RooChangeTracker.cxx:216
 RooChangeTracker.cxx:217
 RooChangeTracker.cxx:218
 RooChangeTracker.cxx:219
 RooChangeTracker.cxx:220
 RooChangeTracker.cxx:221
 RooChangeTracker.cxx:222
 RooChangeTracker.cxx:223
 RooChangeTracker.cxx:224
 RooChangeTracker.cxx:225
 RooChangeTracker.cxx:226
 RooChangeTracker.cxx:227
 RooChangeTracker.cxx:228
 RooChangeTracker.cxx:229
 RooChangeTracker.cxx:230
 RooChangeTracker.cxx:231
 RooChangeTracker.cxx:232