ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/tschum/FWlite_Analysis/PlotTool.cc
(Generate patch)

Comparing UserCode/tschum/FWlite_Analysis/PlotTool.cc (file contents):
Revision 1.5 by thomsen, Fri Nov 20 10:52:48 2009 UTC vs.
Revision 1.6 by gebbert, Wed Nov 25 15:34:38 2009 UTC

# Line 1 | Line 1
1   #include "PlotTool.h"
2   //------------------------------------------------------------------
3   //Constructur: Set some global Variables to define canvas output
4 < PlotTool::PlotTool()
5 < {
4 > PlotTool::PlotTool() {
5  
6 <  this->SetClass("TChain",100);
6 >        this->SetClass("TChain", 100);
7  
8 <  samePad_trees = true;
9 <  samePad_vars  = false;
10 <  samePad_cuts  = false;
11 <  sameCanv_trees = false;
12 <  sameCanv_vars  = false;
13 <  sameCanv_cuts  = false;
15 <
16 <  showLegend = false;
17 <  logY = true;
8 >        samePad_trees = true;
9 >        samePad_vars = false;
10 >        samePad_cuts = false;
11 >        sameCanv_trees = false;
12 >        sameCanv_vars = false;
13 >        sameCanv_cuts = false;
14  
15 +        showLegend = false;
16 +        logY = true;
17  
18   }
19  
20   //------------------------------------------------------------------
21   //Fill object PlotTool with Chains constructed from files from given source
22 < int PlotTool::init(string fileName, string dirPath, string treeName)
23 < {
24 <  this->New( this->GetEntries() );
25 <  int currChain = this->GetEntries() - 1;
26 <
27 <  if(currChain < 0 ) return currChain;
28 <
29 <  //make file for tree friends (adding additional, computed branches)
30 <  string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
31 <  friendFileName += fileName;
32 <  friendFileName += ".root";
33 <  TFile *f = new TFile(friendFileName.c_str(),"recreate");
34 <  string friendTreeName("friendTree");
35 <  //char number = currChain;
36 <  //friendTreeName += number;
37 <  //TTree *friendTree = new TTree("friendTree","friendTree");
38 <  TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
39 <
40 <  fileNames.clear();
41 <
42 <
43 <
44 <  ((TChain*) this->At(currChain))->SetName(fileName.c_str());
45 <  TSystemDirectory dir("sourceDir",dirPath.c_str());
46 <  TList *files = dir.GetListOfFiles();
47 <  if (files) {
48 <    TIter next(files);
49 <    TSystemFile *file;
50 <    TString fname;
51 <    string filePath;
52 <
53 <    cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "<<fileName.c_str()<<endl;
54 <
55 <    while ((file=(TSystemFile*)next())) {
56 <      fname = file->GetName();
57 <      if (!fname.EndsWith(".root")) continue;
58 <      if (!fname.Contains(fileName.c_str())) continue;
59 <
60 <      filePath = dirPath;
61 <      filePath += fname.Data();
62 <      //fwlite::ChainEvent to lop over events, jets, etc
63 <      fileNames.push_back(filePath.c_str());
64 <
65 <      if( ((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1, treeName.c_str()) ) cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
66 <      else return -1;
67 <
68 <    }
69 <  } else return -1;
70 <
71 <  for(int i=0;i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries();++i){
72 <
73 <    string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
74 <    size_t a =  s.find("_");
75 <    if(a == string::npos) a = 0;
76 <    size_t b =  s.find("_", a+1);
77 <    if(b == string::npos) b = 50;
78 <    string branch_alias = s.substr(a+1,b-a-1);
79 <    string branch_name = s;
80 <    if(s.find(".",s.size()-1) != string::npos ) branch_name += "obj";
81 <    ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),branch_name.c_str());
82 <
83 <  }
84 <  // add branch with track  Jets
85 <  
86 <  int nJetsKT;
87 <  TrackJetKT = new float [100];
88 <  fwlite::ChainEvent ev(fileNames);
89 <
90 <  friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
91 <  friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
92 <
93 <  cout<<"calculating additional variables..."<<endl;
94 <  for( ev.toBegin();! ev.atEnd();++ev) {
95 <    
96 <    fwlite::Handle<reco::CaloJetCollection> jets;
97 <    jets.getByLabel(ev,"antikt5CaloJets");
98 <
101 <    fwlite::Handle<reco::TrackCollection> tracks;
102 <    tracks.getByLabel(ev,"generalTracks");
103 <
104 <    if(!jets.isValid()) continue;
105 <    if(!tracks.isValid()) continue;
106 <    double trackSum;
107 <    nJetsKT = 0;
108 <    for(reco::CaloJetCollection::const_iterator jet = jets->begin(); jet!=jets->end();jet++)
109 <      {
110 <        trackSum = 0;
111 <        for(reco::TrackCollection::const_iterator track = tracks->begin(); track!=tracks->end();track++)
112 <          {
113 <            if( deltaR(jet->eta(),jet->phi(),track->eta(),track->phi()) < 0.5) trackSum += track->pt();
114 <          }
115 <        TrackJetKT[nJetsKT] = trackSum;
116 <        nJetsKT++;
117 <      }
118 <    friendTree->Fill();
119 <  }
120 <  f->cd();
121 <  friendTree->Write();
122 <  f->Close();
123 <  ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),friendFileName.c_str());
22 > int PlotTool::init(string fileName, string dirPath, string treeName,
23 >                string fileLabel) {
24 >        this->New(this->GetEntries() );
25 >        int currChain = this->GetEntries() - 1;
26 >
27 >        if (currChain < 0)
28 >                return currChain;
29 >        //make file for tree friends (adding additional, computed branches)
30 >        string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
31 >        friendFileName += fileName;
32 >        friendFileName += ".root";
33 >        TFile *f = new TFile(friendFileName.c_str(),"recreate");
34 >        string friendTreeName("friendTree");
35 >        //char number = currChain;
36 >        //friendTreeName += number;
37 >        //TTree *friendTree = new TTree("friendTree","friendTree");
38 >        TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
39 >
40 >        fileNames.clear();
41 >
42 >        //check if alternative label is different from default(searchString)
43 >        if (fileLabel=="") {
44 >                fileLabel=fileName;
45 >        }
46 >        ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
47 >        TSystemDirectory dir("sourceDir", dirPath.c_str());
48 >        TList *files = dir.GetListOfFiles();
49 >        if (files) {
50 >                TIter next(files);
51 >                TSystemFile *file;
52 >                TString fname;
53 >                string filePath;
54 >
55 >                cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
56 >                                <<fileName.c_str()<<endl;
57 >
58 >                while ((file=(TSystemFile*)next())) {
59 >                        fname = file->GetName();
60 >                        if (!fname.EndsWith(".root"))
61 >                                continue;
62 >                        if (!fname.Contains(fileName.c_str()))
63 >                                continue;
64 >
65 >                        filePath = dirPath;
66 >                        filePath += fname.Data();
67 >                        //fwlite::ChainEvent to lop over events, jets, etc
68 >                        fileNames.push_back(filePath.c_str());
69 >
70 >                        if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
71 >                                        treeName.c_str()) )
72 >                                cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
73 >                        else
74 >                                return -1;
75 >
76 >                }
77 >        } else
78 >                return -1;
79 >
80 >        for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
81 >
82 >                string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
83 >                size_t a = s.find("_");
84 >                if (a == string::npos)
85 >                        a = 0;
86 >                size_t b = s.find("_", a+1);
87 >                if (b == string::npos)
88 >                        b = 50;
89 >                string branch_alias = s.substr(a+1, b-a-1);
90 >                string branch_name = s;
91 >                if (s.find(".", s.size()-1) != string::npos)
92 >                        branch_name += "obj";
93 >                ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
94 >                                branch_name.c_str());
95 >
96 >        }
97 >
98 >        // add branch with track  Jets
99  
100 <  return this->GetEntries();
100 >        int nJetsKT;
101 >        TrackJetKT = new float [100];
102 >        fwlite::ChainEvent ev(fileNames);
103 >
104 >        friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
105 >        friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
106 >
107 >        cout<<"calculating additional variables..."<<endl;
108 >        for (ev.toBegin(); !ev.atEnd(); ++ev) {
109 >
110 >                fwlite::Handle<reco::CaloJetCollection> jets;
111 >                jets.getByLabel(ev, "antikt5CaloJets");
112 >
113 >                fwlite::Handle<reco::TrackCollection> tracks;
114 >                tracks.getByLabel(ev, "generalTracks");
115 >
116 >                if (!jets.isValid())
117 >                        continue;
118 >                if (!tracks.isValid())
119 >                        continue;
120 >                double trackSum;
121 >                nJetsKT = 0;
122 >                for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
123 >                                !=jets->end(); jet++) {
124 >                        trackSum = 0;
125 >                        for (reco::TrackCollection::const_iterator track = tracks->begin(); track
126 >                                        !=tracks->end(); track++) {
127 >                                if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
128 >                                                < 0.5)
129 >                                        trackSum += track->pt();
130 >                        }
131 >                        TrackJetKT[nJetsKT] = trackSum;
132 >                        nJetsKT++;
133 >                }
134 >                friendTree->Fill();
135 >        }
136 >        f->cd();
137 >        friendTree->Write();
138 >        f->Close();
139 >        ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
140 >                        friendFileName.c_str());
141 >
142 >        return this->GetEntries();
143  
144   }
145   //------------------------------------------------------------------
146   //Draw given Chain with given variable and given cuts
147 < int  PlotTool::plot(int chainIndex, string histName, string cutName, int nEntries, string drwOpt)
148 < {
147 > int PlotTool::plot(int chainIndex, string histName, string cutName,
148 >                int nEntries, string drwOpt) {
149  
150 <  if( chainIndex >= this->GetEntries() ) return -1;
150 >        if (chainIndex >= this->GetEntries() )
151 >                return -1;
152  
153 <  int currN = nEntries;
154 <  if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
153 >        int currN = nEntries;
154 >        if (nEntries < 0)
155 >                currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
156 >
157 >        //++++ Create and name Canvases according to global variables +++++++++++++
158 >        ostringstream currHistName;
159 >        if (samePad_trees)
160 >                currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
161 >        if (samePad_vars)
162 >                currHistName<<histName;
163 >        if (samePad_cuts)
164 >                currHistName<<"{"<<cutName<<"}";
165 >
166 >        ostringstream currPadName;
167 >        if (!samePad_trees)
168 >                currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
169 >        if (!samePad_vars)
170 >                currPadName<<histName;
171 >        if (!samePad_cuts)
172 >                currPadName<<"{"<<cutName<<"}";
173 >
174 >        ostringstream currCanvName;
175 >        if (!sameCanv_trees && !samePad_trees)
176 >                currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
177 >        if (!sameCanv_vars && !samePad_vars)
178 >                currCanvName<<histName;
179 >        if (!sameCanv_cuts && !samePad_cuts)
180 >                currCanvName<<"{"<<cutName<<"}";
181 >
182 >        if ( (sameCanv_trees || samePad_trees) && (sameCanv_vars || samePad_vars)
183 >                        && (sameCanv_cuts || samePad_cuts))
184 >                currCanvName<<"All";
185 >
186 >        string currOpt = drwOpt;
187 >        bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars
188 >                        && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
189 >
190 >        if (useSubPads) {
191 >                if ( !canvases_[currCanvName.str()] || !gROOT->FindObject(currCanvName.str().c_str()) ) {
192 >                        canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
193 >                } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
194 >                        canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
195 >                }
196 >        }
197  
198 <  //++++ Create and name Canvases according to global variables +++++++++++++
199 <  ostringstream currHistName;
200 <  if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
201 <  if( samePad_vars)  currHistName<<histName;
202 <  if( samePad_cuts)  currHistName<<"{"<<cutName<<"}";
203 <
204 <   ostringstream currPadName;
205 <  if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
206 <  if(! samePad_vars)  currPadName<<histName;
207 <  if(! samePad_cuts)  currPadName<<"{"<<cutName<<"}";
208 <
149 <  ostringstream currCanvName;
150 <  if(! sameCanv_trees && ! samePad_trees ) currCanvName<<((TChain*) this->At(chainIndex))->GetName()<<":";
151 <  if(! sameCanv_vars  && ! samePad_vars)  currCanvName<<histName;
152 <  if(! sameCanv_cuts  && ! samePad_cuts)  currCanvName<<"{"<<cutName<<"}";
153 <
154 <  if( (sameCanv_trees || samePad_trees) && (sameCanv_vars  || samePad_vars) && (sameCanv_cuts  || samePad_cuts) )  currCanvName<<"All";
155 <
156 <    
157 <  string currOpt = drwOpt;
158 <  bool useSubPads = (sameCanv_trees && !samePad_trees) || (sameCanv_vars  && !samePad_vars) || (sameCanv_cuts  && !samePad_cuts);
159 <
160 <  if( useSubPads ) {
161 <    if( ! canvases_[currCanvName.str()] || ! gROOT->FindObject(currCanvName.str().c_str()) ) {
162 <      canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
163 <    } else if ( canvases_[currCanvName.str()]->GetNumber() >= 0 ) {
164 <      canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
165 <    }
166 <  }
167 <  
168 <  if( ! pads_[currPadName.str()] || ! gROOT->FindObject(currPadName.str().c_str()) ) {
169 <    pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
170 <    if(logY) pads_[currPadName.str()]->SetLogy(1);
171 <  } else {
172 <    pads_[currPadName.str()]->cd();
173 <    currOpt += "SAMES";
174 <    if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
175 <  }
176 <  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
177 <
178 <
179 <  //Draw histogram:
180 <  int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(), currOpt.c_str(), currN);
181 <  if( draw_err < 0 ) return draw_err;
182 <
183 <  //++++ Fix for histos with no entries +++++++++++++
184 <  if( draw_err == 0 ) {
185 <    if( currOpt.find("SAMES") == string::npos ) {
186 <      pads_[currPadName.str()]->Close();
187 <      pads_.erase( currPadName.str() );
188 <      if(useSubPads) canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
189 <    }
190 <    cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()<<" has no entries and is not drawn!"<<endl;
191 <    return draw_err;
192 <  }
193 <  //++++++++++++++++++++++++++++++++++++++++++++++++++
198 >        if ( !pads_[currPadName.str()] || !gROOT->FindObject(currPadName.str().c_str()) ) {
199 >                pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
200 >                if (logY)
201 >                        pads_[currPadName.str()]->SetLogy(1);
202 >        } else {
203 >                pads_[currPadName.str()]->cd();
204 >                currOpt += "SAMES";
205 >                if (useSubPads)
206 >                        canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
207 >        }
208 >        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
209  
195  ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
210  
211 +        //Draw histogram:
212 +        int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(),
213 +                        currOpt.c_str(), currN);
214 +        if (draw_err < 0)
215 +                return draw_err;
216 +
217 +        //++++ Fix for histos with no entries +++++++++++++
218 +        if (draw_err == 0) {
219 +                if (currOpt.find("SAMES") == string::npos) {
220 +                        pads_[currPadName.str()]->Close();
221 +                        pads_.erase(currPadName.str() );
222 +                        if (useSubPads)
223 +                                canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()-1);
224 +                }
225 +                cout<< "Warning: "<<currHistName.str().c_str()<<" in "<<currPadName.str()
226 +                                <<" has no entries and is not drawn!"<<endl;
227 +                return draw_err;
228 +        }
229 +        //++++++++++++++++++++++++++++++++++++++++++++++++++
230  
231 <  return draw_err;
231 >        ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
232 >
233 >
234 >        return draw_err;
235  
236   }
237   //------------------------------------------------------------------
238   //Standard loop to draw all chains and multiple variables and cuts
239 < int  PlotTool::loop(vector<string> _histName, vector<string> _cutName, int nEntries, string drwOpt, bool correspond)
240 < {
205 <
239 > int PlotTool::loop(vector<string> _histName, vector<string> _cutName,
240 >                int nEntries, string drwOpt, bool correspond) {
241  
242 +        int numProcessed = 0;
243  
244 <  int numProcessed = 0;
244 >        if (correspond == false) {
245  
246 <  if( correspond == false ) {
246 >                for (int i=0; i<this->GetEntries(); ++i) {
247 >                        for (vector<string>::iterator it1 =_histName.begin(); it1
248 >                                        != _histName.end(); ++it1) {
249 >                                for (vector<string>::iterator it2 =_cutName.begin(); it2
250 >                                                != _cutName.end(); ++it2) {
251  
252 <    for(int i=0; i<this->GetEntries(); ++i) {
253 <      for(vector<string>::iterator it1 =_histName.begin(); it1 != _histName.end(); ++it1) {
214 <        for(vector<string>::iterator it2 =_cutName.begin(); it2 != _cutName.end(); ++it2) {
215 <
216 <          numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
217 <          updatePads();
218 <        
219 <        }
220 <      }
221 <    }
222 <  } else {
252 >                                        numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
253 >                                        updatePads();
254  
255 <    if( _histName.size() != _cutName.size() ) return -1;
255 >                                }
256 >                        }
257 >                }
258 >        } else {
259  
260 <    for(int i=0; i<this->GetEntries(); ++i) {
261 <      for(vector<string>::iterator it1 =_histName.begin(),it2 =_cutName.begin(); it1 != _cutName.end()&&it2 != _cutName.end(); ++it1, ++it2) {
260 >                if (_histName.size() != _cutName.size() )
261 >                        return -1;
262  
263 <        numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
263 >                for (int i=0; i<this->GetEntries(); ++i) {
264 >                        for (vector<string>::iterator it1 =_histName.begin(), it2 =
265 >                                        _cutName.begin(); it1 != _cutName.end()&&it2
266 >                                        != _cutName.end(); ++it1, ++it2) {
267  
268 +                                numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
269  
270 <      }
271 <    }
272 <  }
270 >                        }
271 >                }
272 >        }
273  
274 <  //  updatePads();
275 <  fillCanvases();
274 >        //  updatePads();
275 >        fillCanvases();
276  
277 <  return numProcessed;
277 >        return numProcessed;
278   }
279  
280   //Helper methods to allow for using simple strings as input
281 < int  PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt)
244 < {
281 > int PlotTool::loop(string histName, string cutName, int nEntries, string drwOpt) {
282  
283 <  vector<string> _histName;
284 <  _histName.push_back( histName );
285 <  vector<string> _cutName;
286 <  _cutName.push_back( cutName );
283 >        vector<string> _histName;
284 >        _histName.push_back(histName);
285 >        vector<string> _cutName;
286 >        _cutName.push_back(cutName);
287  
288 <  return loop(_histName, _cutName, nEntries, drwOpt);
288 >        return loop(_histName, _cutName, nEntries, drwOpt);
289  
290   }
291  
292 < int  PlotTool::loop(vector<string> _histName, string cutName, int nEntries, string drwOpt)
293 < {
292 > int PlotTool::loop(vector<string> _histName, string cutName, int nEntries,
293 >                string drwOpt) {
294  
295 <  vector<string> _cutName;
296 <  _cutName.push_back( cutName );
295 >        vector<string> _cutName;
296 >        _cutName.push_back(cutName);
297  
298 <  return loop(_histName, _cutName, nEntries, drwOpt);
298 >        return loop(_histName, _cutName, nEntries, drwOpt);
299  
300   }
301  
302 < int  PlotTool::loop(string histName, vector<string> _cutName, int nEntries, string drwOpt)
303 < {
302 > int PlotTool::loop(string histName, vector<string> _cutName, int nEntries,
303 >                string drwOpt) {
304  
305 <  vector<string> _histName;
306 <  _histName.push_back( histName );
305 >        vector<string> _histName;
306 >        _histName.push_back(histName);
307  
308 <  return loop(_histName, _cutName, nEntries, drwOpt);
308 >        return loop(_histName, _cutName, nEntries, drwOpt);
309   }
310   //------------------------------------------------------------------
311   //redraw the canvas to make changes in style visible
312 < int  PlotTool::updatePads() {
312 > int PlotTool::updatePads() {
313  
314 <  for(map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it ) {
315 <    if( gROOT->FindObject( (*it).first.c_str() ) ) {
316 <      (*it).second->Draw();
317 <      setCanvas( (*it).second );
318 <    } else pads_.erase( it );  
319 <  }
314 >        for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
315 >                if (gROOT->FindObject((*it).first.c_str() ) ) {
316 >                        (*it).second->Draw();
317 >                        setCanvas((*it).second);
318 >                } else
319 >                        pads_.erase(it);
320 >        }
321  
322 <  return pads_.size();
322 >        return pads_.size();
323  
324   }
325  
326   int PlotTool::fillCanvases() {
327  
328 +        for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
329 +                        != canvases_.end(); ++it) {
330 +                const char* canvName = (*it).first.c_str();
331 +                if (gROOT->FindObject(canvName) ) {
332 +
333 +                        int numP = (*it).second->GetNumber()+1;
334 +
335 +                        if (numP <= 0)
336 +                                continue;
337 +
338 +                        int x = int( sqrt(numP) );
339 +                        int y = x;
340 +                        if (x*y < numP)
341 +                                x += 1;
342 +                        if (x*y < numP)
343 +                                x += 1;
344 +                        if (x*y < numP) {
345 +                                x -= 1;
346 +                                y += 1;
347 +                        }
348 +
349 +                        (*it).second->Divide(x, y);
350 +                        int padIndex = 1;
351 +                        for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
352 +                                        != pads_.end(); ++it2) {
353 +                                string padName = (*it2).first;
354 +                                if (gROOT->FindObject(padName.c_str() ) ) {
355 +                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
356 +                                                        canvName, "All") ) {
357 +                                                (*it).second->cd(padIndex);
358 +                                                (*it2).second->DrawClonePad();
359 +                                                (*it2).second->Close();
360 +                                                pads_.erase(it2);
361 +                                                ++padIndex;
362 +                                        }
363 +                                } else
364 +                                        pads_.erase(it2);
365 +                        }
366 +                        (*it).second->SetNumber(-1);
367  
368 <  for(map< string, TCanvas* >::iterator it=canvases_.begin() ; it != canvases_.end(); ++it ) {
369 <    const char* canvName = (*it).first.c_str();
370 <    if( gROOT->FindObject( canvName ) ) {
294 <
295 <      int numP = (*it).second->GetNumber()+1;
296 <
297 <      if( numP <= 0 ) continue;
298 <
299 <      int x = int( sqrt(numP) );
300 <      int y = x;
301 <      if( x*y     < numP )  x += 1;
302 <      if( x*y     < numP )  x += 1;
303 <      if( x*y     < numP ) {
304 <        x -= 1;
305 <        y += 1;
306 <      }
307 <
308 <      (*it).second->Divide(x,y);
309 <      int padIndex = 1;
310 <      for(map< string, TCanvas* >::iterator it2=pads_.begin() ; it2 != pads_.end(); ++it2 ) {
311 <        string padName = (*it2).first;
312 <        if( gROOT->FindObject( padName.c_str() ) ) {
313 <          if( ( padName.find(canvName) != string::npos ) || !strcmp(canvName,"All") ) {
314 <            (*it).second->cd(padIndex);
315 <            (*it2).second->DrawClonePad();
316 <            (*it2).second->Close();
317 <            pads_.erase( it2 );
318 <            ++padIndex;
319 <          }
320 <        } else pads_.erase( it2 );
321 <      }
322 <      (*it).second->SetNumber(-1);
323 <
324 <    } else canvases_.erase( it );      
325 <  }
368 >                } else
369 >                        canvases_.erase(it);
370 >        }
371  
372 <  return canvases_.size();
372 >        return canvases_.size();
373  
374   }
375   //------------------------------------------------------------------
376   void PlotTool::setCanvas(TCanvas* thisCanvas) {
377  
378 +        TH1* thisHist = 0;
379 +        TPaveStats* thisStatsBox = 0;
380 +        TPaletteAxis* palette =0;
381 +        TLegend* thisLeg = 0;
382 +        int counter =0;
383 +        double maxEntry=0;
384  
385 <  TH1* thisHist = 0;
386 <  TPaveStats* thisStatsBox = 0;
336 <  TPaletteAxis* palette =0;
337 <  TLegend* thisLeg = 0;
338 <  int counter =0;
339 <  double maxEntry=0;
385 >        ((TFrame*) thisCanvas->GetFrame())->Delete();
386 >        thisCanvas->GetFrame()->SetLineWidth(gStyle->GetLineWidth() );
387  
388 +        thisCanvas->SetLeftMargin(0.2);
389 +        thisCanvas->SetRightMargin(0.06);
390 +        thisCanvas->SetBottomMargin(0.2);
391 +        thisCanvas->SetTopMargin(0.1);
392  
393 <  ((TFrame*) thisCanvas->GetFrame())->Delete();
394 <  thisCanvas->GetFrame()->SetLineWidth( gStyle->GetLineWidth() );
393 >        if (logY)
394 >                thisCanvas->SetLogy(1);
395 >        else
396 >                thisCanvas->SetLogy(0);
397  
398 <  thisCanvas->SetLeftMargin(0.2);
399 <  thisCanvas->SetRightMargin(0.06);
347 <  thisCanvas->SetBottomMargin(0.2);
348 <  thisCanvas->SetTopMargin(0.1);
398 >        if (showLegend)
399 >                thisLeg = new TLegend();
400  
401 <  if(logY) thisCanvas->SetLogy(1);
351 <  else     thisCanvas->SetLogy(0);
401 >        for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
402  
403 <  if(showLegend)  thisLeg = new TLegend();
404 <
403 >                if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1"))
404 >                        continue;
405  
406 <  for(int i = 0; i !=  thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
406 >                thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
407 >                setColor(thisHist, counter);
408 >                setMathLabels(thisHist);
409  
410 +                if (thisHist->GetMaximum() > maxEntry)
411 +                        maxEntry = thisHist->GetMaximum();
412  
413 <    if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1")) continue;
413 >                thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
414 >                if (thisStatsBox)
415 >                        setStats(thisCanvas, thisStatsBox, thisHist, counter);
416  
417 <      thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
418 <      setColor(thisHist, counter);
419 <      setMathLabels(thisHist);
417 >                palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
418 >                if (palette)
419 >                        setPalette(thisCanvas, palette);
420  
421 <      if(thisHist->GetMaximum() > maxEntry) maxEntry = thisHist->GetMaximum();
421 >                if (thisLeg)
422 >                        thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
423  
424 <      thisStatsBox = (TPaveStats*) thisHist->GetListOfFunctions()->FindObject("stats");
368 <      if(thisStatsBox) setStats(thisCanvas, thisStatsBox, thisHist, counter);
369 <
370 <      palette = (TPaletteAxis*) thisHist->GetListOfFunctions()->FindObject("palette");
371 <      if(palette) setPalette(thisCanvas, palette);
372 <
373 <      if(thisLeg) thisLeg->AddEntry(thisHist,thisHist->GetName())->SetTextSize(0.04);
424 >                ++counter;
425  
426 +        }
427  
428 <      ++counter;
429 <  
378 <  }
428 >        if (maxEntry != 0)
429 >                setHistMax(thisCanvas, maxEntry);
430  
431 <  if( maxEntry != 0) setHistMax(thisCanvas, maxEntry);
432 <
431 >        thisCanvas->cd();
432 >        thisCanvas->Update();
433  
434 <  thisCanvas->cd();
435 <  thisCanvas->Update();
434 >        if (thisLeg)
435 >                setLegend(thisCanvas, thisLeg, counter);
436  
386  if(thisLeg) setLegend( thisCanvas, thisLeg, counter);
387  
388        
437   }
438   //------------------------------------------------------------------
439   //private helper classes to set the canvas and hist style
440 < void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox, TH1* thisHist, int counter) {
441 <
394 <
395 <  if(thisCanvas->GetRightMargin() < .2) thisCanvas->SetRightMargin(.2);
396 <
397 <  thisStatsBox->SetLineColor(thisHist->GetLineColor());
398 <  thisStatsBox->SetX2NDC(1.);
399 <  thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
400 <  thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
401 <  thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
440 > void PlotTool::setStats(TCanvas* thisCanvas, TPaveStats* thisStatsBox,
441 >                TH1* thisHist, int counter) {
442  
443 +        if (thisCanvas->GetRightMargin() < .2)
444 +                thisCanvas->SetRightMargin(.2);
445  
446 +        thisStatsBox->SetLineColor(thisHist->GetLineColor());
447 +        thisStatsBox->SetX2NDC(1.);
448 +        thisStatsBox->SetY2NDC(1-thisCanvas->GetTopMargin()-0.16*counter);
449 +        thisStatsBox->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
450 +        thisStatsBox->SetY1NDC(thisStatsBox->GetY2NDC()-.15);
451  
452   }
453  
454   void PlotTool::setMathLabels(TH1* thisHist) {
455  
456 <
457 <  string t = thisHist            ->GetTitle();
458 <  string x = thisHist->GetXaxis()->GetTitle();
459 <  string y = thisHist->GetYaxis()->GetTitle();
460 <
461 <  if(t.find(".phi()") != string::npos ) t.replace(t.find(".phi()"),6," #phi");
462 <  if(x.find(".phi()") != string::npos ) x.replace(x.find(".phi()"),6," #phi");
463 <  if(y.find(".phi()") != string::npos ) y.replace(y.find(".phi()"),6," #phi");
464 <
465 <  if(t.find(".eta()") != string::npos ) t.replace(t.find(".eta()"),6," #eta");
466 <  if(x.find(".eta()") != string::npos ) x.replace(x.find(".eta()"),6," #eta");
467 <  if(y.find(".eta()") != string::npos ) y.replace(y.find(".eta()"),6," #eta");
468 <
469 <  if(t.find(".pt()") != string::npos ) t.replace(t.find(".pt()"),5," p_{T}");
470 <  if(x.find(".pt()") != string::npos ) x.replace(x.find(".pt()"),5," p_{T}");
471 <  if(y.find(".pt()") != string::npos ) y.replace(y.find(".pt()"),5," p_{T}");
472 <
473 <  thisHist            ->SetTitle(t.c_str());
474 <  thisHist->GetXaxis()->SetTitle(x.c_str());
475 <  thisHist->GetYaxis()->SetTitle(y.c_str());
476 <
456 >        string t = thisHist ->GetTitle();
457 >        string x = thisHist->GetXaxis()->GetTitle();
458 >        string y = thisHist->GetYaxis()->GetTitle();
459 >
460 >        if (t.find(".phi()") != string::npos)
461 >                t.replace(t.find(".phi()"), 6, " #phi");
462 >        if (x.find(".phi()") != string::npos)
463 >                x.replace(x.find(".phi()"), 6, " #phi");
464 >        if (y.find(".phi()") != string::npos)
465 >                y.replace(y.find(".phi()"), 6, " #phi");
466 >
467 >        if (t.find(".eta()") != string::npos)
468 >                t.replace(t.find(".eta()"), 6, " #eta");
469 >        if (x.find(".eta()") != string::npos)
470 >                x.replace(x.find(".eta()"), 6, " #eta");
471 >        if (y.find(".eta()") != string::npos)
472 >                y.replace(y.find(".eta()"), 6, " #eta");
473 >
474 >        if (t.find(".pt()") != string::npos)
475 >                t.replace(t.find(".pt()"), 5, " p_{T}");
476 >        if (x.find(".pt()") != string::npos)
477 >                x.replace(x.find(".pt()"), 5, " p_{T}");
478 >        if (y.find(".pt()") != string::npos)
479 >                y.replace(y.find(".pt()"), 5, " p_{T}");
480 >
481 >        thisHist ->SetTitle(t.c_str());
482 >        thisHist->GetXaxis()->SetTitle(x.c_str());
483 >        thisHist->GetYaxis()->SetTitle(y.c_str());
484  
485   }
486  
433
487   void PlotTool::setColor(TH1* thisHist, int counter) {
488  
489 +        if (counter == 0) {
490  
491 <      if(counter == 0) {
492 <
493 <        thisHist->SetLineColor  (kRed+1);
494 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kRed+1);
495 <        //else
496 <        thisHist->SetFillStyle(0);
497 <        thisHist->SetMarkerColor(kRed+1);
498 <      } else if(counter == 1) {
499 <        thisHist->SetLineColor  (kBlue+1);
500 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kBlue+1);
501 <        //else
502 <        thisHist->SetFillStyle(0);
503 <        thisHist->SetMarkerColor(kBlue+1);
504 <
505 <      } else if(counter == 2) {
506 <        thisHist->SetLineColor  (kGreen+2);
507 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kGreen+2);
508 <        //else
509 <        thisHist->SetFillStyle(0);
510 <        thisHist->SetMarkerColor(kGreen+2);
511 <
512 <      } else if(counter == 3) {
513 <        thisHist->SetLineColor  (kMagenta+2);
514 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kMagenta+2);
515 <        //else
516 <        thisHist->SetFillStyle(0);
517 <        thisHist->SetMarkerColor(kMagenta+2);
518 <
519 <      } else if(counter == 4) {
520 <        thisHist->SetLineColor  (kCyan+2);
521 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kCyan+2);
522 <        //else
523 <        thisHist->SetFillStyle(0);
524 <        thisHist->SetMarkerColor(kCyan+2);
525 <
526 <      } else {
527 <        thisHist->SetLineColor  (kBlack);
528 <        //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kBlack);
529 <        //else
476 <        thisHist->SetFillStyle(0);
477 <        thisHist->SetMarkerColor(kBlack);
491 >                thisHist->SetLineColor(kRed+1);
492 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kRed+1);
493 >                //else
494 >                thisHist->SetFillStyle(0);
495 >                thisHist->SetMarkerColor(kRed+1);
496 >        } else if (counter == 1) {
497 >                thisHist->SetLineColor(kBlue+1);
498 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kBlue+1);
499 >                //else
500 >                thisHist->SetFillStyle(0);
501 >                thisHist->SetMarkerColor(kBlue+1);
502 >
503 >        } else if (counter == 2) {
504 >                thisHist->SetLineColor(kGreen+2);
505 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kGreen+2);
506 >                //else
507 >                thisHist->SetFillStyle(0);
508 >                thisHist->SetMarkerColor(kGreen+2);
509 >
510 >        } else if (counter == 3) {
511 >                thisHist->SetLineColor(kMagenta+2);
512 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kMagenta+2);
513 >                //else
514 >                thisHist->SetFillStyle(0);
515 >                thisHist->SetMarkerColor(kMagenta+2);
516 >
517 >        } else if (counter == 4) {
518 >                thisHist->SetLineColor(kCyan+2);
519 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kCyan+2);
520 >                //else
521 >                thisHist->SetFillStyle(0);
522 >                thisHist->SetMarkerColor(kCyan+2);
523 >
524 >        } else {
525 >                thisHist->SetLineColor(kBlack);
526 >                //if( gStyle->GetHistFillStyle() ) thisHist->SetFillColor  (kBlack);
527 >                //else
528 >                thisHist->SetFillStyle(0);
529 >                thisHist->SetMarkerColor(kBlack);
530  
531 <      }
531 >        }
532  
533   }
534  
535   void PlotTool::setPalette(TCanvas* thisCanvas, TPaletteAxis* palette) {
536  
537 <
538 <  palette->SetLabelSize(0.045);
539 <  if(thisCanvas->GetRightMargin() < .15) thisCanvas->SetRightMargin(.15);
540 <  palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
541 <  palette->SetY1NDC(thisCanvas->GetBottomMargin());
542 <  palette->SetX2NDC(palette->GetX1NDC()+0.05);
543 <  palette->SetY2NDC(1-thisCanvas->GetTopMargin());
492 <
537 >        palette->SetLabelSize(0.045);
538 >        if (thisCanvas->GetRightMargin() < .15)
539 >                thisCanvas->SetRightMargin(.15);
540 >        palette->SetX1NDC(1-thisCanvas->GetRightMargin()+0.01);
541 >        palette->SetY1NDC(thisCanvas->GetBottomMargin());
542 >        palette->SetX2NDC(palette->GetX1NDC()+0.05);
543 >        palette->SetY2NDC(1-thisCanvas->GetTopMargin());
544  
545   }
546  
547   void PlotTool::setHistMax(TCanvas* thisCanvas, double maxEntry) {
548  
549 <  TH1* thisHist = 0;
549 >        TH1* thisHist = 0;
550  
551 <  for(int i = 0; i !=  thisCanvas->GetListOfPrimitives()->GetSize(); ++i){
551 >        for (int i = 0; i != thisCanvas->GetListOfPrimitives()->GetSize(); ++i) {
552  
553 <    if( ! thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") ) continue;
554 <    thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
555 <    if( thisHist->GetMaximum() < maxEntry ) thisHist->GetYaxis()->SetRangeUser(thisHist->GetMinimum(),maxEntry*1.2);
556 <    break;
557 <
558 <  }
553 >                if ( !thisCanvas->GetListOfPrimitives()->At(i)->InheritsFrom("TH1") )
554 >                        continue;
555 >                thisHist = ((TH1*) thisCanvas->GetListOfPrimitives()->At(i));
556 >                if (thisHist->GetMaximum() < maxEntry) {
557 >                        double minEntry=thisHist->GetBinContent(thisHist->GetMinimumBin());
558 >                        if (logY) {
559 >                                int bin = (thisHist->GetMinimumBin()+1);
560 >                                minEntry=thisHist->GetBinContent(bin);
561 >                        }
562 >                        thisHist->GetYaxis()->SetRangeUser(minEntry, maxEntry*1.2);
563 >                }
564 >                break;
565  
566 +        }
567  
568   }
569  
570   void PlotTool::setLegend(TCanvas* thisCanvas, TLegend* thisLeg, int counter) {
571  
572 <  thisLeg->SetFillStyle(0);
573 <  thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
574 <  thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
575 <  thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
576 <  thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
577 <  thisLeg->SetEntrySeparation(0.5);
578 <  thisLeg->Draw("NDC");
572 >        thisLeg->SetFillStyle(0);
573 >        thisLeg->SetX1NDC(1-thisCanvas->GetRightMargin()-0.5);
574 >        thisLeg->SetY1NDC(1-thisCanvas->GetTopMargin()-0.05*counter);
575 >        thisLeg->SetX2NDC(1-thisCanvas->GetRightMargin());
576 >        thisLeg->SetY2NDC(1-thisCanvas->GetTopMargin());
577 >        thisLeg->SetEntrySeparation(0.5);
578 >        thisLeg->Draw("NDC");
579  
580   }
581  
582 < void PlotTool::createColors()
583 < {
584 <    const Int_t NRGBs = 5;
585 <    const Int_t NCont = 255;
586 <
587 <    Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
588 <    Double_t red[NRGBs]   = { 0.00, 0.00, 0.87, 1.00, 0.51 };
589 <    Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
590 <    Double_t blue[NRGBs]  = { 0.51, 1.00, 0.12, 0.00, 0.00 };
591 <    TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
534 <    gStyle->SetNumberContours(NCont);
582 > void PlotTool::createColors() {
583 >        const Int_t NRGBs = 5;
584 >        const Int_t NCont = 255;
585 >
586 >        Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
587 >        Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
588 >        Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
589 >        Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
590 >        TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
591 >        gStyle->SetNumberContours(NCont);
592  
593   }
594  
538
595   //------------------------------------------------------------------
596  
597   int PlotTool::saveCanvases(string type, string path) {
598  
599 +        TSystemDirectory d("", path.c_str());
600 +        if (!d.GetListOfFiles())
601 +                return -1;
602 +        if (type.find(".") == string::npos)
603 +                return -1;
604 +
605 +        int savedFiles =0;
606 +
607 +        TIter next(gROOT->GetListOfCanvases());
608 +        TCanvas* canv;
609 +        while ((canv=(TCanvas*)next())) {
610 +
611 +                string s = "";
612 +                s += path;
613 +                s += canv->GetName();
614 +                s += type;
615 +
616 +                canv->SaveAs(s.c_str());
617 +                ++savedFiles;
618 +
619 +        }
620  
621 <  TSystemDirectory d("",path.c_str());
545 <  if(!d.GetListOfFiles()) return -1;
546 <  if(type.find(".") == string::npos) return -1;
547 <
548 <  int savedFiles =0;
549 <
550 <  TIter next(gROOT->GetListOfCanvases());
551 <  TCanvas* canv;
552 <  while ((canv=(TCanvas*)next())) {
553 <    
554 <    string s = "";
555 <    s += path;
556 <    s += canv->GetName();
557 <    s += type;
558 <
559 <    canv->SaveAs(s.c_str());
560 <    ++savedFiles;
561 <
562 <  }
563 <
564 <  return savedFiles;  
621 >        return savedFiles;
622  
623   }
624   //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines