RooAbsString.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooAbsString.cxx 36230 2010-10-09 20:21:02Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 // 
00019 // BEGIN_HTML
00020 // RooAbsString is the common abstract base class for objects that represent a
00021 // string value
00022 // 
00023 // Implementation of RooAbsString may be derived, there no interface
00024 // is provided to modify the contents
00025 // END_HTML
00026 //
00027 // 
00028 
00029 #include "RooFit.h"
00030 
00031 #include "Riostream.h"
00032 #include "Riostream.h"
00033 #include "TObjString.h"
00034 #include "TH1.h"
00035 #include "TTree.h"
00036 
00037 #include "RooArgSet.h"
00038 #include "RooAbsString.h"
00039 #include "RooStringVar.h"
00040 #include "RooMsgService.h"
00041 
00042 ClassImp(RooAbsString) 
00043 ;
00044 
00045 
00046 //_____________________________________________________________________________
00047 RooAbsString::RooAbsString() : RooAbsArg(), _len(128) , _value(new char[128])
00048 {
00049   // Default constructor
00050 }
00051 
00052 
00053 //_____________________________________________________________________________
00054 RooAbsString::RooAbsString(const char *name, const char *title, Int_t bufLen) : 
00055   RooAbsArg(name,title), _len(bufLen), _value(new char[bufLen]) 
00056 {
00057   // Constructor
00058 
00059   setValueDirty() ;
00060   setShapeDirty() ;
00061 }
00062 
00063 
00064 
00065 //_____________________________________________________________________________
00066 RooAbsString::RooAbsString(const RooAbsString& other, const char* name) : 
00067   RooAbsArg(other, name), _len(other._len), _value(new char[other._len])
00068 {
00069   // Copy constructor
00070 
00071   strlcpy(_value,other._value,_len) ;
00072 }
00073 
00074 
00075 
00076 //_____________________________________________________________________________
00077 RooAbsString::~RooAbsString()
00078 {
00079   // Destructor
00080 
00081   delete[] _value ;
00082 }
00083 
00084 
00085 
00086 //_____________________________________________________________________________
00087 const char* RooAbsString::getVal() const
00088 {
00089   // Return value of object. Calculated if dirty, otherwise cached value is returned.
00090 
00091   if (isValueDirty()) {
00092     clearValueDirty() ;
00093     strlcpy(_value,traceEval(),_len) ;
00094   } 
00095   
00096   return _value ;
00097 }
00098 
00099 
00100 
00101 //_____________________________________________________________________________
00102 Bool_t RooAbsString::operator==(const char* value) const
00103 {
00104   // Equality operator comparing with a TString
00105 
00106   return !TString(getVal()).CompareTo(value) ;
00107 }
00108 
00109 
00110 
00111 //_____________________________________________________________________________
00112 Bool_t RooAbsString::operator==(const RooAbsArg& other) 
00113 {
00114   // Equality operator comparing to another RooAbsArg
00115 
00116   const RooAbsString* otherString = dynamic_cast<const RooAbsString*>(&other) ;
00117   return otherString ? operator==(otherString->getVal()) : kFALSE ;
00118 }
00119 
00120 
00121 
00122 //_____________________________________________________________________________
00123 Bool_t RooAbsString::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/) 
00124 {
00125   //Read object contents from stream (dummy for now)
00126   return kFALSE ;
00127 } 
00128 
00129 
00130 
00131 //_____________________________________________________________________________
00132 void RooAbsString::writeToStream(ostream& /*os*/, Bool_t /*compact*/) const
00133 {
00134   //Write object contents to stream (dummy for now)
00135 }
00136 
00137 
00138 
00139 //_____________________________________________________________________________
00140 void RooAbsString::printValue(ostream& os) const
00141 {
00142   // Print value
00143   os << getVal() ;
00144 }
00145 
00146 
00147 
00148 //_____________________________________________________________________________
00149 Bool_t RooAbsString::isValid() const 
00150 {
00151   // Check if current value is valid
00152   return isValidString(getVal()) ;
00153 }
00154 
00155 
00156 
00157 //_____________________________________________________________________________
00158 Bool_t RooAbsString::isValidString(const char* value, Bool_t /*printError*/) const 
00159 {
00160   // Check if given string value is valid
00161 
00162   // Protect against string overflows
00163   if (TString(value).Length()>_len) return kFALSE ;
00164 
00165   return kTRUE ;
00166 }
00167 
00168 
00169 //_____________________________________________________________________________
00170 Bool_t RooAbsString::traceEvalHook(const char* /*value*/) const 
00171 { 
00172   // Hook function for trace evaluation
00173   return kFALSE ; 
00174 }
00175 
00176 
00177 
00178 //_____________________________________________________________________________
00179 const char* RooAbsString::traceEval() const
00180 {
00181   // Calculate current value of object, with error tracing wrapper
00182 
00183   const char* value = evaluate() ;
00184   
00185   //Standard tracing code goes here
00186   if (!isValidString(value)) {
00187     cxcoutD(Tracing) << "RooAbsString::traceEval(" << GetName() << "): new output too long (>" << _len << " chars): " << value << endl ;
00188   }
00189 
00190   //Call optional subclass tracing code
00191   traceEvalHook(value) ;
00192 
00193   return value ;
00194 }
00195 
00196 
00197 
00198 //_____________________________________________________________________________
00199 void RooAbsString::syncCache(const RooArgSet*) 
00200 { 
00201   // Forcibly bring internal cache up-to-date
00202   getVal() ; 
00203 }
00204 
00205 
00206 
00207 //_____________________________________________________________________________
00208 void RooAbsString::copyCache(const RooAbsArg* source, Bool_t /*valueOnly*/) 
00209 {
00210   // Copy cache of another RooAbsArg to our cache
00211   //
00212   // Warning: This function copies the cached values of source,
00213   //          it is the callers responsibility to make sure the cache is clean
00214 
00215   RooAbsString* other = dynamic_cast<RooAbsString*>(const_cast<RooAbsArg*>(source)) ;
00216   assert(other!=0) ;
00217 
00218   strlcpy(_value,other->_value,_len) ;
00219   setValueDirty() ;
00220 }
00221 
00222 
00223 
00224 //_____________________________________________________________________________
00225 void RooAbsString::attachToTree(TTree& t, Int_t bufSize)
00226 {
00227   // Attach object to a branch of given TTree
00228 
00229   // First determine if branch is taken
00230   TBranch* branch ;
00231   if ((branch = t.GetBranch(GetName()))) {
00232     t.SetBranchAddress(GetName(),_value) ;
00233     if (branch->GetCompressionLevel()<0) {
00234       cxcoutD(DataHandling) << "RooAbsString::attachToTree(" << GetName() << ") Fixing compression level of branch " << GetName() << endl ;
00235       branch->SetCompressionLevel(1) ;
00236     }
00237   } else {
00238     TString format(GetName());
00239     format.Append("/C");
00240     branch = t.Branch(GetName(), _value, (const Text_t*)format, bufSize);
00241     branch->SetCompressionLevel(1) ;
00242   }
00243 }
00244  
00245 
00246 
00247 //_____________________________________________________________________________
00248 void RooAbsString::fillTreeBranch(TTree& t) 
00249 {
00250   // Fill tree branch associated with this object
00251 
00252   // First determine if branch is taken
00253   TBranch* branch = t.GetBranch(GetName()) ;
00254   if (!branch) { 
00255     coutE(DataHandling) << "RooAbsString::fillTreeBranch(" << GetName() << ") ERROR: not attached to tree" << endl ;
00256     assert(0) ;
00257   }
00258   branch->Fill() ;  
00259 }
00260 
00261 
00262 
00263 //_____________________________________________________________________________
00264 void RooAbsString::setTreeBranchStatus(TTree& t, Bool_t active) 
00265 {
00266   // (De)Activate associated tree branch
00267 
00268   TBranch* branch = t.GetBranch(GetName()) ;
00269   if (branch) { 
00270     t.SetBranchStatus(GetName(),active?1:0) ;
00271   }
00272 }
00273 
00274 
00275 
00276 //_____________________________________________________________________________
00277 RooAbsArg *RooAbsString::createFundamental(const char* newname) const 
00278 {
00279   // Create a RooStringVar fundamental object with our properties.
00280 
00281   RooStringVar *fund= new RooStringVar(newname?newname:GetName(),GetTitle(),"") ; 
00282   return fund;
00283 }

Generated on Tue Jul 5 15:05:55 2011 for ROOT_528-00b_version by  doxygen 1.5.1