All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HistCache.cxx
Go to the documentation of this file.
2 
4 
5 #include "TH2.h"
6 
7 #include <iostream>
8 
9 namespace ana
10 {
11  std::multimap<int, std::unique_ptr<TH1D>> HistCache::fgMap;
12  std::multimap<std::pair<int, int>, std::unique_ptr<TH2D>> HistCache::fgMap2D;
13 
14  int HistCache::fgOut = 0;
15  int HistCache::fgIn = 0;
16 
17  long HistCache::fgEstMemUsage = 0;
19 
20  //---------------------------------------------------------------------
21  TH1D* HistCache::New(const std::string& title, const Binning& bins)
22  {
23  ++fgOut;
24  fgMemHandedOut += 16*bins.NBins();
25 
26  // Look in the cache
27  auto it = fgMap.find(bins.ID());
28  if(it != fgMap.end()){
29  TH1D* ret = it->second.release();
30  fgMap.erase(it);
31  ret->Reset();
32  ret->SetTitle(title.c_str());
33 
34  fgEstMemUsage -= 16*bins.NBins();
35 
36  return ret;
37  }
38 
39  // If not, create a new one directly
40  return MakeTH1D(UniqueName().c_str(), title.c_str(), bins);
41  }
42 
43  //---------------------------------------------------------------------
44  TH1D* HistCache::New(const std::string& title, const TAxis* bins)
45  {
46  return New(title, Binning::FromTAxis(bins));
47  }
48 
49  //---------------------------------------------------------------------
50  TH2D* HistCache::NewTH2D(const std::string& title, const Binning& xbins, const Binning& ybins)
51  {
52  ++fgOut;
53  fgMemHandedOut += 16*xbins.NBins()*ybins.NBins();
54 
55  std::pair<int, int> IDs (xbins.ID(), ybins.ID());
56  auto it = fgMap2D.find(IDs);
57  if(it != fgMap2D.end()){
58  TH2D* ret = it->second.release();
59  fgMap2D.erase(it);
60  ret->Reset();
61  ret->SetTitle(title.c_str());
62  fgEstMemUsage -= 16*xbins.NBins()*ybins.NBins();
63  return ret;
64  }
65 
66  return MakeTH2D(UniqueName().c_str(), title.c_str(), xbins, ybins);
67  }
68 
69  //---------------------------------------------------------------------
70  TH2D* HistCache::NewTH2D(const std::string& title, const TAxis* xbins, const TAxis* ybins)
71  {
72  return NewTH2D(title, Binning::FromTAxis(xbins), Binning::FromTAxis(ybins));
73  }
74 
75  //---------------------------------------------------------------------
76  TH1D* HistCache::Copy(const TH1D* h)
77  {
78  TH1D* ret = New(h->GetTitle(), h->GetXaxis());
79  *ret = *h;
80  return ret;
81  }
82 
83  //---------------------------------------------------------------------
84  TH2D* HistCache::Copy(const TH2D* h)
85  {
86  TH2D* ret = NewTH2D(h->GetTitle(), h->GetXaxis(), h->GetYaxis());
87  *ret = *h;
88  return ret;
89  }
90 
91  //---------------------------------------------------------------------
92  void HistCache::Delete(TH1D*& h)
93  {
94  if(!h) return;
95 
96  ++fgIn;
97  fgMemHandedOut -= 16*h->GetNbinsX();
98 
99  fgMap.emplace(Binning::FromTAxis(h->GetXaxis()).ID(),
100  std::unique_ptr<TH1D>(h));
101 
102  fgEstMemUsage += 16*h->GetNbinsX();
103  CheckMemoryUse();
104 
105  h = 0;
106  }
107 
108  //---------------------------------------------------------------------
109  void HistCache::Delete(TH2D*& h)
110  {
111  if(!h) return;
112 
113  ++fgIn;
114  fgMemHandedOut -= 16*h->GetNbinsX()*h->GetNbinsY();
115 
116  fgMap2D.emplace(std::pair<int, int>(Binning::FromTAxis(h->GetXaxis()).ID(), Binning::FromTAxis(h->GetYaxis()).ID()),
117  std::unique_ptr<TH2D>(h));
118 
119  fgEstMemUsage += 16*h->GetNbinsX()*h->GetNbinsY();
120  CheckMemoryUse();
121 
122  h = 0;
123  }
124 
125  //---------------------------------------------------------------------
127  {
128  if(fgEstMemUsage > 500l*1024*1024){
129  std::cerr << "Warning! HistCache memory usage exceeds 500MB. "
130  << "That probably means histograms are being returned "
131  << "to the cache that weren't originally handed out by it. "
132  << std::endl;
133  PrintStats();
134  std::cerr << "Now clearing cache. This could take a long time..."
135  << std::endl;
136  ClearCache();
137  std::cerr << "Done clearing cache" << std::endl;
138  }
139  }
140 
141  //---------------------------------------------------------------------
143  {
144  fgMap.clear();
145  fgMap2D.clear();
146  fgEstMemUsage = 0;
147  fgOut = 0;
148  fgIn = 0;
149  }
150 
151  //---------------------------------------------------------------------
153  {
154  // Count number of unique keys
155  std::set<int> keys;
156  for(auto& it: fgMap) keys.insert(it.first);
157 
158  std::cout << "Gave out " << fgOut << " histograms, got back "
159  << fgIn << " of them (" << fgOut-fgIn << " still out, totalling "
160  << fgMemHandedOut << " bytes), in "
161  << keys.size() << " different shapes." << std::endl
162  << "Holding " << fgMap.size()+fgMap2D.size()
163  << " histograms for an estimated memory usage of "
164  << fgEstMemUsage << " bytes." << std::endl;
165  }
166 }
TH2D * MakeTH2D(const char *name, const char *title, const Binning &binsx, const Binning &binsy)
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > fgMap2D
Definition: HistCache.h:44
Represent the binning of a Spectrum&#39;s x-axis.
Definition: Binning.h:18
static TH1D * Copy(const TH1D *h)
Definition: HistCache.cxx:76
BEGIN_PROLOG could also be cerr
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static long fgEstMemUsage
Definition: HistCache.h:48
process_name opflashCryoW ana
while getopts h
static TH2D * NewTH2D(const std::string &title, const Binning &xbins, const Binning &ybins)
Definition: HistCache.cxx:50
static void Delete(TH1D *&h)
Definition: HistCache.cxx:92
static int fgOut
Definition: HistCache.h:46
static long fgMemHandedOut
Definition: HistCache.h:49
TH1D * MakeTH1D(const char *name, const char *title, const Binning &bins)
static int fgIn
Definition: HistCache.h:46
static void PrintStats()
Definition: HistCache.cxx:152
int NBins() const
Definition: Binning.h:27
static void CheckMemoryUse()
Definition: HistCache.cxx:126
static TH1D * New(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:21
static std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:43
int ID() const
Definition: Binning.h:42
static void ClearCache()
Definition: HistCache.cxx:142
std::string UniqueName()
Return a different string each time, for creating histograms.
BEGIN_PROLOG could also be cout