Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TClass.h"
34
35#include <iostream>
36#include <iomanip>
37
38using 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
49Roo1DTable::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() ;
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
129void Roo1DTable::printName(ostream& os) const
130{
131 os << GetName() ;
132}
133
134
135
136////////////////////////////////////////////////////////////////////////////////
137/// Print the title of the table
138
139void Roo1DTable::printTitle(ostream& os) const
140{
141 os << GetTitle() ;
142}
143
144
145
146////////////////////////////////////////////////////////////////////////////////
147/// Print the class name of the table
148
149void 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
159void 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{
183}
184
185
186
187////////////////////////////////////////////////////////////////////////////////
188/// Print the formatted table contents on the given stream
189
190void 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
246Double_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
265Double_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
303Double_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
320Double_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}
#define coutW(a)
#define coutE(a)
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
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.
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
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'.
virtual Int_t defaultPrintContents(Option_t *opt) const
Define default contents to print.
virtual void printTitle(std::ostream &os) const
Print the title of the table.
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.
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...
virtual ~Roo1DTable()
Destructor.
Double_t getOverflow() const
Return the number of overflow entries in the table.
virtual void printClassName(std::ostream &os) const
Print the class name of the table.
TObjArray _types
Definition Roo1DTable.h:60
virtual void printName(std::ostream &os) const
Print the name of the table.
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.
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
void Add(TObject *obj)
Definition TObjArray.h:74
Int_t GetEntries() const
Return the number of objects in array (i.e.
virtual void Delete(Option_t *option="")
Remove all objects from the array AND delete all heap based objects.
virtual TObject * FindObject(const char *name) const
Find an object in this collection using its name.
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:136