ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbbAnalysis/VHbbDataFormats/bin/Updater.cc
Revision: 1.2
Committed: Mon Oct 31 14:16:20 2011 UTC (13 years, 6 months ago) by arizzi
Content type: text/plain
Branch: MAIN
Changes since 1.1: +72 -4 lines
Log Message:
add  in The Updater also the code to change the PU reweight without redoing a full step2

File Contents

# User Rev Content
1 arizzi 1.1 #include <TH1F.h>
2     #include <TH3F.h>
3     #include <TH2F.h>
4     #include "PhysicsTools/Utilities/interface/LumiReWeighting.h"
5     #include <TROOT.h>
6     #include <TFile.h>
7     #include <TTree.h>
8     #include <TSystem.h>
9     #include "FWCore/ParameterSet/interface/ProcessDesc.h"
10     #include "FWCore/PythonParameterSet/interface/PythonProcessDesc.h"
11     #include "DataFormats/Math/interface/deltaR.h"
12     #include "DataFormats/Math/interface/deltaPhi.h"
13    
14     //btagging
15     //#include "VHbbAnalysis/VHbbDataFormats/interface/VHbbEvent.h"
16     //#include "VHbbAnalysis/VHbbDataFormats/interface/BTagWeight.h"
17     #include "VHbbAnalysis/VHbbDataFormats/interface/TriggerWeight.h"
18    
19     #include <sstream>
20     #include <string>
21    
22     #define MAXJ 30
23     #define MAXL 10
24     #define MAXB 10
25     typedef struct
26     {
27     float et;
28     float sumet;
29     float sig;
30     float phi;
31     } METInfo;
32    
33    
34     int main(int argc, char* argv[])
35     {
36     gROOT->Reset();
37    
38     // parse arguments
39     if ( argc < 2 ) {
40     return 0;
41     }
42     // get the python configuration
43     PythonProcessDesc builder(argv[1]);
44     const edm::ParameterSet& in = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("fwliteInput" );
45     const edm::ParameterSet& out = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("fwliteOutput");
46     const edm::ParameterSet& ana = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("Analyzer");
47     // now get each parameter
48     // int maxEvents_( in.getParameter<int>("maxEvents") );
49     // int skipEvents_( in.getParameter<int>("skipEvents") );
50     // unsigned int outputEvery_( in.getParameter<unsigned int>("outputEvery") );
51     std::string outputFile_( out.getParameter<std::string>("fileName" ) );
52     std::string inputFile_( in.getParameter<std::string>("fileName" ) );
53     bool replaceWeights( ana.getParameter<bool>("replaceWeights" ) );
54 arizzi 1.2 bool redoPU( ana.getParameter<bool>("redoPU" ) );
55    
56 arizzi 1.1 TriggerWeight triggerWeight(ana);
57    
58 arizzi 1.2 edm::LumiReWeighting lumiWeights;
59     edm::LumiReWeighting lumiWeights2011B;
60    
61     if(redoPU)
62     {
63     std::string PUmcfileName_ = in.getParameter<std::string> ("PUmcfileName") ;
64     std::string PUmcfileName2011B_ = in.getParameter<std::string> ("PUmcfileName2011B") ;
65     std::string PUdatafileName_ = in.getParameter<std::string> ("PUdatafileName") ;
66     std::string PUdatafileName2011B_ = in.getParameter<std::string> ("PUdatafileName2011B") ;
67     lumiWeights = edm::LumiReWeighting(PUmcfileName_,PUdatafileName_ , "pileup", "pileup");
68     lumiWeights2011B = edm::LumiReWeighting(PUmcfileName2011B_,PUdatafileName2011B_ , "pileup", "pileup");
69    
70    
71     //lumiWeights2011B.weight3D_init(); // generate the weights the fisrt time;
72     lumiWeights2011B.weight3D_init("Weight3D.root");
73    
74     }
75    
76 arizzi 1.1
77    
78     //Get old file, old tree and set top branch address
79     TFile *oldfile = new TFile(inputFile_.c_str());
80     TTree *oldtree = (TTree*)oldfile->Get("tree");
81     TH1F * count = (TH1F*)oldfile->Get("Count");
82     TH1F * countWithPU = (TH1F*)oldfile->Get("CountWithPU");
83     TH1F * countWithPU2011B = (TH1F*)oldfile->Get("CountWithPU2011B");
84     TH3F * input3DPU = (TH3F*)oldfile->Get("Input3DPU");
85    
86    
87     Int_t nentries = (Int_t)oldtree->GetEntries();
88     Int_t nvlep;
89     Float_t vLepton_pt[50];
90     Float_t vLepton_eta[50];
91     Int_t nhJets;
92     Float_t hJet_pt[50];
93     Float_t hJet_eta[50];
94     Int_t naJets;
95     Float_t aJet_pt[50];
96     Float_t aJet_eta[50];
97     Int_t Vtype;
98     METInfo MET;
99    
100     oldtree->SetBranchAddress("nvlep", &nvlep);
101     oldtree->SetBranchAddress("nhJets", &nhJets);
102     oldtree->SetBranchAddress("naJets", &naJets);
103    
104     oldtree->SetBranchAddress("vLepton_pt", vLepton_pt);
105     oldtree->SetBranchAddress("vLepton_eta", vLepton_eta);
106    
107     oldtree->SetBranchAddress("hJet_pt", hJet_pt);
108     oldtree->SetBranchAddress("hJet_eta", hJet_eta);
109    
110     oldtree->SetBranchAddress("aJet_pt", aJet_pt);
111     oldtree->SetBranchAddress("aJet_eta", aJet_eta);
112    
113     oldtree->SetBranchAddress("Vtype", &Vtype);
114     oldtree->SetBranchAddress("MET", &MET);
115    
116 arizzi 1.2 // Trigger weights
117 arizzi 1.1 Float_t weightTrig;
118     Float_t weightTrigMay;
119     Float_t weightTrigV4;
120     Float_t weightTrigMET;
121     Float_t weightTrigOrMu30;
122     Float_t weightEleRecoAndId;
123     Float_t weightEleTrigJetMETPart;
124     Float_t weightEleTrigElePart;
125     if(replaceWeights)
126     {
127     std::cout << "Replacing the weights in the same branch names" << std::endl;
128     oldtree->SetBranchAddress("weightTrig", &weightTrig);
129     oldtree->SetBranchAddress("weightTrigMay", &weightTrigMay);
130     oldtree->SetBranchAddress("weightTrigV4", &weightTrigV4);
131     oldtree->SetBranchAddress("weightTrigMET", &weightTrigMET);
132     oldtree->SetBranchAddress("weightTrigOrMu30", &weightTrigOrMu30);
133     oldtree->SetBranchAddress("weightEleRecoAndId", &weightEleRecoAndId);
134     oldtree->SetBranchAddress("weightEleTrigJetMETPart", &weightEleTrigJetMETPart);
135     oldtree->SetBranchAddress("weightEleTrigElePart", &weightEleTrigElePart);
136     }
137 arizzi 1.2
138     //Pileup Info
139     Float_t PUweight;
140     Float_t PUweight2011B;
141     Float_t PU0,PUp1,PUm1;
142     if(redoPU)
143     {
144     oldtree->SetBranchAddress("PU0", &PU0);
145     oldtree->SetBranchAddress("PUp1", &PUp1);
146     oldtree->SetBranchAddress("PUm1", &PUm1);
147     oldtree->SetBranchAddress("PUweight", &PUweight);
148     oldtree->SetBranchAddress("PUweight2011B", &PUweight2011B);
149     }
150    
151    
152 arizzi 1.1 //Create a new file + a clone of old tree in new file + clone of the histos + additional/updated weights
153    
154     TFile *newfile = new TFile(outputFile_.c_str(),"RECREATE");
155     TTree *newtree = oldtree->CloneTree(0);
156     if(count) count->Clone()->Write();
157     if(input3DPU) input3DPU->Clone()->Write();
158 arizzi 1.2
159     if(redoPU)
160     {
161     if(countWithPU) countWithPU->Clone("CountWithPU_OLD")->Write();
162     if(countWithPU2011B) countWithPU2011B->Clone("CountWithPU2011B_OLD")->Write();
163    
164     //recompute the normalization
165     countWithPU = new TH1F("CountWithPU","CountWithPU", 1,0,2 );
166     countWithPU2011B = new TH1F("CountWithPU2011B","CountWithPU2011B", 1,0,2 );
167     for(int ix=1;ix<=input3DPU->GetNbinsX();ix++)
168     for(int iy=1;iy<=input3DPU->GetNbinsY();iy++)
169     for(int iz=1;iz<=input3DPU->GetNbinsZ();iz++)
170     {
171     Float_t nev=input3DPU->GetBinContent(ix,iy,iz);
172     PUweight = lumiWeights.weight( iy-1 ); // bin 1 is [-0.5,0.5]
173     PUweight2011B = lumiWeights2011B.weight3D( ix-1, iy-1, iz-1);
174     countWithPU->Fill(1,PUweight*nev);
175     countWithPU2011B->Fill(1,PUweight2011B*nev);
176     }
177    
178     countWithPU->Write();
179     countWithPU2011B->Write();
180    
181     }
182     else
183     { //Just clone the old ones
184     if(countWithPU) countWithPU->Clone()->Write();
185     if(countWithPU2011B) countWithPU2011B->Clone()->Write();
186     }
187 arizzi 1.1
188     if(!replaceWeights)
189     {
190     std::cout << "Creating new branch names with _up postfix" << std::endl;
191     newtree->Branch("weightTrig_up", &weightTrig, "weightTrig_up/F");
192     newtree->Branch("weightTrigMay_up", &weightTrigMay,"weightTrigMay/F");
193     newtree->Branch("weightTrigV4_up", &weightTrigV4,"weightTrigV4/F");
194     newtree->Branch("weightTrigMET_up", &weightTrigMET,"weightTrigMET/F");
195     newtree->Branch("weightTrigOrMu30_up", &weightTrigOrMu30,"weightTrigOrMu30/F");
196     newtree->Branch("weightEleRecoAndId_up", &weightEleRecoAndId,"weightEleRecoAndId/F");
197     newtree->Branch("weightEleTrigJetMETPart_up", &weightEleTrigJetMETPart,"weightEleTrigJetMETPart/F");
198     newtree->Branch("weightEleTrigElePart_up", &weightEleTrigElePart,"weightEleTrigElePart/F");
199    
200     }
201    
202    
203    
204     // Float_t weightTrigUpdate;
205     // newtree->Branch("weightTrigUpdate" , &weightTrigUpdate , "weightTrigUpdate/F");
206    
207     for (Int_t i=0;i<nentries; i++) {
208     oldtree->GetEntry(i);
209    
210 arizzi 1.2 if(redoPU) {
211     PUweight = lumiWeights.weight( PU0 );
212     PUweight2011B = lumiWeights2011B.weight3D( PUm1, PU0, PUp1);
213     }
214    
215    
216    
217 arizzi 1.1 std::vector<float> jet30eta;
218     std::vector<float> jet30pt;
219     for( int j = 0 ; j < nhJets; j++) if(hJet_pt[j]>30 ) { jet30eta.push_back(hJet_eta[j]); jet30pt.push_back(hJet_pt[j]); }
220     for( int j = 0 ; j < naJets; j++) if(aJet_pt[j]>30 ) { jet30eta.push_back(aJet_eta[j]); jet30pt.push_back(aJet_pt[j]); }
221    
222     if(Vtype == 0 ){
223     float cweightID = triggerWeight.scaleMuID(vLepton_pt[0],vLepton_eta[0]) * triggerWeight.scaleMuID(vLepton_pt[1],vLepton_eta[1]) ;
224     float weightTrig1 = triggerWeight.scaleMuIsoHLT(vLepton_pt[0],vLepton_eta[0]);
225     float weightTrig2 = triggerWeight.scaleMuIsoHLT(vLepton_pt[1],vLepton_eta[1]);
226     float cweightTrig = weightTrig1 + weightTrig2 - weightTrig1*weightTrig2;
227     weightTrig = cweightID * cweightTrig;
228    
229     }
230     if( Vtype == 1 ){
231     std::vector<float> pt,eta;
232     pt.push_back(vLepton_pt[0]); eta.push_back(vLepton_eta[0]);
233     pt.push_back(vLepton_pt[1]); eta.push_back(vLepton_eta[1]);
234     weightEleRecoAndId=triggerWeight.scaleID95Ele(vLepton_pt[0],vLepton_eta[0]) * triggerWeight.scaleRecoEle(vLepton_pt[0],vLepton_eta[0]) *
235     triggerWeight.scaleID95Ele(vLepton_pt[1],vLepton_eta[1]) * triggerWeight.scaleRecoEle(vLepton_pt[1],vLepton_eta[1]);
236     weightEleTrigElePart = triggerWeight.scaleDoubleEle17Ele8(pt,eta);
237     //REMOVE FOR "float" for newer ntupler and add branch
238     float weightEleTrigEleAugPart = triggerWeight.scaleDoubleEle17Ele8Aug(pt,eta);
239     weightTrig = (weightEleTrigElePart*1.14+weightEleTrigEleAugPart*0.98 )/2.12 * weightEleRecoAndId;
240    
241    
242    
243     }
244     if(Vtype == 2 ){
245     float cweightID = triggerWeight.scaleMuID(vLepton_pt[0],vLepton_eta[0]);
246     float weightTrig1 = triggerWeight.scaleMuIsoHLT(vLepton_pt[0],vLepton_eta[0]);
247     float cweightTrig = weightTrig1;
248     weightTrig = cweightID * cweightTrig;
249     float weightTrig1OrMu30 = triggerWeight.scaleMuOr30IsoHLT(vLepton_pt[0],vLepton_eta[0]);
250     weightTrigOrMu30 = cweightID*weightTrig1OrMu30;
251    
252     }
253     if( Vtype == 3 ){
254     weightTrigMay = triggerWeight.scaleSingleEleMay(vLepton_pt[0],vLepton_eta[0]);
255     weightTrigV4 = triggerWeight.scaleSingleEleV4(vLepton_pt[0],vLepton_eta[0]);
256     weightEleRecoAndId=triggerWeight.scaleID80Ele(vLepton_pt[0],vLepton_eta[0]) * triggerWeight.scaleRecoEle(vLepton_pt[0],vLepton_eta[0]);
257     weightEleTrigJetMETPart=triggerWeight.scaleJet30Jet25(jet30pt,jet30eta)*triggerWeight.scalePFMHTEle(MET.et);
258     weightEleTrigElePart= weightTrigV4; //this is for debugging only, checking only the V4 part
259    
260     weightTrigMay*=weightEleRecoAndId;
261     weightTrigV4*=weightEleRecoAndId;
262     weightTrigV4*=weightEleTrigJetMETPart;
263     // weightTrig = weightTrigMay * 0.187 + weightTrigV4 * (1.-0.187); //FIXME: use proper lumi if we reload 2.fb
264     weightTrig = (weightTrigMay * 0.215 + weightTrigV4 * 1.915)/ 2.13; //FIXME: use proper lumi if we reload 2.fb
265    
266    
267     }
268     if( Vtype == 4 ){
269     float weightTrig1 = triggerWeight.scaleMetHLT(MET.et);
270     weightTrig = weightTrig1;
271     weightTrigMET = weightTrig1;
272    
273     }
274    
275    
276    
277    
278     newtree->Fill();
279     }
280     newtree->Print();
281     newtree->AutoSave();
282     delete oldfile;
283     delete newfile;
284     }