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