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.6 by gebbert, Wed Nov 25 15:34:38 2009 UTC vs.
Revision 1.13 by tschum, Wed Dec 9 13:02:16 2009 UTC

# Line 14 | Line 14 | PlotTool::PlotTool() {
14  
15          showLegend = false;
16          logY = true;
17 +        addTrackJets = false;
18 +        addEventInfo = false;
19 +        verbose = true;
20 +
21 +        globalCuts="";
22  
23   }
24  
25   //------------------------------------------------------------------
26   //Fill object PlotTool with Chains constructed from files from given source
27 +
28   int PlotTool::init(string fileName, string dirPath, string treeName,
29 <                string fileLabel) {
30 <        this->New(this->GetEntries() );
31 <        int currChain = this->GetEntries() - 1;
32 <
33 <        if (currChain < 0)
34 <                return currChain;
35 <        //make file for tree friends (adding additional, computed branches)
36 <        string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
37 <        friendFileName += fileName;
38 <        friendFileName += ".root";
39 <        TFile *f = new TFile(friendFileName.c_str(),"recreate");
40 <        string friendTreeName("friendTree");
41 <        //char number = currChain;
42 <        //friendTreeName += number;
43 <        //TTree *friendTree = new TTree("friendTree","friendTree");
44 <        TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
45 <
46 <        fileNames.clear();
47 <
48 <        //check if alternative label is different from default(searchString)
49 <        if (fileLabel=="") {
50 <                fileLabel=fileName;
51 <        }
52 <        ((TChain*) this->At(currChain))->SetName(fileLabel.c_str());
53 <        TSystemDirectory dir("sourceDir", dirPath.c_str());
54 <        TList *files = dir.GetListOfFiles();
55 <        if (files) {
56 <                TIter next(files);
57 <                TSystemFile *file;
58 <                TString fname;
59 <                string filePath;
60 <
61 <                cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
62 <                                <<fileName.c_str()<<endl;
63 <
64 <                while ((file=(TSystemFile*)next())) {
65 <                        fname = file->GetName();
66 <                        if (!fname.EndsWith(".root"))
67 <                                continue;
68 <                        if (!fname.Contains(fileName.c_str()))
69 <                                continue;
29 >                   string fileLabel) {
30 >  this->New(this->GetEntries() );
31 >  int currChain = this->GetEntries() - 1;
32 >
33 >  if (currChain < 0)
34 >    return currChain;
35 >
36 >  TStopwatch timer;
37 >  timer.Start();
38 >
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 >
48 >  TList *files = new TList();
49 >  TSystemFile* sysFile = 0;
50 >  if(fileName.find(".") != string::npos) {
51 >    ifstream f(fileName.c_str());
52 >    if( ! f.is_open() ) return -1;
53 >    string line;
54 >
55 >    while (!f.eof()) {
56 >      getline(f,line);
57 >      sysFile = new TSystemFile(line.c_str(),dirPath.c_str());
58 >      files->Add(sysFile);
59 >    }
60 >
61 >
62 >  } else {
63 >    TSystemDirectory dir("sourceDir", dirPath.c_str());
64 >    files = dir.GetListOfFiles();
65 >  }
66 >  if (files->GetEntries()>0) {
67 >    TIter next(files);
68 >    TSystemFile *file;
69 >    TString fname;
70 >    string filePath;
71 >
72 >    if(verbose && fileName.find(".") == string::npos )  cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
73 >                                                            <<fileName.c_str()<<endl;
74 >    if(verbose && fileName.find(".") != string::npos )  cout<<"Open"<<dirPath.c_str()<<" Search lines with .root in: "
75 >                                                            <<fileName.c_str()<<endl;
76 >
77 >
78 >    while ((file=(TSystemFile*)next())) {
79 >      fname = file->GetName();
80 >      if (!fname.EndsWith(".root"))
81 >        continue;
82 >      if (!fname.Contains(fileName.c_str()) && fileName.find(".") == string::npos )
83 >        continue;
84 >
85 >      filePath = dirPath;
86 >      filePath += fname.Data();
87 >      //fwlite::ChainEvent to lop over events, jets, etc
88 >      fileNames.push_back(filePath.c_str());
89 >
90 >      if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
91 >                                                   treeName.c_str()) )
92 >        if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
93 >        else
94 >          return -1;
95  
96 <                        filePath = dirPath;
97 <                        filePath += fname.Data();
98 <                        //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;
96 >    }
97 >  } else
98 >    return -1;
99  
100 <                }
77 <        } else
78 <                return -1;
100 >  for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
101  
102 <        for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
102 >    string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
103  
104 <                string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
105 <                size_t a = s.find("_");
106 <                if (a == string::npos)
107 <                        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());
104 >    string branch_alias = s;
105 >    string branch_name = s;
106 >    if (s.find(".", s.size()-1) != string::npos)
107 >      branch_name += "obj";
108  
109 +    size_t a = s.find("_");
110 +    if (a != string::npos) {
111 +      size_t b = s.find("_", a+1);
112 +      if (b != string::npos) {
113 +        size_t c = s.find("_", b+1);
114 +        if (c != string::npos) {
115 +          string _prod     =s.substr(0,a);
116 +          string _label    =s.substr(a+1, b-a-1);
117 +          string _instance =s.substr(b+1, c-b-1);
118 +          branch_alias = _label;
119 +          if(_instance.length() > 0 )  {
120 +            branch_alias += "_";
121 +            branch_alias += _instance;
122 +            ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
123 +                                                      branch_name.c_str());
124 +          }
125 +          string branch_alias_full = _prod + "_" + branch_alias;
126 +          ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
127 +                                                    branch_name.c_str());
128          }
129 +      }
130 +    }
131  
132 <        // add branch with track  Jets
132 >    ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
133 >                                              branch_name.c_str());
134  
135 <        int nJetsKT;
101 <        TrackJetKT = new float [100];
102 <        fwlite::ChainEvent ev(fileNames);
135 >  }
136  
137 <        friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
105 <        friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
137 >  if( addEventInfo || addTrackJets) {
138  
139 <        cout<<"calculating additional variables..."<<endl;
108 <        for (ev.toBegin(); !ev.atEnd(); ++ev) {
139 >    if(verbose)   cout<<"calculating additional variables..."<<endl;
140  
141 <                fwlite::Handle<reco::CaloJetCollection> jets;
142 <                jets.getByLabel(ev, "antikt5CaloJets");
141 >    //make file for tree friends (adding additional, computed branches)
142 >    string friendFileName("/scratch/hh/current/cms/user/");
143 >    friendFileName += gSystem->GetUserInfo()->fUser;
144 >    friendFileName += "/temp/",
145 >      friendFileName += fileName;
146 >    friendFileName += ".root";
147 >    TFile *f = new TFile(friendFileName.c_str(),"recreate");
148  
149 <                fwlite::Handle<reco::TrackCollection> tracks;
114 <                tracks.getByLabel(ev, "generalTracks");
149 >    if( f->IsZombie() ) return -1;
150  
151 <                if (!jets.isValid())
152 <                        continue;
153 <                if (!tracks.isValid())
154 <                        continue;
155 <                double trackSum;
156 <                nJetsKT = 0;
157 <                for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
158 <                                !=jets->end(); jet++) {
159 <                        trackSum = 0;
160 <                        for (reco::TrackCollection::const_iterator track = tracks->begin(); track
161 <                                        !=tracks->end(); track++) {
162 <                                if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
163 <                                                < 0.5)
164 <                                        trackSum += track->pt();
165 <                        }
166 <                        TrackJetKT[nJetsKT] = trackSum;
167 <                        nJetsKT++;
168 <                }
169 <                friendTree->Fill();
151 >    string friendTreeName("friendTree");
152 >    //char number = currChain;
153 >    //friendTreeName += number;
154 >    //TTree *friendTree = new TTree("friendTree","friendTree");
155 >    TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
156 >    fwlite::ChainEvent ev(fileNames);
157 >     int _event, _run, _lumi;
158 >     int nJetsKT;
159 >     TrackJetKT = new float [100];
160 >
161 >    if( addEventInfo ) {
162 >
163 >
164 >      friendTree->Branch("event", &_event, "event/I");
165 >      friendTree->Branch("run", &_run, "run/I");
166 >      friendTree->Branch("lumi", &_lumi, "lumi/I");
167 >
168 >    }
169 >
170 >    if( addTrackJets ) {
171 >
172 >
173 >      friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
174 >      friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
175 >    }
176 >
177 >
178 >    int tenth = ev.size() / 10;
179 >    int eventNum =0;
180 >    for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
181 >
182 >
183 >      if( addEventInfo ) {
184 >
185 >        _event = ev.id().event();
186 >        _run   = ev.id().run();
187 >        _lumi = ev.luminosityBlock();
188 >
189 >      }
190 >
191 >      if( addTrackJets ) {
192 >
193 >        fwlite::Handle<reco::CaloJetCollection> jets;
194 >        jets.getByLabel(ev, "antikt5CaloJets");
195 >
196 >        fwlite::Handle<reco::TrackCollection> tracks;
197 >        tracks.getByLabel(ev, "generalTracks");
198 >
199 >        if (!jets.isValid())
200 >          continue;
201 >        if (!tracks.isValid())
202 >          continue;
203 >        double trackSum;
204 >        nJetsKT = 0;
205 >        for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
206 >               !=jets->end(); jet++) {
207 >          trackSum = 0;
208 >          for (reco::TrackCollection::const_iterator track = tracks->begin(); track
209 >                 !=tracks->end(); track++) {
210 >            if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
211 >                < 0.5)
212 >              trackSum += track->pt();
213 >          }
214 >          TrackJetKT[nJetsKT] = trackSum;
215 >          nJetsKT++;
216          }
217 <        f->cd();
218 <        friendTree->Write();
219 <        f->Close();
220 <        ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
140 <                        friendFileName.c_str());
217 >      }
218 >      friendTree->Fill();
219 >
220 >      if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
221  
222 <        return this->GetEntries();
222 >    }
223 >    f->cd();
224 >    friendTree->Write();
225 >    f->Close();
226 >    ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
227 >                                               friendFileName.c_str());
228 >
229 >
230 >
231 >  }
232 >
233 >
234 >  timer.Stop();
235 >  if(verbose) timer.Print();
236 >      
237 >  return this->GetEntries();
238  
239   }
240   //------------------------------------------------------------------
# Line 150 | Line 245 | int PlotTool::plot(int chainIndex, strin
245          if (chainIndex >= this->GetEntries() )
246                  return -1;
247  
248 +        TStopwatch timer;
249 +        if(verbose) {
250 +          cout<<"Plot: "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
251 +          timer.Start();
252 +        }
253 +
254          int currN = nEntries;
255          if (nEntries < 0)
256                  currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
# Line 188 | Line 289 | int PlotTool::plot(int chainIndex, strin
289                          && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
290  
291          if (useSubPads) {
292 <                if ( !canvases_[currCanvName.str()] || !gROOT->FindObject(currCanvName.str().c_str()) ) {
292 >                if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
293                          canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
294                  } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
295                          canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
296                  }
297          }
298  
299 <        if ( !pads_[currPadName.str()] || !gROOT->FindObject(currPadName.str().c_str()) ) {
299 >        if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
300                  pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
301 <                if (logY)
302 <                        pads_[currPadName.str()]->SetLogy(1);
301 >                //              if (logY)
302 >                //                      pads_[currPadName.str()]->SetLogy(1);
303          } else {
304                  pads_[currPadName.str()]->cd();
305                  currOpt += "SAMES";
# Line 209 | Line 310 | int PlotTool::plot(int chainIndex, strin
310  
311  
312          //Draw histogram:
313 <        int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutName.c_str(),
313 >        string cutNameWithGlobal = cutName;
314 >        if(cutName!="" && globalCuts!="")    cutNameWithGlobal += "&&";
315 >        if(globalCuts!="")                   cutNameWithGlobal += globalCuts;
316 >
317 >        int draw_err = ((TChain*) this->At(chainIndex))->Draw(histName.c_str(), cutNameWithGlobal.c_str(),
318                          currOpt.c_str(), currN);
319          if (draw_err < 0)
320                  return draw_err;
# Line 230 | Line 335 | int PlotTool::plot(int chainIndex, strin
335  
336          ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
337  
338 +        // Update for 2D histograms
339 +        if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
340 +                        pads_[currPadName.str()]->Draw();
341 +                        setCanvas( pads_[currPadName.str()] );
342 +        }
343 +
344  
345 +        if(verbose && draw_err > 0) {
346 +          timer.Stop();
347 +          cout<<"Done: Selected "<<draw_err<<" objects in "<< currN <<" processed events."<<endl;
348 +          timer.Print();
349 +        }
350          return draw_err;
351  
352   }
# Line 250 | Line 366 | int PlotTool::loop(vector<string> _histN
366                                                  != _cutName.end(); ++it2) {
367  
368                                          numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
369 <                                        updatePads();
369 >                                
370  
371                                  }
372                          }
# Line 271 | Line 387 | int PlotTool::loop(vector<string> _histN
387                  }
388          }
389  
390 <        //  updatePads();
390 >        updatePads();
391          fillCanvases();
392  
393          return numProcessed;
# Line 312 | Line 428 | int PlotTool::loop(string histName, vect
428   int PlotTool::updatePads() {
429  
430          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
431 <                if (gROOT->FindObject((*it).first.c_str() ) ) {
431 >                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
432                          (*it).second->Draw();
433                          setCanvas((*it).second);
434                  } else
# Line 327 | Line 443 | int PlotTool::fillCanvases() {
443  
444          for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
445                          != canvases_.end(); ++it) {
446 <                const char* canvName = (*it).first.c_str();
447 <                if (gROOT->FindObject(canvName) ) {
446 >                string canvName = (*it).first;
447 >                if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
448  
449                          int numP = (*it).second->GetNumber()+1;
450  
# Line 346 | Line 462 | int PlotTool::fillCanvases() {
462                                  y += 1;
463                          }
464  
465 +
466                          (*it).second->Divide(x, y);
467                          int padIndex = 1;
468                          for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
469                                          != pads_.end(); ++it2) {
470                                  string padName = (*it2).first;
471 <                                if (gROOT->FindObject(padName.c_str() ) ) {
472 <                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
473 <                                                        canvName, "All") ) {
471 >                                if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
472 >                string n1 = canvName.substr(0,canvName.find(":"));
473 >                string n2("");
474 >                if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
475 >
476 >                if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
477                                                  (*it).second->cd(padIndex);
478                                                  (*it2).second->DrawClonePad();
479                                                  (*it2).second->Close();
# Line 457 | Line 577 | void PlotTool::setMathLabels(TH1* thisHi
577          string x = thisHist->GetXaxis()->GetTitle();
578          string y = thisHist->GetYaxis()->GetTitle();
579  
580 <        if (t.find(".phi()") != string::npos)
581 <                t.replace(t.find(".phi()"), 6, " #phi");
580 > //      if (x.find("__") != string::npos)
581 > //        x = x.substr(0,x.find("__"));
582 > //      if (y.find("__") != string::npos)
583 > //        y = y.substr(0,y.find("__"));
584 >
585          if (x.find(".phi()") != string::npos)
586                  x.replace(x.find(".phi()"), 6, " #phi");
587          if (y.find(".phi()") != string::npos)
588                  y.replace(y.find(".phi()"), 6, " #phi");
589  
467        if (t.find(".eta()") != string::npos)
468                t.replace(t.find(".eta()"), 6, " #eta");
590          if (x.find(".eta()") != string::npos)
591                  x.replace(x.find(".eta()"), 6, " #eta");
592          if (y.find(".eta()") != string::npos)
593                  y.replace(y.find(".eta()"), 6, " #eta");
594  
595 <        if (t.find(".pt()") != string::npos)
596 <                t.replace(t.find(".pt()"), 5, " p_{T}");
595 >        if (x.find(".theta()") != string::npos)
596 >                x.replace(x.find(".theta()"), 6, " #theta");
597 >        if (y.find(".theta()") != string::npos)
598 >                y.replace(y.find(".theta()"), 6, " #theta");
599 >
600 >        if (x.find(".chi2()") != string::npos)
601 >                x.replace(x.find(".chi2()"), 7, " #chi^{2}");
602 >        if (y.find(".chi2()") != string::npos)
603 >                y.replace(y.find(".chi2()"), 7, " #chi^{2}");
604 >
605          if (x.find(".pt()") != string::npos)
606 <                x.replace(x.find(".pt()"), 5, " p_{T}");
606 >                x.replace(x.find(".pt()"), 5, " p_{T} [GeV]");
607          if (y.find(".pt()") != string::npos)
608 <                y.replace(y.find(".pt()"), 5, " p_{T}");
608 >                y.replace(y.find(".pt()"), 5, " p_{T} [GeV]");
609 >
610 >        if (x.find(".et()") != string::npos)
611 >                x.replace(x.find(".et()"), 5, " E_{T} [GeV]");
612 >        if (y.find(".et()") != string::npos)
613 >                y.replace(y.find(".et()"), 5, " E_{T} [GeV]");
614 >
615 >        //splitlines for many cuts
616 >        string test1= "{" + globalCuts + "}";
617 >        string test2= "&&" + globalCuts;
618 >
619 >        if(t.find(test1) != string::npos) t.replace(t.find(test1),test1.length(),"");
620 >        if(t.find(test2) != string::npos) t.replace(t.find(test2),test2.length(),"");
621 >
622 >        if (t.find("{") != string::npos && t.find("#splitline") == string::npos) {
623 >          t.replace(t.find_last_of("{"), 1, "}{");
624 >          string t_old = t;
625 >          t = "#splitline{";
626 >          t +=  t_old;
627 >        }
628  
629          thisHist ->SetTitle(t.c_str());
630          thisHist->GetXaxis()->SetTitle(x.c_str());
# Line 594 | Line 742 | void PlotTool::createColors() {
742  
743   //------------------------------------------------------------------
744  
745 < int PlotTool::saveCanvases(string type, string path) {
745 > int PlotTool::saveCanvases(string name, string path) {
746  
747          TSystemDirectory d("", path.c_str());
748          if (!d.GetListOfFiles())
749                  return -1;
602        if (type.find(".") == string::npos)
603                return -1;
750  
751 <        int savedFiles =0;
751 >        string namePs = path +"/" + name;
752 >        string nameRt = path +"/" + name;
753 >
754 >        if (name.find(".ps") == string::npos)
755 >                namePs += ".ps";
756 >
757 >        if (name.find(".root") == string::npos)
758 >                nameRt += ".root";
759 >
760 >        int savedCanvs =0;
761 >
762  
763          TIter next(gROOT->GetListOfCanvases());
764          TCanvas* canv;
765          while ((canv=(TCanvas*)next())) {
766 +          string modName = (*canv).GetName();
767 +          if(modName.find("{") != string::npos) modName = modName.substr(0,modName.find("{"));
768 +          (*canv).SetTitle( modName.c_str() );
769 +
770 +        }
771 +
772 +
773 +        TPostScript ps(namePs.c_str(),112);
774 +        TFile rt(nameRt.c_str(),"recreate");
775 +
776 +        next.Reset();
777 +        while ((canv=(TCanvas*)next())) {
778 +          ps.NewPage();
779  
611                string s = "";
612                s += path;
613                s += canv->GetName();
614                s += type;
780  
781 <                canv->SaveAs(s.c_str());
782 <                ++savedFiles;
781 >          (*canv).Write();
782 >          (*canv).Draw();
783 >          (*canv).Update();
784 >
785 >
786 >          ++savedCanvs;
787  
788          }
789  
790 <        return savedFiles;
790 >        ps.Close();
791 >        rt.Close();
792 >
793 >
794 >        if(verbose && savedCanvs) {
795 >          cout<<"Saved file "<<rt.GetName()<<" with "<<savedCanvs<<" canvases."<<endl;
796 >          cout<<"Saved file "<<ps.GetName()<<" with "<<savedCanvs<<" pages."<<endl;
797 > }
798 >        return savedCanvs;
799 >
800 > }
801 > //------------------------------------------------------------------
802 >
803 > int PlotTool::clearCanvases()
804 > {
805 >
806 >  while(gROOT->GetListOfCanvases()->GetEntries()) ((TCanvas*) gROOT->GetListOfCanvases()->At(0))->Close();
807 >  pads_.clear();
808 >  autoVars.clear();
809 >  return pads_.size() + gROOT->GetListOfCanvases()->GetEntries();
810 >
811 > }
812 > //------------------------------------------------------------------
813 > int PlotTool::setVariables(string label)
814 > {
815 >
816 >        for (int i=0; i< this->GetEntries(); ++i) {
817 >          cout<<"--------------------------------"<<endl;
818 >          cout<<((TChain*) this->At(i))->GetName()<<endl;
819 >          cout<<"------------"<<endl;
820 >
821 >          TList* leaves = (TList*) ((TChain*) this->At(i))->GetListOfLeaves();
822 >          for (int j=0; j< leaves->GetEntries(); ++j) {
823 >            TString leafName ( leaves->At(j)->GetName() );
824 >            if(! leafName.EndsWith(".") ) continue;
825 >            if(! leafName.Contains(label.c_str() ) ) continue;
826 >
827 >            TClass cl( ( (TLeafElement*) leaves->At(j) )->GetTypeName() );
828 >            cout<<"++++++"<<endl;
829 >            cout<< leafName.Data() <<endl;
830 >            for(int k=0;k<cl.GetListOfAllPublicMethods()->GetEntries();++k) {
831 >              string typeName ( ((TMethod*) cl.GetListOfAllPublicMethods()->At(k))->GetReturnTypeName() );
832 >              string methName ( cl.GetListOfAllPublicMethods()->At(k)->GetName() );
833 >              if( methName != "product") continue;
834 >                cout<< typeName <<endl;
835 >                string _type;
836 >                TString testString( typeName.c_str() );
837 >                if( testString.BeginsWith("vector<") ) {
838 >                  _type = typeName.substr(typeName.find("<")+1,typeName.find(">")-typeName.find("<")-1);
839 >                  string _varSize = leafName.Data();
840 >                  _varSize += "obj@.size()";
841 >                  autoVars.push_back( _varSize );
842 >                  }
843 >                else _type = typeName.substr(0,typeName.find("*"));
844 >                  TClass _cl( _type.c_str() );
845 >                  for(int l=0;l<_cl.GetListOfAllPublicMethods()->GetEntries();++l) {
846 >                    string _typeName ( ((TMethod*) _cl.GetListOfAllPublicMethods()->At(l))->GetReturnTypeName() );
847 >                    string _methName ( _cl.GetListOfAllPublicMethods()->At(l)->GetName() );
848 >                    //              if(_typeName.find("void") != string::npos ) continue;
849 >                    //              cout<<"   "<<_typeName<<"  "<<_methName<<endl;
850 >
851 >                    cout<<" "<<_typeName<<"  "<<_methName<<endl;
852 >                    if(_methName.find("operator")==string::npos&&(_typeName=="float"||_typeName=="double"||_typeName=="int"||_typeName=="unsigned int"||_typeName=="bool"||_typeName=="unsigned short"||_typeName=="unsigned long"||_typeName=="unsigned long long")) {
853 >
854 >                      cout<<"-->   "<<_typeName<<"  "<<_methName<<endl;
855 >
856 >                      string  _varName = leafName.Data();
857 >                      _varName += "obj.";
858 >                      _varName += _methName;
859 >                      _varName += "()";
860 >                      autoVars.push_back( _varName );
861 >                    }
862 >                  }
863 >                  
864 >              
865 >            }
866 >
867 >          }
868 >
869 >        }
870 >
871 >        return autoVars.size();
872 >
873 > }
874 > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
875 > //Draw efficiencies
876 > int  PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
877 > {
878 >
879 >  if( chainIndex >= this->GetEntries() ) return -1;
880 >
881 >  int currN = nEntries;
882 >  if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
883 >
884 >  //++++ Create and name Canvases according to global variables +++++++++++++
885 >  ostringstream currHistName;
886 >  if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
887 >  if( samePad_vars)  currHistName<<histName;
888 >  if( samePad_cuts)  currHistName<<"{"<<cutName<<"}";
889 >
890 >  ostringstream currPadName;
891 >  if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
892 >  if(! samePad_vars)  currPadName<<histName;
893 >  if(! samePad_cuts)  currPadName<<"{"<<cutName<<"}";
894 >
895 >
896 >  //Draw total histogram:
897 >  if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
898 >    pads_["total"] = new TCanvas("total", "total");
899 >  } else {
900 >    pads_["total"]->cd();
901 >  }
902 >  ostringstream bins_total;
903 >  bins_total<<histName;
904 >  bins_total<<">>total(100,0,1000)";
905 >
906 >
907 >  int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
908 >  if( draw_err <= 0 ) return draw_err;
909 >
910 >
911 >  TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
912 >
913 >  ostringstream bins_bkg;
914 >  bins_bkg<<histName;
915 >  bins_bkg<<">>bkg(";
916 >  bins_bkg<<total->GetNbinsX();
917 >  bins_bkg<<",";
918 >  bins_bkg<<total->GetXaxis()->GetXmin();
919 >  bins_bkg<<",";
920 >  bins_bkg<<total->GetXaxis()->GetXmax();
921 >  bins_bkg<<")";
922 >
923 >  //Draw bkg histogram:
924 >  if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
925 >    pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
926 >  } else {
927 >    pads_["bkg"]->cd();
928 >  }
929 >
930 >  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
931 >  if( draw_err <= 0 ) return draw_err;
932 >
933 >
934 >  TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
935 >
936 >  //Draw pass histogram:
937 >  ostringstream bins_pass;
938 >  bins_pass<<histName;
939 >  bins_pass<<">>pass(";
940 >  bins_pass<<total->GetNbinsX();
941 >  bins_pass<<",";
942 >  bins_pass<<total->GetXaxis()->GetXmin();
943 >  bins_pass<<",";
944 >  bins_pass<<total->GetXaxis()->GetXmax();
945 >  bins_pass<<")";
946 >
947 >  ostringstream cut_pass;
948 >  cut_pass<<"!(";
949 >  cut_pass<<cutName;
950 >  cut_pass<<")";
951 >  
952 >
953 >  if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
954 >    pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
955 >  } else {
956 >    pads_["pass"]->cd();
957 >  }
958 >
959 >  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
960 >  if( draw_err <= 0 ) return draw_err;
961 >
962 >
963 >  TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
964 >
965 >
966 >  currPadName<<"Eff";
967 >  //Draw Efficiency Graph:
968 >  if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
969 >    pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
970 >  } else {
971 >    pads_["EffGraph"]->cd();
972 >  }
973 >
974 >  TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
975 >  EffGraph->SetName(currHistName.str().c_str());
976 >
977 >  TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
978 >  EffGraph->Apply(reverse);
979 >  EffGraph->Draw("A*");
980 >
981 >  TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
982 >  EffGraph->Fit("fitfunc","R+");
983 >
984 >
985 >  //Save fit function
986 >
987 >  ostringstream savefuncName;
988 >  savefuncName<<histName;
989 >  savefuncName<<"_";
990 >
991 >  if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
992 >  if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
993 >  savefuncName<<cutName;
994 >
995 >
996 >  TFile file("ABCDFunctions.root","UPDATE");
997 >  TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
998 >  savefunc->SetParameters( fitfunc->GetParameters() );
999 >  savefunc->SetParErrors ( fitfunc->GetParErrors() );
1000 >
1001 >  //Show results
1002 >  if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
1003 >    pads_["abcd"] = new TCanvas("abcd", "abcd");
1004 >  } else {
1005 >    pads_["abcd"]->cd();
1006 >  }
1007 >
1008 >  bkg->Multiply(savefunc);
1009 >  bkg->Draw();
1010 >
1011 >  //  total->Add(bkg,-1);
1012 >  pass->SetLineColor(kRed);
1013 >  pass->Draw("sames,e");
1014 >  pads_["abcd"]->SetLogy();
1015 >
1016 >
1017 >  savefunc->Write();
1018 >  //  file.Close();
1019 >
1020 >
1021 >  return draw_err;
1022  
1023   }
1024   //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines