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