Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "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 
44 using namespace std;
45 
46 ClassImp(RooAbsString);
47 ;
48 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Default constructor
52 
53 RooAbsString::RooAbsString() : RooAbsArg(), _len(128) , _value(new char[128])
54 {
55 }
56 
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Constructor
60 
61 RooAbsString::RooAbsString(const char *name, const char *title, Int_t bufLen) :
62  RooAbsArg(name,title), _len(bufLen), _value(new char[bufLen])
63 {
64  setValueDirty() ;
65  setShapeDirty() ;
66 }
67 
68 
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Copy constructor
72 
73 RooAbsString::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 
84 RooAbsString::~RooAbsString()
85 {
86  delete[] _value ;
87 }
88 
89 
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Return value of object. Calculated if dirty, otherwise cached value is returned.
93 
94 const char* RooAbsString::getVal() const
95 {
96  if (isValueDirty()) {
97  clearValueDirty() ;
98  strlcpy(_value,traceEval(),_len) ;
99  }
100 
101  return _value ;
102 }
103 
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Equality operator comparing with a TString
108 
109 Bool_t RooAbsString::operator==(const char* value) const
110 {
111  return !TString(getVal()).CompareTo(value) ;
112 }
113 
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 
117 Bool_t RooAbsString::isIdentical(const RooAbsArg& other, Bool_t assumeSameType)
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 
132 Bool_t RooAbsString::operator==(const RooAbsArg& other)
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 
143 Bool_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 
153 void RooAbsString::writeToStream(ostream& /*os*/, Bool_t /*compact*/) const
154 {
155 }
156 
157 
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Print value
161 
162 void RooAbsString::printValue(ostream& os) const
163 {
164  os << getVal() ;
165 }
166 
167 
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Check if current value is valid
171 
172 Bool_t RooAbsString::isValid() const
173 {
174  return isValidString(getVal()) ;
175 }
176 
177 
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Check if given string value is valid
181 
182 Bool_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 
194 Bool_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 
204 TString RooAbsString::traceEval() const
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 
224 void RooAbsString::syncCache(const RooArgSet*)
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 
237 void 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 
253 void RooAbsString::attachToTree(TTree& t, Int_t bufSize)
254 {
255  // First determine if branch is taken
256  TBranch* branch ;
257  if ((branch = t.GetBranch(GetName()))) {
258  t.SetBranchAddress(GetName(),_value) ;
259  if (branch->GetCompressionLevel()<0) {
260  cxcoutD(DataHandling) << "RooAbsString::attachToTree(" << GetName() << ") Fixing compression level of branch " << GetName() << endl ;
261  branch->SetCompressionLevel(ROOT::RCompressionSetting::EDefaults::kUseGlobal % 100) ;
262  }
263  } else {
264  TString format(GetName());
265  format.Append("/C");
266  branch = t.Branch(GetName(), _value, (const Text_t*)format, bufSize);
267  branch->SetCompressionLevel(ROOT::RCompressionSetting::EDefaults::kUseGlobal % 100) ;
268  }
269 }
270 
271 
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Fill tree branch associated with this object
275 
276 void RooAbsString::fillTreeBranch(TTree& t)
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 
292 void RooAbsString::setTreeBranchStatus(TTree& t, Bool_t active)
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 
305 RooAbsArg *RooAbsString::createFundamental(const char* newname) const
306 {
307  RooStringVar *fund= new RooStringVar(newname?newname:GetName(),GetTitle(),"") ;
308  return fund;
309 }