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