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
22Controls 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 guarantee 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 std::cout, std::endl, std::ostream, std::setw, std::hex, std::dec, std::map, std::string;
97
99
101
102
103////////////////////////////////////////////////////////////////////////////////
104
106{
107 if (_instance==nullptr) _instance = new RooTrace() ;
108 return *_instance ;
109}
110
111
112////////////////////////////////////////////////////////////////////////////////
113
114RooTrace::RooTrace() : _active(false), _verbose(false)
115{
116}
117
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Register creation of object 'obj'
122
123void RooTrace::create(const TObject* obj)
124{
126 if (instance._active) {
127 instance.create3(obj) ;
128 }
129
130}
131
132
133////////////////////////////////////////////////////////////////////////////////
134/// Register deletion of object 'obj'
135
137{
139 if (instance._active) {
140 instance.destroy3(obj) ;
141 }
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146
147void RooTrace::createSpecial(const char* name, int size)
148{
150 if (instance._active) {
152 }
153}
154
155
156////////////////////////////////////////////////////////////////////////////////
157
159{
161 if (instance._active) {
163 }
164}
165
166
167////////////////////////////////////////////////////////////////////////////////
168
169void RooTrace::createSpecial3(const char* name, int size)
170{
173}
174
175
176////////////////////////////////////////////////////////////////////////////////
177
179{
181}
182
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// If flag is true, memory tracing is activated
187
188void RooTrace::active(bool flag)
189{
190 RooTrace::instance()._active = flag ;
191}
192
193
194////////////////////////////////////////////////////////////////////////////////
195/// If flag is true, a message will be printed at each
196/// object creation or deletion
197
198void RooTrace::verbose(bool flag)
199{
201}
202
203
204
205
206
207////////////////////////////////////////////////////////////////////////////////
208/// Back end function of create(), register creation of object 'obj'
209
211{
212 _list.Add(const_cast<RooAbsArg *>(static_cast<RooAbsArg const*>(obj)));
213 if (_verbose) {
214 cout << "RooTrace::create: object " << obj << " of type " << obj->ClassName()
215 << " created " << endl ;
216 }
217}
218
219
220
221
222////////////////////////////////////////////////////////////////////////////////
223/// Back end function of destroy(), register deletion of object 'obj'
224
226{
227 if (!_list.Remove(const_cast<RooAbsArg *>(static_cast<RooAbsArg const*>(obj)))) {
228 } else if (_verbose) {
229 cout << "RooTrace::destroy: object " << obj << " of type " << obj->ClassName()
230 << " destroyed [" << obj->GetTitle() << "]" << endl ;
231 }
232}
233
234
235
236//_____________________________________________________________________________
237
239{
240 // Back end function of create(), register creation of object 'obj'
241 _objectCount[obj->IsA()]++ ;
242}
243
244
245
246
247////////////////////////////////////////////////////////////////////////////////
248/// Back end function of destroy(), register deletion of object 'obj'
249
251{
252 _objectCount[obj->IsA()]-- ;
253}
254
255
256
257////////////////////////////////////////////////////////////////////////////////
258/// Put marker in object list, that allows to dump contents of list
259/// relative to this marker
260
262{
264}
265
266
267
268////////////////////////////////////////////////////////////////////////////////
269/// Put marker in object list, that allows to dump contents of list
270/// relative to this marker
271
273{
274 _markList = _list ;
275}
276
277
278
279////////////////////////////////////////////////////////////////////////////////
280/// Dump contents of object registry to stdout
281
283{
284 RooTrace::instance().dump3(cout,false) ;
285}
286
287
288////////////////////////////////////////////////////////////////////////////////
289
290void RooTrace::dump(ostream& os, bool sinceMarked)
291{
292 RooTrace::instance().dump3(os,sinceMarked) ;
293}
294
295
296////////////////////////////////////////////////////////////////////////////////
297/// Dump contents of object register to stream 'os'. If sinceMarked is
298/// true, only object created after the last call to mark() are shown.
299
300void RooTrace::dump3(ostream& os, bool sinceMarked)
301{
302 os << "List of RooFit objects allocated while trace active:" << endl ;
303
304 Int_t i;
305 Int_t nMarked(0);
306 for(i=0 ; i<_list.GetSize() ; i++) {
307 if (!sinceMarked || _markList.IndexOf(_list.At(i)) == -1) {
308 os << hex << setw(10) << _list.At(i) << dec << " : " << setw(20) << _list.At(i)->ClassName() << setw(0) << " - " << _list.At(i)->GetName() << endl ;
309 } else {
310 nMarked++ ;
311 }
312 }
313 if (sinceMarked) os << nMarked << " marked objects suppressed" << endl ;
314}
315
316
317////////////////////////////////////////////////////////////////////////////////
318
320{
322}
323
324////////////////////////////////////////////////////////////////////////////////
325
327{
328 double total(0) ;
329 for (map<TClass*,int>::iterator iter = _objectCount.begin() ; iter != _objectCount.end() ; ++iter) {
330 double tot= 1.0*(iter->first->Size()*iter->second)/(1024*1024) ;
331 cout << " class " << iter->first->GetName() << " count = " << iter->second << " sizeof = " << iter->first->Size() << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
332 total+=tot ;
333 }
334
335 for (map<string,int>::iterator iter = _specialCount.begin() ; iter != _specialCount.end() ; ++iter) {
336 int size = _specialSize[iter->first] ;
337 double tot=1.0*(size*iter->second)/(1024*1024) ;
338 cout << " speeial " << iter->first << " count = " << iter->second << " sizeof = " << size << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
339 total+=tot ;
340 }
341 cout << "Grand total memory = " << Form("%5.2f",total) << " Mb" << endl ;
342
343}
344
345
346////////////////////////////////////////////////////////////////////////////////
347/// Utility function to trigger zeroing of callgrind counters.
348///
349/// Note that this function does _not_ do anything, other than optionally printing this message
350/// To trigger callgrind zero counter action, run callgrind with
351/// argument '--zero-before=RooTrace::callgrind_zero()' (include single quotes in cmdline)
352
354{
355 ooccoutD((TObject*)nullptr,Tracing) << "RooTrace::callgrind_zero()" << endl ;
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Utility function to trigger dumping of callgrind counters.
360///
361/// Note that this function does _not_ do anything, other than optionally printing this message
362/// To trigger callgrind dumping action, run callgrind with
363/// argument '--dump-before=RooTrace::callgrind_dump()' (include single quotes in cmdline)
364
366{
367 ooccoutD((TObject*)nullptr,Tracing) << "RooTrace::callgrind_dump()" << endl ;
368}
bool _verbose
Verbose messaging if true.
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:2489
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
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.
Controls the memory tracing hooks in all RooFit objects.
Definition RooTrace.h:26
static void destroySpecial(const char *name)
Definition RooTrace.cxx:158
static void dump()
Dump contents of object registry to stdout.
Definition RooTrace.cxx:282
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:198
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:353
std::map< std::string, int > _specialSize
Definition RooTrace.h:82
static void printObjectCounts()
Definition RooTrace.cxx:319
static void createSpecial(const char *name, int size)
Definition RooTrace.cxx:147
static void destroy(const TObject *obj)
Register deletion of object 'obj'.
Definition RooTrace.cxx:136
static RooTrace & instance()
Definition RooTrace.cxx:105
static void mark()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition RooTrace.cxx:261
void mark3()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition RooTrace.cxx:272
void create2(const TObject *obj)
Back end function of create(), register creation of object 'obj'.
Definition RooTrace.cxx:210
void dump3(std::ostream &, bool sinceMarked)
Dump contents of object register to stream 'os'.
Definition RooTrace.cxx:300
std::map< std::string, int > _specialCount
Definition RooTrace.h:81
void create3(const TObject *obj)
Definition RooTrace.cxx:238
void destroySpecial3(const char *name)
Definition RooTrace.cxx:178
static void create(const TObject *obj)
Register creation of object 'obj'.
Definition RooTrace.cxx:123
static void active(bool flag)
If flag is true, memory tracing is activated.
Definition RooTrace.cxx:188
void printObjectCounts3()
Definition RooTrace.cxx:326
RooLinkedList _markList
Definition RooTrace.h:79
void destroy2(const TObject *obj)
Back end function of destroy(), register deletion of object 'obj'.
Definition RooTrace.cxx:225
static void callgrind_dump()
Utility function to trigger dumping of callgrind counters.
Definition RooTrace.cxx:365
void destroy3(const TObject *obj)
Back end function of destroy(), register deletion of object 'obj'.
Definition RooTrace.cxx:250
void createSpecial3(const char *name, int size)
Definition RooTrace.cxx:169
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:243