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 
66  //basic checks
67  if ( isMC && ( dataset != "Summer11" && dataset != "Fall11"
68  && dataset != "Summer12" && dataset != "Summer12_DR53X_HCP2012"
69  && dataset != "Summer12_LegacyPaper" ) )
70  {
71  throw cms::Exception("CalibratedPATElectronProducer|ConfigError") << "Unknown MC dataset";
72  }
73  if ( !isMC && ( dataset != "Prompt" && dataset != "ReReco"
74  && dataset != "Jan16ReReco" && dataset != "ICHEP2012"
75  && dataset != "Moriond2013" && dataset != "22Jan2013ReReco" ) )
76  {
77  throw cms::Exception("CalibratedPATElectronProducer|ConfigError") << "Unknown Data dataset";
78  }
79 
80  // Linearity correction only applied on combined momentum obtain with regression combination
81  if(combinationType!=3 && applyLinearityCorrection)
82  {
83  std::cout << "[CalibratedElectronProducer] "
84  << "Warning: you chose combinationType!=3 and applyLinearityCorrection=True. Linearity corrections are only applied on top of combination 3." << std::endl;
85  }
86 
87 
88  std::cout << "[CalibratedPATElectronProducer] Correcting scale for dataset " << dataset << std::endl;
89 
90  //initializations
91  std::string pathToDataCorr;
92  switch ( correctionsType )
93  {
94  case 0:
95  break;
96  case 1:
97  if ( verbose )
98  {
99  std::cout << "You choose regression 1 scale corrections" << std::endl;
100  }
101  break;
102  case 2:
103  if ( verbose )
104  {
105  std::cout << "You choose regression 2 scale corrections." << std::endl;
106  }
107  break;
108  case 3:
109  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
110  << "You choose standard non-regression ecal energy scale corrections. They are not implemented yet.";
111  break;
112  default:
113  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
114  << "Unknown correctionsType !!!";
115  }
116 
117  theEnCorrector = new ElectronEnergyCalibrator
118  (
119  edm::FileInPath(scaleCorrectionsInputPath.c_str()).fullPath().c_str(),
120  edm::FileInPath(linCorrectionsInputPath.c_str()).fullPath().c_str(),
121  dataset,
122  correctionsType,
123  applyLinearityCorrection,
124  lumiRatio,
125  isMC,
126  updateEnergyError,
127  verbose,
128  synchronization
129  );
130 
131  if ( verbose )
132  {
133  std::cout << "[CalibratedPATElectronProducer] "
134  << "ElectronEnergyCalibrator object is created " << std::endl;
135  }
136 
137  myEpCombinationTool = new EpCombinationTool();
138  myEpCombinationTool->init
139  (
140  edm::FileInPath(combinationRegressionInputPath.c_str()).fullPath().c_str(),
141  "CombinationWeight"
142  );
143 
144  myCombinator = new ElectronEPcombinator();
145 
146  if ( verbose )
147  {
148  std::cout << "[CalibratedPATElectronProducer] "
149  << "Combination tools are created and initialized " << std::endl;
150  }
151 }
152 
153 
154 
156 {}
157 
159 {
160 
162  event.getByToken(inputPatElectronsToken,oldElectrons) ;
163  std::auto_ptr<ElectronCollection> electrons( new ElectronCollection ) ;
164  ElectronCollection::const_iterator electron ;
165  ElectronCollection::iterator ele ;
166  // first clone the initial collection
167  for
168  (
169  edm::View<reco::Candidate>::const_iterator ele=oldElectrons->begin();
170  ele!=oldElectrons->end();
171  ++ele
172  )
173  {
174  const pat::ElectronRef elecsRef = edm::RefToBase<reco::Candidate>(oldElectrons,ele-oldElectrons->begin()).castTo<pat::ElectronRef>();
175  pat::Electron clone = *edm::RefToBase<reco::Candidate>(oldElectrons,ele-oldElectrons->begin()).castTo<pat::ElectronRef>();
176  electrons->push_back(clone);
177  }
178 
179  if (correctionsType != 0 )
180  {
181  for
182  (
183  ele = electrons->begin();
184  ele != electrons->end() ;
185  ++ele
186  )
187  {
188  int elClass = -1;
189  int run = event.run();
190 
191  float r9 = ele->r9();
192  double correctedEcalEnergy = ele->correctedEcalEnergy();
193  double correctedEcalEnergyError = ele->correctedEcalEnergyError();
194  double trackMomentum = ele->trackMomentumAtVtx().R();
195  double trackMomentumError = ele->trackMomentumError();
196  double combinedMomentum = ele->p();
197  double combinedMomentumError = 0;
198  if ( ele->candidateP4Kind() != GsfElectron::P4_UNKNOWN )
199  {
200  combinedMomentumError = ele->p4Error(ele->candidateP4Kind());
201  }
202  // FIXME : p4Error not filled for pure tracker electrons
203  // Recompute it using the parametrization implemented in
204  // RecoEgamma/EgammaElectronAlgos/src/ElectronEnergyCorrector.cc::simpleParameterizationUncertainty()
205  if( !ele->ecalDrivenSeed() )
206  {
207  double error = 999. ;
208  double momentum = (combinedMomentum<15. ? 15. : combinedMomentum);
209  if ( ele->isEB() )
210  {
211  float parEB[3] = { 5.24e-02, 2.01e-01, 1.00e-02} ;
212  error = momentum * sqrt( pow(parEB[0]/sqrt(momentum),2) + pow(parEB[1]/momentum,2) + pow(parEB[2],2) );
213  }
214  else if ( ele->isEE() )
215  {
216  float parEE[3] = { 1.46e-01, 9.21e-01, 1.94e-03} ;
217  error = momentum * sqrt( pow(parEE[0]/sqrt(momentum),2) + pow(parEE[1]/momentum,2) + pow(parEE[2],2) );
218  }
219  combinedMomentumError = error;
220  }
221 
222  if (ele->classification() == reco::GsfElectron::GOLDEN) {elClass = 0;}
223  if (ele->classification() == reco::GsfElectron::BIGBREM) {elClass = 1;}
224  if (ele->classification() == reco::GsfElectron::BADTRACK) {elClass = 2;}
225  if (ele->classification() == reco::GsfElectron::SHOWERING) {elClass = 3;}
226  if (ele->classification() == reco::GsfElectron::GAP) {elClass = 4;}
227 
228  SimpleElectron mySimpleElectron
229  (
230  run,
231  elClass,
232  r9,
233  correctedEcalEnergy,
234  correctedEcalEnergyError,
235  trackMomentum,
236  trackMomentumError,
237  ele->ecalRegressionEnergy(),
238  ele->ecalRegressionError(),
239  combinedMomentum,
240  combinedMomentumError,
241  ele->superCluster()->eta(),
242  ele->isEB(),
243  isMC,
244  ele->ecalDriven(),
245  ele->trackerDrivenSeed()
246  );
247 
248  // energy calibration for ecalDriven electrons
249  if ( ele->core()->ecalDrivenSeed() || correctionsType==2 || combinationType==3 )
250  {
251  theEnCorrector->calibrate(mySimpleElectron, event.streamID());
252 
253  // E-p combination
254 
255  switch ( combinationType )
256  {
257  case 0:
258  if ( verbose )
259  {
260  std::cout << "[CalibratedPATElectronProducer] "
261  << "You choose not to combine." << std::endl;
262  }
263  break;
264  case 1:
265  if ( verbose )
266  {
267  std::cout << "[CalibratedPATElectronProducer] "
268  << "You choose corrected regression energy for standard combination" << std::endl;
269  }
270  myCombinator->setCombinationMode(1);
271  myCombinator->combine(mySimpleElectron);
272  break;
273  case 2:
274  if ( verbose )
275  {
276  std::cout << "[CalibratedPATElectronProducer] "
277  << "You choose uncorrected regression energy for standard combination" << std::endl;
278  }
279  myCombinator->setCombinationMode(2);
280  myCombinator->combine(mySimpleElectron);
281  break;
282  case 3:
283  if ( verbose )
284  {
285  std::cout << "[CalibratedPATElectronProducer] "
286  << "You choose regression combination." << std::endl;
287  }
288  myEpCombinationTool->combine(mySimpleElectron);
289  theEnCorrector->correctLinearity(mySimpleElectron);
290  break;
291  default:
292  throw cms::Exception("CalibratedPATElectronProducer|ConfigError")
293  << "Unknown combination Type !!!" ;
294  }
295 
296  math::XYZTLorentzVector oldMomentum = ele->p4() ;
297  math::XYZTLorentzVector newMomentum_ ;
298  newMomentum_ = math::XYZTLorentzVector
299  ( oldMomentum.x()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
300  oldMomentum.y()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
301  oldMomentum.z()*mySimpleElectron.getCombinedMomentum()/oldMomentum.t(),
302  mySimpleElectron.getCombinedMomentum() ) ;
303 
304  ele->correctMomentum
305  (
306  newMomentum_,
307  mySimpleElectron.getTrackerMomentumError(),
308  mySimpleElectron.getCombinedMomentumError()
309  );
310 
311  if ( verbose )
312  {
313  std::cout << "[CalibratedPATElectronProducer] Combined momentum after saving "
314  << ele->p4().t() << std::endl;
315  }
316  }// end of if (ele.core()->ecalDrivenSeed())
317  }// end of loop on electrons
318  } else
319  {
320  if ( verbose )
321  {
322  std::cout << "[CalibratedPATElectronProducer] "
323  << "You choose not to correct. Uncorrected Regression Energy is taken." << std::endl;
324  }
325  }
326  // Save the electrons
327  event.put(electrons) ;
328 }
329 
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:18
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:81
StreamID streamID() const
Definition: Event.h:80
const_iterator begin() const
first daughter const_iterator
Definition: Candidate.h:144
tuple cout
Definition: gather_cfg.py:145
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