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