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.8 by tschum, Wed Dec 2 15:57:44 2009 UTC

# Line 14 | Line 14 | PlotTool::PlotTool() {
14  
15          showLegend = false;
16          logY = true;
17 +        addTrackJets = true;
18 +        verbose = true;
19  
20   }
21  
22   //------------------------------------------------------------------
23   //Fill object PlotTool with Chains constructed from files from given source
24 +
25   int PlotTool::init(string fileName, string dirPath, string treeName,
26                  string fileLabel) {
27          this->New(this->GetEntries() );
# Line 26 | Line 29 | int PlotTool::init(string fileName, stri
29  
30          if (currChain < 0)
31                  return currChain;
32 <        //make file for tree friends (adding additional, computed branches)
33 <        string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
34 <        friendFileName += fileName;
35 <        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());
32 >
33 >        TStopwatch timer;
34 >        timer.Start();
35 >
36  
37          fileNames.clear();
38  
# Line 52 | Line 49 | int PlotTool::init(string fileName, stri
49                  TString fname;
50                  string filePath;
51  
52 <                cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
52 >                if(verbose)     cout<<"Open"<<dirPath.c_str()<<" Search for .root files that contain: "
53                                  <<fileName.c_str()<<endl;
54  
55                  while ((file=(TSystemFile*)next())) {
# Line 69 | Line 66 | int PlotTool::init(string fileName, stri
66  
67                          if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
68                                          treeName.c_str()) )
69 <                                cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
69 >                if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
70                          else
71                                  return -1;
72  
# Line 80 | Line 77 | int PlotTool::init(string fileName, stri
77          for (int i=0; i<((TChain*) this->At(currChain))->GetListOfBranches()->GetEntries(); ++i) {
78  
79                  string s(((TChain*) this->At(currChain))->GetListOfBranches()->At(i)->GetName());
80 <                size_t a = s.find("_");
81 <                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);
80 >
81 >                string branch_alias = s;
82                  string branch_name = s;
83                  if (s.find(".", s.size()-1) != string::npos)
84                          branch_name += "obj";
85 +
86 +                size_t a = s.find("_");
87 +                if (a != string::npos) {
88 +                  size_t b = s.find("_", a+1);
89 +                  if (b != string::npos) {
90 +                    size_t c = s.find("_", b+1);
91 +                    if (c != string::npos) {
92 +                      string _label    =s.substr(a+1, b-a-1);
93 +                      string _instance =s.substr(b+1, c-b-1);
94 +                      branch_alias = _label;
95 +                      if(_instance.length() > 0 )  {
96 +                        branch_alias += "_";
97 +                        branch_alias += _instance;
98 +                      }
99 +                    }
100 +                  }
101 +                }
102 +
103                  ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
104                                  branch_name.c_str());
105  
# Line 97 | Line 107 | int PlotTool::init(string fileName, stri
107  
108          // add branch with track  Jets
109  
110 <        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());
110 >        if( addTrackJets ) {
111  
112 +          //make file for tree friends (adding additional, computed branches)
113 +          string friendFileName("/scratch/hh/current/cms/user/");
114 +          friendFileName += gSystem->GetUserInfo()->fUser;
115 +          friendFileName += "/temp/",
116 +          friendFileName += fileName;
117 +          friendFileName += ".root";
118 +          TFile *f = new TFile(friendFileName.c_str(),"recreate");
119 +
120 +          if( f->IsZombie() ) return -1;
121 +
122 +          string friendTreeName("friendTree");
123 +          //char number = currChain;
124 +          //friendTreeName += number;
125 +          //TTree *friendTree = new TTree("friendTree","friendTree");
126 +          TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
127 +
128 +          int nJetsKT;
129 +          TrackJetKT = new float [100];
130 +          fwlite::ChainEvent ev(fileNames);
131 +
132 +          friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
133 +          friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
134 +
135 +          if(verbose)   cout<<"calculating additional variables..."<<endl;
136 +          
137 +          
138 +
139 +          int tenth = ev.size() / 10;
140 +          int eventNum =0;
141 +          for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
142 +
143 +            fwlite::Handle<reco::CaloJetCollection> jets;
144 +            jets.getByLabel(ev, "antikt5CaloJets");
145 +
146 +            fwlite::Handle<reco::TrackCollection> tracks;
147 +            tracks.getByLabel(ev, "generalTracks");
148 +
149 +            if (!jets.isValid())
150 +              continue;
151 +            if (!tracks.isValid())
152 +              continue;
153 +            double trackSum;
154 +            nJetsKT = 0;
155 +            for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
156 +                   !=jets->end(); jet++) {
157 +              trackSum = 0;
158 +              for (reco::TrackCollection::const_iterator track = tracks->begin(); track
159 +                     !=tracks->end(); track++) {
160 +                if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
161 +                    < 0.5)
162 +                  trackSum += track->pt();
163 +              }
164 +              TrackJetKT[nJetsKT] = trackSum;
165 +              nJetsKT++;
166 +            }
167 +            friendTree->Fill();
168 +
169 +            if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
170 +
171 +          }
172 +          f->cd();
173 +          friendTree->Write();
174 +          f->Close();
175 +          ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
176 +                                                     friendFileName.c_str());
177 +        }
178 +
179 +        timer.Stop();
180 +        if(verbose) timer.Print();
181 +      
182          return this->GetEntries();
183  
184   }
# Line 150 | Line 190 | int PlotTool::plot(int chainIndex, strin
190          if (chainIndex >= this->GetEntries() )
191                  return -1;
192  
193 +        TStopwatch timer;
194 +        timer.Start();
195 +
196          int currN = nEntries;
197          if (nEntries < 0)
198                  currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
# Line 188 | Line 231 | int PlotTool::plot(int chainIndex, strin
231                          && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
232  
233          if (useSubPads) {
234 <                if ( !canvases_[currCanvName.str()] || !gROOT->FindObject(currCanvName.str().c_str()) ) {
234 >                if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
235                          canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
236                  } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
237                          canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
238                  }
239          }
240  
241 <        if ( !pads_[currPadName.str()] || !gROOT->FindObject(currPadName.str().c_str()) ) {
241 >        if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
242                  pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
243                  if (logY)
244                          pads_[currPadName.str()]->SetLogy(1);
# Line 230 | Line 273 | int PlotTool::plot(int chainIndex, strin
273  
274          ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
275  
276 +        // Update for 2D histograms
277 +        if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
278  
279 +                        pads_[currPadName.str()]->Draw();
280 +                        setCanvas( pads_[currPadName.str()] );
281 +        }
282 +
283 +        timer.Stop();
284 +        if(verbose) {
285 +          cout<<"Done: "<<currN<<" events in "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
286 +          timer.Print();
287 +        }
288          return draw_err;
289  
290   }
# Line 250 | Line 304 | int PlotTool::loop(vector<string> _histN
304                                                  != _cutName.end(); ++it2) {
305  
306                                          numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
307 <                                        updatePads();
307 >                                
308  
309                                  }
310                          }
# Line 271 | Line 325 | int PlotTool::loop(vector<string> _histN
325                  }
326          }
327  
328 <        //  updatePads();
328 >        updatePads();
329          fillCanvases();
330  
331          return numProcessed;
# Line 312 | Line 366 | int PlotTool::loop(string histName, vect
366   int PlotTool::updatePads() {
367  
368          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
369 <                if (gROOT->FindObject((*it).first.c_str() ) ) {
369 >                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
370                          (*it).second->Draw();
371                          setCanvas((*it).second);
372                  } else
# Line 327 | Line 381 | int PlotTool::fillCanvases() {
381  
382          for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
383                          != canvases_.end(); ++it) {
384 <                const char* canvName = (*it).first.c_str();
385 <                if (gROOT->FindObject(canvName) ) {
384 >                string canvName = (*it).first;
385 >                if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
386  
387                          int numP = (*it).second->GetNumber()+1;
388  
# Line 346 | Line 400 | int PlotTool::fillCanvases() {
400                                  y += 1;
401                          }
402  
403 +
404                          (*it).second->Divide(x, y);
405                          int padIndex = 1;
406                          for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
407                                          != pads_.end(); ++it2) {
408                                  string padName = (*it2).first;
409 <                                if (gROOT->FindObject(padName.c_str() ) ) {
410 <                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
411 <                                                        canvName, "All") ) {
409 >                                if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
410 >                string n1 = canvName.substr(0,canvName.find(":"));
411 >                string n2("");
412 >                if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
413 >
414 >                if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
415                                                  (*it).second->cd(padIndex);
416                                                  (*it2).second->DrawClonePad();
417                                                  (*it2).second->Close();
# Line 622 | Line 680 | int PlotTool::saveCanvases(string type,
680  
681   }
682   //------------------------------------------------------------------
683 + void PlotTool::showChainInfo()
684 + {
685 +
686 +
687 +  cout<<endl;
688 +  cout<<"****** Show Chain Information:"<<endl;
689 +
690 +
691 +  this->ls();
692 +
693 +  cout<<endl;
694 +  cout<<"We have "<<this->GetEntries()<<" TChains with following aliases set:"<<endl;
695 +        for (int i=0; i< this->GetEntries(); ++i) {
696 +          cout<<endl;
697 +          cout<<((TChain*) this->At(i))->GetName()<<endl;
698 +          ((TChain*) this->At(i))->GetListOfAliases()->ls();
699 +
700 +        }
701 +
702 +
703 +
704 + }
705 + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
706 + //Draw efficiencies
707 + int  PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
708 + {
709 +
710 +  if( chainIndex >= this->GetEntries() ) return -1;
711 +
712 +  int currN = nEntries;
713 +  if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
714 +
715 +  //++++ Create and name Canvases according to global variables +++++++++++++
716 +  ostringstream currHistName;
717 +  if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
718 +  if( samePad_vars)  currHistName<<histName;
719 +  if( samePad_cuts)  currHistName<<"{"<<cutName<<"}";
720 +
721 +  ostringstream currPadName;
722 +  if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
723 +  if(! samePad_vars)  currPadName<<histName;
724 +  if(! samePad_cuts)  currPadName<<"{"<<cutName<<"}";
725 +
726 +
727 +  //Draw total histogram:
728 +  if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
729 +    pads_["total"] = new TCanvas("total", "total");
730 +  } else {
731 +    pads_["total"]->cd();
732 +  }
733 +  ostringstream bins_total;
734 +  bins_total<<histName;
735 +  bins_total<<">>total(100,0,1000)";
736 +
737 +
738 +  int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
739 +  if( draw_err <= 0 ) return draw_err;
740 +
741 +
742 +  TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
743 +
744 +  ostringstream bins_bkg;
745 +  bins_bkg<<histName;
746 +  bins_bkg<<">>bkg(";
747 +  bins_bkg<<total->GetNbinsX();
748 +  bins_bkg<<",";
749 +  bins_bkg<<total->GetXaxis()->GetXmin();
750 +  bins_bkg<<",";
751 +  bins_bkg<<total->GetXaxis()->GetXmax();
752 +  bins_bkg<<")";
753 +
754 +  //Draw bkg histogram:
755 +  if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
756 +    pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
757 +  } else {
758 +    pads_["bkg"]->cd();
759 +  }
760 +
761 +  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
762 +  if( draw_err <= 0 ) return draw_err;
763 +
764 +
765 +  TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
766 +
767 +  //Draw pass histogram:
768 +  ostringstream bins_pass;
769 +  bins_pass<<histName;
770 +  bins_pass<<">>pass(";
771 +  bins_pass<<total->GetNbinsX();
772 +  bins_pass<<",";
773 +  bins_pass<<total->GetXaxis()->GetXmin();
774 +  bins_pass<<",";
775 +  bins_pass<<total->GetXaxis()->GetXmax();
776 +  bins_pass<<")";
777 +
778 +  ostringstream cut_pass;
779 +  cut_pass<<"!(";
780 +  cut_pass<<cutName;
781 +  cut_pass<<")";
782 +  
783 +
784 +  if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
785 +    pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
786 +  } else {
787 +    pads_["pass"]->cd();
788 +  }
789 +
790 +  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
791 +  if( draw_err <= 0 ) return draw_err;
792 +
793 +
794 +  TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
795 +
796 +
797 +  currPadName<<"Eff";
798 +  //Draw Efficiency Graph:
799 +  if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
800 +    pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
801 +  } else {
802 +    pads_["EffGraph"]->cd();
803 +  }
804 +
805 +  TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
806 +  EffGraph->SetName(currHistName.str().c_str());
807 +
808 +  TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
809 +  EffGraph->Apply(reverse);
810 +  EffGraph->Draw("A*");
811 +
812 +  TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
813 +  EffGraph->Fit("fitfunc","R+");
814 +
815 +
816 +  //Save fit function
817 +
818 +  ostringstream savefuncName;
819 +  savefuncName<<histName;
820 +  savefuncName<<"_";
821 +
822 +  if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
823 +  if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
824 +  savefuncName<<cutName;
825 +
826 +
827 +  TFile file("ABCDFunctions.root","UPDATE");
828 +  TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
829 +  savefunc->SetParameters( fitfunc->GetParameters() );
830 +  savefunc->SetParErrors ( fitfunc->GetParErrors() );
831 +
832 +  //Show results
833 +  if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
834 +    pads_["abcd"] = new TCanvas("abcd", "abcd");
835 +  } else {
836 +    pads_["abcd"]->cd();
837 +  }
838 +
839 +  bkg->Multiply(savefunc);
840 +  bkg->Draw();
841 +
842 +  //  total->Add(bkg,-1);
843 +  pass->SetLineColor(kRed);
844 +  pass->Draw("sames,e");
845 +  pads_["abcd"]->SetLogy();
846 +
847 +
848 +  savefunc->Write();
849 +  //  file.Close();
850 +
851 +
852 +  return draw_err;
853 +
854 + }
855 + //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines