All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Static Public Member Functions | Static Protected Member Functions | Static Protected Attributes | List of all members
ana::HistCache Class Reference

Helper for Spectrum. More...

#include <HistCache.h>

Static Public Member Functions

static TH1D * New (const std::string &title, const Binning &bins)
 
static TH1D * New (const std::string &title, const TAxis *bins)
 
static TH1D * Copy (const TH1D *h)
 
static TH2D * NewTH2D (const std::string &title, const Binning &xbins, const Binning &ybins)
 
static TH2D * NewTH2D (const std::string &title, const TAxis *xbins, const TAxis *ybins)
 
static TH2D * Copy (const TH2D *h)
 
static void Delete (TH1D *&h)
 
static void Delete (TH2D *&h)
 
static void PrintStats ()
 
static void ClearCache ()
 

Static Protected Member Functions

static void CheckMemoryUse ()
 

Static Protected Attributes

static std::multimap< int,
std::unique_ptr< TH1D > > 
fgMap
 
static std::multimap
< std::pair< int, int >
, std::unique_ptr< TH2D > > 
fgMap2D
 
static int fgOut = 0
 
static int fgIn = 0
 
static long fgEstMemUsage = 0
 
static long fgMemHandedOut = 0
 

Detailed Description

Helper for Spectrum.

ROOT's handling of allocations, and especially deletions, can be very slow. It keeps everything in a big map that it then has to lookups in. This class provides a simple cache of histograms, recycling an old histogram of the same binning instead of creating a new one.

Allocate new histograms with New, and return them to the cache with Delete.

Definition at line 24 of file HistCache.h.

Member Function Documentation

void ana::HistCache::CheckMemoryUse ( )
staticprotected

Definition at line 126 of file HistCache.cxx.

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  }
BEGIN_PROLOG could also be cerr
static long fgEstMemUsage
Definition: HistCache.h:48
static void PrintStats()
Definition: HistCache.cxx:152
static void ClearCache()
Definition: HistCache.cxx:142
void ana::HistCache::ClearCache ( )
static

Definition at line 142 of file HistCache.cxx.

143  {
144  fgMap.clear();
145  fgMap2D.clear();
146  fgEstMemUsage = 0;
147  fgOut = 0;
148  fgIn = 0;
149  }
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > fgMap2D
Definition: HistCache.h:44
static long fgEstMemUsage
Definition: HistCache.h:48
static int fgOut
Definition: HistCache.h:46
static int fgIn
Definition: HistCache.h:46
static std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:43
TH1D * ana::HistCache::Copy ( const TH1D *  h)
static

Definition at line 76 of file HistCache.cxx.

77  {
78  TH1D* ret = New(h->GetTitle(), h->GetXaxis());
79  *ret = *h;
80  return ret;
81  }
while getopts h
static TH1D * New(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:21
TH2D * ana::HistCache::Copy ( const TH2D *  h)
static

Definition at line 84 of file HistCache.cxx.

85  {
86  TH2D* ret = NewTH2D(h->GetTitle(), h->GetXaxis(), h->GetYaxis());
87  *ret = *h;
88  return ret;
89  }
while getopts h
static TH2D * NewTH2D(const std::string &title, const Binning &xbins, const Binning &ybins)
Definition: HistCache.cxx:50
void ana::HistCache::Delete ( TH1D *&  h)
static

Definition at line 92 of file HistCache.cxx.

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  }
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static long fgEstMemUsage
Definition: HistCache.h:48
while getopts h
static long fgMemHandedOut
Definition: HistCache.h:49
static int fgIn
Definition: HistCache.h:46
static void CheckMemoryUse()
Definition: HistCache.cxx:126
static std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:43
void ana::HistCache::Delete ( TH2D *&  h)
static

Definition at line 109 of file HistCache.cxx.

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  }
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > fgMap2D
Definition: HistCache.h:44
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static long fgEstMemUsage
Definition: HistCache.h:48
while getopts h
static long fgMemHandedOut
Definition: HistCache.h:49
static int fgIn
Definition: HistCache.h:46
static void CheckMemoryUse()
Definition: HistCache.cxx:126
TH1D * ana::HistCache::New ( const std::string &  title,
const Binning bins 
)
static

Definition at line 21 of file HistCache.cxx.

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  }
static long fgEstMemUsage
Definition: HistCache.h:48
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 std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:43
std::string UniqueName()
Return a different string each time, for creating histograms.
TH1D * ana::HistCache::New ( const std::string &  title,
const TAxis *  bins 
)
static

Definition at line 44 of file HistCache.cxx.

45  {
46  return New(title, Binning::FromTAxis(bins));
47  }
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static TH1D * New(const std::string &title, const Binning &bins)
Definition: HistCache.cxx:21
TH2D * ana::HistCache::NewTH2D ( const std::string &  title,
const Binning xbins,
const Binning ybins 
)
static

Definition at line 50 of file HistCache.cxx.

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  }
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
static long fgEstMemUsage
Definition: HistCache.h:48
static int fgOut
Definition: HistCache.h:46
static long fgMemHandedOut
Definition: HistCache.h:49
std::string UniqueName()
Return a different string each time, for creating histograms.
TH2D * ana::HistCache::NewTH2D ( const std::string &  title,
const TAxis *  xbins,
const TAxis *  ybins 
)
static

Definition at line 70 of file HistCache.cxx.

71  {
72  return NewTH2D(title, Binning::FromTAxis(xbins), Binning::FromTAxis(ybins));
73  }
static Binning FromTAxis(const TAxis *ax)
Definition: Binning.cxx:122
static TH2D * NewTH2D(const std::string &title, const Binning &xbins, const Binning &ybins)
Definition: HistCache.cxx:50
void ana::HistCache::PrintStats ( )
static

Definition at line 152 of file HistCache.cxx.

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  }
static std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > fgMap2D
Definition: HistCache.h:44
static long fgEstMemUsage
Definition: HistCache.h:48
static int fgOut
Definition: HistCache.h:46
static long fgMemHandedOut
Definition: HistCache.h:49
static int fgIn
Definition: HistCache.h:46
static std::multimap< int, std::unique_ptr< TH1D > > fgMap
Definition: HistCache.h:43
BEGIN_PROLOG could also be cout

Member Data Documentation

long ana::HistCache::fgEstMemUsage = 0
staticprotected

Definition at line 48 of file HistCache.h.

int ana::HistCache::fgIn = 0
staticprotected

Definition at line 46 of file HistCache.h.

std::multimap< int, std::unique_ptr< TH1D > > ana::HistCache::fgMap
staticprotected

Definition at line 43 of file HistCache.h.

std::multimap< std::pair< int, int >, std::unique_ptr< TH2D > > ana::HistCache::fgMap2D
staticprotected

Definition at line 44 of file HistCache.h.

long ana::HistCache::fgMemHandedOut = 0
staticprotected

Definition at line 49 of file HistCache.h.

int ana::HistCache::fgOut = 0
staticprotected

Definition at line 46 of file HistCache.h.


The documentation for this class was generated from the following files: