Belle II Software  release-06-01-15
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 }
20 
21 HistoManager::~HistoManager()
22 {
23 }
24 
25 bool HistoManager::add(string& subdir, string& name, int pid, TH1* histo)
26 {
27  // printf ( "HistoManager: adding %s to subdir %s from id %d\n",
28  // name.c_str(), subdir.c_str(), pid );
29 
30  // Check for subdirectory
31  if (m_subdir.find(subdir) == m_subdir.end()) {
32  // Work dir and hist
33  map<string, map<int, TH1*>>* newsubdir = new map<string, map<int, TH1*>>;
34  map<int, TH1*>* newhlist = new map<int, TH1*> ;
35  (*newsubdir)[name] = *newhlist;
36  m_subdir[subdir] = *newsubdir;
37  // Merge dir and hist
38  map<string, TH1*>* newmergedir = new map<string, TH1*>;
39  (*newmergedir)[name] = NULL; // TH1 is not yet created
40  m_mergedir[subdir] = *newmergedir;
41  printf("HistoManager: new list created for subdir %s\n", subdir.c_str());
42  delete newsubdir;
43  delete newhlist;
44  delete newmergedir;
45  }
46 
47  // Get histogram map of subdir
48  map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
49  map<int, TH1*>& hlist = dirlist[name];
50  if (hlist.find(pid) == hlist.end()) {
51  hlist[pid] = histo;
52  // printf("HistoManager: histogram %s from %d registered in %s\n",
53  // histo->GetName(), pid, subdir.c_str());
54  // histo->Print();
55  return true;
56  }
57  return false;
58 }
59 
60 bool HistoManager::update(string& subdir, string& name, int pid, TH1* histo)
61 {
62  // Register the histogram if not yet done
63  if (add(subdir, name, pid, histo)) return true;
64 
65 
66  // Retrieve the histogram list for the name
67  map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
68  map<int, TH1*>& hlist = dirlist[name];
69 
70  // Replace histogram
71  TH1* prevhisto = hlist[pid];
72  if (prevhisto != NULL) delete prevhisto;
73  hlist[pid] = histo;
74  // printf ( "HistoManager: histogram %s replaced in subdir %s, entry = %f\n",
75  // name.c_str(), subdir.c_str(), histo->GetEntries());
76  // hlist[pid]->Print();
77  // Return
78  return true;
79 }
80 
81 TH1* HistoManager::get(string& subdir, string& name, int pid)
82 {
83  map<string, map<int, TH1*>>& dirlist = m_subdir[subdir];
84  map<int, TH1*>& hlist = dirlist[name];
85  TH1* hist = hlist[pid];
86  return hist;
87 }
88 
89 bool HistoManager::merge()
90 {
91  // Loop over subdir list
92  string subdir;
93  for (map<string, map<string, map<int, TH1*>>>::iterator is =
94  m_subdir.begin(); is != m_subdir.end(); ++is) {
95  map<string, map<int, TH1*>>& dirlist = is->second;
96  map<string, TH1*>& mergelist = m_mergedir[is->first];
97  // Move to the root directory of TMapFile
98  // printf("TMemFile = %8.8x\n", m_memfile->GetMemFile());
99  if (m_memfile->GetMemFile() == NULL) exit(-99);
100  (m_memfile->GetMemFile())->cd();
101  // cd to subdirectory if defined
102  subdir = is->first;
103  /*
104  if ( is->first != "root" ) {
105  TDirectory* fdir = (m_memfile->GetmemFile())->GetDirectory();
106  fdir->mkdir ( (is->first).c_str() );
107  fdir->cd ( (is->first).c_str() );
108  printf ( "TMemFile: subdir set to %s\n", (is->first).c_str() );
109  fdir->ls();
110  }
111  */
112  // Loop over histogram list
113  for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin();
114  it != dirlist.end(); ++it) {
115  string name = it->first;
116  map<int, TH1*>& hmap = it->second;
117  if (mergelist[name] != NULL) {
118  TH1* merge_hist = mergelist[name];
119  merge_hist->Reset();
120 
121  }
122  // Loop over pid list
123  for (map<int, TH1*>::iterator ih = hmap.begin(); ih != hmap.end();
124  ++ih) {
125  // int pid = ih->first;
126  TH1* hist = ih->second;
127  // printf ( "Retrieving histo %s from pid = %d\n", hist->GetName(), pid );
128  // hist->Print();
129  // Create new histogram in merge list if not exist yet
130  if (mergelist[name] == NULL) {
131  // printf ( "HistoManager: adding %s (class %s) to mergelist\n",
132  // hist->GetName(), hist->ClassName() );
133  string newname;
134  if (subdir != "")
135  newname = subdir + "/" + string(hist->GetName());
136  else
137  newname = string(hist->GetName());
138  hist->SetName(newname.c_str());
139  TH1* mhist = (TH1*)hist->Clone();
140  mergelist[name] = mhist;
141  // mergelist[name] = (TH1*)hist->Clone();
142  // m_mapfile->Add(mergelist[name]);
143  // delete hist;
144  }
145  // Add histogram if exist
146  else {
147  TH1* merge_hist = mergelist[name];
148  merge_hist->Add(hist);
149  merge_hist->SetTitle(hist->GetTitle());
150  }
151  }
152  }
153  }
154  // m_mapfile->Update();
155  m_memfile->UpdateSharedMem();
156 
157  // m_mapfile->ls();
158  // printf ( "HistoManager: merge called and mapfile updated!!!!!\n" );
159  return true;
160 }
161 
162 void HistoManager::clear()
163 {
164  for (map<string, map<string, map<int, TH1*>>>::iterator is = m_subdir.begin(); is != m_subdir.end(); ++is) {
165  map<string, map<int, TH1*>>& dirlist = is->second;
166 
167  for (map<string, std::map<int, TH1*> >::iterator it = dirlist.begin(); it != dirlist.end(); ++it) {
168  map<int, TH1*>& hmap = it->second;
169 
170  for (map<int, TH1*>::iterator ih = hmap.begin(); ih != hmap.end(); ++ih) {
171  TH1* hist = ih->second;
172  if (hist != NULL) delete hist;
173  //if (hist != NULL) hist->Reset();
174  }
175  hmap.clear();
176  }
177  dirlist.clear();
178  }
179  m_subdir.clear();
180 
181  for (map<string, map<string, TH1*> >::iterator is = m_mergedir.begin(); is != m_mergedir.end(); ++is) {
182  map<string, TH1*>& dirlist = is->second;
183 
184  for (map<string, TH1*>::iterator it = dirlist.begin(); it != dirlist.end(); ++it) {
185  TH1* hist = it->second;
186  if (hist != NULL) delete hist;
187  //it->second = NULL;
188  //if (hist != NULL) hist->Reset();
189  }
190  dirlist.clear();
191  }
192  m_mergedir.clear();
193 
194  printf("HistoManager: clear\n");
195  m_memfile->ClearSharedMem();
196 }
197 
Abstract base class for different kinds of events.