CMS 3D CMS Logo

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