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