Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
42 ClassImp(RooTrace);
43 ;
44 
45 RooTrace* RooTrace::_instance=0 ;
46 
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 
50 RooTrace& RooTrace::instance()
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 {
70  RooTrace& instance = RooTrace::instance() ;
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 {
83  RooTrace& instance = RooTrace::instance() ;
84  if (instance._active) {
85  instance.destroy3(obj) ;
86  }
87 }
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 
92 void RooTrace::createSpecial(const char* name, int size)
93 {
94  RooTrace& instance = RooTrace::instance() ;
95  if (instance._active) {
96  instance.createSpecial3(name,size) ;
97  }
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 
103 void RooTrace::destroySpecial(const char* name)
104 {
105  RooTrace& instance = RooTrace::instance() ;
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 
133 void RooTrace::active(Bool_t flag)
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 
143 void RooTrace::verbose(Bool_t flag)
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 
206 void RooTrace::mark()
207 {
208  RooTrace::instance().mark3() ;
209 }
210 
211 
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Put marker in object list, that allows to dump contents of list
215 /// relative to this marker
216 
217 void RooTrace::mark3()
218 {
219  _markList = _list ;
220 }
221 
222 
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// Dump contents of object registry to stdout
226 
227 void RooTrace::dump()
228 {
229  RooTrace::instance().dump3(cout,kFALSE) ;
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 
264 void RooTrace::printObjectCounts()
265 {
266  RooTrace::instance().printObjectCounts3() ;
267 }
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 
271 void RooTrace::printObjectCounts3()
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 
298 void RooTrace::callgrind_zero()
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 
310 void RooTrace::callgrind_dump()
311 {
312  ooccoutD((TObject*)0,Tracing) << "RooTrace::callgrind_dump()" << endl ;
313 }