Belle II Software  release-08-01-10
HistoManager.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include "daq/dqm/HistoManager.h"
10 
11 using namespace Belle2;
12 using namespace std;
13 
14 // Constructor / Destructor
15 
16 HistoManager::HistoManager(DqmMemFile* memfile)
17 {
18  m_memfile = memfile;
19  clear();
20  merge();
21 }
22 
23 HistoManager::~HistoManager()
24 {
25 }
26 
27 bool HistoManager::add(const string& subdir, const string& name, int pid, TH1* histo)
28 {
29  // printf ( "HistoManager: adding %s to subdir %s from id %d\n",
30  // name.c_str(), subdir.c_str(), pid );
31 
32  // Check for subdirectory
33  if (m_subdir.find(subdir) == m_subdir.end()) {
34  // Work dir and hist
35  map<string, map<int, TH1*>>* newsubdir = new map<string, map<int, TH1*>>;
36  map<int, TH1*>* newhlist = new map<int, TH1*> ;
37  (*newsubdir)[name] = *newhlist;
38  m_subdir[subdir] = *newsubdir;
39  // Merge dir and hist
40  map<string, TH1*>* newmergedir = new map<string, TH1*>;
41  (*newmergedir)[name] = NULL; // TH1 is not yet created
42  m_mergedir[subdir] = *newmergedir;
43  printf("HistoManager: new list created for subdir %s\n", subdir.c_str());
44  delete newsubdir;
45  delete newhlist;
46  delete newmergedir;
47  }
48 
49  // Get histogram map of subdir
50  map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
51  map<int, TH1*>& hlist = dirlist[name];
52  if (hlist.find(pid) == hlist.end()) {
53  hlist[pid] = histo;
54  // printf("HistoManager: histogram %s from %d registered in %s\n",
55  // histo->GetName(), pid, subdir.c_str());
56  // histo->Print();
57  return true;
58  }
59  return false;
60 }
61 
62 bool HistoManager::update(const string& subdir, const string& name, int pid, TH1* histo)
63 {
64  // Register the histogram if not yet done
65  if (add(subdir, name, pid, histo)) return true;
66 
67 
68  // Retrieve the histogram list for the name
69  map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
70  map<int, TH1*>& hlist = dirlist[name];
71 
72  // Replace histogram
73  TH1* prevhisto = hlist[pid];
74  if (prevhisto != NULL) delete prevhisto;
75  hlist[pid] = histo;
76  // printf ( "HistoManager: histogram %s replaced in subdir %s, entry = %f\n",
77  // name.c_str(), subdir.c_str(), histo->GetEntries());
78  // hlist[pid]->Print();
79  // Return
80  return true;
81 }
82 
83 TH1* HistoManager::get(const string& subdir, const string& name, int pid)
84 {
85  const map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
86  const map<int, TH1*>& hlist = dirlist.at(name);
87  TH1* hist = hlist.at(pid);
88  return hist;
89 }
90 
91 bool HistoManager::merge()
92 {
93  // Loop over subdir list
94  string subdir;
95  for (map<string, map<string, map<int, TH1*>>>::iterator is =
96  m_subdir.begin(); is != m_subdir.end(); ++is) {
97  map<string, map<int, TH1*>>& dirlist = is->second;
98  map<string, TH1*>& mergelist = m_mergedir[is->first];
99  // Move to the root directory of TMapFile
100  // printf("TMemFile = %8.8x\n", m_memfile->GetMemFile());
101  if (m_memfile->GetMemFile() == NULL) exit(-99);
102  (m_memfile->GetMemFile())->cd();
103  // cd to subdirectory if defined
104  subdir = is->first;
105  /*
106  if ( is->first != "root" ) {
107  TDirectory* fdir = (m_memfile->GetmemFile())->GetDirectory();
108  fdir->mkdir ( (is->first).c_str() );
109  fdir->cd ( (is->first).c_str() );
110  printf ( "TMemFile: subdir set to %s\n", (is->first).c_str() );
111  fdir->ls();
112  }
113  */
114  // Loop over histogram list
115  for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin();
116  it != dirlist.end(); ++it) {
117  string name = it->first;
118  map<int, TH1*>& hmap = it->second;
119  if (mergelist[name] != NULL) {
120  TH1* merge_hist = mergelist[name];
121  merge_hist->Reset();
122 
123  }
124  // Loop over pid list
125  for (map<int, TH1*>::iterator ih = hmap.begin(); ih != hmap.end();
126  ++ih) {
127  // int pid = ih->first;
128  TH1* hist = ih->second;
129  // printf ( "Retrieving histo %s from pid = %d\n", hist->GetName(), pid );
130  // hist->Print();
131  // Create new histogram in merge list if not exist yet
132  if (mergelist[name] == NULL) {
133  // printf ( "HistoManager: adding %s (class %s) to mergelist\n",
134  // hist->GetName(), hist->ClassName() );
135  string newname;
136  if (subdir != "")
137  newname = subdir + "/" + string(hist->GetName());
138  else
139  newname = string(hist->GetName());
140  hist->SetName(newname.c_str());
141  TH1* mhist = (TH1*)hist->Clone();
142  mergelist[name] = mhist;
143  // mergelist[name] = (TH1*)hist->Clone();
144  // m_mapfile->Add(mergelist[name]);
145  // delete hist;
146  }
147  // Add histogram if exist
148  else {
149  TH1* merge_hist = mergelist[name];
150  merge_hist->Add(hist);
151  merge_hist->SetTitle(hist->GetTitle());
152  }
153  }
154  }
155  }
156  // m_mapfile->Update();
157  m_memfile->UpdateSharedMem();
158 
159  // m_mapfile->ls();
160  // printf ( "HistoManager: merge called and mapfile updated!!!!!\n" );
161  return true;
162 }
163 
164 void HistoManager::clear()
165 {
166  for (map<string, map<string, map<int, TH1*>>>::iterator is = m_subdir.begin(); is != m_subdir.end(); ++is) {
167  map<string, map<int, TH1*>>& dirlist = is->second;
168 
169  for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin(); it != dirlist.end(); ++it) {
170  map<int, TH1*>& hmap = it->second;
171 
172  for (map<int, TH1*>::iterator ih = hmap.begin(); ih != hmap.end(); ++ih) {
173  TH1* hist = ih->second;
174  if (hist != NULL) delete hist;
175  //if (hist != NULL) hist->Reset();
176  }
177  hmap.clear();
178  }
179  dirlist.clear();
180  }
181  m_subdir.clear();
182 
183  for (map<string, map<string, TH1*> >::iterator is = m_mergedir.begin(); is != m_mergedir.end(); ++is) {
184  map<string, TH1*>& dirlist = is->second;
185 
186  for (map<string, TH1*>::iterator it = dirlist.begin(); it != dirlist.end(); ++it) {
187  TH1* hist = it->second;
188  if (hist != NULL) delete hist;
189  //it->second = NULL;
190  //if (hist != NULL) hist->Reset();
191  }
192  dirlist.clear();
193  }
194  m_mergedir.clear();
195 
196  printf("HistoManager: clear\n");
197  m_memfile->ClearSharedMem();
198 }
199 
200 
201 void HistoManager::filedump(std::string outfile)
202 {
203  printf("dump to dqm file = %s\n", outfile.c_str());
204  merge(); // Smart move to first merge, but necessary?
205  m_memfile->SaveToFile(outfile);
206 }
std::vector< std::vector< double > > merge(std::vector< std::vector< std::vector< double >>> toMerge)
merge { vector<double> a, vector<double> b} into {a, b}
Definition: tools.h:41
Abstract base class for different kinds of events.