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.9 by tschum, Thu Dec 3 11:24:14 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 _prod     =s.substr(0,a);
93 +                      string _label    =s.substr(a+1, b-a-1);
94 +                      string _instance =s.substr(b+1, c-b-1);
95 +                      branch_alias = _label;
96 +                      if(_instance.length() > 0 )  {
97 +                        branch_alias += "_";
98 +                        branch_alias += _instance;
99 +                        ((TChain*) this->At(currChain))->SetAlias(_instance.c_str(),
100 +                                                                  branch_name.c_str());
101 +                      }
102 +                      string branch_alias_full = _prod + "_" + branch_alias;
103 +                      ((TChain*) this->At(currChain))->SetAlias(branch_alias_full.c_str(),
104 +                                branch_name.c_str());
105 +                    }
106 +                  }
107 +                }
108 +
109                  ((TChain*) this->At(currChain))->SetAlias(branch_alias.c_str(),
110                                  branch_name.c_str());
111  
# Line 97 | Line 113 | int PlotTool::init(string fileName, stri
113  
114          // add branch with track  Jets
115  
116 <        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());
116 >        if( addTrackJets ) {
117  
118 +          //make file for tree friends (adding additional, computed branches)
119 +          string friendFileName("/scratch/hh/current/cms/user/");
120 +          friendFileName += gSystem->GetUserInfo()->fUser;
121 +          friendFileName += "/temp/",
122 +          friendFileName += fileName;
123 +          friendFileName += ".root";
124 +          TFile *f = new TFile(friendFileName.c_str(),"recreate");
125 +
126 +          if( f->IsZombie() ) return -1;
127 +
128 +          string friendTreeName("friendTree");
129 +          //char number = currChain;
130 +          //friendTreeName += number;
131 +          //TTree *friendTree = new TTree("friendTree","friendTree");
132 +          TTree *friendTree = new TTree(friendTreeName.c_str(),friendTreeName.c_str());
133 +
134 +          int nJetsKT;
135 +          TrackJetKT = new float [100];
136 +          fwlite::ChainEvent ev(fileNames);
137 +
138 +          friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
139 +          friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
140 +
141 +          if(verbose)   cout<<"calculating additional variables..."<<endl;
142 +          
143 +          
144 +
145 +          int tenth = ev.size() / 10;
146 +          int eventNum =0;
147 +          for (ev.toBegin(); !ev.atEnd(); ++ev, ++eventNum) {
148 +
149 +            fwlite::Handle<reco::CaloJetCollection> jets;
150 +            jets.getByLabel(ev, "antikt5CaloJets");
151 +
152 +            fwlite::Handle<reco::TrackCollection> tracks;
153 +            tracks.getByLabel(ev, "generalTracks");
154 +
155 +            if (!jets.isValid())
156 +              continue;
157 +            if (!tracks.isValid())
158 +              continue;
159 +            double trackSum;
160 +            nJetsKT = 0;
161 +            for (reco::CaloJetCollection::const_iterator jet = jets->begin(); jet
162 +                   !=jets->end(); jet++) {
163 +              trackSum = 0;
164 +              for (reco::TrackCollection::const_iterator track = tracks->begin(); track
165 +                     !=tracks->end(); track++) {
166 +                if (deltaR(jet->eta(), jet->phi(), track->eta(), track->phi())
167 +                    < 0.5)
168 +                  trackSum += track->pt();
169 +              }
170 +              TrackJetKT[nJetsKT] = trackSum;
171 +              nJetsKT++;
172 +            }
173 +            friendTree->Fill();
174 +
175 +            if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
176 +
177 +          }
178 +          f->cd();
179 +          friendTree->Write();
180 +          f->Close();
181 +          ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
182 +                                                     friendFileName.c_str());
183 +        }
184 +
185 +        timer.Stop();
186 +        if(verbose) timer.Print();
187 +      
188          return this->GetEntries();
189  
190   }
# Line 150 | Line 196 | int PlotTool::plot(int chainIndex, strin
196          if (chainIndex >= this->GetEntries() )
197                  return -1;
198  
199 +        TStopwatch timer;
200 +        timer.Start();
201 +
202          int currN = nEntries;
203          if (nEntries < 0)
204                  currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
# Line 188 | Line 237 | int PlotTool::plot(int chainIndex, strin
237                          && !samePad_vars) || (sameCanv_cuts && !samePad_cuts);
238  
239          if (useSubPads) {
240 <                if ( !canvases_[currCanvName.str()] || !gROOT->FindObject(currCanvName.str().c_str()) ) {
240 >                if ( !canvases_[currCanvName.str()] || !gROOT->GetListOfCanvases()->FindObject(currCanvName.str().c_str()) ) {
241                          canvases_[currCanvName.str()] = new TCanvas(currCanvName.str().c_str(), currCanvName.str().c_str());
242                  } else if (canvases_[currCanvName.str()]->GetNumber() >= 0) {
243                          canvases_[currCanvName.str()]->SetNumber(canvases_[currCanvName.str()]->GetNumber()+1);
244                  }
245          }
246  
247 <        if ( !pads_[currPadName.str()] || !gROOT->FindObject(currPadName.str().c_str()) ) {
247 >        if ( !pads_[currPadName.str()] || !gROOT->GetListOfCanvases()->FindObject(currPadName.str().c_str()) ) {
248                  pads_[currPadName.str()] = new TCanvas(currPadName.str().c_str(), currPadName.str().c_str());
249 <                if (logY)
250 <                        pads_[currPadName.str()]->SetLogy(1);
249 >                //              if (logY)
250 >                //                      pads_[currPadName.str()]->SetLogy(1);
251          } else {
252                  pads_[currPadName.str()]->cd();
253                  currOpt += "SAMES";
# Line 230 | Line 279 | int PlotTool::plot(int chainIndex, strin
279  
280          ((TH1F*) pads_[currPadName.str()]->GetPrimitive("htemp"))->SetName(currHistName.str().c_str()); // Set Name of histogram
281  
282 +        // Update for 2D histograms
283 +        if( pads_[currPadName.str()]->GetPrimitive(currHistName.str().c_str())->InheritsFrom("TH2") ) {
284 +                        pads_[currPadName.str()]->Draw();
285 +                        setCanvas( pads_[currPadName.str()] );
286 +        }
287  
288 +        timer.Stop();
289 +        if(verbose) {
290 +          cout<<"Done: "<<currN<<" events in "<<histName<<" "<<cutName<<" from chain "<<this->At(chainIndex)->GetName()<<endl;
291 +          timer.Print();
292 +        }
293          return draw_err;
294  
295   }
# Line 250 | Line 309 | int PlotTool::loop(vector<string> _histN
309                                                  != _cutName.end(); ++it2) {
310  
311                                          numProcessed += plot(i, *it1, *it2, nEntries, drwOpt);
312 <                                        updatePads();
312 >                                
313  
314                                  }
315                          }
# Line 271 | Line 330 | int PlotTool::loop(vector<string> _histN
330                  }
331          }
332  
333 <        //  updatePads();
333 >        updatePads();
334          fillCanvases();
335  
336          return numProcessed;
# Line 312 | Line 371 | int PlotTool::loop(string histName, vect
371   int PlotTool::updatePads() {
372  
373          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
374 <                if (gROOT->FindObject((*it).first.c_str() ) ) {
374 >                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
375                          (*it).second->Draw();
376                          setCanvas((*it).second);
377                  } else
# Line 327 | Line 386 | int PlotTool::fillCanvases() {
386  
387          for (map< string, TCanvas* >::iterator it=canvases_.begin() ; it
388                          != canvases_.end(); ++it) {
389 <                const char* canvName = (*it).first.c_str();
390 <                if (gROOT->FindObject(canvName) ) {
389 >                string canvName = (*it).first;
390 >                if (gROOT->GetListOfCanvases()->FindObject(canvName.c_str()) ) {
391  
392                          int numP = (*it).second->GetNumber()+1;
393  
# Line 346 | Line 405 | int PlotTool::fillCanvases() {
405                                  y += 1;
406                          }
407  
408 +
409                          (*it).second->Divide(x, y);
410                          int padIndex = 1;
411                          for (map< string, TCanvas* >::iterator it2=pads_.begin() ; it2
412                                          != pads_.end(); ++it2) {
413                                  string padName = (*it2).first;
414 <                                if (gROOT->FindObject(padName.c_str() ) ) {
415 <                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
416 <                                                        canvName, "All") ) {
414 >                                if (gROOT->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
415 >                string n1 = canvName.substr(0,canvName.find(":"));
416 >                string n2("");
417 >                if(canvName.find("{") != string::npos ) n2 = canvName.substr(canvName.find("{"),canvName.find("}"));
418 >
419 >                if ( (padName.find(canvName.c_str()) != string::npos ) || (padName.find(n1) != string::npos && padName.find(n2) != string::npos) || !strcmp( canvName.c_str(), "All") ) {
420                                                  (*it).second->cd(padIndex);
421                                                  (*it2).second->DrawClonePad();
422                                                  (*it2).second->Close();
# Line 594 | Line 657 | void PlotTool::createColors() {
657  
658   //------------------------------------------------------------------
659  
660 < int PlotTool::saveCanvases(string type, string path) {
660 > int PlotTool::saveCanvases(string name, string path) {
661  
662          TSystemDirectory d("", path.c_str());
663          if (!d.GetListOfFiles())
664                  return -1;
602        if (type.find(".") == string::npos)
603                return -1;
665  
666 <        int savedFiles =0;
666 >        string namePs = path +"/" + name;
667 >        string nameRt = path +"/" + name;
668 >
669 >        if (name.find(".ps") == string::npos)
670 >                namePs += ".ps";
671 >
672 >        if (name.find(".root") == string::npos)
673 >                nameRt += ".root";
674 >
675 >        int savedCanvs =0;
676 >
677 >        TPostScript ps(namePs.c_str(),112);
678 >        TFile rt(nameRt.c_str(),"recreate");
679  
680          TIter next(gROOT->GetListOfCanvases());
681          TCanvas* canv;
682          while ((canv=(TCanvas*)next())) {
683 +          ps.NewPage();
684 +          (*canv).Draw();
685 +          (*canv).Update();
686 +          (*canv).Write();
687 +          ++savedCanvs;
688 +
689 +        }
690 +
691 +        ps.Close();
692 +        rt.Close();
693 +
694 +        return savedCanvs;
695 +
696 + }
697 + //------------------------------------------------------------------
698 + void PlotTool::showChainInfo()
699 + {
700 +
701 +
702 +  cout<<endl;
703 +  cout<<"****** Show Chain Information:"<<endl;
704  
611                string s = "";
612                s += path;
613                s += canv->GetName();
614                s += type;
705  
706 <                canv->SaveAs(s.c_str());
707 <                ++savedFiles;
706 >  this->ls();
707 >
708 >  cout<<endl;
709 >  cout<<"We have "<<this->GetEntries()<<" TChains with following aliases set:"<<endl;
710 >        for (int i=0; i< this->GetEntries(); ++i) {
711 >          cout<<endl;
712 >          cout<<((TChain*) this->At(i))->GetName()<<endl;
713 >          ((TChain*) this->At(i))->GetListOfAliases()->ls();
714  
715          }
716  
717 <        return savedFiles;
717 >
718 >
719 > }
720 > //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
721 > //Draw efficiencies
722 > int  PlotTool::plotEff(int chainIndex, string histName, string cutName, int nEntries, double fitXmin, double fitXmax, string fitFormula)
723 > {
724 >
725 >  if( chainIndex >= this->GetEntries() ) return -1;
726 >
727 >  int currN = nEntries;
728 >  if(nEntries < 0) currN = ((TChain*) this->At(chainIndex))->GetEntries(); //nEntries<0 : all entries are plotted!
729 >
730 >  //++++ Create and name Canvases according to global variables +++++++++++++
731 >  ostringstream currHistName;
732 >  if( samePad_trees) currHistName<<((TChain*) this->At(chainIndex))->GetName()<<":";
733 >  if( samePad_vars)  currHistName<<histName;
734 >  if( samePad_cuts)  currHistName<<"{"<<cutName<<"}";
735 >
736 >  ostringstream currPadName;
737 >  if(! samePad_trees) currPadName<<((TChain*) this->At(chainIndex))->GetName()<<":";
738 >  if(! samePad_vars)  currPadName<<histName;
739 >  if(! samePad_cuts)  currPadName<<"{"<<cutName<<"}";
740 >
741 >
742 >  //Draw total histogram:
743 >  if( ! pads_["total"] || ! gROOT->FindObject("total") ) {
744 >    pads_["total"] = new TCanvas("total", "total");
745 >  } else {
746 >    pads_["total"]->cd();
747 >  }
748 >  ostringstream bins_total;
749 >  bins_total<<histName;
750 >  bins_total<<">>total(100,0,1000)";
751 >
752 >
753 >  int draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_total.str().c_str(), "", "", currN);
754 >  if( draw_err <= 0 ) return draw_err;
755 >
756 >
757 >  TH1* total = ((TH1*) pads_["total"]->GetPrimitive("total"));
758 >
759 >  ostringstream bins_bkg;
760 >  bins_bkg<<histName;
761 >  bins_bkg<<">>bkg(";
762 >  bins_bkg<<total->GetNbinsX();
763 >  bins_bkg<<",";
764 >  bins_bkg<<total->GetXaxis()->GetXmin();
765 >  bins_bkg<<",";
766 >  bins_bkg<<total->GetXaxis()->GetXmax();
767 >  bins_bkg<<")";
768 >
769 >  //Draw bkg histogram:
770 >  if( ! pads_["bkg"] || ! gROOT->FindObject("bkg") ) {
771 >    pads_["bkg"] = new TCanvas("bkg", currPadName.str().c_str());
772 >  } else {
773 >    pads_["bkg"]->cd();
774 >  }
775 >
776 >  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_bkg.str().c_str(), cutName.c_str(),"" , currN);
777 >  if( draw_err <= 0 ) return draw_err;
778 >
779 >
780 >  TH1* bkg = ((TH1*) pads_["bkg"]->GetPrimitive("bkg"));
781 >
782 >  //Draw pass histogram:
783 >  ostringstream bins_pass;
784 >  bins_pass<<histName;
785 >  bins_pass<<">>pass(";
786 >  bins_pass<<total->GetNbinsX();
787 >  bins_pass<<",";
788 >  bins_pass<<total->GetXaxis()->GetXmin();
789 >  bins_pass<<",";
790 >  bins_pass<<total->GetXaxis()->GetXmax();
791 >  bins_pass<<")";
792 >
793 >  ostringstream cut_pass;
794 >  cut_pass<<"!(";
795 >  cut_pass<<cutName;
796 >  cut_pass<<")";
797 >  
798 >
799 >  if( ! pads_["pass"] || ! gROOT->FindObject("pass") ) {
800 >    pads_["pass"] = new TCanvas("pass", currPadName.str().c_str());
801 >  } else {
802 >    pads_["pass"]->cd();
803 >  }
804 >
805 >  draw_err = ((TChain*) this->At(chainIndex))->Draw(bins_pass.str().c_str(),cut_pass.str().c_str() ,"" , currN);
806 >  if( draw_err <= 0 ) return draw_err;
807 >
808 >
809 >  TH1* pass = ((TH1*) pads_["pass"]->GetPrimitive("pass"));
810 >
811 >
812 >  currPadName<<"Eff";
813 >  //Draw Efficiency Graph:
814 >  if( ! pads_["EffGraph"] || ! gROOT->FindObject("EffGraph") ) {
815 >    pads_["EffGraph"] = new TCanvas("EffGraph", currPadName.str().c_str());
816 >  } else {
817 >    pads_["EffGraph"]->cd();
818 >  }
819 >
820 >  TGraphAsymmErrors* EffGraph = new TGraphAsymmErrors(bkg, total);
821 >  EffGraph->SetName(currHistName.str().c_str());
822 >
823 >  TF1* reverse = new TF2("reverse","1/y-1",total->GetXaxis()->GetXmin(),total->GetXaxis()->GetXmax());
824 >  EffGraph->Apply(reverse);
825 >  EffGraph->Draw("A*");
826 >
827 >  TF1* fitfunc = new TF1("fitfunc",fitFormula.c_str(),fitXmin,fitXmax);
828 >  EffGraph->Fit("fitfunc","R+");
829 >
830 >
831 >  //Save fit function
832 >
833 >  ostringstream savefuncName;
834 >  savefuncName<<histName;
835 >  savefuncName<<"_";
836 >
837 >  if(cutName.find("<") != std::string::npos ) cutName.replace(cutName.find("<"),1,"_");
838 >  if(cutName.find(">") != std::string::npos ) cutName.replace(cutName.find(">"),1,"_");
839 >  savefuncName<<cutName;
840 >
841 >
842 >  TFile file("ABCDFunctions.root","UPDATE");
843 >  TF1* savefunc = new TF1(savefuncName.str().c_str(), fitfunc->GetExpFormula().Data(),0,10000);
844 >  savefunc->SetParameters( fitfunc->GetParameters() );
845 >  savefunc->SetParErrors ( fitfunc->GetParErrors() );
846 >
847 >  //Show results
848 >  if( ! pads_["abcd"] || ! gROOT->FindObject("abcd") ) {
849 >    pads_["abcd"] = new TCanvas("abcd", "abcd");
850 >  } else {
851 >    pads_["abcd"]->cd();
852 >  }
853 >
854 >  bkg->Multiply(savefunc);
855 >  bkg->Draw();
856 >
857 >  //  total->Add(bkg,-1);
858 >  pass->SetLineColor(kRed);
859 >  pass->Draw("sames,e");
860 >  pads_["abcd"]->SetLogy();
861 >
862 >
863 >  savefunc->Write();
864 >  //  file.Close();
865 >
866 >
867 >  return draw_err;
868  
869   }
870   //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines