Logo ROOT  
Reference Guide
Roo1DTable.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 Roo1DTable.cxx
19 \class Roo1DTable
20 \ingroup Roofitcore
21 
22 Roo1DTable implements a one-dimensional table. A table is the category
23 equivalent of a plot. To create a table use the RooDataSet::table method.
24 **/
25 
26 #include "Roo1DTable.h"
27 
28 #include "RooFit.h"
29 #include "RooMsgService.h"
31 
32 #include "TString.h"
33 #include "TClass.h"
34 
35 #include <iostream>
36 #include <iomanip>
37 
38 using namespace std;
39 
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Create an empty table from abstract category. The number of table entries and
45 /// their names are taken from the category state labels at the time of construction,
46 /// but not reference to the category is retained after the construction phase.
47 /// Use fill() to fill the table.
48 
49 Roo1DTable::Roo1DTable(const char *name, const char *title, const RooAbsCategory& cat) :
50  RooTable(name,title), _total(0), _nOverflow(0)
51 {
52  //Take types from reference category
53  Int_t nbin=0 ;
54  TIterator* tIter = cat.typeIterator() ;
55  RooCatType* type ;
56  while (((type = (RooCatType*)tIter->Next()))) {
57  _types.Add(new RooCatType(*type)) ;
58  nbin++ ;
59  }
60  delete tIter ;
61 
62  // Create counter array and initialize
63  _count.resize(nbin) ;
64  for (int i=0 ; i<nbin ; i++) _count[i] = 0 ;
65 }
66 
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Copy constructor
71 
73  RooTable(other), _count(other._count), _total(other._total), _nOverflow(other._nOverflow)
74 {
75  // Take types from reference category
76 
77  int i;
78  for (i=0 ; i<other._types.GetEntries() ; i++) {
79  _types.Add(new RooCatType(*(RooCatType*)other._types.At(i))) ;
80  }
81 
82 }
83 
84 
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Destructor
88 
90 {
91  // We own the contents of the object array
92  _types.Delete() ;
93 }
94 
95 
96 
97 ////////////////////////////////////////////////////////////////////////////////
98 /// Increment the counter of the table slot with the name
99 /// corresponding to that of the current category state. If the
100 /// current category state matches no table slot name, the table
101 /// overflow counter is incremented.
102 
104 {
105  if (weight==0) return ;
106 
107  _total += weight ;
108 
109  //Bool_t found(kFALSE) ;
110  for (int i=0 ; i<_types.GetEntries() ; i++) {
111  RooCatType* entry = (RooCatType*) _types.At(i) ;
112  if (cat.getCurrentIndex()==entry->getVal()) {
113  _count[i] += weight ; ;
114  //found=kTRUE ;
115  return;
116  }
117  }
118 
119  //if (!found) {
120  _nOverflow += weight ;
121  //}
122 }
123 
124 
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Print the name of the table
128 
129 void Roo1DTable::printName(ostream& os) const
130 {
131  os << GetName() ;
132 }
133 
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Print the title of the table
138 
139 void Roo1DTable::printTitle(ostream& os) const
140 {
141  os << GetTitle() ;
142 }
143 
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Print the class name of the table
148 
149 void Roo1DTable::printClassName(ostream& os) const
150 {
151  os << IsA()->GetName() ;
152 }
153 
154 
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Print the table value, i.e. the contents, in 'inline' format
158 
159 void Roo1DTable::printValue(ostream& os) const
160 {
161  os << "(" ;
162  for (Int_t i=0 ; i<_types.GetEntries() ; i++) {
163  RooCatType* entry = (RooCatType*) _types.At(i) ;
164  if (_count[i]>0) {
165  if (i>0) {
166  os << "," ;
167  }
168  os << entry->GetName() << "=" << _count[i] ;
169  }
170  }
171  os << ")" ;
172 }
173 
174 
175 
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Define default contents to print
179 
181 {
182  return kName|kClassName|kValue|kArgs ;
183 }
184 
185 
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Print the formatted table contents on the given stream
189 
190 void Roo1DTable::printMultiline(ostream& os, Int_t /*contents*/, Bool_t verbose, TString indent) const
191 {
192  os << indent << endl ;
193  os << indent << " Table " << GetName() << " : " << GetTitle() << endl ;
194 
195  // Determine maximum label and count width
196  Int_t labelWidth(0) ;
197  Double_t maxCount(1) ;
198 
199  int i;
200  for (i=0 ; i<_types.GetEntries() ; i++) {
201  RooCatType* entry = (RooCatType*) _types.At(i) ;
202 
203  // Disable warning about a signed/unsigned mismatch by MSCV 6.0 by
204  // using the lwidth temporary.
205  Int_t lwidth = strlen(entry->GetName());
206  labelWidth = lwidth > labelWidth ? lwidth : labelWidth;
207  maxCount=_count[i]>maxCount?_count[i]:maxCount ;
208  }
209  // Adjust formatting if overflow field will be present
210  if (_nOverflow>0) {
211  labelWidth=labelWidth>8?labelWidth:8 ;
212  maxCount=maxCount>_nOverflow?maxCount:_nOverflow ;
213  }
214 
215  // Header
216  Int_t countWidth=((Int_t)log10(maxCount))+1 ;
217  os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
218  os << setfill(' ') ;
219 
220  // Contents
221  for (i=0 ; i<_types.GetEntries() ; i++) {
222  RooCatType* entry = (RooCatType*) _types.At(i) ;
223  if (_count[i]>0 || verbose) {
224  os << " | " << setw(labelWidth) << entry->GetName() << " | " << setw(countWidth) << _count[i] << " |" << endl ;
225  }
226  }
227 
228  // Overflow field
229  if (_nOverflow) {
230  os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
231  os << indent << " | " << "Overflow" << " | " << setw(countWidth) << _nOverflow << " |" << endl ;
232  }
233 
234  // Footer
235  os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
236  os << setfill(' ') ;
237  os << indent << endl ;
238 }
239 
240 
241 
242 ////////////////////////////////////////////////////////////////////////////////
243 /// Return the table entry named 'label'. Zero is returned if given
244 /// label doesn't occur in table.
245 
246 Double_t Roo1DTable::get(const char* label, Bool_t silent) const
247 {
248 
249  TObject* cat = _types.FindObject(label) ;
250  if (!cat) {
251  if (!silent) {
252  coutE(InputArguments) << "Roo1DTable::get: ERROR: no such entry: " << label << endl ;
253  }
254  return 0 ;
255  }
256  return _count[_types.IndexOf(cat)] ;
257 }
258 
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 /// Return the table entry named 'label'. Zero is returned if given
263 /// label doesn't occur in table.
264 
265 Double_t Roo1DTable::get(const int index, Bool_t silent) const
266 {
267  const RooCatType* cat = 0;
268  int i = 0;
269  for (; i < _types.GetEntries(); ++i) {
270  cat = static_cast<const RooCatType*>(_types[i]);
271  if (cat->getVal() == index) {
272  break;
273  } else {
274  cat = 0;
275  }
276  }
277  if (!cat) {
278  if (!silent) {
279  coutE(InputArguments) << "Roo1DTable::get: ERROR: no such entry: " << index << endl ;
280  }
281  return 0 ;
282  }
283  return _count[i] ;
284 }
285 
286 
287 
288 ////////////////////////////////////////////////////////////////////////////////
289 /// Return the number of overflow entries in the table.
290 
292 {
293  return _nOverflow ;
294 }
295 
296 
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// Return the fraction of entries in the table contained in the slot named 'label'.
300 /// The normalization includes the number of overflows.
301 /// Zero is returned if given label doesn't occur in table.
302 
303 Double_t Roo1DTable::getFrac(const char* label, Bool_t silent) const
304 {
305  if (_total) {
306  return get(label,silent) / _total ;
307  } else {
308  if (!silent) coutW(Contents) << "Roo1DTable::getFrac: WARNING table empty, returning 0" << endl ;
309  return 0. ;
310  }
311 }
312 
313 
314 
315 ////////////////////////////////////////////////////////////////////////////////
316 /// Return the fraction of entries in the table contained in the slot named 'label'.
317 /// The normalization includes the number of overflows.
318 /// Zero is returned if given label doesn't occur in table.
319 
320 Double_t Roo1DTable::getFrac(const int index, Bool_t silent) const
321 {
322  if (_total) {
323  return get(index, silent) / _total ;
324  } else {
325  if (!silent) coutW(Contents) << "Roo1DTable::getFrac: WARNING table empty, returning 0" << endl ;
326  return 0. ;
327  }
328 }
329 
330 
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// Return true if table is identical in contents to given reference table
334 
336 {
337  const Roo1DTable* other1d = &dynamic_cast<const Roo1DTable&>(other) ;
338 
339  if (!other1d) {
340  return kFALSE ;
341  }
342 
343  int i;
344  for (i=0 ; i<_types.GetEntries() ; i++) {
345  // RooCatType* entry = (RooCatType*) _types.At(i) ;
346  if (_count[i] != other1d->_count[i]) {
347  return kFALSE ;
348  }
349  }
350  return kTRUE ;
351 }
Roo1DTable::printClassName
virtual void printClassName(std::ostream &os) const
Print the class name of the table.
Definition: Roo1DTable.cxx:149
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObjArray::Delete
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
Definition: TObjArray.cxx:356
RooMsgService.h
Roo1DTable::~Roo1DTable
virtual ~Roo1DTable()
Destructor.
Definition: Roo1DTable.cxx:89
RooAbsCategory::getCurrentIndex
virtual value_type getCurrentIndex() const
Return index number of current state.
Definition: RooAbsCategory.cxx:114
Roo1DTable
Definition: Roo1DTable.h:23
RooFit.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
coutE
#define coutE(a)
Definition: RooMsgService.h:33
coutW
#define coutW(a)
Definition: RooMsgService.h:32
RooCatType::GetName
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatTypeLegacy.h:59
RooCatType::getVal
Int_t getVal() const
Definition: RooCatTypeLegacy.h:94
TObjArray::IndexOf
Int_t IndexOf(const TObject *obj) const
Definition: TObjArray.cxx:605
Roo1DTable::_nOverflow
Double_t _nOverflow
Definition: Roo1DTable.h:77
Int_t
int Int_t
Definition: RtypesCore.h:45
TObjArray::GetEntries
Int_t GetEntries() const
Return the number of objects in array (i.e.
Definition: TObjArray.cxx:523
TClass.h
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
Roo1DTable::printTitle
virtual void printTitle(std::ostream &os) const
Print the title of the table.
Definition: Roo1DTable.cxx:139
Roo1DTable::getFrac
Double_t getFrac(const char *label, Bool_t silent=kFALSE) const
Return the fraction of entries in the table contained in the slot named 'label'.
Definition: Roo1DTable.cxx:303
TObjArray::At
TObject * At(Int_t idx) const
Definition: TObjArray.h:166
Roo1DTable::getOverflow
Double_t getOverflow() const
Return the number of overflow entries in the table.
Definition: Roo1DTable.cxx:291
TString
Definition: TString.h:136
Roo1DTable::isIdentical
virtual Bool_t isIdentical(const RooTable &other)
Return true if table is identical in contents to given reference table.
Definition: Roo1DTable.cxx:335
TString.h
Roo1DTable::printName
virtual void printName(std::ostream &os) const
Print the name of the table.
Definition: Roo1DTable.cxx:129
bool
TIterator
Definition: TIterator.h:30
TObjArray::Add
void Add(TObject *obj)
Definition: TObjArray.h:74
Roo1DTable::_types
TObjArray _types
Definition: Roo1DTable.h:74
RooAbsCategory
Definition: RooAbsCategory.h:38
Roo1DTable::Roo1DTable
Roo1DTable()
Definition: Roo1DTable.h:41
RooPrintable::kName
@ kName
Definition: RooPrintable.h:33
RooTable
Definition: RooTable.h:23
Option_t
const typedef char Option_t
Definition: RtypesCore.h:66
log10
double log10(double)
Roo1DTable::_total
Double_t _total
Definition: Roo1DTable.h:76
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooPrintable::kArgs
@ kArgs
Definition: RooPrintable.h:33
Roo1DTable::fill
virtual void fill(RooAbsCategory &cat, Double_t weight=1.0)
Increment the counter of the table slot with the name corresponding to that of the current category s...
Definition: Roo1DTable.cxx:103
RooPrintable::kValue
@ kValue
Definition: RooPrintable.h:33
RooAbsCategory::typeIterator
TIterator * typeIterator() const
Definition: RooAbsCategory.cxx:653
TIterator::Next
virtual TObject * Next()=0
RooPrintable::kClassName
@ kClassName
Definition: RooPrintable.h:33
Double_t
double Double_t
Definition: RtypesCore.h:59
TObjArray::FindObject
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
Definition: TObjArray.cxx:415
Roo1DTable.h
Roo1DTable::get
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:246
TObject
Definition: TObject.h:37
name
char name[80]
Definition: TGX11.cxx:110
RooCatType
Definition: RooCatTypeLegacy.h:23
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
Roo1DTable::_count
std::vector< Double_t > _count
Definition: Roo1DTable.h:75
Roo1DTable::printValue
virtual void printValue(std::ostream &os) const
Print the table value, i.e. the contents, in 'inline' format.
Definition: Roo1DTable.cxx:159
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
type
int type
Definition: TGX11.cxx:121
Roo1DTable::defaultPrintContents
virtual Int_t defaultPrintContents(Option_t *opt) const
Define default contents to print.
Definition: Roo1DTable.cxx:180
RooCatTypeLegacy.h
RooFit::Contents
@ Contents
Definition: RooGlobalFunc.h:69
Roo1DTable::printMultiline
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print the formatted table contents on the given stream.
Definition: Roo1DTable.cxx:190
int