Logo ROOT   6.14/05
Reference Guide
RooAbsString.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 RooAbsString.cxx
19 \class RooAbsString
20 \ingroup Roofitcore
21 
22 RooAbsString is the common abstract base class for objects that represent a
23 string value
24 
25 Implementation of RooAbsString may be derived, there no interface
26 is provided to modify the contents
27 **/
28 //
29 
30 #include "RooFit.h"
31 
32 #include "Riostream.h"
33 #include "Riostream.h"
34 #include "TObjString.h"
35 #include "TH1.h"
36 #include "TTree.h"
37 
38 #include "RooArgSet.h"
39 #include "RooAbsString.h"
40 #include "RooStringVar.h"
41 #include "RooMsgService.h"
42 
43 using namespace std;
44 
46 ;
47 
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Default constructor
51 
52 RooAbsString::RooAbsString() : RooAbsArg(), _len(128) , _value(new char[128])
53 {
54 }
55 
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Constructor
59 
60 RooAbsString::RooAbsString(const char *name, const char *title, Int_t bufLen) :
61  RooAbsArg(name,title), _len(bufLen), _value(new char[bufLen])
62 {
63  setValueDirty() ;
64  setShapeDirty() ;
65 }
66 
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Copy constructor
71 
72 RooAbsString::RooAbsString(const RooAbsString& other, const char* name) :
73  RooAbsArg(other, name), _len(other._len), _value(new char[other._len])
74 {
75  strlcpy(_value,other._value,_len) ;
76 }
77 
78 
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Destructor
82 
84 {
85  delete[] _value ;
86 }
87 
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Return value of object. Calculated if dirty, otherwise cached value is returned.
92 
93 const char* RooAbsString::getVal() const
94 {
95  if (isValueDirty()) {
96  clearValueDirty() ;
97  strlcpy(_value,traceEval(),_len) ;
98  }
99 
100  return _value ;
101 }
102 
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Equality operator comparing with a TString
107 
108 Bool_t RooAbsString::operator==(const char* value) const
109 {
110  return !TString(getVal()).CompareTo(value) ;
111 }
112 
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 
116 Bool_t RooAbsString::isIdentical(const RooAbsArg& other, Bool_t assumeSameType)
117 {
118  if (!assumeSameType) {
119  const RooAbsString* otherString = dynamic_cast<const RooAbsString*>(&other) ;
120  return otherString ? operator==(otherString->getVal()) : kFALSE ;
121  } else {
122  return !TString(getVal()).CompareTo(((RooAbsString&)other).getVal()) ; ;
123  }
124 }
125 
126 
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Equality operator comparing to another RooAbsArg
130 
132 {
133  const RooAbsString* otherString = dynamic_cast<const RooAbsString*>(&other) ;
134  return otherString ? operator==(otherString->getVal()) : kFALSE ;
135 }
136 
137 
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 ///Read object contents from stream (dummy for now)
141 
142 Bool_t RooAbsString::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
143 {
144  return kFALSE ;
145 }
146 
147 
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 ///Write object contents to stream (dummy for now)
151 
152 void RooAbsString::writeToStream(ostream& /*os*/, Bool_t /*compact*/) const
153 {
154 }
155 
156 
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 /// Print value
160 
161 void RooAbsString::printValue(ostream& os) const
162 {
163  os << getVal() ;
164 }
165 
166 
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Check if current value is valid
170 
172 {
173  return isValidString(getVal()) ;
174 }
175 
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Check if given string value is valid
180 
181 Bool_t RooAbsString::isValidString(const char* value, Bool_t /*printError*/) const
182 {
183  // Protect against string overflows
184  if (TString(value).Length()>_len) return kFALSE ;
185 
186  return kTRUE ;
187 }
188 
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// Hook function for trace evaluation
192 
193 Bool_t RooAbsString::traceEvalHook(const char* /*value*/) const
194 {
195  return kFALSE ;
196 }
197 
198 
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Calculate current value of object, with error tracing wrapper
202 
203 TString RooAbsString::traceEval() const
204 {
205  TString value = evaluate() ;
206 
207  //Standard tracing code goes here
208  if (!isValidString(value)) {
209  cxcoutD(Tracing) << "RooAbsString::traceEval(" << GetName() << "): new output too long (>" << _len << " chars): " << value << endl ;
210  }
211 
212  //Call optional subclass tracing code
213  traceEvalHook(value) ;
214 
215  return value ;
216 }
217 
218 
219 
220 ////////////////////////////////////////////////////////////////////////////////
221 /// Forcibly bring internal cache up-to-date
222 
224 {
225  getVal() ;
226 }
227 
228 
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Copy cache of another RooAbsArg to our cache
232 ///
233 /// Warning: This function copies the cached values of source,
234 /// it is the callers responsibility to make sure the cache is clean
235 
236 void RooAbsString::copyCache(const RooAbsArg* source, Bool_t /*valueOnly*/, Bool_t setValDirty)
237 {
238  RooAbsString* other = dynamic_cast<RooAbsString*>(const_cast<RooAbsArg*>(source)) ;
239  assert(other!=0) ;
240 
241  strlcpy(_value,other->_value,_len) ;
242  if (setValDirty) {
243  setValueDirty() ;
244  }
245 }
246 
247 
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Attach object to a branch of given TTree
251 
252 void RooAbsString::attachToTree(TTree& t, Int_t bufSize)
253 {
254  // First determine if branch is taken
255  TBranch* branch ;
256  if ((branch = t.GetBranch(GetName()))) {
257  t.SetBranchAddress(GetName(),_value) ;
258  if (branch->GetCompressionLevel()<0) {
259  cxcoutD(DataHandling) << "RooAbsString::attachToTree(" << GetName() << ") Fixing compression level of branch " << GetName() << endl ;
260  branch->SetCompressionLevel(1) ;
261  }
262  } else {
263  TString format(GetName());
264  format.Append("/C");
265  branch = t.Branch(GetName(), _value, (const Text_t*)format, bufSize);
266  branch->SetCompressionLevel(1) ;
267  }
268 }
269 
270 
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Fill tree branch associated with this object
274 
276 {
277  // First determine if branch is taken
278  TBranch* branch = t.GetBranch(GetName()) ;
279  if (!branch) {
280  coutE(DataHandling) << "RooAbsString::fillTreeBranch(" << GetName() << ") ERROR: not attached to tree" << endl ;
281  assert(0) ;
282  }
283  branch->Fill() ;
284 }
285 
286 
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// (De)Activate associated tree branch
290 
292 {
293  TBranch* branch = t.GetBranch(GetName()) ;
294  if (branch) {
295  t.SetBranchStatus(GetName(),active?1:0) ;
296  }
297 }
298 
299 
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 /// Create a RooStringVar fundamental object with our properties.
303 
304 RooAbsArg *RooAbsString::createFundamental(const char* newname) const
305 {
306  RooStringVar *fund= new RooStringVar(newname?newname:GetName(),GetTitle(),"") ;
307  return fund;
308 }
void clearValueDirty() const
Definition: RooAbsArg.h:449
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
#define coutE(a)
Definition: RooMsgService.h:34
Int_t GetCompressionLevel() const
Definition: TBranch.h:259
virtual void setTreeBranchStatus(TTree &t, Bool_t active)
(De)Activate associated tree branch
#define cxcoutD(a)
Definition: RooMsgService.h:79
void SetCompressionLevel(Int_t level=1)
Set compression level.
Definition: TBranch.cxx:2359
virtual Bool_t isValidString(const char *, Bool_t printError=kFALSE) const
Check if given string value is valid.
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream (dummy for now)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
void copyCache(const RooAbsArg *source, Bool_t valueOnly=kFALSE, Bool_t setValDiry=kTRUE)
Copy cache of another RooAbsArg to our cache.
void setValueDirty() const
Definition: RooAbsArg.h:441
virtual TString evaluate() const
Definition: RooAbsString.h:55
virtual Bool_t isIdentical(const RooAbsArg &other, Bool_t assumeSameType=kFALSE)
Int_t Fill()
Definition: TBranch.h:162
virtual void fillTreeBranch(TTree &t)
Fill tree branch associated with this object.
virtual void attachToTree(TTree &t, Int_t bufSize=32000)
Attach object to a branch of given TTree.
char * _value
Definition: RooAbsString.h:68
virtual void syncCache(const RooArgSet *nset=0)
Forcibly bring internal cache up-to-date.
RooAbsString()
Default constructor.
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
virtual ~RooAbsString()
Destructor.
Bool_t operator==(const char *) const
Equality operator comparing with a TString.
virtual Bool_t traceEvalHook(const char *value) const
Hook function for trace evaluation.
virtual const char * getVal() const
Return value of object. Calculated if dirty, otherwise cached value is returned.
Bool_t isValueDirty() const
Definition: RooAbsArg.h:336
RooAbsString is the common abstract base class for objects that represent a string value...
Definition: RooAbsString.h:25
void setShapeDirty() const
Definition: RooAbsArg.h:442
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to stream (dummy for now)
char Text_t
Definition: RtypesCore.h:58
TString traceEval() const
Calculate current value of object, with error tracing wrapper.
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
A TTree is a list of TBranches.
Definition: TBranch.h:62
virtual void printValue(std::ostream &os) const
Print value.
RooStringVar implements a string values RooAbsArg.
Definition: RooStringVar.h:23
const Bool_t kTRUE
Definition: RtypesCore.h:87
RooAbsArg * createFundamental(const char *newname=0) const
Create a RooStringVar fundamental object with our properties.
virtual Bool_t isValid() const
Check if current value is valid.
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48