1 |
#include <string>
|
2 |
#include <vector>
|
3 |
#include <iostream>
|
4 |
#include <map>
|
5 |
#include <utility>
|
6 |
|
7 |
|
8 |
#include "TFile.h"
|
9 |
#include "TTree.h"
|
10 |
#include "TH1I.h"
|
11 |
#include "TH1F.h"
|
12 |
#include "TH1D.h"
|
13 |
#include "TH2F.h"
|
14 |
#include "TH2D.h"
|
15 |
#include "TProfile.h"
|
16 |
#include "TString.h"
|
17 |
|
18 |
class HistogramManager{
|
19 |
|
20 |
public:
|
21 |
|
22 |
|
23 |
HistogramManager();
|
24 |
~HistogramManager();
|
25 |
static HistogramManager* getInstance();
|
26 |
static void dispose();
|
27 |
void print();
|
28 |
void openFile(TString name,TString option);
|
29 |
void openTree(std::string name);
|
30 |
TTree* getTree(){return fTree;}
|
31 |
void closeFile();
|
32 |
void fillHisto(TString name, double x, double y, double weight);
|
33 |
void addBinContent(TString name, int bin, double x);
|
34 |
void scale(TString name, double x);
|
35 |
void fillTree();
|
36 |
void printTree();
|
37 |
|
38 |
// return histograms by using their names
|
39 |
TH1* getHisto1F(TString name);
|
40 |
TH1* getHisto1D(TString name);
|
41 |
TH2* getHisto2D(TString name);
|
42 |
TH2* getHisto2F(TString name);
|
43 |
TH1* getHisto1I(TString name);
|
44 |
TProfile* getHistoProfile(TString name);
|
45 |
// add branches to the tree
|
46 |
void addTreeD(TString branchName, double branchVariable);
|
47 |
void addTreeDArray(TString branchName, double* branchVariable, int size);
|
48 |
void addTreeFArray(TString branchName, float* branchVariable, int size);
|
49 |
void addTreeF(TString branchName, float branchVariable);
|
50 |
void addTreeI(TString branchName, int branchVariable);
|
51 |
// add histograms to the file
|
52 |
void addHisto1F(TString name, TString title, int nbinsX, float xMin, float xMax, bool setError);
|
53 |
void addHisto1D(TString name, TString title, int nbinsX, double xMin, double xMax, bool setError);
|
54 |
void addHisto2F(TString name, TString title, int nbinsX, float xMin, float xMax,int nbinsY, float yMin, float yMax, bool setError);
|
55 |
void addHisto2D(TString name, TString title, int nbinsX, double xMin, double xMax,int nbinsY, float yMin, float yMax, bool setError);
|
56 |
void addHistoProfile(TString name, TString title, int nbinsX, double xMin, double xMax, bool setError);
|
57 |
void setTitles(TString name, TString xAxis, TString yAxis);
|
58 |
|
59 |
|
60 |
private:
|
61 |
static HistogramManager* instance;
|
62 |
TFile* fOutFile;
|
63 |
TTree* fTree;
|
64 |
std::map<TString, TH1*> fHists1I;
|
65 |
std::map<TString, TH1*> fHists1F;
|
66 |
std::map<TString, TH1*> fHists1D;
|
67 |
std::map<TString, TH2*> fHists2F;
|
68 |
std::map<TString, TH2*> fHists2D;
|
69 |
std::map<TString, TProfile*> fHistsTProfile;
|
70 |
};
|
71 |
|
72 |
using namespace std;
|
73 |
HistogramManager* HistogramManager::instance = 0;
|
74 |
HistogramManager* HistogramManager::getInstance(){
|
75 |
if (instance == 0) instance = new HistogramManager();
|
76 |
return instance;
|
77 |
}
|
78 |
|
79 |
void HistogramManager::dispose(){
|
80 |
if (instance != 0){
|
81 |
delete instance;
|
82 |
instance = 0;
|
83 |
}
|
84 |
}
|
85 |
|
86 |
HistogramManager::HistogramManager()
|
87 |
: fOutFile(0)
|
88 |
{
|
89 |
|
90 |
}
|
91 |
HistogramManager::~HistogramManager()
|
92 |
{
|
93 |
|
94 |
}
|
95 |
void HistogramManager::openFile(TString name,TString option){
|
96 |
fOutFile = new TFile(name.Data(), option);
|
97 |
}
|
98 |
|
99 |
void HistogramManager::openTree(std::string name){
|
100 |
fTree = new TTree(name.c_str(), "analysis tree");
|
101 |
}
|
102 |
|
103 |
void HistogramManager::printTree(){
|
104 |
fTree->Print();
|
105 |
}
|
106 |
void HistogramManager::closeFile(){
|
107 |
fOutFile->Write("", TObject::kOverwrite);
|
108 |
fOutFile->Close();
|
109 |
}
|
110 |
void HistogramManager::addTreeD(TString branchName, double branchVariable){
|
111 |
TString branchType = branchName+"/D";//this is wrong, must be changed
|
112 |
fTree->Branch(branchName.Data(),&branchVariable,branchType.Data());
|
113 |
return;
|
114 |
// fTree->Branch("response2", response,"response[size]/F");
|
115 |
|
116 |
}
|
117 |
void HistogramManager::addTreeDArray(TString branchName, double* branchVariable, int size){
|
118 |
|
119 |
TString branchType = branchName+"[size]/D";
|
120 |
fTree->Branch(branchName.Data(),branchVariable,branchType.Data());
|
121 |
return;
|
122 |
}
|
123 |
void HistogramManager::addTreeFArray(TString branchName, float* branchVariable, int size){
|
124 |
|
125 |
TString branchType = branchName+"[size]/F";
|
126 |
fTree->Branch(branchName.Data(),branchVariable,branchType.Data());
|
127 |
return;
|
128 |
}
|
129 |
void HistogramManager::addTreeF(TString branchName, float branchVariable){
|
130 |
TString branchType = branchName+"/F";
|
131 |
fTree->Branch(branchName.Data(),&branchVariable,branchType.Data());
|
132 |
return;
|
133 |
}
|
134 |
void HistogramManager::addTreeI(TString branchName, int branchVariable){
|
135 |
TString branchType = branchName+"/I";
|
136 |
fTree->Branch(branchName.Data(),&branchVariable,branchType.Data());
|
137 |
return;
|
138 |
}
|
139 |
|
140 |
void HistogramManager::addHisto1F(TString name, TString title, int nbinsX, float xMin, float xMax, bool setError){
|
141 |
fHists1F[name] = new TH1F(name.Data(),title.Data(),nbinsX, xMin, xMax);
|
142 |
if(setError)fHists1F[name]->Sumw2();
|
143 |
return;
|
144 |
}
|
145 |
void HistogramManager::addHisto1D(TString name, TString title, int nbinsX, double xMin, double xMax, bool setError){
|
146 |
fHists1D[name] = new TH1D(name.Data(),title.Data(),nbinsX, xMin, xMax);
|
147 |
if(setError)fHists1D[name]->Sumw2();
|
148 |
return;
|
149 |
}
|
150 |
void HistogramManager::addHisto2F(TString name, TString title, int nbinsX, float xMin, float xMax,int nbinsY, float yMin, float yMax, bool setError){
|
151 |
fHists2F[name] = new TH2F(name.Data(),title.Data(),nbinsX, xMin, xMax,nbinsY , yMin, yMax);
|
152 |
if(setError)fHists2F[name]->Sumw2();
|
153 |
return;
|
154 |
}
|
155 |
void HistogramManager::addHisto2D(TString name, TString title, int nbinsX, double xMin, double xMax,int nbinsY, float yMin, float yMax, bool setError){
|
156 |
fHists2F[name] = new TH2D(name.Data(),title.Data(),nbinsX, xMin, xMax,nbinsY , yMin, yMax);
|
157 |
if(setError)fHists2D[name]->Sumw2();
|
158 |
return;
|
159 |
}
|
160 |
////////////////////////
|
161 |
|
162 |
void HistogramManager::addHistoProfile(TString name, TString title, int nbinsX, double xMin, double xMax, bool setError) {
|
163 |
fHistsTProfile[name] = new TProfile(name.Data(),title.Data(),nbinsX, xMin,xMax);
|
164 |
//char title[1024];
|
165 |
//sprintf(title, "%s [RUN:%i]", fTitle.c_str(), fRunno);
|
166 |
//return fDir.make < TProfile > (fName.c_str(), title, fNbins, fXmin, fXmax);
|
167 |
}
|
168 |
//TProfile(const char* name, const char* title, Int_t nbinsx, Double_t xlow, Double_t xup, Double_t ylow, Double_t yup, Option_t* option = "")
|
169 |
|
170 |
/////////////////////////77
|
171 |
|
172 |
void HistogramManager::fillHisto(TString name, double x, double y, double weight){
|
173 |
bool found = false;
|
174 |
std::map<TString, TH1*>::iterator iter = fHists1I.find(name);
|
175 |
// this is wrong! fix it
|
176 |
if(iter != fHists1I.end()){//key is found
|
177 |
found = true;
|
178 |
iter->second->Fill(x,weight);
|
179 |
}
|
180 |
if(!found){
|
181 |
std::map<TString, TH1*>::iterator iter = fHists1F.find(name);
|
182 |
if(iter != fHists1F.end()){//key is found
|
183 |
found = true;
|
184 |
if(name == "FiredTriggerBits"){
|
185 |
if(y==1){//bit is fired
|
186 |
iter->second->Fill(x,weight);
|
187 |
}
|
188 |
}else{
|
189 |
iter->second->Fill(x,weight);
|
190 |
}
|
191 |
}
|
192 |
}
|
193 |
if(!found){
|
194 |
std::map<TString, TH2*>::iterator iter = fHists2F.find(name);
|
195 |
if(iter != fHists2F.end()){//key is found
|
196 |
found = true;
|
197 |
iter->second->Fill(x,y,weight);
|
198 |
|
199 |
}
|
200 |
}
|
201 |
if(!found){
|
202 |
std::map<TString, TH1*>::iterator iter = fHists1D.find(name);
|
203 |
if(iter != fHists1D.end()){//key is found
|
204 |
found = true;
|
205 |
if(name == "FiredTriggerBits"){
|
206 |
if(y==1){//bit is fired
|
207 |
iter->second->Fill(x,weight);
|
208 |
}
|
209 |
}else{
|
210 |
iter->second->Fill(x,weight);
|
211 |
}
|
212 |
}
|
213 |
}
|
214 |
if(!found){
|
215 |
std::map<TString, TH2*>::iterator iter = fHists2D.find(name);
|
216 |
if(iter != fHists2D.end()){//key is found
|
217 |
found = true;
|
218 |
iter->second->Fill(x,y,weight);
|
219 |
}
|
220 |
}
|
221 |
if(!found){
|
222 |
std::map<TString, TProfile*>::iterator iter = fHistsTProfile.find(name);
|
223 |
if(iter != fHistsTProfile.end()){//key is found
|
224 |
found = true;
|
225 |
iter->second->Fill(x,y);
|
226 |
}
|
227 |
}
|
228 |
|
229 |
|
230 |
}
|
231 |
void HistogramManager::scale(TString name, double x){
|
232 |
bool found = false;
|
233 |
std::map<TString, TH1*>::iterator iter = fHists1I.find(name);
|
234 |
// this is wrong! fix it
|
235 |
if(iter != fHists1I.end()){//key is found
|
236 |
found = true;
|
237 |
iter->second->Scale(x);
|
238 |
}
|
239 |
if(!found){
|
240 |
std::map<TString, TH1*>::iterator iter = fHists1F.find(name);
|
241 |
if(iter != fHists1F.end()){//key is found
|
242 |
found = true;
|
243 |
iter->second->Scale(x);
|
244 |
}
|
245 |
}
|
246 |
if(!found){
|
247 |
std::map<TString, TH1*>::iterator iter = fHists1D.find(name);
|
248 |
if(iter != fHists1D.end()){//key is found
|
249 |
found = true;
|
250 |
iter->second->Scale(x);
|
251 |
}
|
252 |
}
|
253 |
}
|
254 |
void HistogramManager::addBinContent(TString name, int bin, double x){
|
255 |
bool found = false;
|
256 |
std::map<TString, TH1*>::iterator iter = fHists1I.find(name);
|
257 |
// this is wrong! fix it
|
258 |
if(iter != fHists1I.end()){//key is found
|
259 |
found = true;
|
260 |
iter->second->AddBinContent(bin,x);
|
261 |
}
|
262 |
if(!found){
|
263 |
std::map<TString, TH1*>::iterator iter = fHists1F.find(name);
|
264 |
if(iter != fHists1F.end()){//key is found
|
265 |
found = true;
|
266 |
iter->second->AddBinContent(bin,x);
|
267 |
}
|
268 |
}
|
269 |
if(!found){
|
270 |
std::map<TString, TH1*>::iterator iter = fHists1D.find(name);
|
271 |
if(iter != fHists1D.end()){//key is found
|
272 |
found = true;
|
273 |
iter->second->AddBinContent(bin,x);
|
274 |
}
|
275 |
}
|
276 |
}
|
277 |
void HistogramManager::fillTree(){
|
278 |
fTree->Fill();
|
279 |
}
|
280 |
//////////////////////////
|
281 |
TProfile* HistogramManager::getHistoProfile(TString name){
|
282 |
//string name = getName(id, type);
|
283 |
bool found = false;
|
284 |
std::map<TString, TProfile*>::iterator iter = fHistsTProfile.find(name);
|
285 |
if (iter != fHistsTProfile.end()) { // key is found
|
286 |
found = true;
|
287 |
//iter->second->Fill(x, y);
|
288 |
return (iter->second);
|
289 |
} else {
|
290 |
cout << name << " was not found!" << endl;
|
291 |
}
|
292 |
return 0;
|
293 |
}
|
294 |
//////////////////////////////7
|
295 |
TH1* HistogramManager::getHisto1F(TString name){
|
296 |
bool found = false;
|
297 |
std::map<TString, TH1*>::iterator iter = fHists1F.find(name);
|
298 |
if(iter != fHists1F.end()){//key is found
|
299 |
found = true;
|
300 |
return (iter->second);
|
301 |
}
|
302 |
return 0;
|
303 |
}
|
304 |
TH1* HistogramManager::getHisto1D(TString name){
|
305 |
bool found = false;
|
306 |
std::map<TString, TH1*>::iterator iter = fHists1D.find(name);
|
307 |
if(iter != fHists1D.end()){//key is found
|
308 |
found = true;
|
309 |
return (iter->second);
|
310 |
}else{
|
311 |
cout << name << " not found" << endl;
|
312 |
}
|
313 |
return 0;
|
314 |
}
|
315 |
TH2* HistogramManager::getHisto2F(TString name){
|
316 |
bool found = false;
|
317 |
std::map<TString, TH2*>::iterator iter = fHists2F.find(name);
|
318 |
if(iter != fHists2F.end()){//key is found
|
319 |
found = true;
|
320 |
return (iter->second);
|
321 |
}
|
322 |
return 0;
|
323 |
}
|
324 |
TH2* HistogramManager::getHisto2D(TString name){
|
325 |
bool found = false;
|
326 |
std::map<TString, TH2*>::iterator iter = fHists2D.find(name);
|
327 |
if(iter != fHists2D.end()){//key is found
|
328 |
found = true;
|
329 |
return (iter->second);
|
330 |
}
|
331 |
return 0;
|
332 |
}
|
333 |
void HistogramManager::setTitles(TString name, TString xAxis, TString yAxis){
|
334 |
bool found = false;
|
335 |
std::map<TString, TH1*>::iterator iter = fHists1I.find(name);
|
336 |
if(iter != fHists1I.end()){//key is found
|
337 |
found = true;
|
338 |
iter->second->SetXTitle(xAxis);
|
339 |
iter->second->SetYTitle(yAxis);
|
340 |
}
|
341 |
if(!found){
|
342 |
std::map<TString, TH1*>::iterator iter = fHists1F.find(name);
|
343 |
if(iter != fHists1F.end()){//key is found
|
344 |
found = true;
|
345 |
iter->second->SetXTitle(xAxis);
|
346 |
iter->second->SetYTitle(yAxis);
|
347 |
}
|
348 |
}
|
349 |
if(!found){
|
350 |
std::map<TString, TH2*>::iterator iter = fHists2F.find(name);
|
351 |
if(iter != fHists2F.end()){//key is found
|
352 |
found = true;
|
353 |
iter->second->SetXTitle(xAxis);
|
354 |
iter->second->SetYTitle(yAxis);
|
355 |
}
|
356 |
}
|
357 |
if(!found){
|
358 |
std::map<TString, TH1*>::iterator iter = fHists1D.find(name);
|
359 |
if(iter != fHists1D.end()){//key is found
|
360 |
found = true;
|
361 |
iter->second->SetXTitle(xAxis);
|
362 |
iter->second->SetYTitle(yAxis);
|
363 |
}
|
364 |
}
|
365 |
if(!found){
|
366 |
std::map<TString, TH2*>::iterator iter = fHists2D.find(name);
|
367 |
if(iter != fHists2D.end()){//key is found
|
368 |
found = true;
|
369 |
iter->second->SetXTitle(xAxis);
|
370 |
iter->second->SetYTitle(yAxis);
|
371 |
}
|
372 |
}
|
373 |
return;
|
374 |
}
|
375 |
|
376 |
TH1* HistogramManager::getHisto1I(TString name){
|
377 |
bool found = false;
|
378 |
std::map<TString, TH1*>::iterator iter = fHists1I.find(name);
|
379 |
if(iter != fHists1I.end()){//key is found
|
380 |
found = true;
|
381 |
return (iter->second);
|
382 |
}
|
383 |
return 0;
|
384 |
}
|
385 |
void HistogramManager::print(){
|
386 |
cout << "====================== HISTOGRAMS =======================" << endl;
|
387 |
for (std::map<TString, TH1*>::iterator iter = fHists1I.begin(); iter != fHists1I.end(); ++iter) {
|
388 |
cout << iter->first << endl;
|
389 |
}
|
390 |
for (std::map<TString, TH1*>::iterator iter = fHists1F.begin(); iter != fHists1F.end(); ++iter) {
|
391 |
cout << iter->first << endl;
|
392 |
}
|
393 |
for (std::map<TString, TH2*>::iterator iter = fHists2F.begin(); iter != fHists2F.end(); ++iter) {
|
394 |
cout << iter->first << endl;
|
395 |
}
|
396 |
for (std::map<TString, TH1*>::iterator iter = fHists1D.begin(); iter != fHists1D.end(); ++iter) {
|
397 |
cout << iter->first << endl;
|
398 |
}
|
399 |
for (std::map<TString, TH2*>::iterator iter = fHists2D.begin(); iter != fHists2D.end(); ++iter) {
|
400 |
cout << iter->first << endl;
|
401 |
}
|
402 |
for (std::map<TString, TProfile*>::iterator iter = fHistsTProfile.begin(); iter != fHistsTProfile.end(); ++iter) {
|
403 |
cout << iter->first << endl;
|
404 |
}
|
405 |
|
406 |
cout << "=========================================================" << endl;
|
407 |
}
|
408 |
|
409 |
|