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.13 by tschum, Wed Dec 9 13:02:16 2009 UTC vs.
Revision 1.18 by tschum, Wed Feb 10 11:41:57 2010 UTC

# Line 14 | Line 14 | PlotTool::PlotTool() {
14  
15          showLegend = false;
16          logY = true;
17        addTrackJets = false;
18        addEventInfo = false;
17          verbose = true;
18  
19          globalCuts="";
20  
21 +        varBlockNames = "";   //Set to "ALL" or add Names, i.e. varBlockNames += "CaloTower"
22 +        recreateTree = false; //force recreation of friend tree
23 +
24   }
25  
26   //------------------------------------------------------------------
# Line 88 | Line 89 | int PlotTool::init(string fileName, stri
89        fileNames.push_back(filePath.c_str());
90  
91        if (((TChain*) this->At(currChain))->AddFile(filePath.c_str(), -1,
92 <                                                   treeName.c_str()) )
92 >                                                   treeName.c_str()) ) {
93          if(verbose)     cout<<"Chained "<<((TChain*) this->At(currChain))->GetNtrees()<<" file(s) with "<<((TChain*) this->At(currChain))->GetEntries()<<" events."<<endl;
94 <        else
95 <          return -1;
94 >      } else {
95 >        return -1;
96 >      }
97  
98      }
99    } else
# Line 134 | Line 136 | int PlotTool::init(string fileName, stri
136  
137    }
138  
137  if( addEventInfo || addTrackJets) {
139  
139    if(verbose)   cout<<"calculating additional variables..."<<endl;
140  
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    if( f->IsZombie() ) return -1;
150
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 ) {
141  
142 +    
143 +  string friendTreeName("friendTree");
144  
145 <      friendTree->Branch("event", &_event, "event/I");
146 <      friendTree->Branch("run", &_run, "run/I");
147 <      friendTree->Branch("lumi", &_lumi, "lumi/I");
148 <
149 <    }
150 <
151 <    if( addTrackJets ) {
152 <
153 <
154 <      friendTree->Branch("nJetsKT", &nJetsKT, "nJetsKT/I");
155 <      friendTree->Branch("TrackJetKT", TrackJetKT, "TrackJetKT[nJetsKT]/F");
156 <    }
157 <
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 <      }
218 <      friendTree->Fill();
219 <
220 <      if( eventNum != 0 && eventNum%tenth == 0) cout<<"Processed "<<eventNum <<" of "<<ev.size()<<" events. "<<endl;
221 <
222 <    }
223 <    f->cd();
224 <    friendTree->Write();
225 <    f->Close();
145 >  string friendFileName("/scratch/hh/current/cms/user/");
146 >  friendFileName += gSystem->GetUserInfo()->fUser;
147 >  friendFileName += "/temp/";
148 >  friendFileName += fileLabel;
149 >  friendFileName += ".root";
150 >  string fileOpt = "update";
151 >  if( recreateTree )    fileOpt = "recreate";
152 >
153 >  FWliteVariables fwVars(friendTreeName, friendFileName, fileOpt);
154 >
155 >  if(varBlockNames.length() > 1 ) {
156 >    fwVars.loop(fileNames, varBlockNames);
157 >    if(verbose)   cout<<"add friend tree with additional variables: "<<varBlockNames<<endl;
158      ((TChain*) this->At(currChain))->AddFriend(friendTreeName.c_str(),
159                                                 friendFileName.c_str());
160 <
161 <
230 <
160 >  } else {
161 >    if(verbose)   cout<<"No additional variables added."<<endl;
162    }
163  
164 +  
165 +    
166 +
167  
168    timer.Stop();
169    if(verbose) timer.Print();
# Line 428 | Line 362 | int PlotTool::loop(string histName, vect
362   int PlotTool::updatePads() {
363  
364          for (map< string, TCanvas* >::iterator it=pads_.begin() ; it != pads_.end(); ++it) {
365 <                if (gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
366 <                        (*it).second->Draw();
367 <                        setCanvas((*it).second);
368 <                } else
369 <                        pads_.erase(it);
365 >                if ( ! gROOT->GetListOfCanvases()->FindObject((*it).first.c_str() ) ) {
366 >                  pads_.erase(it);
367 >                  continue;
368 >                }
369 >                if( (*it).second->GetListOfPrimitives()->GetEntries() == 0 ) {
370 >                  (*it).second->Close();
371 >                  pads_.erase(it);
372 >                  continue;
373 >                }
374 >                (*it).second->Draw();
375 >                setCanvas((*it).second);
376 >
377          }
378  
379          return pads_.size();
# Line 871 | Line 812 | int PlotTool::setVariables(string label)
812          return autoVars.size();
813  
814   }
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!
815  
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 }
816   //------------------------------------------------------------------

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines