ROOT  6.06/08
Reference Guide
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 //
19 // BEGIN_HTML
20 // Class RooTrace controls the memory tracing hooks in all RooFit
21 // objects. When tracing is active, a table of live RooFit objects
22 // is kept that can be queried at any time. In verbose mode, messages
23 // are printed in addition at the construction and destruction of
24 // each object.
25 // END_HTML
26 //
27 
28 #include "RooFit.h"
29 
30 #include "RooTrace.h"
31 #include "RooAbsArg.h"
32 #include "Riostream.h"
33 #include "RooMsgService.h"
34 
35 #include <iomanip>
36 
37 
38 
39 using namespace std;
40 
42 ;
43 
44 RooTrace* RooTrace::_instance=0 ;
45 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 
50 {
51  if (_instance==0) _instance = new RooTrace() ;
52  return *_instance ;
53 }
54 
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 
58 RooTrace::RooTrace() : _active(kFALSE), _verbose(kFALSE)
59 {
60 }
61 
62 
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Register creation of object 'obj'
66 
67 void RooTrace::create(const TObject* obj)
68 {
70  if (instance._active) {
71  instance.create3(obj) ;
72  }
73 
74 }
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Register deletion of object 'obj'
79 
80 void RooTrace::destroy(const TObject* obj)
81 {
83  if (instance._active) {
84  instance.destroy3(obj) ;
85  }
86 }
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 
91 void RooTrace::createSpecial(const char* name, int size)
92 {
94  if (instance._active) {
95  instance.createSpecial3(name,size) ;
96  }
97 }
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 
102 void RooTrace::destroySpecial(const char* name)
103 {
105  if (instance._active) {
106  instance.destroySpecial3(name) ;
107  }
108 }
109 
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 
113 void RooTrace::createSpecial3(const char* name, int size)
114 {
115  _specialCount[name]++ ;
116  _specialSize[name] = size ;
117 }
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 
122 void RooTrace::destroySpecial3(const char* name)
123 {
124  _specialCount[name]-- ;
125 }
126 
127 
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// If flag is true, memory tracing is activated
131 
133 {
134  RooTrace::instance()._active = flag ;
135 }
136 
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// If flag is true, a message will be printed at each
140 /// object creation or deletion
141 
143 {
144  RooTrace::instance()._verbose = flag ;
145 }
146 
147 
148 
149 
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Back end function of create(), register creation of object 'obj'
153 
154 void RooTrace::create2(const TObject* obj)
155 {
156  _list.Add((RooAbsArg*)obj) ;
157  if (_verbose) {
158  cout << "RooTrace::create: object " << obj << " of type " << obj->ClassName()
159  << " created " << endl ;
160  }
161 }
162 
163 
164 
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Back end function of destroy(), register deletion of object 'obj'
168 
169 void RooTrace::destroy2(const TObject* obj)
170 {
171  if (!_list.Remove((RooAbsArg*)obj)) {
172  } else if (_verbose) {
173  cout << "RooTrace::destroy: object " << obj << " of type " << obj->ClassName()
174  << " destroyed [" << obj->GetTitle() << "]" << endl ;
175  }
176 }
177 
178 
179 
180 //_____________________________________________________________________________
181 
182 void RooTrace::create3(const TObject* obj)
183 {
184  // Back end function of create(), register creation of object 'obj'
185  _objectCount[obj->IsA()]++ ;
186 }
187 
188 
189 
190 
191 ////////////////////////////////////////////////////////////////////////////////
192 /// Back end function of destroy(), register deletion of object 'obj'
193 
194 void RooTrace::destroy3(const TObject* obj)
195 {
196  _objectCount[obj->IsA()]-- ;
197 }
198 
199 
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// Put marker in object list, that allows to dump contents of list
203 /// relative to this marker
204 
206 {
208 }
209 
210 
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// Put marker in object list, that allows to dump contents of list
214 /// relative to this marker
215 
217 {
218  _markList = _list ;
219 }
220 
221 
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// Dump contents of object registry to stdout
225 
227 {
229 }
230 
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 
234 void RooTrace::dump(ostream& os, Bool_t sinceMarked)
235 {
236  RooTrace::instance().dump3(os,sinceMarked) ;
237 }
238 
239 
240 ////////////////////////////////////////////////////////////////////////////////
241 /// Dump contents of object register to stream 'os'. If sinceMarked is
242 /// true, only object created after the last call to mark() are shown.
243 
244 void RooTrace::dump3(ostream& os, Bool_t sinceMarked)
245 {
246  os << "List of RooFit objects allocated while trace active:" << endl ;
247 
248 
249  Int_t i, nMarked(0) ;
250  for(i=0 ; i<_list.GetSize() ; i++) {
251  if (!sinceMarked || _markList.IndexOf(_list.At(i)) == -1) {
252  os << hex << setw(10) << _list.At(i) << dec << " : " << setw(20) << _list.At(i)->ClassName() << setw(0) << " - " << _list.At(i)->GetName() << endl ;
253  } else {
254  nMarked++ ;
255  }
256  }
257  if (sinceMarked) os << nMarked << " marked objects suppressed" << endl ;
258 }
259 
260 
261 ////////////////////////////////////////////////////////////////////////////////
262 
264 {
266 }
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 
271 {
272  Double_t total(0) ;
273  for (map<TClass*,int>::iterator iter = _objectCount.begin() ; iter != _objectCount.end() ; ++iter) {
274  Double_t tot= 1.0*(iter->first->Size()*iter->second)/(1024*1024) ;
275  cout << " class " << iter->first->GetName() << " count = " << iter->second << " sizeof = " << iter->first->Size() << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
276  total+=tot ;
277  }
278 
279  for (map<string,int>::iterator iter = _specialCount.begin() ; iter != _specialCount.end() ; ++iter) {
280  int size = _specialSize[iter->first] ;
281  Double_t tot=1.0*(size*iter->second)/(1024*1024) ;
282  cout << " speeial " << iter->first << " count = " << iter->second << " sizeof = " << size << " total memory = " << Form("%5.2f",tot) << " Mb" << endl ;
283  total+=tot ;
284  }
285  cout << "Grand total memory = " << Form("%5.2f",total) << " Mb" << endl ;
286 
287 }
288 
289 
290 ////////////////////////////////////////////////////////////////////////////////
291 /// Utility function to trigger zeroing of callgrind counters.
292 ///
293 /// Note that this function does _not_ do anything, other than optionally printing this message
294 /// To trigger callgrind zero counter action, run callgrind with
295 /// argument '--zero-before=RooTrace::callgrind_zero()' (include single quotes in cmdline)
296 
298 {
299  ooccoutD((TObject*)0,Tracing) << "RooTrace::callgrind_zero()" << endl ;
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// Utility function to trigger dumping of callgrind counters.
304 ///
305 /// Note that this function does _not_ do anything, other than optionally printing this message
306 /// To trigger callgrind dumping action, run callgrind with
307 /// argument '--dump-before=RooTrace::callgrind_dump()' (include single quotes in cmdline)
308 
310 {
311  ooccoutD((TObject*)0,Tracing) << "RooTrace::callgrind_dump()" << endl ;
312 }
Int_t IndexOf(const char *name) const
Return position of given object in list.
RooLinkedList _list
Definition: RooTrace.h:78
unsigned int hex
Definition: math.cpp:442
virtual Bool_t Remove(TObject *arg)
Remove object from collection.
static void callgrind_zero()
Utility function to trigger zeroing of callgrind counters.
Definition: RooTrace.cxx:297
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
static RooTrace & instance()
Definition: RooTrace.cxx:49
STL namespace.
std::map< std::string, int > _specialCount
Definition: RooTrace.h:81
Int_t GetSize() const
Definition: RooLinkedList.h:60
static void active(Bool_t flag)
If flag is true, memory tracing is activated.
Definition: RooTrace.cxx:132
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
void printObjectCounts3()
Definition: RooTrace.cxx:270
void create3(const TObject *obj)
Definition: RooTrace.cxx:182
RooTrace()
Definition: RooTrace.cxx:58
static void create(const TObject *obj)
Register creation of object &#39;obj&#39;.
Definition: RooTrace.cxx:67
void createSpecial3(const char *name, int size)
Definition: RooTrace.cxx:113
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
Bool_t _active
Definition: RooTrace.h:76
TObject * At(Int_t index) const
Return object stored in sequential position given by index.
static void mark()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition: RooTrace.cxx:205
void create2(const TObject *obj)
Back end function of create(), register creation of object &#39;obj&#39;.
Definition: RooTrace.cxx:154
char * Form(const char *fmt,...)
void destroySpecial3(const char *name)
Definition: RooTrace.cxx:122
static void verbose(Bool_t flag)
If flag is true, a message will be printed at each object creation or deletion.
Definition: RooTrace.cxx:142
void dump3(std::ostream &, Bool_t sinceMarked)
Dump contents of object register to stream &#39;os&#39;.
Definition: RooTrace.cxx:244
static unsigned int total
static void destroy(const TObject *obj)
Register deletion of object &#39;obj&#39;.
Definition: RooTrace.cxx:80
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
void destroy3(const TObject *obj)
Back end function of destroy(), register deletion of object &#39;obj&#39;.
Definition: RooTrace.cxx:194
std::map< std::string, int > _specialSize
Definition: RooTrace.h:82
static void destroySpecial(const char *name)
Definition: RooTrace.cxx:102
#define ooccoutD(o, a)
Definition: RooMsgService.h:51
#define name(a, b)
Definition: linkTestLib0.cpp:5
RooLinkedList _markList
Definition: RooTrace.h:79
void destroy2(const TObject *obj)
Back end function of destroy(), register deletion of object &#39;obj&#39;.
Definition: RooTrace.cxx:169
static void createSpecial(const char *name, int size)
Definition: RooTrace.cxx:91
Mother of all ROOT objects.
Definition: TObject.h:58
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:459
void mark3()
Put marker in object list, that allows to dump contents of list relative to this marker.
Definition: RooTrace.cxx:216
static void printObjectCounts()
Definition: RooTrace.cxx:263
static void dump()
Dump contents of object registry to stdout.
Definition: RooTrace.cxx:226
static void callgrind_dump()
Utility function to trigger dumping of callgrind counters.
Definition: RooTrace.cxx:309
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:415
Bool_t _verbose
Definition: RooTrace.h:77
std::map< TClass *, int > _objectCount
Definition: RooTrace.h:80