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