forked from sPHENIX-Collaboration/coresoftware
/
Fun4AllHistoManager.cc
297 lines (269 loc) · 7.19 KB
/
Fun4AllHistoManager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "Fun4AllHistoManager.h"
#include "TDirectoryHelper.h"
#include <phool/phool.h>
#include <phool/recoConsts.h>
#include <TFile.h>
#include <TH1.h>
#include <TNamed.h>
#include <TTree.h>
#include <RVersion.h>
#if ROOT_VERSION_CODE >= ROOT_VERSION(5, 20, 0)
#define HAS_THNSPARSE 1
#include <THnSparse.h>
#endif
#include <iomanip>
#include <iostream>
#include <sstream>
#include <utility> // for pair
using namespace std;
Fun4AllHistoManager::Fun4AllHistoManager(const string &name)
: Fun4AllBase(name)
{
return;
}
Fun4AllHistoManager::~Fun4AllHistoManager()
{
while (Histo.begin() != Histo.end())
{
delete Histo.begin()->second;
Histo.erase(Histo.begin());
}
return;
}
int Fun4AllHistoManager::dumpHistos(const string &filename, const string &openmode)
{
int iret = 0;
if (!filename.empty())
{
outfilename = filename;
}
else
{
if (outfilename.empty())
{
recoConsts *rc = recoConsts::instance();
ostringstream filnam;
int runnumber = -1;
if (rc->FlagExist("RUNNUMBER"))
{
runnumber = rc->get_IntFlag("RUNNUMBER");
}
// this will set the filename to the name of the manager
// add the runnumber in the std 10 digit format and
// end it with a .root extension
filnam << Name() << "-"
<< setfill('0') << setw(10)
<< runnumber << ".root";
outfilename = filnam.str();
}
}
cout << "Fun4AllHistoManager::dumpHistos() Writing root file: " << outfilename << endl;
const int compress = 9;
ostringstream creator;
creator << "Created by " << Name();
TFile hfile(outfilename.c_str(), openmode.c_str(), creator.str().c_str(), compress);
if (!hfile.IsOpen())
{
cout << PHWHERE << " Could not open output file" << outfilename << endl;
return -1;
}
map<const string, TNamed *>::const_iterator hiter;
for (hiter = Histo.begin(); hiter != Histo.end(); ++hiter)
{
const std::string &hname = hiter->first;
const TNamed *hptr = hiter->second;
if (Verbosity() > 0)
{
std::cout << PHWHERE << " Saving histo "
<< hname
<< std::endl;
}
// Decode the string to see if it wants a directory
string::size_type pos = hname.find_last_of('/');
string dirname;
if (pos != string::npos) // string::npos is the result if search unsuccessful
{
dirname = hname.substr(0, pos);
}
else
{
dirname = "";
}
if (Verbosity())
{
cout << " Histogram named " << hptr->GetName();
cout << " key " << hname;
if (dirname.size())
{
cout << " being saved to directory " << dirname;
}
cout << endl;
}
if (dirname.size())
{
TDirectoryHelper::mkdir(&hfile, dirname.c_str());
hfile.cd(dirname.c_str());
}
if (hptr)
{
int byteswritten = hptr->Write();
if (!byteswritten)
{
cout << PHWHERE << "Error saving histogram "
<< hptr->GetName()
<< endl;
iret = -2;
}
}
else
{
cout << PHWHERE << "dumpHistos : histogram "
<< hname << " is a null pointer! Won't be saved."
<< std::endl;
}
}
hfile.Close();
return iret;
}
bool Fun4AllHistoManager::registerHisto(TNamed *h1d, const int replace)
{
return registerHisto(h1d->GetName(), h1d, replace);
}
bool Fun4AllHistoManager::registerHisto(const string &hname, TNamed *h1d, const int replace)
{
map<const string, TNamed *>::const_iterator histoiter = Histo.find(hname);
if (histoiter != Histo.end() && replace == 0)
{
cerr << "Histogram " << hname << " already registered, I won't overwrite it" << endl;
cerr << "Use a different name and try again" << endl;
return false;
}
string::size_type pos = hname.find_last_of('/');
string histoname = hname;
if (pos != string::npos) // okay someone wants damn TDirectories
{
histoname = hname.substr(pos + 1);
}
if (Verbosity() > 1)
{
if (histoname != h1d->GetName())
{
cout << PHWHERE << "Histogram " << h1d->GetName()
<< " at " << h1d << " renamed to " << histoname << endl;
}
}
// this one did some very ugly mutilation to a const char *
// using a string seems to avoid the damage
h1d->SetName(histoname.c_str());
Histo[hname] = h1d;
// reset directory for TTree
if (h1d->InheritsFrom("TTree"))
static_cast<TTree *>(h1d)->SetDirectory(0);
// For histograms, enforce error calculation and propagation
if (h1d->InheritsFrom("TH1"))
static_cast<TH1 *>(h1d)->Sumw2();
return true;
}
int Fun4AllHistoManager::isHistoRegistered(const std::string &name) const
{
map<const string, TNamed *>::const_iterator histoiter = Histo.find(name);
if (histoiter != Histo.end())
{
return 1;
}
return 0;
}
TNamed *
Fun4AllHistoManager::getHisto(const unsigned int ihisto) const
{
map<const string, TNamed *>::const_iterator histoiter = Histo.begin();
unsigned int size = Histo.size();
if (Verbosity() > 3)
{
cout << "Map contains " << size << " Elements" << endl;
}
if (ihisto < size)
{
for (unsigned int i = 0; i < ihisto; i++)
{
++histoiter;
}
return histoiter->second;
}
else
{
cout << "Fun4AllHistoManager::getHisto: ERROR Invalid histogram number: "
<< ihisto << ", maximum number is " << size << endl;
}
return nullptr;
}
string
Fun4AllHistoManager::getHistoName(const unsigned int ihisto) const
{
map<const string, TNamed *>::const_iterator histoiter = Histo.begin();
unsigned int size = Histo.size();
if (Verbosity() > 3)
{
cout << "Map contains " << size << " Elements" << endl;
}
if (ihisto < size)
{
for (unsigned int i = 0; i < ihisto; i++)
{
++histoiter;
}
return histoiter->first;
}
else
{
cout << "Fun4AllHistoManager::getHisto: ERROR Invalid histogram number: "
<< ihisto << ", maximum number is " << size << endl;
}
return "";
}
TNamed *
Fun4AllHistoManager::getHisto(const string &hname) const
{
map<const string, TNamed *>::const_iterator histoiter = Histo.find(hname);
if (histoiter != Histo.end())
{
return histoiter->second;
}
cout << "Fun4AllHistoManager::getHisto: ERROR Unknown Histogram " << hname
<< ", The following are implemented: " << endl;
Print("ALL");
return nullptr;
}
void Fun4AllHistoManager::Print(const string &what) const
{
if (what == "ALL" || what == "HISTOS")
{
// loop over the map and print out the content (name and location in memory)
cout << "--------------------------------------" << endl
<< endl;
cout << "List of Histos in Fun4AllHistoManager "
<< Name() << ":" << endl;
map<const string, TNamed *>::const_iterator hiter;
for (hiter = Histo.begin(); hiter != Histo.end(); ++hiter)
{
cout << hiter->first << " is " << hiter->second << endl;
}
cout << endl;
}
return;
}
void Fun4AllHistoManager::Reset()
{
map<const string, TNamed *>::const_iterator hiter;
for (hiter = Histo.begin(); hiter != Histo.end(); ++hiter)
{
TNamed *h = hiter->second;
if (h->InheritsFrom("TH1"))
(dynamic_cast<TH1 *>(h))->Reset();
#if HAS_THNSPARSE
else if (h->InheritsFrom("THnSparse"))
(dynamic_cast<THnSparse *>(h))->Reset();
#endif
}
return;
}