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