CMS 3D CMS Logo

JetCorrectorDemo.cc
Go to the documentation of this file.
1 #include <memory>
2 #include "TH2F.h"
3 #include "TGraph.h"
4 #include "TGraphErrors.h"
5 #include "TRandom.h"
6 #include "TLorentzVector.h"
7 // user include files
10 
20 //TFile Service
23 
24 //
25 // class declaration
26 //
27 
29 public:
30  explicit JetCorrectorDemo(const edm::ParameterSet&);
31  ~JetCorrectorDemo() override;
33 
34 private:
35  void beginJob() override ;
36  void analyze(const edm::Event&, const edm::EventSetup&) override;
37  void endJob() override ;
38 
44  std::vector<double> mVEta,mVPt;
45  double vjec_eta[100][1000],vjec_pt[100][1000],vpt[100][1000],vptcor[100][1000],veta[100][1000];
46  double vjecUnc_eta[100][1000],vUnc_eta[100][1000],vjecUnc_pt[100][1000],vUnc_pt[100][1000],vex_eta[100][1000],vex_pt[100][1000];
49  TGraphErrors *mVGraphEta[100],*mVGraphPt[100],*mVGraphCorPt[100];
50  TGraph *mUncEta[100], *mUncCorPt[100];
51  TRandom *mRandom;
52 };
53 //
54 //----------- Class Implementation ------------------------------------------
55 //
56 //---------------------------------------------------------------------------
58 {
59  mJetCorrector = consumes<reco::JetCorrector>(iConfig.getParameter<edm::InputTag>("JetCorrector"));
60  mPayloadName = iConfig.getParameter<std::string> ("PayloadName");
61  mUncertaintyTag = iConfig.getParameter<std::string> ("UncertaintyTag");
62  mUncertaintyFile = iConfig.getParameter<std::string> ("UncertaintyFile");
63  mNHistoPoints = iConfig.getParameter<int> ("NHistoPoints");
64  mNGraphPoints = iConfig.getParameter<int> ("NGraphPoints");
65  mEtaMin = iConfig.getParameter<double> ("EtaMin");
66  mEtaMax = iConfig.getParameter<double> ("EtaMax");
67  mPtMin = iConfig.getParameter<double> ("PtMin");
68  mPtMax = iConfig.getParameter<double> ("PtMax");
69  mVEta = iConfig.getParameter<std::vector<double> > ("VEta");
70  mVPt = iConfig.getParameter<std::vector<double> > ("VPt");
71  mDebug = iConfig.getUntrackedParameter<bool> ("Debug",false);
72  mUseCondDB = iConfig.getUntrackedParameter<bool> ("UseCondDB",false);
73 }
74 //---------------------------------------------------------------------------
76 {
77 
78 }
79 //---------------------------------------------------------------------------
81 {
83  iEvent.getByToken(mJetCorrector, corrector);
84  JetCorrectionUncertainty *jecUnc(nullptr);
85  if (mUncertaintyTag != "")
86  {
87  if (mUseCondDB)
88  {
90  iSetup.get<JetCorrectionsRecord>().get(mPayloadName,JetCorParColl);
91  JetCorrectorParameters const & JetCorPar = (*JetCorParColl)[mUncertaintyTag];
92  jecUnc = new JetCorrectionUncertainty(JetCorPar);
93  std::cout<<"Configured Uncertainty from CondDB"<<std::endl;
94  }
95  else
96  {
97  edm::FileInPath fip("CondFormats/JetMETObjects/data/"+mUncertaintyFile+".txt");
98  jecUnc = new JetCorrectionUncertainty(fip.fullPath());
99  }
100  }
101  double jec,rawPt,corPt,eta,unc;
102  TLorentzVector P4;
103  double dEta = (mEtaMax-mEtaMin)/mNGraphPoints;
104  for(int i=0;i<mNHistoPoints;i++)
105  {
106  rawPt = mRandom->Uniform(mPtMin,mPtMax);
107  eta = mRandom->Uniform(mEtaMin,mEtaMax);
108  P4.SetPtEtaPhiE(rawPt,eta,0,0);
109  LorentzVector rawP4(P4.Px(),P4.Py(),P4.Pz(),P4.E());
110  jec = corrector->correction(rawP4);
111  mJECvsEta->Fill(eta,jec);
112  mJECvsPt->Fill(rawPt,jec);
113  }
114  //--------- Pt Graphs ------------------
115  for(unsigned ieta=0;ieta<mVEta.size();ieta++)
116  {
117  double rPt = pow((3500./TMath::CosH(mVEta[ieta]))/mPtMin,1./mNGraphPoints);
118  for(int i=0;i<mNGraphPoints;i++)
119  {
120  rawPt = mPtMin*pow(rPt,i);
121  eta = mVEta[ieta];
122  vpt[ieta][i] = rawPt;
123  P4.SetPtEtaPhiE(rawPt,eta,0,0);
124  LorentzVector rawP4(P4.Px(),P4.Py(),P4.Pz(),P4.E());
125  jec = corrector->correction(rawP4);// the jec is a function of the raw pt
126  vjec_eta[ieta][i] = jec;
127  vptcor[ieta][i] = rawPt*jec;
128  unc = 0.0;
129  if (mUncertaintyTag != "")
130  {
131  jecUnc->setJetEta(eta);
132  jecUnc->setJetPt(rawPt*jec);// the uncertainty is a function of the corrected pt
133  unc = jecUnc->getUncertainty(true);
134  }
135  vjecUnc_eta[ieta][i] = unc*jec;
136  vUnc_eta[ieta][i] = unc;
137  vex_eta[ieta][i] = 0.0;
138  if (mDebug)
139  std::cout<<rawPt<<" "<<eta<<" "<<jec<<" "<<rawPt*jec<<" "<<unc<<std::endl;
140  }
141  }
142  //--------- Eta Graphs -------------------
143  for(unsigned ipt=0;ipt<mVPt.size();ipt++)
144  {
145  for(int i=0;i<mNGraphPoints;i++)
146  {
147  eta = mEtaMin + i*dEta;
148  corPt = mVPt[ipt];
149  veta[ipt][i] = eta;
150  //---------- find the raw pt -----------
151  double e = 1.0;
152  int nLoop(0);
153  rawPt = corPt;
154  while(e > 0.0001 && nLoop < 10)
155  {
156  P4.SetPtEtaPhiE(rawPt,eta,0,0);
157  LorentzVector rawP4(P4.Px(),P4.Py(),P4.Pz(),P4.E());
158  jec = corrector->correction(rawP4);
159  double tmp = rawPt * jec;
160  e = fabs(tmp-corPt)/corPt;
161  if (jec > 0)
162  rawPt = corPt/jec;
163  nLoop++;
164  }
165  //--------- calculate the jec for the rawPt --------
166  P4.SetPtEtaPhiE(rawPt,eta,0,0);
167  LorentzVector rawP4(P4.Px(),P4.Py(),P4.Pz(),P4.E());
168  jec = corrector->correction(rawP4);// the jec is a function of the raw pt
169  vjec_pt[ipt][i] = jec;
170  unc = 0.0;
171  if (mUncertaintyTag != "")
172  {
173  jecUnc->setJetEta(eta);
174  jecUnc->setJetPt(corPt);// the uncertainty is a function of the corrected pt
175  unc = jecUnc->getUncertainty(true);
176  }
177  vjecUnc_pt[ipt][i] = unc*jec;
178  vUnc_pt[ipt][i] = unc;
179  vex_pt[ipt][i] = 0.0;
180  if (mDebug)
181  std::cout<<rawPt<<" "<<eta<<" "<<jec<<" "<<rawPt*jec<<" "<<unc<<std::endl;
182  }
183  }
184 
185 }
186 //---------------------------------------------------------------------------
188 {
189  if (mNGraphPoints > 1000)
190  throw cms::Exception("JetCorrectorDemo","Too many graph points !!! Maximum is 1000 !!!");
191  if (mVEta.size() > 100)
192  throw cms::Exception("JetCorrectorDemo","Too many eta values !!! Maximum is 100 !!!");
193  if (mVPt.size() > 100)
194  throw cms::Exception("JetCorrectorDemo","Too many pt values !!! Maximum is 100 !!!");
195  mJECvsEta = fs->make<TH2F>("JECvsEta","JECvsEta",200,mEtaMin,mEtaMax,100,0,5);
196  mJECvsPt = fs->make<TH2F>("JECvsPt","JECvsPt",200,mPtMin,mPtMax,100,0,5);
197  mRandom = new TRandom();
198  mRandom->SetSeed(0);
199 }
200 //---------------------------------------------------------------------------
202 {
203  char name[1000];
204  for(unsigned ipt=0;ipt<mVPt.size();ipt++)
205  {
206  mVGraphEta[ipt] = fs->make<TGraphErrors>(mNGraphPoints,veta[ipt],vjec_pt[ipt],vex_pt[ipt],vjecUnc_pt[ipt]);
207  sprintf(name,"JEC_vs_Eta_CorPt%1.1f",mVPt[ipt]);
208  mVGraphEta[ipt]->SetName(name);
209  mUncEta[ipt] = fs->make<TGraph>(mNGraphPoints,veta[ipt],vUnc_pt[ipt]);
210  sprintf(name,"UNC_vs_Eta_CorPt%1.1f",mVPt[ipt]);
211  mUncEta[ipt]->SetName(name);
212  }
213  for(unsigned ieta=0;ieta<mVEta.size();ieta++)
214  {
215  mVGraphPt[ieta] = fs->make<TGraphErrors>(mNGraphPoints,vpt[ieta],vjec_eta[ieta],vex_eta[ieta],vjecUnc_eta[ieta]);
216  sprintf(name,"JEC_vs_RawPt_eta%1.1f",mVEta[ieta]);
217  mVGraphPt[ieta]->SetName(name);
218  mVGraphCorPt[ieta] = fs->make<TGraphErrors>(mNGraphPoints,vptcor[ieta],vjec_eta[ieta],vex_eta[ieta],vjecUnc_eta[ieta]);
219  sprintf(name,"JEC_vs_CorPt_eta%1.1f",mVEta[ieta]);
220  mVGraphCorPt[ieta]->SetName(name);
221  mUncCorPt[ieta] = fs->make<TGraph>(mNGraphPoints,vptcor[ieta],vUnc_eta[ieta]);
222  sprintf(name,"UNC_vs_CorPt_eta%1.1f",mVEta[ieta]);
223  mUncCorPt[ieta]->SetName(name);
224  }
225 }
226 //---------------------------------------------------------------------------
227 //define this as a plug-in
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
double vptcor[100][1000]
double vex_pt[100][1000]
double veta[100][1000]
std::string mPayloadName
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< double > mVEta
edm::Service< TFileService > fs
TGraph * mUncEta[100]
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
reco::Particle::LorentzVector LorentzVector
double vjec_pt[100][1000]
~JetCorrectorDemo() override
std::string mUncertaintyTag
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:49
double vUnc_eta[100][1000]
TGraph * mUncCorPt[100]
TGraphErrors * mVGraphPt[100]
JetCorrectorDemo(const edm::ParameterSet &)
TGraphErrors * mVGraphCorPt[100]
double vex_eta[100][1000]
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::string mUncertaintyFile
void endJob() override
double vjecUnc_eta[100][1000]
double vpt[100][1000]
double vjec_eta[100][1000]
void beginJob() override
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
std::vector< double > mVPt
T get() const
Definition: EventSetup.h:71
std::string fullPath() const
Definition: FileInPath.cc:163
float getUncertainty(bool fDirection)
double vjecUnc_pt[100][1000]
double vUnc_pt[100][1000]
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
edm::EDGetTokenT< reco::JetCorrector > mJetCorrector
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
TGraphErrors * mVGraphEta[100]