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.7 by tschum, Fri Nov 27 17:39:46 2009 UTC vs.
Revision 1.8 by tschum, Wed Dec 2 15:57:44 2009 UTC

# Line 15 | Line 15 | PlotTool::PlotTool() {
15          showLegend = false;
16          logY = true;
17          addTrackJets = true;
18 +        verbose = true;
19  
20   }
21  
# Line 29 | Line 30 | int PlotTool::init(string fileName, stri
30          if (currChain < 0)
31                  return currChain;
32  
33 +        TStopwatch timer;
34 +        timer.Start();
35 +
36 +
37          fileNames.clear();
38  
39          //check if alternative label is different from default(searchString)
# Line 44 | 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 61 | 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 105 | Line 110 | int PlotTool::init(string fileName, stri
110          if( addTrackJets ) {
111  
112            //make file for tree friends (adding additional, computed branches)
113 <          string friendFileName("/scratch/hh/lustre/cms/user/thomsen/temp/");
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;
# Line 122 | Line 132 | int PlotTool::init(string fileName, stri
132            friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
133            friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
134  
135 <          cout<<"calculating additional variables..."<<endl;
136 <          for (ev.toBegin(); !ev.atEnd(); ++ev) {
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");
# Line 150 | Line 165 | int PlotTool::init(string fileName, stri
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();
# Line 157 | Line 175 | int PlotTool::init(string fileName, stri
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 168 | 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 248 | 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 268 | 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 289 | Line 325 | int PlotTool::loop(vector<string> _histN
325                  }
326          }
327  
328 <        //  updatePads();
328 >        updatePads();
329          fillCanvases();
330  
331          return numProcessed;
# Line 345 | 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->GetListOfCanvases()->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 364 | 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->GetListOfCanvases()->FindObject(padName.c_str() ) ) {
410 <                                        if ( (padName.find(canvName) != string::npos ) || !strcmp(
411 <                                                        canvName, "All") ) {
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 662 | Line 702 | void PlotTool::showChainInfo()
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