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