HistCache.cxx
Go to the documentation of this file.
2 
4 
5 #include "TH2.h"
6 #include "TH3.h"
7 
8 #include <iostream>
9 
10 namespace ana
11 {
12  std::multimap<int, std::unique_ptr<TH1D>> HistCache::fgMap;
13  std::multimap<int, std::unique_ptr<TH1F>> HistCache::fgMapF;
14  std::multimap<std::pair<int, int>, std::unique_ptr<TH2D>> HistCache::fgMap2D;
15  std::multimap<std::pair<int, int>, std::unique_ptr<TH2F>> HistCache::fgMap2F;
16  std::multimap<std::pair<int, int>, std::unique_ptr<TH3D>> HistCache::fgMap3D;
17 
18  int HistCache::fgOut = 0;
19  int HistCache::fgIn = 0;
20 
21  long HistCache::fgEstMemUsage = 0;
22 
23  //---------------------------------------------------------------------
25  {
26  ++fgOut;
27 
28  // Look in the cache
29  auto it = fgMap.find(bins.ID());
30  if(it != fgMap.end()){
31  TH1D* ret = it->second.release();
32  fgMap.erase(it);
33  ret->Reset();
34  ret->SetTitle(title.c_str());
35 
36  fgEstMemUsage -= 16*bins.NBins();
37 
38  return ret;
39  }
40 
41  // If not, create a new one directly
42  return MakeTH1D(UniqueName().c_str(), title.c_str(), bins);
43  }
44 
45  //---------------------------------------------------------------------
47  {
48  ++fgOut;
49 
50  // Look in the cache
51  auto it = fgMapF.find(bins.ID());
52  if(it != fgMapF.end()){
53  TH1F* ret = it->second.release();
54  fgMapF.erase(it);
55  ret->Reset();
56  ret->SetTitle(title.c_str());
57 
58  fgEstMemUsage -= 8*bins.NBins();
59 
60  return ret;
61  }
62 
63  // If not, create a new one directly
64  return MakeTH1F(UniqueName().c_str(), title.c_str(), bins);
65  }
66 
67  //---------------------------------------------------------------------
68  TH1D* HistCache::New(const std::string& title, const TAxis* bins)
69  {
70  return New(title, Binning::FromTAxis(bins));
71  }
72 
73  //---------------------------------------------------------------------
74  TH1F* HistCache::NewF(const std::string& title, const TAxis* bins)
75  {
76  return NewF(title, Binning::FromTAxis(bins));
77  }
78 
79  //---------------------------------------------------------------------
81  {
82  return NewTH2D(title, bins, kTrueEnergyBins);
83  }
84 
85  //---------------------------------------------------------------------
86  TH2D* HistCache::NewTH2D(const std::string& title, const TAxis* bins)
87  {
88  return NewTH2D(title, Binning::FromTAxis(bins));
89  }
90 
91  //---------------------------------------------------------------------
93  {
94  return NewTH2F(title, bins, kTrueEnergyBins);
95  }
96 
97  //---------------------------------------------------------------------
98  TH2F* HistCache::NewTH2F(const std::string& title, const TAxis* bins)
99  {
100  return NewTH2F(title, Binning::FromTAxis(bins));
101  }
102 
103  //---------------------------------------------------------------------
105  {
106  ++fgOut;
107  std::pair<int, int> IDs (xbins.ID(), ybins.ID());
108  auto it = fgMap2D.find(IDs);
109  if(it != fgMap2D.end()){
110  TH2D* ret = it->second.release();
111  fgMap2D.erase(it);
112  ret->Reset();
113  ret->SetTitle(title.c_str());
114  fgEstMemUsage -= 16*xbins.NBins()*ybins.NBins();
115  return ret;
116  }
117 
118  return MakeTH2D(UniqueName().c_str(), title.c_str(), xbins, ybins);
119  }
120 
121  //---------------------------------------------------------------------
123  {
124  ++fgOut;
125  std::pair<int, int> IDs (xbins.ID(), ybins.ID());
126  auto it = fgMap2F.find(IDs);
127  if(it != fgMap2F.end()){
128  TH2F* ret = it->second.release();
129  fgMap2F.erase(it);
130  ret->Reset();
131  ret->SetTitle(title.c_str());
132  fgEstMemUsage -= 8*xbins.NBins()*ybins.NBins();
133  return ret;
134  }
135 
136  return MakeTH2F(UniqueName().c_str(), title.c_str(), xbins, ybins);
137  }
138 
139  //---------------------------------------------------------------------
140  TH2D* HistCache::NewTH2D(const std::string& title, const TAxis* xbins, const TAxis* ybins)
141  {
142  return NewTH2D(title, Binning::FromTAxis(xbins), Binning::FromTAxis(ybins));
143  }
144 
145  //---------------------------------------------------------------------
146  TH2F* HistCache::NewTH2F(const std::string& title, const TAxis* xbins, const TAxis* ybins)
147  {
148  return NewTH2F(title, Binning::FromTAxis(xbins), Binning::FromTAxis(ybins));
149  }
150 
151  //---------------------------------------------------------------------
153  {
154  ++fgOut;
155  std::pair<int, int> IDs (xbins.ID(), ybins.ID());
156  auto it = fgMap3D.find(IDs);
157  if(it != fgMap3D.end()){
158  TH3D* ret = it->second.release();
159  fgMap3D.erase(it);
160  ret->Reset();
161  ret->SetTitle(title.c_str());
162  fgEstMemUsage -= 16*xbins.NBins()*ybins.NBins()*zbins.NBins();
163  return ret;
164  }
165 
166  return MakeTH3D(UniqueName().c_str(), title.c_str(), xbins, ybins, zbins);
167  }
168 
169  TH3D* HistCache::NewTH3D(const std::string& title, const TAxis* xbins, const TAxis* ybins, const TAxis* zbins)
170  {
171  return MakeTH3D(UniqueName().c_str(), title.c_str(), Binning::FromTAxis(xbins), Binning::FromTAxis(ybins), Binning::FromTAxis(zbins));
172  }
173 
174  //---------------------------------------------------------------------
175  TH1D* HistCache::Copy(const TH1D* h)
176  {
177  TH1D* ret = New(h->GetTitle(), h->GetXaxis());
178  *ret = *h;
179  return ret;
180  }
181 
182  //---------------------------------------------------------------------
183  TH1F* HistCache::Copy(const TH1F* h)
184  {
185  TH1F* ret = NewF(h->GetTitle(), h->GetXaxis());
186  *ret = *h;
187  return ret;
188  }
189 
190  //---------------------------------------------------------------------
191  TH2D* HistCache::Copy(const TH2D* h)
192  {
193  TH2D* ret = NewTH2D(h->GetTitle(), h->GetXaxis(), h->GetYaxis());
194  *ret = *h;
195  return ret;
196  }
197 
198  //---------------------------------------------------------------------
199  TH2F* HistCache::Copy(const TH2F* h)
200  {
201  TH2F* ret = NewTH2F(h->GetTitle(), h->GetXaxis(), h->GetYaxis());
202  *ret = *h;
203  return ret;
204  }
205 
206  //---------------------------------------------------------------------
207  TH3D* HistCache::Copy(const TH3D* h)
208  {
209  TH3D* ret = NewTH3D(h->GetTitle(), h->GetXaxis(), h->GetYaxis(), h->GetZaxis());
210  *ret = *h;
211  return ret;
212  }
213 
214  //---------------------------------------------------------------------
215  void HistCache::Delete(TH1D*& h)
216  {
217  if(!h) return;
218 
219  ++fgIn;
220 
221  fgMap.emplace(Binning::FromTAxis(h->GetXaxis()).ID(),
222  std::unique_ptr<TH1D>(h));
223 
224  fgEstMemUsage += 16*h->GetNbinsX();
225  CheckMemoryUse();
226 
227  h = 0;
228  }
229 
230  //---------------------------------------------------------------------
231  void HistCache::Delete(TH1F*& h)
232  {
233  if(!h) return;
234 
235  ++fgIn;
236 
237  fgMapF.emplace(Binning::FromTAxis(h->GetXaxis()).ID(),
238  std::unique_ptr<TH1F>(h));
239 
240  fgEstMemUsage += 8*h->GetNbinsX();
241  CheckMemoryUse();
242 
243  h = 0;
244  }
245 
246  //---------------------------------------------------------------------
247  void HistCache::Delete(TH2D*& h)
248  {
249  if(!h) return;
250 
251  ++fgIn;
252 
253  fgMap2D.emplace(std::pair<int, int>(Binning::FromTAxis(h->GetXaxis()).ID(), Binning::FromTAxis(h->GetYaxis()).ID()),
254  std::unique_ptr<TH2D>(h));
255 
256  fgEstMemUsage += 16*h->GetNbinsX()*h->GetNbinsY();
257  CheckMemoryUse();
258 
259  h = 0;
260  }
261 
262  //---------------------------------------------------------------------
263  void HistCache::Delete(TH2F*& h)
264  {
265  if(!h) return;
266 
267  ++fgIn;
268 
269  fgMap2F.emplace(std::pair<int, int>(Binning::FromTAxis(h->GetXaxis()).ID(), Binning::FromTAxis(h->GetYaxis()).ID()),
270  std::unique_ptr<TH2F>(h));
271 
272  fgEstMemUsage += 8*h->GetNbinsX()*h->GetNbinsY();
273  CheckMemoryUse();
274 
275  h = 0;
276  }
277 
278  //---------------------------------------------------------------------
279  void HistCache::Delete(TH3D*& h)
280  {
281  if(!h) return;
282 
283  ++fgIn;
284 
285  fgMap3D.emplace(std::pair<int, int>(Binning::FromTAxis(h->GetXaxis()).ID(), Binning::FromTAxis(h->GetYaxis()).ID()),
286  std::unique_ptr<TH3D>(h));
287 
288  fgEstMemUsage += 16*h->GetNbinsX()*h->GetNbinsY()*h->GetNbinsZ();
289  CheckMemoryUse();
290 
291  h = 0;
292  }
293  //---------------------------------------------------------------------
295  {
296  if(fgEstMemUsage > 500l*1024*1024){
297  std::cerr << "Warning! HistCache memory usage exceeds 500MB. "
298  << "That probably means histograms are being returned "
299  << "to the cache that weren't originally handed out by it. "
300  << std::endl;
301  PrintStats();
302  std::cerr << "Now clearing cache. This could take a long time..."
303  << std::endl;
304  ClearCache();
305  std::cerr << "Done clearing cache" << std::endl;
306  }
307  }
308 
309  //---------------------------------------------------------------------
311  {
312  fgMap.clear();
313  fgMapF.clear();
314  fgMap2D.clear();
315  fgMap2F.clear();
316  fgMap3D.clear();
317  fgEstMemUsage = 0;
318  fgOut = 0;
319  fgIn = 0;
320  }
321 
322  //---------------------------------------------------------------------
324  {
325  // Count number of unique keys
326  std::set<int> keys;
327  for(auto& it: fgMap) keys.insert(it.first);
328  for(auto& it: fgMapF) keys.insert(it.first);
329  std::set<std::pair<int, int>> keys2;
330  for(auto& it: fgMap2D) keys2.insert(it.first);
331  for(auto& it: fgMap2F) keys2.insert(it.first);
332  for(auto& it: fgMap3D) keys2.insert(it.first);
333 
334  std::cout << "Gave out " << fgOut << " histograms, got back "
335  << fgIn << " of them (" << fgOut-fgIn << " lost), in "
336  << keys.size()+keys2.size() << " different shapes." << std::endl
337  << "Holding " << fgMap.size()+fgMap2D.size()
338  << " histograms for an estimated memory usage of "
339  << fgEstMemUsage << " bytes." << std::endl;
340  }
341 
342  //---------------------------------------------------------------------
344  {
345  for(auto& it: fgMap) it.second.release();
346  for(auto& it: fgMapF) it.second.release();
347  for(auto& it: fgMap2D) it.second.release();
348  for(auto& it: fgMap2F) it.second.release();
349  for(auto& it: fgMap3D) it.second.release();
350 
351  ClearCache();
352  }
353 }
const Binning ybins
Definition: sa_fd_arrays.h:137
TH2D * MakeTH2D(const char *name, const char *title, const Binning &binsx, const Binning &binsy)
Definition: Utilities.cxx:345
static TH2D * NewTH2D(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:80
keys
Reco plots.
Definition: caf_analysis.py:46
TH3D * MakeTH3D(const char *name, const char *title, const Binning &binsx, const Binning &binsy, const Binning &binsz)
Definition: Utilities.cxx:362
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > fgMap2D
Definition: HistCache.h:76
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:16
Oscillation analysis framework, runs over CAF files outside of ART.
set< int >::iterator it
static TH1D * Copy(const TH1D *h)
Definition: HistCache.cxx:175
const Binning zbins
Definition: NumuCCIncBins.h:21
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
OStream cerr
Definition: OStream.cxx:7
static long fgEstMemUsage
Definition: HistCache.h:82
static TH2F * NewTH2F(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:92
static std::multimap< int, std::unique_ptr< TH1F > > fgMapF
Definition: HistCache.h:75
static void LeakAll()
for debugging shutdown crashes
Definition: HistCache.cxx:343
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2F > > fgMap2F
Definition: HistCache.h:77
static void Delete(TH1D *&h)
Definition: HistCache.cxx:215
static TH3D * NewTH3D(const std::string &title, const Binning &xbins, const Binning &ybins, const Binning &zbins)
For TH3D.
Definition: HistCache.cxx:152
static int fgOut
Definition: HistCache.h:80
const Binning kTrueEnergyBins
Default true-energy bin edges.
Definition: Binning.h:69
static std::multimap< std::pair< int, int >, std::unique_ptr< TH3D > > fgMap3D
Definition: HistCache.h:78
static TH1F * NewF(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:46
OStream cout
Definition: OStream.cxx:6
TH1D * MakeTH1D(const char *name, const char *title, const Binning &bins)
Definition: Utilities.cxx:333
const Binning bins
Definition: NumuCC_CPiBin.h:8
TH2F * MakeTH2F(const char *name, const char *title, const Binning &binsx, const Binning &binsy)
Definition: Utilities.cxx:353
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
static int fgIn
Definition: HistCache.h:80
static void PrintStats()
Definition: HistCache.cxx:323
const int xbins
Definition: MakePlots.C:82
int NBins() const
Definition: Binning.h:25
static void CheckMemoryUse()
Definition: HistCache.cxx:294
static TH1D * New(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:24
static std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:74
int ID() const
Definition: Binning.h:40
static void ClearCache()
Definition: HistCache.cxx:310
std::string UniqueName()
Return a different string each time, for creating histograms.
Definition: Utilities.cxx:30
TH1F * MakeTH1F(const char *name, const char *title, const Binning &bins)
Definition: Utilities.cxx:339
h
Definition: demo3.py:41