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