Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooTrace.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 RooTrace.cxx
19\class RooTrace
20\ingroup Roofitcore
21
22Class RooTrace controls the memory tracing hooks in all RooFit
23objects. When tracing is active, a table of live RooFit objects
24is kept that can be queried at any time. In verbose mode, messages
25are printed in addition at the construction and destruction of
26each object.
27
28Usage example:
29\code{.cpp}
30void exampleRooTrace()
31{
32 using namespace RooFit;
33
34 // Activate RooFit memory tracing
35 RooTrace::active(true);
36
37 // Construct gauss(x,m,s)
38 RooRealVar x("x", "x", -10, 10);
39 RooRealVar m("m", "m", 0, -10, 10);
40 RooRealVar s("s", "s", 1, -10, 10);
41 RooGaussian gauss("g", "g", x, m, s);
42
43 // Show dump of all RooFit object in memory
44 RooTrace::dump();
45
46 // Activate verbose mode
47 RooTrace::verbose(true);
48
49 // Construct poly(x,p0)
50 RooRealVar p0("p0", "p0", 0.01, 0., 1.);
51 RooPolynomial poly("p", "p", x, p0);
52
53 // Put marker in trace list for future reference
54 RooTrace::mark();
55
56 // Construct model = f*gauss(x) + (1-f)*poly(x)
57 RooRealVar f("f", "f", 0.5, 0., 1.);
58 RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);
59
60 // Show object added to memory since marker
61 RooTrace::printObjectCounts();
62
63 // Since verbose mode is still on, you will see messages
64 // pertaining to destructor calls of all RooFit objects
65 // made in this macro
66 //
67 // A call to RooTrace::dump() at the end of this macro
68 // should show that there a no RooFit object left in memory
69}
70\endcode
71
72\note In the ROOT releases, the RooTrace is disabled at compile time and the
73example above will not print any objects. If you are an advanced developer who
74wants to use the RooTrace, you need to recompile ROOT after changing the
75`TRACE_CREATE` and `TRACE_DESTROY` macros in RooTrace.h to call the RooTrace
76functions:
77
78\code{.cpp}
79#define TRACE_CREATE RooTrace::create(this);
80#define TRACE_DESTROY RooTrace::destroy(this);
81\endcode
82
83However, as ROOT is not build with this by default, the RooTrace is not tested
84and there is no gurantee that this works.
85**/
86
87#include "RooTrace.h"
88#include "RooAbsArg.h"
89#include "Riostream.h"
90#include "RooMsgService.h"
91
92#include <iomanip>
93#include "TClass.h"
94
95
96using namespace std;
97
99;
100
102
103
104////////////////////////////////////////////////////////////////////////////////
105
107{
108 if (_instance==0) _instance = new RooTrace() ;
109 return *_instance ;
110}
111
112
113////////////////////////////////////////////////////////////////////////////////
114
115RooTrace::RooTrace() : _active(false), _verbose(false)
116{
117}
118
119
120
121////////////////////////////////////////////////////////////////////////////////
122/// Register creation of object 'obj'
123
124void RooTrace::create(const TObject* obj)
125{
127 if (instance._active) {
128 instance.create3(obj) ;
129 }
130
131}
132
133
134////////////////////////////////////////////////////////////////////////////////
135/// Register deletion of object 'obj'
136
138{
140 if (instance._active) {
141 instance.destroy3(obj) ;
142 }
143}
144
145
146////////////////////////////////////////////////////////////////////////////////
147
148void RooTrace::createSpecial(const char* name, int size)
149{
151 if (instance._active) {
153 }
154}
155
156
157////////////////////////////////////////////////////////////////////////////////
158
160{
162 if (instance._active) {
164 }
165}
166
167
168////////////////////////////////////////////////////////////////////////////////
169
170void RooTrace::createSpecial3(const char* name, int size)
171{
174}
175
176
177////////////////////////////////////////////////////////////////////////////////
178
180{
182}
183
184
185
186////////////////////////////////////////////////////////////////////////////////
187/// If flag is true, memory tracing is activated
188
189void RooTrace::active(bool flag)
190{
191 RooTrace::instance()._active = flag ;
192}
193
194
195////////////////////////////////////////////////////////////////////////////////
196/// If flag is true, a message will be printed at each
197/// object creation or deletion
198
199void RooTrace::verbose(bool flag)
200{
202}
203
204
205
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Back end function of create(), register creation of object 'obj'
210
212{
213 _list.Add((RooAbsArg*)obj) ;
214 if (_verbose) {
215 cout << "RooTrace::create: object " << obj << " of type " << obj->ClassName()
216 << " created " << endl ;
217 }
218}
219
220
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Back end function of destroy(), register deletion of object 'obj'
225
227{
228 if (!_list.Remove((RooAbsArg*)obj)) {
229 } else if (_verbose) {
230 cout << "RooTrace::destroy: object " << obj << " of type " << obj->ClassName()
231 << " destroyed [" << obj->GetTitle() << "]" << endl ;
232 }
233}
234
235
236
237//_____________________________________________________________________________
238
240{
241 // Back end function of create(), register creation of object 'obj'
242 _objectCount[obj->IsA()]++ ;
243}
244
245
246
247
248////////////////////////////////////////////////////////////////////////////////
249/// Back end function of destroy(), register deletion of object 'obj'
250
252{
253 _objectCount[obj->IsA()]-- ;
254}
255
256
257
258////////////////////////////////////////////////////////////////////////////////
259/// Put marker in object list, that allows to dump contents of list
260/// relative to this marker
261
263{
265}
266
267
268
269////////////////////////////////////////////////////////////////////////////////
270/// Put marker in object list, that allows to dump contents of list
271/// relative to this marker
272
274{
275 _markList = _list ;
276}
277
278
279
280////////////////////////////////////////////////////////////////////////////////
281/// Dump contents of object registry to stdout
282
284{
285 RooTrace::instance().dump3(cout,false) ;
286}
287
288
289////////////////////////////////////////////////////////////////////////////////
290
291void RooTrace::dump(ostream& os, bool sinceMarked)
292{
293 RooTrace::instance().dump3(os,sinceMarked) ;
294}
295
296
297////////////////////////////////////////////////////////////////////////////////
298/// Dump contents of object register to stream 'os'. If sinceMarked is
299/// true, only object created after the last call to mark() are shown.
300
301void RooTrace::dump3(ostream& os, bool sinceMarked)
302{
303 os << "List of RooFit objects allocated while trace active:" << endl ;
304
305
306 Int_t i, nMarked(0) ;
307 for(i=0 ; i<_list.GetSize() ; i++) {
308 if (!sinceMarked || _markList.IndexOf(_list.At(i)) == -1) {
309 os << hex << setw(10) << _list.At(i) << dec << " : " << setw(20) << _list.At(i)->ClassName() << setw(0) << " - " << _list.At(i)->GetName() << endl ;
310 } else {
311 nMarked++ ;
312 }
313 }
314 if (sinceMarked) os << nMarked << " marked objects suppressed" << endl ;
315}
316
317
318////////////////////////////////////////////////////////////////////////////////
319
321{
323}
324
325////////////////////////////////////////////////////////////////////////////////
326
328{
329 double total(0) ;
330 for (map<TClass*,int>::iterator iter = _objectCount.begin() ; iter != _objectCount.end() ; ++iter) {
331 double tot= 1.0*(iter->first->Size()*iter->second)/(1024*1024) ;
332 cout << " class " << iter->first->GetName() << " count = " << iter->second << " sizeof = " << iter->first->Size() << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
333 total+=tot ;
334 }
335
336 for (map<string,int>::iterator iter = _specialCount.begin() ; iter != _specialCount.end() ; ++iter) {
337 int size = _specialSize[iter->first] ;
338 double tot=1.0*(size*iter->second)/(1024*1024) ;
339 cout << " speeial " << iter->first << " count = " << iter->second << " sizeof = " << size << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
340 total+=tot ;
341 }
342 cout << "Grand total memory = " << Form("%5.2f",total) << " Mb" << endl ;
343
344}
345
346
347////////////////////////////////////////////////////////////////////////////////
348/// Utility function to trigger zeroing of callgrind counters.
349///
350/// Note that this function does _not_ do anything, other than optionally printing this message
351/// To trigger callgrind zero counter action, run callgrind with
352/// argument '--zero-before=RooTrace::callgrind_zero()' (include single quotes in cmdline)
353
355{
356 ooccoutD((TObject*)0,Tracing) << "RooTrace::callgrind_zero()" << endl ;
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Utility function to trigger dumping of callgrind counters.
361///
362/// Note that this function does _not_ do anything, other than optionally printing this message
363/// To trigger callgrind dumping action, run callgrind with
364/// argument '--dump-before=RooTrace::callgrind_dump()' (include single quotes in cmdline)
365
367{
368 ooccoutD((TObject*)0,Tracing) << "RooTrace::callgrind_dump()" << endl ;
369}
bool _verbose
Definition RooMinuit.h:94
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define ooccoutD(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
static unsigned int total
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
Int_t GetSize() const
TObject * At(int index) const
Return object stored in sequential position given by index.
virtual void Add(TObject *arg)
Int_t IndexOf(const char *name) const
Return position of given object in list.
virtual bool Remove(TObject *arg)
Remove object from collection.
Class RooTrace controls the memory tracing hooks in all RooFit objects.
Definition RooTrace.h:26
static void destroySpecial(const char *name)
Definition RooTrace.cxx:159
static void dump()
Dump contents of object registry to stdout.
Definition RooTrace.cxx:283
std::map< TClass *, int > _objectCount
Definition RooTrace.h:80
static void verbose(bool flag)
If flag is true, a message will be printed at each object creation or deletion.
Definition RooTrace.cxx:199
static RooTrace * _instance
Definition RooTrace.h:57
bool _active
Definition RooTrace.h:76
bool _verbose
Definition RooTrace.h:77
static void callgrind_zero()
Utility function to trigger zeroing of callgrind counters.
Definition RooTrace.cxx:354
std::map< std::string, int > _specialSize
Definition RooTrace.h:82
static void printObjectCounts()
Definition RooTrace.cxx:320
static void createSpecial(const char *name, int size)
Definition RooTrace.cxx:148
static void destroy(const TObject *obj)
Register deletion of object 'obj'.
Definition RooTrace.cxx:137
static RooTrace & instance()
Definition RooTrace.cxx:106
static void mark()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition RooTrace.cxx:262
void mark3()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition RooTrace.cxx:273
void create2(const TObject *obj)
Back end function of create(), register creation of object 'obj'.
Definition RooTrace.cxx:211
void dump3(std::ostream &, bool sinceMarked)
Dump contents of object register to stream 'os'.
Definition RooTrace.cxx:301
std::map< std::string, int > _specialCount
Definition RooTrace.h:81
void create3(const TObject *obj)
Definition RooTrace.cxx:239
void destroySpecial3(const char *name)
Definition RooTrace.cxx:179
static void create(const TObject *obj)
Register creation of object 'obj'.
Definition RooTrace.cxx:124
static void active(bool flag)
If flag is true, memory tracing is activated.
Definition RooTrace.cxx:189
void printObjectCounts3()
Definition RooTrace.cxx:327
RooLinkedList _markList
Definition RooTrace.h:79
void destroy2(const TObject *obj)
Back end function of destroy(), register deletion of object 'obj'.
Definition RooTrace.cxx:226
static void callgrind_dump()
Utility function to trigger dumping of callgrind counters.
Definition RooTrace.cxx:366
void destroy3(const TObject *obj)
Back end function of destroy(), register deletion of object 'obj'.
Definition RooTrace.cxx:251
void createSpecial3(const char *name, int size)
Definition RooTrace.cxx:170
RooLinkedList _list
Definition RooTrace.h:78
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:439
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:483
virtual TClass * IsA() const
Definition TObject.h:245