RooNameSet.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooNameSet.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 // RooNameSet is a utility class that stores the names the objects
00021 // in a RooArget. This allows to preserve the contents of a RooArgSet
00022 // in a specific use contents beyond the lifespan of the object in
00023 // the RooArgSet. A new RooArgSet can be created from a RooNameSet
00024 // by offering it a list of new RooAbsArg objects. 
00025 // END_HTML
00026 //
00027 
00028 #include "RooFit.h"
00029 #include "Riostream.h"
00030 
00031 #include "TObjString.h"
00032 #include "TClass.h"
00033 #include "RooNameSet.h"
00034 #include "RooArgSet.h"
00035 #include "RooArgList.h"
00036 
00037 
00038 
00039 ClassImp(RooNameSet)
00040 ;
00041 
00042 
00043 //_____________________________________________________________________________
00044 RooNameSet::RooNameSet()
00045 {
00046   // Default constructor
00047 
00048   _len = 1024 ;
00049   _nameList = new char[_len] ;
00050   _nameList[0] = 0 ;
00051   
00052 }
00053 
00054 
00055 
00056 
00057 //_____________________________________________________________________________
00058 RooNameSet::RooNameSet(const RooArgSet& argSet)
00059 {
00060   // Construct from RooArgSet
00061 
00062   _len = 1024 ;
00063   _nameList = new char[_len] ;
00064   _nameList[0] = 0 ;
00065   refill(argSet) ;
00066 }
00067 
00068 
00069 
00070 //_____________________________________________________________________________
00071 RooNameSet::RooNameSet(const RooNameSet& other) : TObject(other), RooPrintable(other), _nameList()
00072 {
00073   // Copy constructor
00074 
00075   _len = other._len ;
00076   _nameList = new char[_len] ;
00077   strlcpy(_nameList,other._nameList,_len) ;
00078 }
00079 
00080 
00081 
00082 //_____________________________________________________________________________
00083 void RooNameSet::extendBuffer(Int_t inc)
00084 {
00085   // Increment internal buffer by specified amount
00086   char * newbuf = new char[_len+inc] ;
00087   strncpy(newbuf,_nameList,_len) ;
00088   delete[] _nameList ;
00089   _nameList = newbuf ;
00090   _len += inc ;
00091 }
00092 
00093 
00094 
00095 //_____________________________________________________________________________
00096 void RooNameSet::refill(const RooArgSet& argSet) 
00097 {
00098   // Refill internal contents from names in given argSet
00099 
00100   RooArgList tmp(argSet) ;
00101   tmp.sort() ;
00102   TIterator* iter = tmp.createIterator() ;
00103   RooAbsArg* arg ;
00104   char *ptr=_nameList ;
00105   char *end=_nameList+_len-2 ;
00106   *ptr = 0 ;
00107   while((arg=(RooAbsArg*)iter->Next())) {    
00108     const char* argName = arg->GetName() ;
00109     while((*ptr++ = *argName++)) {
00110       if (ptr>=end) {
00111         // Extend buffer
00112         Int_t offset = ptr-_nameList ;
00113         extendBuffer(1024) ;
00114         ptr = _nameList + offset ;
00115         end = _nameList + _len - 2;
00116       }
00117     }
00118     *(ptr-1) = ':' ;
00119   }
00120   if (ptr>_nameList) *(ptr-1)= 0 ;
00121   delete iter ;
00122 }
00123 
00124 
00125 
00126 //_____________________________________________________________________________
00127 RooArgSet* RooNameSet::select(const RooArgSet& list) const 
00128 {
00129   // Construct a RooArgSet of objects in input 'list'
00130   // whose names match to those in the internal name
00131   // list of RooNameSet
00132 
00133   RooArgSet* output = new RooArgSet ;
00134 
00135   char buffer[1024] ;
00136   strlcpy(buffer,_nameList,1024) ;
00137   char* token = strtok(buffer,":") ;
00138   
00139   while(token) {
00140     RooAbsArg* arg =  list.find(token) ;
00141     if (arg) output->add(*arg) ;
00142     token = strtok(0,":") ;
00143   }
00144 
00145   return output ;
00146 }
00147 
00148 
00149 
00150 //_____________________________________________________________________________
00151 RooNameSet::~RooNameSet() 
00152 {
00153   // Destructor
00154 
00155   delete[] _nameList ;
00156 }
00157 
00158 
00159 
00160 //_____________________________________________________________________________
00161 Bool_t RooNameSet::operator==(const RooNameSet& other) 
00162 {
00163   // Comparison operator
00164 
00165   // Check comparison against self
00166   if (&other==this) return kTRUE ;
00167 
00168   // First check for equal length
00169   if (strlen(_nameList) != strlen(other._nameList)) return kFALSE ;
00170 
00171   return (!strcmp(_nameList,other._nameList)) ;
00172 }
00173 
00174 
00175 
00176 //_____________________________________________________________________________
00177 RooNameSet& RooNameSet::operator=(const RooNameSet& other) 
00178 {
00179   // Assignment operator
00180 
00181   delete[] _nameList ;
00182 
00183   _len = other._len ;
00184   _nameList = new char[_len] ;
00185   strlcpy(_nameList,other._nameList,_len) ;  
00186 
00187   return *this ;
00188 }
00189 
00190 
00191 //_____________________________________________________________________________
00192 void RooNameSet::printName(ostream& os) const 
00193 {
00194   // Print name of nameset
00195   os << GetName() ;
00196 }
00197 
00198 
00199 //_____________________________________________________________________________
00200 void RooNameSet::printTitle(ostream& os) const 
00201 {
00202   // Print title of nameset
00203   os << GetTitle() ;
00204 }
00205 
00206 
00207 //_____________________________________________________________________________
00208 void RooNameSet::printClassName(ostream& os) const 
00209 {
00210   // Print class name of nameset
00211   os << IsA()->GetName() ;
00212 }
00213 
00214 
00215 //_____________________________________________________________________________
00216 void RooNameSet::printValue(ostream& os) const 
00217 {
00218   // Print value of nameset, i.e the list of names
00219   os << _nameList ;
00220 }

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