Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooAbsCategoryLValue.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 RooAbsCategoryLValue.cxx
19 \class RooAbsCategoryLValue
20 \ingroup Roofitcore
21 
22 RooAbsCategoryLValue is the common abstract base class for objects that represent a
23 discrete value that may appear on the left hand side of an equation ('lvalue')
24 
25 Each implementation must provide setIndex()/setLabel() members to allow direct modification
26 of the value. RooAbsCategoryLValue may be derived, but its functional relation
27 to other RooAbsArgs must be invertible
28 **/
29 
30 #include "RooFit.h"
31 
32 #include "Riostream.h"
33 #include "Riostream.h"
34 #include <stdlib.h>
35 #include "TTree.h"
36 #include "TString.h"
37 #include "TH1.h"
38 #include "RooAbsCategoryLValue.h"
39 #include "RooArgSet.h"
40 #include "RooStreamParser.h"
41 #include "RooRandom.h"
42 #include "RooMsgService.h"
43 
44 using namespace std;
45 
46 ClassImp(RooAbsCategoryLValue);
47 ;
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Constructor
52 
53 RooAbsCategoryLValue::RooAbsCategoryLValue(const char *name, const char *title) :
54  RooAbsCategory(name,title)
55 {
56  setValueDirty() ;
57  setShapeDirty() ;
58 }
59 
60 
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Copy constructor
64 
65 RooAbsCategoryLValue::RooAbsCategoryLValue(const RooAbsCategoryLValue& other, const char* name) :
66  RooAbsCategory(other, name), RooAbsLValue(other)
67 {
68 }
69 
70 
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Destructor
74 
75 RooAbsCategoryLValue::~RooAbsCategoryLValue()
76 {
77 }
78 
79 
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Assignment operator from integer index number
83 
84 RooAbsArg& RooAbsCategoryLValue::operator=(Int_t index)
85 {
86  setIndex(index,kTRUE) ;
87  return *this ;
88 }
89 
90 
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Assignment operator from string pointer
94 
95 RooAbsArg& RooAbsCategoryLValue::operator=(const char *label)
96 {
97  setLabel(label) ;
98  return *this ;
99 }
100 
101 
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Assignment from another RooAbsCategory
105 
106 RooAbsArg& RooAbsCategoryLValue::operator=(const RooAbsCategory& other)
107 {
108  if (&other==this) return *this ;
109 
110  const RooCatType* type = lookupType(other.getLabel(),kTRUE) ;
111  if (!type) return *this ;
112 
113  _value = *type ;
114  setValueDirty() ;
115  return *this ;
116 }
117 
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Set our state to our n'th defined type and return kTRUE.
122 /// Return kFALSE if n is out of range.
123 
124 Bool_t RooAbsCategoryLValue::setOrdinal(UInt_t n, const char* rangeName)
125 {
126  const RooCatType *newValue= getOrdinal(n,rangeName);
127  if(newValue) {
128  return setIndex(newValue->getVal());
129  }
130  else {
131  return kFALSE;
132  }
133 }
134 
135 
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// Copy the cached value from given source and raise dirty flag.
139 /// It is the callers responsability to ensure that the sources
140 /// cache is clean(valid) before this function is called, e.g. by
141 /// calling syncCache() on the source.
142 
143 void RooAbsCategoryLValue::copyCache(const RooAbsArg* source, Bool_t valueOnly, Bool_t setValDirty)
144 {
145  RooAbsCategory::copyCache(source,valueOnly,setValDirty) ;
146  if (isValid(_value)) {
147  setIndex(_value.getVal()) ; // force back-propagation
148  }
149 }
150 
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Read object contents from given stream (dummy implementation)
155 
156 Bool_t RooAbsCategoryLValue::readFromStream(istream&, Bool_t, Bool_t)
157 {
158  return kTRUE ;
159 }
160 
161 
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Write object contents to given stream (dummy implementation)
165 
166 void RooAbsCategoryLValue::writeToStream(ostream&, Bool_t) const
167 {
168 }
169 
170 
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// Randomize current value
174 
175 void RooAbsCategoryLValue::randomize(const char* rangeName)
176 {
177  UInt_t ordinal= RooRandom::integer(numTypes(rangeName));
178  setOrdinal(ordinal,rangeName);
179 }
180 
181 
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Set category to i-th fit bin, which is the i-th registered state.
185 
186 void RooAbsCategoryLValue::setBin(Int_t ibin, const char* rangeName)
187 {
188  // Check validity of ibin
189  if (ibin<0 || ibin>=numBins(rangeName)) {
190  coutE(InputArguments) << "RooAbsCategoryLValue::setBin(" << GetName() << ") ERROR: bin index " << ibin
191  << " is out of range (0," << numBins(rangeName)-1 << ")" << endl ;
192  return ;
193  }
194 
195  // Retrieve state corresponding to bin
196  const RooCatType* type = getOrdinal(ibin,rangeName) ;
197 
198  // Set value to requested state
199  setIndex(type->getVal()) ;
200 }
201 
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Get index of plot bin for current value this category.
206 
207 Int_t RooAbsCategoryLValue::getBin(const char* /*rangeName*/) const
208 {
209  //Synchronize _value
210  getLabel() ;
211 
212  // Lookup ordinal index number
213  std::string theName = _value.GetName();
214  auto item = std::find_if(_types.begin(), _types.end(), [&theName](const RooCatType* cat){
215  return cat->GetName() == theName;
216  });
217 
218  return item - _types.begin();
219 }
220 
221 
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// Returm the number of fit bins ( = number of types )
225 
226 Int_t RooAbsCategoryLValue::numBins(const char* rangeName) const
227 {
228  return numTypes(rangeName) ;
229 }