CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CalibratedPatElectronProducer.cc
Go to the documentation of this file.
1 // This file is imported from:
2 //http://cmssw.cvs.cern.ch/cgi-bin/cmssw.cgi/UserCode/Mangano/WWAnalysis/AnalysisStep/plugins/CalibratedPatElectronProducer.cc?revision=1.2&view=markup
3 
4 
5 // -*- C++ -*-
6 //
7 // Package: EgammaElectronProducers
8 // Class: CalibratedPatElectronProducer
9 //
18 //#if CMSSW_VERSION>500
19 
21 
29 
37 
39 
40 #include <iostream>
41 
42 using namespace edm ;
43 using namespace std ;
44 using namespace reco ;
45 using namespace pat ;
46 
48 // : PatElectronBaseProducer(cfg)
49 {
50  produces<ElectronCollection>();
51 
52  inputPatElectronsToken = consumes<edm::View<reco::Candidate> >(cfg.getParameter<edm::InputTag>("inputPatElectronsTag"));
53  dataset = cfg.getParameter<std::string>("inputDataset");
54  isMC = cfg.getParameter<bool>("isMC");
55  updateEnergyError = cfg.getParameter<bool>("updateEnergyError");
56  lumiRatio = cfg.getParameter<double>("lumiRatio");
57  correctionsType = cfg.getParameter<int>("correctionsType");
58  applyLinearityCorrection = cfg.getParameter<bool>("applyLinearityCorrection");
59  combinationType = cfg.getParameter<int>("combinationType");
60  verbose = cfg.getParameter<bool>("verbose");
61  synchronization = cfg.getParameter<bool>("synchronization");
62  combinationRegressionInputPath = cfg.getParameter<std::string>("combinationRegressionInputPath");
63  scaleCorrectionsInputPath = cfg.getParameter<std::string>("scaleCorrectionsInputPath");
64  linCorrectionsInputPath = cfg.getParameter<std::string>("linearityCorrectionsInputPath");
65  applyExtraHighEnergyProtection = cfg.getParameter<bool>("applyExtraHighEnergyProtection");
66 
67  //basic checks
68  if ( isMC && ( dataset != "Summer11" && dataset != "Fall11"
69  && dataset != "Summer12" && dataset != "Summer12_DR53X_HCP2012"
70  && dataset != "Summer12_LegacyPaper" ) )
71  {
72  throw cms::Exception("CalibratedPATElectronProducer|ConfigError") << "Unknown MC dataset";
73  }
74  if ( !isMC && ( dataset != "Prompt" && dataset != "ReReco"
75  && dataset != "Jan16ReReco" && dataset != "ICHEP2012"
76  && dataset != "Moriond2013" && dataset != "22Jan2013ReReco" ) )
77  {
78  throw cms::Exception("CalibratedPATElectronProducer|ConfigError") << "Unknown Data dataset";
79  }
80 
81  // Linearity correction only applied on combined momentum obtain with regression combination
82  if(combinationType!=3 && applyLinearityCorrection)
83  {
84  std::cout << "[CalibratedElectronProducer] "
85  << "Warning: you chose combinationType!=3 and applyLinearityCorrection=True. Linearity corrections are only applied on top of combination 3." << std::endl;
86  }
87 
88 
89  std::cout << "[CalibratedPATElectronProducer] Correcting scale for dataset " << dataset << std::endl;
90 
91  //initializations
92  std::string pathToDataCorr;
93  switch ( correctionsType )
94  {
95  case 0:
96  break;
97  case 1:
98  if ( verbose )
99  {
100  std::cout << "You choose regression 1 scale corrections" << std::endl;
101  }
102  break;
103  case 2:
104  if ( verbose )
105  {
106  std::cout << "You choose regression 2 scale corrections." << std::endl;
107  }
108  break;
109  case 3:
110  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
111  << "You choose standard non-regression ecal energy scale corrections. They are not implemented yet.";
112  break;
113  default:
114  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
115  << "Unknown correctionsType !!!";
116  }
117 
118  theEnCorrector = new ElectronEnergyCalibrator
119  (
120  edm::FileInPath(scaleCorrectionsInputPath.c_str()).fullPath().c_str(),
121  edm::FileInPath(linCorrectionsInputPath.c_str()).fullPath().c_str(),
122  dataset,
123  correctionsType,
124  applyLinearityCorrection,
125  lumiRatio,
126  isMC,
127  updateEnergyError,
128  verbose,
129  synchronization
130  );
131 
132  if ( verbose )
133  {
134  std::cout << "[CalibratedPATElectronProducer] "
135  << "ElectronEnergyCalibrator object is created " << std::endl;
136  }
137 
138  myEpCombinationTool = new EpCombinationTool();
139  myEpCombinationTool->init
140  (
141  edm::FileInPath(combinationRegressionInputPath.c_str()).fullPath().c_str(),
142  "CombinationWeight"
143  );
144 
145  myCombinator = new ElectronEPcombinator();
146 
147  if ( verbose )
148  {
149  std::cout << "[CalibratedPATElectronProducer] "
150  << "Combination tools are created and initialized " << std::endl;
151  }
152 }
153 
154 
155 
157 {}
158 
160 {
161 
163  event.getByToken(inputPatElectronsToken,oldElectrons) ;
164  std::auto_ptr<ElectronCollection> electrons( new ElectronCollection ) ;
165  ElectronCollection::const_iterator electron ;
166  ElectronCollection::iterator ele ;
167  // first clone the initial collection
168  for
169  (
170  edm::View<reco::Candidate>::const_iterator ele=oldElectrons->begin();
171  ele!=oldElectrons->end();
172  ++ele
173  )
174  {
175  const pat::ElectronRef elecsRef = edm::RefToBase<reco::Candidate>(oldElectrons,ele-oldElectrons->begin()).castTo<pat::ElectronRef>();
176  pat::Electron clone = *edm::RefToBase<reco::Candidate>(oldElectrons,ele-oldElectrons->begin()).castTo<pat::ElectronRef>();
177  electrons->push_back(clone);
178  }
179 
180  if (correctionsType != 0 )
181  {
182  for
183  (
184  ele = electrons->begin();
185  ele != electrons->end() ;
186  ++ele
187  )
188  {
189  int elClass = -1;
190  int run = event.run();
191 
192  float r9 = ele->r9();
193  double correctedEcalEnergy = ele->correctedEcalEnergy();
194  double correctedEcalEnergyError = ele->correctedEcalEnergyError();
195  double trackMomentum = ele->trackMomentumAtVtx().R();
196  double trackMomentumError = ele->trackMomentumError();
197  double combinedMomentum = ele->p();
198  double combinedMomentumError = 0;
199  if ( ele->candidateP4Kind() != GsfElectron::P4_UNKNOWN )
200  {
201  combinedMomentumError = ele->p4Error(ele->candidateP4Kind());
202  }
203  // FIXME : p4Error not filled for pure tracker electrons
204  // Recompute it using the parametrization implemented in
205  // RecoEgamma/EgammaElectronAlgos/src/ElectronEnergyCorrector.cc::simpleParameterizationUncertainty()
206  if( !ele->ecalDrivenSeed() )
207  {
208  double error = 999. ;
209  double momentum = (combinedMomentum<15. ? 15. : combinedMomentum);
210  if ( ele->isEB() )
211  {
212  float parEB[3] = { 5.24e-02, 2.01e-01, 1.00e-02} ;
213  error = momentum * sqrt( pow(parEB[0]/sqrt(momentum),2) + pow(parEB[1]/momentum,2) + pow(parEB[2],2) );
214  }
215  else if ( ele->isEE() )
216  {
217  float parEE[3] = { 1.46e-01, 9.21e-01, 1.94e-03} ;
218  error = momentum * sqrt( pow(parEE[0]/sqrt(momentum),2) + pow(parEE[1]/momentum,2) + pow(parEE[2],2) );
219  }
220  combinedMomentumError = error;
221  }
222 
223  if (ele->classification() == reco::GsfElectron::GOLDEN) {elClass = 0;}
224  if (ele->classification() == reco::GsfElectron::BIGBREM) {elClass = 1;}
225  if (ele->classification() == reco::GsfElectron::BADTRACK) {elClass = 2;}
226  if (ele->classification() == reco::GsfElectron::SHOWERING) {elClass = 3;}
227  if (ele->classification() == reco::GsfElectron::GAP) {elClass = 4;}
228 
229  SimpleElectron mySimpleElectron
230  (
231  run,
232  elClass,
233  r9,
234  correctedEcalEnergy,
235  correctedEcalEnergyError,
236  trackMomentum,
237  trackMomentumError,
238  ele->ecalRegressionEnergy(),
239  ele->ecalRegressionError(),
240  combinedMomentum,
241  combinedMomentumError,
242  ele->superCluster()->eta(),
243  ele->isEB(),
244  isMC,
245  ele->ecalDriven(),
246  ele->trackerDrivenSeed()
247  );
248 
249  // energy calibration for ecalDriven electrons
250  if ( ele->core()->ecalDrivenSeed() || correctionsType==2 || combinationType==3 )
251  {
252  theEnCorrector->calibrate(mySimpleElectron, event.streamID());
253 
254  // E-p combination
255 
256  switch ( combinationType )
257  {
258  case 0:
259  if ( verbose )
260  {
261  std::cout << "[CalibratedPATElectronProducer] "
262  << "You choose not to combine." << std::endl;
263  }
264  break;
265  case 1:
266  if ( verbose )
267  {
268  std::cout << "[CalibratedPATElectronProducer] "
269  << "You choose corrected regression energy for standard combination" << std::endl;
270  }
271  myCombinator->setCombinationMode(1);
272  myCombinator->combine(mySimpleElectron);
273  break;
274  case 2:
275  if ( verbose )
276  {
277  std::cout << "[CalibratedPATElectronProducer] "
278  << "You choose uncorrected regression energy for standard combination" << std::endl;
279  }
280  myCombinator->setCombinationMode(2);
281  myCombinator->combine(mySimpleElectron);
282  break;
283  case 3:
284  if ( verbose )
285  {
286  std::cout << "[CalibratedPATElectronProducer] "
287  << "You choose regression combination." << std::endl;
288  }
289  myEpCombinationTool->combine(mySimpleElectron, applyExtraHighEnergyProtection);
290  theEnCorrector->correctLinearity(mySimpleElectron);
291  break;
292  default:
293  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
294  << "Unknown combination Type !!!" ;
295  }
296 
297  math::XYZTLorentzVector oldMomentum = ele->p4() ;
298  math::XYZTLorentzVector newMomentum_ ;
299  newMomentum_ = math::XYZTLorentzVector
300  ( oldMomentum.x()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
301  oldMomentum.y()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
302  oldMomentum.z()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
303  mySimpleElectron.getCombinedMomentum() ) ;
304 
305  ele->correctMomentum
306  (
307  newMomentum_,
308  mySimpleElectron.getTrackerMomentumError(),
309  mySimpleElectron.getCombinedMomentumError()
310  );
311 
312  if ( verbose )
313  {
314  std::cout << "[CalibratedPATElectronProducer] Combined momentum after saving "
315  << ele->p4().t() << std::endl;
316  }
317  }// end of if (ele.core()->ecalDrivenSeed())
318  }// end of loop on electrons
319  } else
320  {
321  if ( verbose )
322  {
323  std::cout << "[CalibratedPATElectronProducer] "
324  << "You choose not to correct. Uncorrected Regression Energy is taken." << std::endl;
325  }
326  }
327  // Save the electrons
328  event.put(electrons) ;
329 }
330 
T getParameter(std::string const &) const
tuple cfg
Definition: looper.py:293
bool verbose
virtual void produce(edm::Event &, const edm::EventSetup &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
CalibratedPatElectronProducer(const edm::ParameterSet &)
T sqrt(T t)
Definition: SSEVec.h:48
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::vector< Electron > ElectronCollection
collectin of Electron objects
Definition: ElectronFwd.h:9
tuple dataset
Definition: dataset.py:859
Analysis-level electron class.
Definition: Electron.h:52
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:85
StreamID streamID() const
Definition: Event.h:79
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
tuple cout
Definition: gather_cfg.py:121
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40