ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/mstein/triggerStudy/tools.h
Revision: 1.1
Committed: Thu Nov 22 15:32:47 2012 UTC (12 years, 5 months ago) by mstein
Content type: text/plain
Branch: MAIN
CVS Tags: v1
Log Message:
first skeleton

File Contents

# Content
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <map>
5 #include <fstream>
6 #include <sstream>
7 #include <Rtypes.h>
8 #include "TChain.h"
9 #include "TFile.h"
10 #include "TString.h"
11 #include "TStopwatch.h"
12 #include "TCanvas.h"
13 #include "TH1D.h"
14 #include "TLeafC.h"
15 #include "TSystem.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18
19
20 using namespace std;
21
22 #ifndef tools_h
23 #define tools_h
24 namespace tools{
25 UInt_t toollevel = 0;
26 Int_t toolverbosity = 0;
27 TStopwatch tooltimer;
28 TChain *toolfChain;
29
30 void adjusth1d(TH1D *h1, TH1D *h2);
31 void enterFcn(TString name, Int_t max, Int_t timer_flg=0);
32 void exitFcn(TString name, Int_t max, Int_t timer_flg=0);
33 void SetVerbosity(Int_t v){toolverbosity=v;}
34 void SetLevel(Int_t l){toollevel=l;}
35 void progress(Long64_t cnt);
36 void percent(Double_t arg1, Double_t arg2, ostream& os=cout);
37 void SetCurrentChain(TChain *chain){toolfChain = chain;}
38 template <typename T>
39 void Swap(T &value1, T &value2);
40
41 TString create_number(Int_t number, Int_t number_max);
42 vector<TString> splitToWords(TString rawString, TString newWord=";");
43 map<TString, TString> splitToWords(TString rawString, TString newWord, TString newValue);
44 TChain* GetCurrentChain(){return toolfChain;}
45
46 template <typename T>
47 void printVector(TString name, vector<T> v, Int_t length=40, ostream& os=cout);
48 template <typename T>
49 void printVectorSize(TString name, vector<T> v, Int_t length=40, ostream& os=cout);
50
51 template<typename T>
52 void addLeaf(TChain *chain, TString name, vector<T> **pt);
53 template<typename T>
54 void addLeaf(TChain *chain, TString name, vector<T> &pt);
55 template<typename T>
56 void addLeaf(TChain *chain, TString name, T **pt);
57 template <typename T>
58 void addLeaf(TChain *chain, TString name, T &pt);
59
60 template<typename T>
61 void addLeaf(TString name, vector<T> **pt){addLeaf(toolfChain, name, &(*pt));}
62 template<typename T>
63 void addLeaf(TString name, vector<T> &pt){addLeaf(toolfChain, name, (pt));}
64 template<typename T>
65 void addLeaf(TString name, T **pt){addLeaf(toolfChain, name, &(*pt));}
66 template<typename T>
67 void addLeaf(TString name, T &pt){addLeaf(toolfChain, name, (pt));}
68 template <typename T>
69
70
71 void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, vector<T> **pt, Long64_t entry=0);
72 template<typename T1, typename T2>
73 void printMap(map<T1, T2> m);
74
75 //template <typename T>
76 //vector<T> GetValueFromLeaf(TString fileName, TString branchName, TString leafName, Long64_t entry=0);
77
78 //TString GetStringFromLeaf(TString fileName, TString branchName, TString leafName, Long64_t entry=0);
79
80
81
82 // // -------------------------------------------------------------------------------------
83 // static std::string toString(double d) {
84 // std::stringstream ss;
85 // ss << d;
86 // return ss.str();
87 // }
88 //
89 // // -------------------------------------------------------------------------------------
90 // static std::string toString(double d, int decPlaces) {
91 // std::stringstream ss;
92 // ss << round(d,decPlaces);
93 // return ss.str();
94 // }
95 //
96 // // -------------------------------------------------------------------------------------
97 // static TString toTString(double d, int decPlaces) {
98 // return toString(d,decPlaces).c_str();
99 // }
100 //
101 //
102 // // -------------------------------------------------------------------------------------
103 // static TString toTString(double d) {
104 // return toString(d).c_str();
105 // }
106 //
107
108
109
110
111 //================================================================================= addToCanvas
112 // template <typename T>
113 // void addToCanvas(TCanvas* c, T* h){
114 // static map<TCanvas*, Bool_t> canExists;
115 // TCanvas *ich = new TCanvas();
116 //
117 // canExists[ich] = 1;
118 //
119 // if(canExists.find(c) != canExists.end())
120 // cout<<"jo den gibts schon" <<endl;
121 // else{
122 // cout<<"ein neuer!"<<endl;
123 // }
124 // }
125
126
127
128 /*
129 void addToCanvas(TCanvas *c, TH1D* h){
130 static map<TCanvas* , Bool_t> canExists;
131 if(canExists.find(c) != canExists.end())
132 cout<<"jo den gibts schon" <<endl;
133 else{
134 canExists[c] = 1;
135 cout<<"ein neuer!"<<endl;
136 }
137 }*/
138
139
140
141
142 //template <typename T>
143 //T GetHistFromFile(TString fileName, TString histName);
144
145 //TH1D* GetHistFromFile(TString &fileName, TString &histName);
146
147 //template <typename T>
148 //void addToCanvas(TCanvas* can, T* h);
149
150 //template <typename T>
151
152 //================================================================================= enterFcn
153 //Help for debugging and to measure running time of a function
154 //0: silent
155 //1: functions called once per run
156 //2: functions multiple times per event
157 //3: functions called once per event
158 //4: functions called multiple times per event
159 void enterFcn(TString name, Int_t max, Int_t timer_flg){
160 ++toollevel;
161 if(toolverbosity>=max){
162 for(UInt_t ilevel=0; ilevel<toollevel; ++ilevel) cout << "++";
163 cout << " enter " << name << endl;
164 }
165 if(timer_flg){tooltimer.Reset(); tooltimer.Start();}
166 }
167
168
169 //================================================================================= exitFcn
170 void exitFcn(TString name, Int_t max, Int_t timer_flg){
171 if(timer_flg){
172 tooltimer.Stop();
173 cout<< name << ": Time (real|CPU) = "<<tooltimer.RealTime()<<" | "<<tooltimer.CpuTime()<<endl;
174 }
175 --toollevel;
176 if(toolverbosity>=max){
177 for(UInt_t ilevel=0; ilevel<=toollevel; ++ilevel) cout << "++";
178 cout << " exit " << name << endl;
179 }
180 }
181
182
183 //================================================================================= progress
184 void progress(Long64_t cnt){
185 static Long64_t step(1),next(1);
186 ++cnt;
187 if(cnt==next || cnt%10000==0){
188 cout << "=========================================================== " << "Enter event " << cnt << endl;
189 next+=step;
190 }
191 if((cnt+1)/step==10)step*=10;
192 }
193
194
195 //================================================================================= printVector
196 template <typename T>
197 void printVector(TString name, vector<T> v, Int_t length, ostream& os){
198 printVectorSize(name,v);
199 if(v.size()>0){
200 for(Int_t i=0,N=v.size(); i<N; ++i){
201 TString temp_name = name;
202 temp_name += "[";
203 temp_name += i;
204 temp_name += "] = ";
205 os << setw(length) << temp_name << v[i] << endl;
206 }
207 os<<endl;
208 }
209 }
210
211
212 //================================================================================= printVectorSize
213 template <typename T>
214 void printVectorSize(TString name, vector<T> v, Int_t length, ostream& os){
215 name += ".size() = ";
216 os << setw(length) << name << v.size();
217 os << endl;
218 }
219
220
221 //================================================================================= percent
222 void percent(Double_t arg1, Double_t arg2, ostream& os){
223 os<<setw(5)<<setprecision(4)<<arg1/arg2*100<<"%";
224 }
225
226
227 //================================================================================= create_number
228 TString create_number(Int_t number, Int_t number_max){
229 TString name = ""; name+=number;
230 Int_t mag_number=1, mag_number_max=1;
231 Int_t n_zero=0;
232 if(number==0) mag_number=2;
233 while(number>=1){
234 number/=10;
235 ++mag_number;
236 }
237 while(number_max>=1){
238 number_max/=10;
239 ++mag_number_max;
240 }
241 n_zero = mag_number_max - mag_number;
242 for(Int_t i=0; i<n_zero; ++i) name.Insert(0,"0");
243 return name;
244 }
245
246
247 //================================================================================= addLeaf
248 template<typename T>
249 void addLeaf(TChain *chain, TString name, vector<T> **pt){
250 *pt=0;
251 chain->SetBranchStatus(name + "*", 1);
252 chain->SetBranchAddress(name, &(*pt));
253 //skim_data->Branch(name, &(*pt));
254 }
255
256
257
258
259
260 // //================================================================================= addLeaf
261 // template<typename T>
262 // void addLeaf(TChain *chain, TString name, vector<T> &pt){
263 // //*pt=0;
264 // chain->SetBranchStatus(name + "*", 1);
265 // chain->SetBranchAddress(name, &pt);
266 // //skim_data->Branch(name, pt);
267 // }
268
269
270 //================================================================================= addLeaf
271 template<typename T>
272 void addLeaf(TChain *chain, TString name, T **pt){
273 *pt=0;
274 chain->SetBranchStatus(name + "*", 1);
275 chain->SetBranchAddress(name, &(*pt));
276 //skim_data->Branch(name, &(*pt));
277 }
278
279
280 //================================================================================= addLeaf
281 template<typename T>
282 void addLeaf(TChain *chain, TString name, T &pt){
283 //pt=0;
284 chain->SetBranchStatus(name + "*", 1);
285 chain->SetBranchAddress(name, &pt);
286 //skim_data->Branch(name + "*", &pt);
287 //skim_data->Branch(name, pt);
288 }
289
290
291 //================================================================================= addLeafString
292 void addLeafString(TString name, TString &pt, Long64_t jentry=0){
293 toolfChain->SetBranchStatus(name + "*", 1);
294 toolfChain->GetEntry(jentry);
295 TLeafC *l = dynamic_cast<TLeafC*>(toolfChain->GetLeaf(name));
296 pt = l->GetValueString();
297 }
298
299
300 //================================================================================= addLeafString
301 void addLeafString(TChain *chain, TString name, TString &pt, Long64_t jentry=0){
302 chain->SetBranchStatus(name + "*", 1);
303 chain->GetEntry(jentry);
304 TLeafC *l = dynamic_cast<TLeafC*>(chain->GetLeaf(name));
305 pt = l->GetValueString();
306 }
307
308
309 //================================================================================= splitToWords
310 vector<TString> splitToWords(TString rawString, TString newWord){
311 vector<TString> words;
312 int pos_beg = 0;
313 int pos_end = 0;
314 while(pos_beg < rawString.Sizeof()){
315 int i;
316 for(i=pos_beg; i<rawString.Sizeof(); ++i){
317 if(rawString(i,1) == newWord) break;
318 }
319 pos_end = i;
320 words.push_back(rawString(pos_beg, pos_end-pos_beg));
321 pos_beg = pos_end+1;
322 }
323 return words;
324 }
325
326
327 //================================================================================= splitToWords
328 map<TString, TString> splitToWords(TString rawString, TString newWord, TString newValue){
329 map<TString, TString> wordsAndValues;
330 vector<TString> words;
331 int pos_beg = 0;
332 int pos_end = 0;
333 while(pos_beg < rawString.Sizeof()){
334 int i;
335 for(i=pos_beg; i<rawString.Sizeof(); ++i){
336 if(rawString(i,1) == newWord) break;
337 }
338 pos_end = i;
339 words.push_back(rawString(pos_beg, pos_end-pos_beg));
340 pos_beg = pos_end+1;
341 vector<TString> temp_words = splitToWords(words.back(), newValue);
342 if(temp_words.size()==2)
343 wordsAndValues[temp_words[0]] = temp_words[1];
344 else if(words[0]!=""){
345 cout<<"ERROR: Something is wrong with the String: " << rawString << " here: " << words.back() << endl;
346 }
347 }
348 return wordsAndValues;
349 }
350
351
352 //================================================================================= GetHistFromFile
353 //Usage: TH1D* h = myplots.GetHistFromFile<TH1D*>("evaluation.root", "Multiplicity");
354 template <typename T>
355 T* GetHistFromFile(TString fileName, TString histName){
356 T *hist = new T();
357
358 // TFile f(fileName);
359 // if (f.IsZombie()) {
360 // cout << "Error opening file" << endl;
361 // exit(-1);
362 // }
363 TFile *file = new TFile(fileName,"READ");
364 if(file->FindObjectAny(histName)) hist = (T*) file->FindObjectAny(histName)->Clone();
365 else cout<<"Histogram does not exist! Creating empty histogram: "<< histName << endl;
366 //file->Close();
367 //delete file;
368 return hist;
369 }
370
371
372 // //================================================================================= GetHistFromFile
373 // template <typename T>
374 // T* GetHistFromFile(TString fileName, TString histName) {
375 // TFile *file = new TFile(fileName,"READ");
376 // T* histo = (T*) file->FindObjectAny(histName)->Clone();
377 // return histo;
378 // }
379
380
381
382 //================================================================================= GetHistFromFile
383 template <typename T>
384 T* GetHistFromFile(TFile *file, TString histName){
385 file->cd();
386 T *hist = new T();
387 if(file->FindObjectAny(histName)) hist = (T*) file->FindObjectAny(histName);
388 else cout<<"Histogram does not exist! Creating empty histogram: "<< histName << endl;
389 //file->Close();
390 return hist;
391 }
392
393
394
395
396 // //================================================================================= GetHistFromFile
397 // //Usage: TH1D* h = myplots.GetHistFromFile<TH1D*>("evaluation.root", "Multiplicity");
398 // template <typename T>
399 // T GetHistFromFile(TString fileName, TString histName){
400 // TFile *file = new TFile(fileName,"READ");
401 // T hist = (T) file->FindObjectAny(histName);
402 // //file->Close();
403 // return hist;
404 // }
405 //
406 //
407 // //================================================================================= GetHistFromFile
408 // template <typename T>
409 // T GetHistFromFile(TFile *f, TString histName){
410 // f->cd();
411 // T hist = (T) f->FindObjectAny(histName);
412 // //file->Close();
413 // return hist;
414 // }
415
416
417
418
419 // //================================================================================= GetValueFromLeaf
420 // template <typename T>
421 // void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, vector<T> **v, Long64_t entry){
422 // TFile *file = new TFile(fileName,"READ");
423 // TTree *fChain = (TTree*)file->Get(branchName);
424 // //fChain->AddFile(fileName);
425 // fChain->SetBranchStatus("*",0);
426 // fChain->SetBranchStatus(leafName, 1);
427 // *v=0;
428 // fChain->SetBranchAddress(leafName, &(*v));
429 // fChain->GetEntry(entry);
430 // }
431
432
433 // //================================================================================= GetValueFromLeaf
434 // template <typename T>
435 // void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, vector<T> &v, Long64_t entry){
436 // TFile *file = new TFile(fileName,"READ");
437 // TTree *fChain = (TTree*)file->Get(branchName);
438 // //fChain->AddFile(fileName);
439 // fChain->SetBranchStatus("*",0);
440 // fChain->SetBranchStatus(leafName, 1);
441 // vector<T> *pt=&v;
442 // fChain->SetBranchAddress(leafName, &(pt));
443 // fChain->GetEntry(entry);
444 // }
445
446
447 // //================================================================================= GetValueFromLeaf
448 // template <typename T>
449 // void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, T &v, Long64_t entry){
450 // cout<<"leafName = "<< leafName<<endl;
451 // TFile *file = new TFile(fileName,"READ");
452 // TTree *fChain = (TTree*)file->Get(branchName);
453 // //fChain->AddFile(fileName);
454 // fChain->SetBranchStatus("*",0);
455 // fChain->SetBranchStatus(leafName, 1);
456 // //T *pt=&v;
457 // T *pt=0;
458 // pt=&v;
459 //
460 // fChain->SetBranchAddress(leafName, &pt);
461 // fChain->GetEntry(entry);
462 // }
463
464
465 //================================================================================= GetValueFromLeaf
466 template <typename T>
467 void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, T &v, Long64_t entry){
468 TFile *file = new TFile(fileName,"READ");
469 TTree *fChain = (TTree*)file->Get(branchName);
470 fChain->SetBranchStatus("*",0);
471 fChain->SetBranchStatus(leafName, 1);
472 fChain->SetBranchAddress(leafName, &v);
473 fChain->GetEntry(entry);
474 }
475
476 //================================================================================= GetValueFromLeaf
477 template <typename T>
478 void GetValueFromLeaf(TString fileName, TString branchName, TString leafName, T **v, Long64_t entry){
479 TFile *file = new TFile(fileName,"READ");
480 TTree *fChain = (TTree*)file->Get(branchName);
481 fChain->SetBranchStatus("*",0);
482 fChain->SetBranchStatus(leafName, 1);
483 //*v=0;
484 fChain->SetBranchAddress(leafName, &(*v));
485 fChain->GetEntry(entry);
486 }
487
488
489 //================================================================================= GetValueFromLeaf
490 template <typename T>
491 void GetVectorFromLeaf(TString fileName, TString brName, TString leafName, T &v, Long64_t entry){
492 TFile *file = new TFile(fileName,"READ");
493 TTree *fChain = (TTree*)file->Get(brName);
494 fChain->SetBranchStatus("*",0);
495 fChain->SetBranchStatus(leafName, 1);
496 T *pt=&v;
497 fChain->SetBranchAddress(leafName, &pt);
498 fChain->GetEntry(entry);
499 }
500
501
502 //================================================================================= GetValueFromLeaf
503 template <typename T>
504 void GetVectorFromLeaf(TString fileName, TString branchName, TString leafName, vector<T> **v, Long64_t entry){
505 TFile *file = new TFile(fileName,"READ");
506 TTree *fChain = (TTree*)file->Get(branchName);
507 fChain->SetBranchStatus("*",0);
508 fChain->SetBranchStatus(leafName, 1);
509 *v=0;
510 fChain->SetBranchAddress(leafName, &(*v));
511 fChain->GetEntry(entry);
512 }
513
514 //================================================================================= GetValueFromLeaf
515 template <typename T>
516 void GetVectorFromLeaf(TFile* file, TString branchName, TString leafName, vector<T> **v, Long64_t entry){
517 TTree *fChain = (TTree*)file->Get(branchName);
518 fChain->SetBranchStatus("*",0);
519 fChain->SetBranchStatus(leafName, 1);
520 *v=0;
521 fChain->SetBranchAddress(leafName, &(*v));
522 fChain->GetEntry(entry);
523 }
524
525
526
527 //================================================================================= GetStringFromLeaf
528 void GetStringFromLeaf(TString fileName, TString branchName, TString leafName, TString &s, Long64_t entry){
529 TFile *file = new TFile(fileName,"READ");
530 TTree *t = (TTree*)file->Get(branchName);
531 t->GetEntry(entry);
532 TLeafC *l = dynamic_cast<TLeafC*>(t->GetLeaf(leafName));
533 s = (l->GetValueString());
534 }
535
536
537 //================================================================================= Swap
538 template <typename T>
539 void Swap(T &value1, T &value2){
540 T dummy;
541 dummy = value1;
542 value1 = value2;
543 value2 = dummy;
544 }
545
546
547 //================================================================================= hist_maximum
548 double hist_maximum(TH1* h){
549 const int i = h->GetMaximumBin();
550 return h->GetBinContent(i) + h->GetBinError(i);
551 }
552
553
554 //================================================================================= removeDuplicates
555 template <typename T>
556 void removeDuplicates(vector<T> &v){
557 for(UInt_t i=0; i<v.size(); ++i){
558 for(UInt_t j=i+1; j<v.size(); ++j){
559 if(v[j]==v[i]){
560 v.erase(v.begin()+j);
561 --j;
562 }
563 }
564 }
565 }
566
567
568 //================================================================================= GetMinimum
569 template <typename T>
570 T GetMinimum(vector<T> &v){
571 T min = 9999999;
572 for(Int_t i=0,N=v.size(); i<N; ++i){
573 if(min>v[i]) min = v[i];
574 }
575 return min;
576 }
577
578
579 //================================================================================= GetMinimum
580 template <typename T1, typename T2>
581 T1 GetMinimum(map<T1, T2> &m){
582 T1 minkey=0;
583 T2 min=9999999;
584 for(typename map<T1, T2>::iterator it=m.begin(); it !=m.end(); ++it){
585 if(min>it->second){
586 minkey = it->first;
587 min = it->second;
588 }
589 }
590 return minkey;
591 }
592
593
594 //================================================================================= GetMaximum
595 template <typename T>
596 T GetMaximum(vector<T> &v){
597 T max = -9999999;
598 for(Int_t i=0,N=v.size(); i<N; ++i){
599 if(max<v[i]) max = v[i];
600 }
601 return max;
602 }
603
604
605 //================================================================================= GetMinimum
606 template <typename T1, typename T2>
607 T1 GetMaximum(map<T1, T2> &m){
608 T1 maxkey=0;
609 T2 max=-9999999;
610 for(typename map<T1, T2>::iterator it=m.begin(); it !=m.end(); ++it){
611 if(max<it->second){
612 maxkey = it->first;
613 max = it->second;
614 }
615 }
616 return maxkey;
617 }
618
619
620 //================================================================================= adjusth1d
621 void adjusth1d(TH1D *h1, TH1D *h2){
622 Double_t max1 = h1->GetMaximum();
623 Double_t max2 = h2->GetMaximum();
624 Double_t min1 = h1->GetMinimum();
625 Double_t min2 = h2->GetMinimum();
626 Double_t norm1 = h1->GetNormFactor();
627 Double_t norm2 = h2->GetNormFactor();
628 Double_t scale1 = 1.; //if the histo ist normed to some value, SetUserRange etc. depends on the normalization (which is, of course a root-bug)
629 Double_t scale2 = 1.;
630
631 if(norm1) scale1 = norm1/h1->Integral();
632 if(norm2) scale2 = norm2/h2->Integral();
633 max1 *= scale1;
634 max2 *= scale2;
635 Double_t maximum = max(max1, max2)*1.1;
636 Double_t minimum = min(min1, min2)*1.1;
637 h1->GetYaxis()->SetRangeUser(minimum/scale1, maximum/scale1);
638 h2->GetYaxis()->SetRangeUser(minimum/scale2, maximum/scale2);
639
640 // cout<<"max1 = " << max1 << endl;
641 // cout<<"max2 = " << max2 << endl;
642 // cout<<"norm1 = " << norm1 << endl;
643 // cout<<"norm2 = " << norm2 << endl;
644 // cout<<"h1->GetName() = " << h1->GetName() << endl;
645 // cout<<"h1->GetMaximum() = " << h1->GetMaximum() << endl;
646 // cout<<"h1->GetNormFactor() = " << h1->GetNormFactor() << endl;
647 // cout<<"h1->GetEntries() = " << h1->GetEntries() << endl;
648 // cout<<"h1->GetMaximumStored() = " << h1->GetMaximumStored() << endl;
649 // cout<<"h1->Integral() = " << h1->Integral() << endl;
650 // cout<<"hist_maximum(h1) = " << hist_maximum(h1) << endl;
651 // cout<<"h2->GetName() = " << h2->GetName() << endl;
652 // cout<<"h2->GetMaximum() = " << h2->GetMaximum() << endl;
653 // cout<<"h2->GetNormFactor() = " << h2->GetNormFactor() << endl;
654 // cout<<"h2->GetEntries() = " << h2->GetEntries() << endl;
655 // cout<<"h2->GetMaximumStored() = " << h2->GetMaximumStored() << endl;
656 // cout<<"h2->Integral() = " << h2->Integral() << endl;
657 // cout<<"hist_maximum(h2) = " << hist_maximum(h2) << endl;
658 // cout<<"maximum = " << maximum << endl;
659 // cout<<endl;
660 }
661
662
663 //================================================================================= adjusth1d
664 void adjusth1d(vector<TH1D*> h){
665 vector<Double_t> max, min, norm, scale;
666 for(Int_t i=0,N=h.size(); i<N; ++i){
667 max .push_back(h[i]->GetMaximum());
668 min .push_back(h[i]->GetMinimum());
669 norm.push_back(h[i]->GetNormFactor());
670 scale.push_back(1.);
671 if(norm[i]) scale[i] = norm[i]/h[i]->Integral();
672 max[i] *= scale[i];
673 }
674 Double_t maximum = -99999999.;
675 Double_t minimum = 99999999.;
676 for(Int_t i=0,N=h.size(); i<N; ++i){
677 if(maximum < max[i]) maximum = max[i];
678 if(minimum > min[i]) minimum = min[i];
679 }
680 maximum*=1.1;
681 minimum*=1.1;
682 for(Int_t i=0,N=h.size(); i<N; ++i){
683 h[i]->GetYaxis()->SetRangeUser(minimum/scale[i], maximum/scale[i]);
684 }
685 }
686
687
688 //================================================================================= printMap
689 template<typename T1, typename T2>
690 void printMap(map<T1, T2> m){
691 cout<<"size = " << m.size() << endl;
692 for(typename map<T1, T2>::iterator it=m.begin(); it !=m.end(); ++it){
693 cout<<it->first << " = " << it->second << endl;
694 }
695 }
696
697 //================================================================================= printMap
698 template<typename T1, typename T2, typename T3>
699 void printMap(TString name, map<T1, map<T2, T3> > m, Int_t width1=10, Int_t width2=10){
700 cout<<"size = " << m.size() << endl;
701 for(typename map<T1, map<T2, T3> >::iterator it=m.begin(); it !=m.end(); ++it){
702 for(typename map<T2, T3>::iterator it2=m[it->first].begin(); it2!=m[it->first].end(); ++it2){
703 cout<<name<<"["
704 <<setw(width1)<<it ->first << "]["
705 <<setw(width2)<<it2->first << "] = "
706 <<it2->second
707 <<endl;
708 }
709 }
710 }
711
712
713 //================================================================================= printMap
714 template<typename T1, typename T2>
715 void printMap(map<T1, T2> m, Int_t setWidth){
716 cout<<"size = " << m.size() << endl;
717 for(typename map<T1, T2>::iterator it=m.begin(); it !=m.end(); ++it){
718 cout<<setw(setWidth)<<it->first << " = " << it->second << endl;
719 }
720 }
721
722
723 //================================================================================= FillVectorFromFile
724 template<typename T>
725 vector<T> FillVectorFromFile(TString fileName){
726 vector<T> v;
727 ifstream file(fileName);
728 TString s;
729 while(!file.eof()){
730 file >> s;
731 stringstream xmorph;
732 xmorph << s;
733 T my;
734 xmorph >> my;
735 v.push_back(my);
736 //cout<<"myint = "<< my << endl;
737 }
738 return v;
739 }
740
741
742 //================================================================================= FillVectorFromFile
743 template<typename T>
744 vector<T> FillVectorFromLs(TString path, TString name){
745 vector<T> v;
746 TString command="";
747 command="ls ";
748 command+= path;
749 command+=name;
750 command+=" > tempLsCommand.txt";
751 system(command);
752 //cout<<"command = " << command << endl;
753 v = tools::FillVectorFromFile<TString>("tempLsCommand.txt");
754 v.erase(v.end()-1);
755 //------------------------------------ remove the path from file names
756 for(Int_t i=0,N=v.size(); i<N; ++i) v[i].ReplaceAll(path, "");
757 // system("rm tempLsCommand.txt");
758 return v;
759 }
760
761
762 //================================================================================= save
763 template<typename T>
764 void save(map<TString, T> m, TString fileName, TString option="RECREATE"){
765 TFile *file = new TFile(fileName, option);
766 for(typename map<TString, T>::iterator it = m.begin(); it != m.end(); ++it){
767 it->second->Write(it->second->GetName());
768 }
769 file->Close();
770 }
771
772 //================================================================================= createDir
773 void createDir(TString folderName){
774 Int_t dummy = system("mkdir " + folderName + " -p");
775 dummy=1;
776 }
777
778
779 //================================================================================= createRatioPlot
780 template <typename T>
781 T* createRatioPlot(T* h1, T* h2, TString name="", TString yTitle=""){
782 T* ratioPlot = (T*)h1->Clone();
783 ratioPlot->Divide(h2);
784 if(name !=""){
785 ratioPlot->SetName(name);
786 ratioPlot->SetTitle(name);
787 }
788 if(yTitle!=""){
789 ratioPlot->GetYaxis()->SetTitle(yTitle);
790 }
791 return ratioPlot;
792 }
793
794
795 //================================================================================= isNew
796 template <typename T>
797 Bool_t isNew(TString name, map<TString, T> m){
798 return (m.find(name) == m.end());
799 }
800
801
802 //================================================================================= isNew
803 template <typename T1, typename T2>
804 Bool_t isNew(TString name, map<TString, map<T1, T2> > m){
805 return (m.find(name) == m.end());
806 }
807
808 // map<TString, map<TString, map<Int_t, Double_t> > > eventYield;
809
810
811
812
813 //================================================================================= isNew
814 template <typename T>
815 Bool_t isNew(TString name, TString label, map<TString, map<TString, T> > m){
816 return (m[name].find(label) == m[name].end());
817 }
818
819
820
821 //================================================================================= isNew
822 template <typename T>
823 Bool_t isNew(T find, vector<T> v){
824 for(Int_t i=0,N=v.size(); i<N; ++i){
825 if(v[i]==find) return false;
826 }
827 return true;
828 }
829
830
831
832 //================================================================================= GetValueFromTH1
833 template <typename T>
834 Double_t GetBinFromTH1(T *h, Double_t value){
835 Double_t maxValue = h->GetXaxis()->GetXmax();
836 Int_t xMaxBin = h->GetNbinsX();
837 Int_t xBin = 0;
838 if(value > maxValue) xBin = xMaxBin;
839 else{while(value > h->GetXaxis()->GetBinLowEdge(xBin+1)) ++xBin;}
840
841 for(Int_t i=0; i<xMaxBin; ++i){
842 cout<<"bin[" <<i<< "] = " << h->GetBinContent(i)<< endl;
843 }
844 cout<<"xBin = " << endl;
845 return xBin;
846 }
847
848
849 //================================================================================= GetValueFromTH1
850 template <typename T>
851 Double_t GetValueFromTH1(T *h, Double_t value){
852 Int_t bin = GetBinFromTH1<T>(h, value);
853 return h->GetBinContent(bin);
854 }
855
856
857 //================================================================================= GetBinFromTH2
858 template <typename T>
859 void GetBinFromTH2(T *h, Int_t &xBin, Int_t &yBin, Double_t value1, Double_t value2){
860 Double_t xMaxValue = h->GetXaxis()->GetXmax();
861 Double_t yMaxValue = h->GetYaxis()->GetXmax();
862 Int_t xMaxBin = h->GetNbinsX();
863 Int_t yMaxBin = h->GetNbinsY();
864 xBin = 0;
865 yBin = 0;
866 if(value1 > xMaxValue) xBin = xMaxBin;
867 else{while(value1 > h->GetXaxis()->GetBinLowEdge(xBin+1)) ++xBin;}
868 if(value2 > yMaxValue) yBin = yMaxBin;
869 else{while(value2 > h->GetYaxis()->GetBinLowEdge(yBin+1)) ++yBin;}
870 }
871
872
873 //================================================================================= GetValueFromTH2
874 template <typename T>
875 Double_t GetValueFromTH2(T *h, Double_t value1, Double_t value2){
876 Int_t xBin = 0;
877 Int_t yBin = 0;
878 GetBinFromTH2<T>(h, xBin, yBin, value1, value2);
879 return h->GetBinContent(xBin, yBin);
880 }
881
882
883
884
885
886
887
888
889 }
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908 #endif
909
910
911
912
913 // //================================================================================= GetValueFromLeaf
914 // template <typename T>
915 // void GetValueFromLeafPtr(TString fileName, TString branchName, TString leafName, vector<T> **ptr, Long64_t entry){
916 // TFile *file = new TFile(fileName,"READ");
917 // TTree *fChain = (TTree*)file->Get(branchName);
918 // //fChain->AddFile(fileName);
919 // fChain->SetBranchStatus("*",0);
920 // fChain->SetBranchStatus(leafName, 1);
921 // //*ptr=0;
922 // fChain->SetBranchAddress(leafName, &(*ptr));
923 // fChain->GetEntry(entry);
924 // }
925
926 // //================================================================================= GetValueFromLeaf
927 // template <typename T>
928 // vector<T> GetValueFromLeaf(TString fileName, TString branchName, TString leafName, Long64_t entry){
929 // TFile *file = new TFile(fileName,"READ");
930 // TTree *fChain = (TTree*)file->Get(branchName);
931 // vector<T> *value;
932 // //fChain->AddFile(fileName);
933 // fChain->SetBranchStatus("*",0);
934 // fChain->SetBranchStatus(leafName, 1);
935 // fChain->SetBranchAddress(leafName, &(*value));
936 // fChain->GetEntry(entry);
937 // return &(*value);
938 // }
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954