CMS 3D CMS Logo

L1ECALPrefiringWeightProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ProdTutorial/L1ECALPrefiringWeightProducer
4 // Class: L1ECALPrefiringWeightProducer
5 //
13 //
14 // Original Author: localusers user
15 // Created: Thu, 08 Nov 2018 16:16:00 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
25 
28 
31 
34 #include "TH2.h"
35 
36 #include <iostream>
38 
40 public:
43 
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45 
46 private:
47  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
48 
49  double getPrefiringRate( double eta, double pt, TH2F * h_prefmap, fluctuations fluctuation) const ;
50 
53 
55  TH2F * h_prefmap_jet;
57  bool useEMpt_;
60 };
61 
62 
64 {
65  photons_token_ = consumes<std::vector<pat::Photon> >( iConfig.getParameter<edm::InputTag>( "ThePhotons" ) );
66  jets_token_ = consumes<std::vector<pat::Jet> >( iConfig.getParameter<edm::InputTag>( "TheJets" ) );
67 
68  dataera_ = iConfig.getParameter<std::string>( "DataEra" ) ;
69  useEMpt_ = iConfig.getParameter<bool>( "UseJetEMPt" ) ;
70  prefiringRateSystUnc_ = iConfig.getParameter<double>( "PrefiringRateSystematicUncty" ) ;
71  skipwarnings_ = iConfig.getParameter<bool>( "SkipWarnings" ) ;
72 
73  TFile * file_prefiringmaps_;
74  std::string fname = iConfig.getParameter<std::string>( "L1Maps" );
75  edm::FileInPath mapsfilepath("PhysicsTools/PatUtils/data/"+fname);
76  file_prefiringmaps_ = new TFile(mapsfilepath.fullPath().c_str(),"read" );
77  if(file_prefiringmaps_ == nullptr && !skipwarnings_)
78  std::cout << "File with maps not found. All prefiring weights set to 0. " <<std::endl;
79 
80  TString mapphotonfullname= "L1prefiring_photonptvseta_"+ dataera_;
81  if(!file_prefiringmaps_->Get(mapphotonfullname)&& !skipwarnings_)
82  std::cout << "Photon map not found. All photons prefiring weights set to 0. " <<std::endl;
83  h_prefmap_photon =(TH2F*) file_prefiringmaps_->Get(mapphotonfullname);
84 
85  TString mapjetfullname= (useEMpt_) ? "L1prefiring_jetemptvseta_"+ dataera_ : "L1prefiring_jetptvseta_"+ dataera_ ;
86  if(!file_prefiringmaps_->Get(mapjetfullname)&& !skipwarnings_)
87  std::cout << "Jet map not found. All jets prefiring weights set to 0. "<< std::endl ;
88  h_prefmap_jet =(TH2F*) file_prefiringmaps_->Get(mapjetfullname);
89  file_prefiringmaps_->Close();
90  delete file_prefiringmaps_;
91  produces<double>( "nonPrefiringProb" ).setBranchAlias( "nonPrefiringProb");
92  produces<double>( "nonPrefiringProbUp" ).setBranchAlias( "nonPrefiringProbUp");
93  produces<double>( "nonPrefiringProbDown" ).setBranchAlias( "nonPrefiringProbDown");
94 
95 }
96 
98 {
99  delete h_prefmap_photon;
100  delete h_prefmap_jet;
101 }
102 
103 void
105 {
106  using namespace edm;
107 
108  //Photons
110  iEvent.getByToken(photons_token_,thePhotons);
111 
112  //Jets
114  iEvent.getByToken(jets_token_,theJets );
115 
116  //Probability for the event NOT to prefire, computed with the prefiring maps per object.
117  //Up and down values correspond to the resulting value when shifting up/down all prefiring rates in prefiring maps.
118  double nonPrefiringProba[3]={1.,1.,1.};//0: central, 1: up, 2: down
119 
120  for ( const auto fluct : { fluctuations::central, fluctuations::up, fluctuations::down } ) {
121  for ( const auto & photon : *thePhotons ) {
122  double pt_gam= photon.pt();
123  double eta_gam= photon.eta();
124  if( pt_gam < 20.) continue;
125  if( fabs(eta_gam) <2.) continue;
126  if( fabs(eta_gam) >3.) continue;
127  double prefiringprob_gam= getPrefiringRate( eta_gam, pt_gam, h_prefmap_photon, fluct);
128  nonPrefiringProba[fluct] *= (1.-prefiringprob_gam);
129  }
130 
131  //Now applying the prefiring maps to jets in the affected regions.
132  for ( const auto &jet: *theJets ) {
133 
134  double pt_jet= jet.pt();
135  double eta_jet= jet.eta();
136  double phi_jet= jet.phi();
137  if( pt_jet < 20.) continue;
138  if( fabs(eta_jet) <2.) continue;
139  if( fabs(eta_jet) >3.) continue;
140 
141  //Loop over photons to remove overlap
142  double nonprefiringprobfromoverlappingphotons =1.;
143  for ( const auto & photon : *thePhotons ) {
144  double pt_gam= photon.pt();
145  double eta_gam= photon.eta();
146  double phi_gam= photon.phi();
147  if( pt_gam < 20.) continue;
148  if( fabs(eta_gam) <2.) continue;
149  if( fabs(eta_gam) >3.) continue;
150  double dR = reco::deltaR( eta_jet,phi_jet,eta_gam,phi_gam );
151  if(dR>0.4)continue;
152  double prefiringprob_gam = getPrefiringRate( eta_gam, pt_gam, h_prefmap_photon , fluct);
153  nonprefiringprobfromoverlappingphotons *= (1.-prefiringprob_gam) ;
154  }
155  //useEMpt =true if one wants to use maps parametrized vs Jet EM pt instead of pt.
156  if (useEMpt_)
157  pt_jet *= (jet.neutralEmEnergyFraction()+jet.chargedEmEnergyFraction());
158  double nonprefiringprobfromoverlappingjet = 1. - getPrefiringRate( eta_jet, pt_jet , h_prefmap_jet , fluct);
159 
160  if(nonprefiringprobfromoverlappingphotons ==1.){
161  nonPrefiringProba[fluct] *= nonprefiringprobfromoverlappingjet ;
162  }
163  //If overlapping photons have a non prefiring rate larger than the jet, then replace these weights by the jet one
164  else if(nonprefiringprobfromoverlappingphotons > nonprefiringprobfromoverlappingjet ) {
165  if(nonprefiringprobfromoverlappingphotons !=0.) {
166  nonPrefiringProba[fluct] *= nonprefiringprobfromoverlappingjet / nonprefiringprobfromoverlappingphotons;
167  }
168  else{
169  nonPrefiringProba[fluct] = 0.;
170  }
171  }
172  //Last case: if overlapping photons have a non prefiring rate smaller than the jet, don't consider the jet in the event weight, and do nothing.
173  }
174  }
175 
176  auto nonPrefiringProb =std::make_unique <double> ( nonPrefiringProba[0]);
177  auto nonPrefiringProbUp =std::make_unique <double> ( nonPrefiringProba[1]);
178  auto nonPrefiringProbDown =std::make_unique <double> ( nonPrefiringProba[2]);
179  iEvent.put( std::move(nonPrefiringProb), "nonPrefiringProb" );
180  iEvent.put( std::move(nonPrefiringProbUp), "nonPrefiringProbUp" );
181  iEvent.put( std::move(nonPrefiringProbDown), "nonPrefiringProbDown" );
182 
183 }
184 
185 
186 double L1ECALPrefiringWeightProducer::getPrefiringRate( double eta, double pt, TH2F * h_prefmap, fluctuations fluctuation) const{
187 
188  if(h_prefmap==nullptr&& !skipwarnings_)std::cout << "Prefiring map not found, setting prefiring rate to 0 " <<std::endl;
189  if(h_prefmap==nullptr ) return 0.;
190  //Check pt is not above map overflow
191  int nbinsy = h_prefmap->GetNbinsY();
192  double maxy= h_prefmap->GetYaxis()->GetBinLowEdge(nbinsy+1);
193  if(pt>=maxy) pt = maxy-0.01;
194  int thebin= h_prefmap->FindBin(eta,pt);
195 
196  double prefrate = h_prefmap->GetBinContent(thebin);
197 
198  double statuncty = h_prefmap->GetBinError(thebin) ;
199  double systuncty = prefiringRateSystUnc_*prefrate;
200 
201  if(fluctuation == up) prefrate = std::min(1.,prefrate+sqrt(pow(statuncty,2)+pow(systuncty,2)));
202  if(fluctuation == down) prefrate = std::max(0.,prefrate-sqrt(pow(statuncty,2)+pow(systuncty,2)));
203  return prefrate;
204 
205 }
206 
207 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
208 void
210 
212 
213  desc.add<edm::InputTag>("ThePhotons", edm::InputTag("slimmedPhotons"));
214  desc.add<edm::InputTag>("TheJets", edm::InputTag("slimmedJets"));
215  desc.add<std::string>("L1Maps","L1PrefiringMaps.root");
216  desc.add<std::string>("DataEra", "2017BtoF");
217  desc.add<bool>("UseJetEMPt", false);
218  desc.add<double>("PrefiringRateSystematicUncty",0.2);
219  desc.add<bool>("SkipWarnings", true);
220  descriptions.add("l1ECALPrefiringWeightProducer", desc);
221 
222 }
223 
224 //define this as a plug-in
226 
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
double getPrefiringRate(double eta, double pt, TH2F *h_prefmap, fluctuations fluctuation) const
int iEvent
Definition: GenABIO.cc:230
T sqrt(T t)
Definition: SSEVec.h:18
auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
L1ECALPrefiringWeightProducer(const edm::ParameterSet &)
edm::EDGetTokenT< std::vector< pat::Photon > > photons_token_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
string fname
main script
HLT enums.
edm::EDGetTokenT< std::vector< pat::Jet > > jets_token_
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:510