Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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::ostream, std::setw, std::setfill;
38
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
47Roo1DTable::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 ;
54 while (((type = static_cast<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(*static_cast<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
101void Roo1DTable::fill(RooAbsCategory& cat, double weight)
102{
103 if (weight==0) return ;
104
105 _total += weight ;
106
107 //bool found(false) ;
108 for (int i=0 ; i<_types.GetEntries() ; i++) {
109 RooCatType* entry = static_cast<RooCatType*>(_types.At(i)) ;
110 if (cat.getCurrentIndex()==entry->getVal()) {
111 _count[i] += weight ;
112 //found=true ;
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
127void Roo1DTable::printName(ostream& os) const
128{
129 os << GetName() ;
130}
131
132
133
134////////////////////////////////////////////////////////////////////////////////
135/// Print the title of the table
136
137void Roo1DTable::printTitle(ostream& os) const
138{
139 os << GetTitle() ;
140}
141
142
143
144////////////////////////////////////////////////////////////////////////////////
145/// Print the class name of the table
146
147void Roo1DTable::printClassName(ostream& os) const
148{
149 os << ClassName() ;
150}
151
152
153
154////////////////////////////////////////////////////////////////////////////////
155/// Print the table value, i.e. the contents, in 'inline' format
156
157void Roo1DTable::printValue(ostream& os) const
158{
159 os << "(" ;
160 for (Int_t i=0 ; i<_types.GetEntries() ; i++) {
161 RooCatType* entry = static_cast<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
182
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// Print the formatted table contents on the given stream
187
188void Roo1DTable::printMultiline(ostream& os, Int_t /*contents*/, bool verbose, TString indent) const
189{
190 os << indent << std::endl ;
191 os << indent << " Table " << GetName() << " : " << GetTitle() << std::endl ;
192
193 // Determine maximum label and count width
194 Int_t labelWidth(0) ;
195 double maxCount(1) ;
196
197 int i;
198 for (i=0 ; i<_types.GetEntries() ; i++) {
199 RooCatType* entry = static_cast<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());
206 }
207 // Adjust formatting if overflow field will be present
208 if (_nOverflow>0) {
211 }
212
213 // Header
214 Int_t countWidth=((Int_t)log10(maxCount))+1 ;
215 os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << std::endl ;
216 os << setfill(' ') ;
217
218 // Contents
219 for (i=0 ; i<_types.GetEntries() ; i++) {
220 RooCatType* entry = static_cast<RooCatType*>(_types.At(i)) ;
221 if (_count[i]>0 || verbose) {
222 os << " | " << setw(labelWidth) << entry->GetName() << " | " << setw(countWidth) << _count[i] << " |" << std::endl ;
223 }
224 }
225
226 // Overflow field
227 if (_nOverflow) {
228 os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << std::endl ;
229 os << indent << " | " << "Overflow" << " | " << setw(countWidth) << _nOverflow << " |" << std::endl ;
230 }
231
232 // Footer
233 os << indent << " +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << std::endl ;
234 os << setfill(' ') ;
235 os << indent << std::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
244double Roo1DTable::get(const char* label, bool 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 << std::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
263double Roo1DTable::get(const int index, bool silent) const
264{
265 const RooCatType* cat = nullptr;
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 = nullptr;
273 }
274 }
275 if (!cat) {
276 if (!silent) {
277 coutE(InputArguments) << "Roo1DTable::get: ERROR: no such entry: " << index << std::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
301double Roo1DTable::getFrac(const char* label, bool 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" << std::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
318double Roo1DTable::getFrac(const int index, bool 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" << std::endl ;
324 return 0. ;
325 }
326}
327
328
329
330////////////////////////////////////////////////////////////////////////////////
331/// Return true if table is identical in contents to given reference table
332
333bool Roo1DTable::isIdentical(const RooTable& other, bool /*verbose*/)
334{
335 const Roo1DTable* other1d = &dynamic_cast<const Roo1DTable&>(other) ;
336
337 if (!other1d) {
338 return false ;
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 false ;
346 }
347 }
348 return true ;
349}
#define coutW(a)
#define coutE(a)
int Int_t
Definition RtypesCore.h:45
const char Option_t
Definition RtypesCore.h:66
static void indent(ostringstream &buf, int indent_level)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
Abstract interface for table objects.
Definition RooTable.h:32
Iterator abstract base class.
Definition TIterator.h:30
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