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