ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/mstein/triggerStudy/tools.h
Revision: 1.2
Committed: Tue Nov 27 12:50:57 2012 UTC (12 years, 5 months ago) by mstein
Content type: text/plain
Branch: MAIN
CVS Tags: v2, HEAD
Changes since 1.1: +68 -803 lines
Log Message:
first running version

File Contents

# User Rev Content
1 mstein 1.2
2 mstein 1.1 #include <iostream>
3     #include <iomanip>
4     #include <vector>
5     #include <map>
6 mstein 1.2 // #include <fstream>
7     // #include <sstream>
8     // #include <stdio.h>
9     // #include <stdlib.h>
10    
11     // #include <Rtypes.h>
12     // #include "TChain.h"
13     // #include "TFile.h"
14 mstein 1.1 #include "TString.h"
15 mstein 1.2 // #include "TStopwatch.h"
16     // #include "TCanvas.h"
17     // #include "TH1D.h"
18     // #include "TLeafC.h"
19     // #include "TSystem.h"
20 mstein 1.1
21    
22     using namespace std;
23    
24     #ifndef tools_h
25     #define tools_h
26     namespace tools{
27     //================================================================================= progress
28     void progress(Long64_t cnt){
29     static Long64_t step(1),next(1);
30     ++cnt;
31     if(cnt==next || cnt%10000==0){
32     cout << "=========================================================== " << "Enter event " << cnt << endl;
33     next+=step;
34     }
35     if((cnt+1)/step==10)step*=10;
36     }
37    
38    
39 mstein 1.2 // //================================================================================= progressReset
40     // void progressReset(){
41     // static Long64_t step(1),next(1);
42     // step=next;
43     // next=step;
44     // }
45    
46    
47    
48 mstein 1.1 //================================================================================= printVector
49     template <typename T>
50 mstein 1.2 void printVector(TString name, vector<T> v, Int_t length=40, ostream& os=cout){
51     TString nameSize = name;
52     nameSize += ".size() ";
53     // os << setw(length) << name << ".size() = " << v.size() << endl;
54     //os << setw(length) << nameSize << "------> " << v.size() << endl;
55     os << setw(length) << "size " << "------> " << name << ": " << v.size() << endl;
56    
57 mstein 1.1 if(v.size()>0){
58     for(Int_t i=0,N=v.size(); i<N; ++i){
59     TString temp_name = name;
60     temp_name += "[";
61     temp_name += i;
62     temp_name += "] = ";
63     os << setw(length) << temp_name << v[i] << endl;
64     }
65     }
66 mstein 1.2 os<<endl;
67 mstein 1.1 }
68 mstein 1.2
69    
70     //================================================================================= printVector
71 mstein 1.1 template <typename T>
72 mstein 1.2 void printVector(TString name, vector<T> v, ostream& os, Int_t length=40){
73     printVector<T>(name, v, length, os);
74 mstein 1.1 }
75 mstein 1.2
76    
77     //================================================================================= printMap
78     template<typename T1, typename T2, typename T3>
79     void printMap(TString name, map<T1, map<T2, T3> > m, Int_t width1=10, Int_t width2=10){
80     cout<<"size = " << m.size() << endl;
81     for(typename map<T1, map<T2, T3> >::iterator it=m.begin(); it !=m.end(); ++it){
82     for(typename map<T2, T3>::iterator it2=m[it->first].begin(); it2!=m[it->first].end(); ++it2){
83     cout<<name<<"["
84     <<setw(width1)<<it ->first << "]["
85     <<setw(width2)<<it2->first << "] = "
86     <<it2->second
87     <<endl;
88     }
89     }
90 mstein 1.1 }
91    
92    
93 mstein 1.2 //================================================================================= printMap
94     template<typename T1, typename T2>
95     void printMap(map<T1, T2> m, Int_t setWidth){
96     cout<<"size = " << m.size() << endl;
97     for(typename map<T1, T2>::iterator it=m.begin(); it !=m.end(); ++it){
98     cout<<setw(setWidth)<<left<<it->first << " = " << it->second << endl;
99 mstein 1.1 }
100     }
101 mstein 1.2
102 mstein 1.1
103     //================================================================================= splitToWords
104     vector<TString> splitToWords(TString rawString, TString newWord){
105     vector<TString> words;
106     int pos_beg = 0;
107     int pos_end = 0;
108     while(pos_beg < rawString.Sizeof()){
109     int i;
110     for(i=pos_beg; i<rawString.Sizeof(); ++i){
111     if(rawString(i,1) == newWord) break;
112     }
113     pos_end = i;
114     words.push_back(rawString(pos_beg, pos_end-pos_beg));
115     pos_beg = pos_end+1;
116     }
117     return words;
118     }
119    
120    
121     //================================================================================= splitToWords
122     map<TString, TString> splitToWords(TString rawString, TString newWord, TString newValue){
123     map<TString, TString> wordsAndValues;
124     vector<TString> words;
125     int pos_beg = 0;
126     int pos_end = 0;
127     while(pos_beg < rawString.Sizeof()){
128     int i;
129     for(i=pos_beg; i<rawString.Sizeof(); ++i){
130     if(rawString(i,1) == newWord) break;
131     }
132     pos_end = i;
133     words.push_back(rawString(pos_beg, pos_end-pos_beg));
134     pos_beg = pos_end+1;
135     vector<TString> temp_words = splitToWords(words.back(), newValue);
136     if(temp_words.size()==2)
137     wordsAndValues[temp_words[0]] = temp_words[1];
138     else if(words[0]!=""){
139     cout<<"ERROR: Something is wrong with the String: " << rawString << " here: " << words.back() << endl;
140     }
141     }
142     return wordsAndValues;
143     }
144    
145    
146     //================================================================================= GetHistFromFile
147     //Usage: TH1D* h = myplots.GetHistFromFile<TH1D*>("evaluation.root", "Multiplicity");
148     template <typename T>
149     T* GetHistFromFile(TString fileName, TString histName){
150     T *hist = new T();
151    
152 mstein 1.2 // TFile f(fileName);
153     // if (f.IsZombie()) {
154     // cout << "Error opening file" << endl;
155     // exit(-1);
156     // }
157 mstein 1.1 TFile *file = new TFile(fileName,"READ");
158     if(file->FindObjectAny(histName)) hist = (T*) file->FindObjectAny(histName)->Clone();
159     else cout<<"Histogram does not exist! Creating empty histogram: "<< histName << endl;
160     //file->Close();
161     //delete file;
162     return hist;
163     }
164    
165 mstein 1.2
166 mstein 1.1 //================================================================================= GetHistFromFile
167     template <typename T>
168     T* GetHistFromFile(TFile *file, TString histName){
169     file->cd();
170     T *hist = new T();
171     if(file->FindObjectAny(histName)) hist = (T*) file->FindObjectAny(histName);
172     else cout<<"Histogram does not exist! Creating empty histogram: "<< histName << endl;
173     //file->Close();
174     return hist;
175     }
176    
177    
178 mstein 1.2 //================================================================================= createRatioPlot
179 mstein 1.1 template <typename T>
180 mstein 1.2 T* createRatioPlot(T* h1, T* h2, TString name="", TString yTitle=""){
181     T* ratioPlot = (T*)h1->Clone();
182     ratioPlot->Divide(h2);
183 mstein 1.1 if(name !=""){
184     ratioPlot->SetName(name);
185     ratioPlot->SetTitle(name);
186     }
187     if(yTitle!=""){
188     ratioPlot->GetYaxis()->SetTitle(yTitle);
189     }
190     return ratioPlot;
191     }
192    
193    
194    
195    
196     }
197    
198    
199    
200    
201    
202     #endif
203    
204    
205    
206    
207    
208    
209    
210    
211    
212    
213    
214    
215    
216    
217    
218    
219