Roo1DTable.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: Roo1DTable.cxx 25184 2008-08-20 13:59:55Z 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 // Roo1DTable implements a one-dimensional table. A table is the category
00021 // equivalent of a plot. To create a table use the RooDataSet::table method.
00022 // END_HTML
00023 //
00024 
00025 #include "RooFit.h"
00026 
00027 #include "Riostream.h"
00028 #include <iomanip>
00029 #include "TString.h"
00030 #include "TMath.h"
00031 #include "Roo1DTable.h"
00032 #include "RooMsgService.h"
00033 #include "TClass.h"
00034 
00035 using namespace std ;
00036 
00037 ClassImp(Roo1DTable)
00038 
00039 
00040 //_____________________________________________________________________________
00041 Roo1DTable::Roo1DTable(const char *name, const char *title, const RooAbsCategory& cat) : 
00042   RooTable(name,title), _total(0), _nOverflow(0)
00043 {
00044   // Create an empty table from abstract category. The number of table entries and 
00045   // their names are taken from the category state labels at the time of construction,
00046   // but not reference to the category is retained after the construction phase.
00047   // Use fill() to fill the table.
00048 
00049   //Take types from reference category
00050   Int_t nbin=0 ;
00051   TIterator* tIter = cat.typeIterator() ;
00052   RooCatType* type ;
00053   while (((type = (RooCatType*)tIter->Next()))) {
00054     _types.Add(new RooCatType(*type)) ;
00055     nbin++ ;
00056   }
00057   delete tIter ;
00058 
00059   // Create counter array and initialize
00060   _count.resize(nbin) ;
00061   for (int i=0 ; i<nbin ; i++) _count[i] = 0 ;
00062 }
00063 
00064 
00065 
00066 //_____________________________________________________________________________
00067 Roo1DTable::Roo1DTable(const Roo1DTable& other) : 
00068   RooTable(other), _count(other._count), _total(other._total), _nOverflow(other._nOverflow)
00069 {  
00070   // Copy constructor
00071 
00072   // Take types from reference category
00073 
00074   int i;
00075   for (i=0 ; i<other._types.GetEntries() ; i++) {
00076     _types.Add(new RooCatType(*(RooCatType*)other._types.At(i))) ;
00077   }
00078 
00079 }
00080 
00081 
00082 
00083 //_____________________________________________________________________________
00084 Roo1DTable::~Roo1DTable()
00085 {
00086   // Destructor
00087 
00088   // We own the contents of the object array
00089   _types.Delete() ;
00090 }
00091 
00092 
00093 
00094 //_____________________________________________________________________________
00095 void Roo1DTable::fill(RooAbsCategory& cat, Double_t weight) 
00096 {
00097   // Increment the counter of the table slot with the name
00098   // corresponding to that of the current category state. If the
00099   // current category state matches no table slot name, the table
00100   // overflow counter is incremented.
00101 
00102   if (weight==0) return ;
00103 
00104   _total += weight ;
00105 
00106   //Bool_t found(kFALSE) ;
00107   for (int i=0 ; i<_types.GetEntries() ; i++) {
00108     RooCatType* entry = (RooCatType*) _types.At(i) ;
00109     if (cat.getIndex()==entry->getVal()) {
00110       _count[i] += weight ; ;
00111       //found=kTRUE ;
00112       return;
00113     }
00114   }  
00115 
00116   //if (!found) {
00117   _nOverflow += weight ;
00118   //}
00119 }
00120 
00121 
00122 
00123 //_____________________________________________________________________________
00124 void Roo1DTable::printName(ostream& os) const 
00125 {
00126   // Print the name of the table
00127   os << GetName() ;
00128 }
00129 
00130 
00131 
00132 //_____________________________________________________________________________
00133 void Roo1DTable::printTitle(ostream& os) const 
00134 {
00135   // Print the title of the table
00136   os << GetTitle() ;
00137 }
00138 
00139 
00140 
00141 //_____________________________________________________________________________
00142 void Roo1DTable::printClassName(ostream& os) const 
00143 {
00144   // Print the class name of the table
00145   os << IsA()->GetName() ;
00146 }
00147 
00148 
00149 
00150 //_____________________________________________________________________________
00151 void Roo1DTable::printValue(ostream& os) const 
00152 {
00153   // Print the table value, i.e. the contents, in 'inline' format
00154   os << "(" ;
00155   for (Int_t i=0 ; i<_types.GetEntries() ; i++) {
00156     RooCatType* entry = (RooCatType*) _types.At(i) ;
00157     if (_count[i]>0) {
00158       if (i>0) {
00159         os << "," ;
00160       }
00161       os << entry->GetName() << "=" << _count[i] ;
00162     }
00163   }
00164   os << ")" ;
00165 }
00166 
00167 
00168 
00169 
00170 //_____________________________________________________________________________
00171 Int_t Roo1DTable::defaultPrintContents(Option_t* /*opt*/) const 
00172 {
00173   // Define default contents to print
00174   return kName|kClassName|kValue|kArgs ;
00175 }
00176 
00177 
00178 
00179 //_____________________________________________________________________________
00180 void Roo1DTable::printMultiline(ostream& os, Int_t /*contents*/, Bool_t verbose, TString indent) const 
00181 {
00182   // Print the formatted table contents on the given stream
00183   
00184   os << indent << endl ;
00185   os << indent << "  Table " << GetName() << " : " << GetTitle() << endl ;
00186 
00187   // Determine maximum label and count width
00188   Int_t labelWidth(0) ;
00189   Double_t maxCount(1) ;
00190 
00191   int i;
00192   for (i=0 ; i<_types.GetEntries() ; i++) {
00193     RooCatType* entry = (RooCatType*) _types.At(i) ;
00194 
00195     // Disable warning about a signed/unsigned mismatch by MSCV 6.0 by
00196     // using the lwidth temporary.
00197     Int_t lwidth = strlen(entry->GetName());
00198     labelWidth = lwidth > labelWidth ? lwidth : labelWidth;
00199     maxCount=_count[i]>maxCount?_count[i]:maxCount ;
00200   }
00201   // Adjust formatting if overflow field will be present
00202   if (_nOverflow>0) {
00203     labelWidth=labelWidth>8?labelWidth:8 ;
00204     maxCount=maxCount>_nOverflow?maxCount:_nOverflow ;
00205   }
00206 
00207   // Header
00208   Int_t countWidth=((Int_t)log10(maxCount))+1 ;
00209   os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
00210   os << setfill(' ') ;
00211 
00212   // Contents
00213   for (i=0 ; i<_types.GetEntries() ; i++) {
00214     RooCatType* entry = (RooCatType*) _types.At(i) ;
00215     if (_count[i]>0 || verbose) {
00216       os << "  | " << setw(labelWidth) << entry->GetName() << " | " << setw(countWidth) << _count[i] << " |" << endl ;
00217     }
00218   }
00219 
00220   // Overflow field
00221   if (_nOverflow) {
00222     os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
00223     os << indent << "  | " << "Overflow" << " | " << setw(countWidth) << _nOverflow << " |" << endl ;    
00224   }
00225 
00226   // Footer
00227   os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
00228   os << setfill(' ') ;
00229   os << indent << endl ;
00230 }
00231 
00232 
00233 
00234 //_____________________________________________________________________________
00235 Double_t Roo1DTable::get(const char* label, Bool_t silent) const 
00236 {
00237   // Return the table entry named 'label'. Zero is returned if given
00238   // label doesn't occur in table.
00239 
00240 
00241   TObject* cat = _types.FindObject(label) ;
00242   if (!cat) {
00243     if (!silent) {
00244       coutE(InputArguments) << "Roo1DTable::get: ERROR: no such entry: " << label << endl ;
00245     }
00246     return 0 ;
00247   }
00248   return _count[_types.IndexOf(cat)] ;
00249 }
00250 
00251 
00252 
00253 //_____________________________________________________________________________
00254 Double_t Roo1DTable::getOverflow() const 
00255 {
00256   // Return the number of overflow entries in the table.
00257 
00258   return _nOverflow ;
00259 }
00260 
00261 
00262 
00263 //_____________________________________________________________________________
00264 Double_t Roo1DTable::getFrac(const char* label, Bool_t silent) const 
00265 {
00266   // Return the fraction of entries in the table contained in the slot named 'label'. 
00267   // The normalization includes the number of overflows.
00268   // Zero is returned if given label doesn't occur in table.   
00269 
00270   if (_total) {
00271     return get(label,silent) / _total ;
00272   } else {
00273     if (!silent) coutW(Contents) << "Roo1DTable::getFrac: WARNING table empty, returning 0" << endl ;
00274     return 0. ;
00275   }
00276 }
00277 
00278 
00279 
00280 //_____________________________________________________________________________
00281 Bool_t Roo1DTable::isIdentical(const RooTable& other) 
00282 {
00283   // Return true if table is identical in contents to given reference table
00284 
00285   const Roo1DTable* other1d = &dynamic_cast<const Roo1DTable&>(other) ;
00286 
00287   if (!other1d) {
00288     return kFALSE ;
00289   }
00290 
00291   int i;
00292   for (i=0 ; i<_types.GetEntries() ; i++) {
00293     // RooCatType* entry = (RooCatType*) _types.At(i) ;        
00294     if (_count[i] != other1d->_count[i]) {
00295       return kFALSE ;
00296     }
00297   }
00298   return kTRUE ;
00299 }

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