CMS 3D CMS Logo

ElectronRecalibSuperClusterAssociator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 //
4 
5 // user include files
7 //#include "DataFormats/EgammaReco/interface/SuperCluster.h"
8 //#include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
10 
11 //#include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
12 //#include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
13 //#include "DataFormats/EgammaCandidates/interface/GsfElectronCoreFwd.h"
14 //#include "DataFormats/EgammaCandidates/interface/GsfElectronCore.h"
16 #include <iostream>
19 //#define DEBUG
20 
21 using namespace reco;
22 using namespace edm;
23 
24 
26 {
27 #ifdef DEBUG
28  std::cout<< "ElectronRecalibSuperClusterAssociator::ElectronRecalibSuperClusterAssociator" << std::endl;
29 #endif
30 
31  //register your products
32  produces<GsfElectronCollection>();
33  produces<GsfElectronCoreCollection>() ;
34  // produces<SuperClusterCollection>();
35 
36  superClusterCollectionEB_ = iConfig.getParameter<edm::InputTag > ("superClusterCollectionEB");
37  superClusterCollectionEE_ = iConfig.getParameter<edm::InputTag > ("superClusterCollectionEE");
38 
39  outputLabel_ = iConfig.getParameter<std::string>("outputLabel");
40  electronSrc_ = iConfig.getParameter<edm::InputTag > ("electronSrc");
41 
42  electronToken_ = consumes<reco::GsfElectronCollection>(electronSrc_);
43  ebScToken_ = consumes<reco::SuperClusterCollection>(superClusterCollectionEB_);
44  eeScToken_ = consumes<reco::SuperClusterCollection>(superClusterCollectionEE_);
45 
46 #ifdef DEBUG
47  std::cout<< "ElectronRecalibSuperClusterAssociator::ElectronRecalibSuperClusterAssociator::end" << std::endl;
48 #endif
49 }
50 
52 {
53 }
54 
55 // ------------ method called to produce the data ------------
57 {
58 #ifdef DEBUG
59  std::cout<< "GEDElectronRecalibSuperClusterAssociator::produce" << std::endl;
60 #endif
61 
62  // Create the output collections
63  auto pOutEle = std::make_unique<GsfElectronCollection>();
64  auto pOutEleCore = std::make_unique<GsfElectronCoreCollection>();
65 
66  // Get SuperClusters in EB
67  Handle<reco::SuperClusterCollection> superClusterEBHandle;
68  e.getByToken(ebScToken_, superClusterEBHandle);
69  //const reco::SuperClusterCollection* scCollection = superClusterEBHandle.product();
70 
71 #ifdef DEBUG
72  std::cout<<"EB scCollection->size()" << superClusterEBHandle->size()<<std::endl;
73 #endif
74 
75  // Get SuperClusters in EE
76  Handle<reco::SuperClusterCollection> superClusterEEHandle;
77  e.getByToken(eeScToken_, superClusterEEHandle);
78  // const reco::SuperClusterCollection* eeScCollection = superClusterEEHandle.product();
79 
80 #ifdef DEBUG
81  std::cout<<"EE scCollection->size()" << superClusterEEHandle->size() << std::endl;
82 #endif
83 
84  // Get Electrons
86  e.getByToken(electronToken_, eleHandle);
87  // const reco::GsfElectronCollection* electronCollection = eleHandle.product();
88 
89  // GsfElectronCoreRefProd rEleCore = const_cast<edm::Event&>(iEvent).getRefBeforePut<GsfElectronCoreCollection>();
92 
93  for(reco::GsfElectronCollection::const_iterator eleIt = eleHandle->begin(); eleIt != eleHandle->end(); eleIt++)
94  {
95  float DeltaRMineleSCbarrel(0.15); //initial minDeltaR
96  float DeltaRMineleSCendcap(0.15);
97  const reco::SuperCluster* nearestSCbarrel=nullptr;
98  const reco::SuperCluster* nearestSCendcap=nullptr;
99  int iscRef=-1, iscRefendcap=-1;
100  int iSC=0;
101 
102  if(eleIt->trackerDrivenSeed()){
103  edm::LogError("trackerDriven") << "skipping trackerDriven electrons";
104  continue;
105  }
106  // first loop is on EB superClusters
107  iSC=0;
108  for(reco::SuperClusterCollection::const_iterator scIt = superClusterEBHandle->begin();
109  scIt != superClusterEBHandle->end(); scIt++, iSC++){
110 
111  double DeltaReleSC = sqrt(reco::deltaR2(eleIt->eta(), eleIt->phi(),
112  scIt->eta(), scIt->phi()));
113 
114  if(DeltaReleSC<DeltaRMineleSCbarrel) //save the nearest SC
115  {
116  DeltaRMineleSCbarrel = DeltaReleSC;
117  nearestSCbarrel = &*scIt;
118  iscRef = iSC;
119  }
120 #ifdef DEBUG
121  std::cout << "EB: " << scIt - superClusterEBHandle->begin() << " " << iSC << " " << iscRef
122  << "\t" << std::setprecision(4) << scIt->energy()
123  << " " << scIt->eta() << " " << scIt->phi()
124  << "\t--\t" << eleIt->energy() << " " << eleIt->eta() << " " << eleIt->phi()
125  << "\t" << DeltaRMineleSCbarrel
126  << std::endl;
127 #endif
128  }
129 
130  // second loop is on EE superClusters
131  iSC=0;
132  for(reco::SuperClusterCollection::const_iterator scIt = superClusterEEHandle->begin();
133  scIt != superClusterEEHandle->end(); scIt++, iSC++){
134 #ifdef DEBUG
135  std::cout << "EE: " << scIt - superClusterEEHandle->begin() << " " << iSC << " " << iscRef
136  << "\t" << std::setprecision(4) << scIt->energy()
137  << " " << scIt->eta() << " " << scIt->phi()
138  << "\t--\t " << eleIt->energy() << " " << eleIt->eta() << " " << eleIt->phi()
139  << "\t" << DeltaRMineleSCendcap
140  << std::endl;
141 #endif
142 
143  double DeltaReleSC = sqrt(reco::deltaR2(eleIt->eta(), eleIt->phi(),
144  scIt->eta(), scIt->phi()));
145 
146  if(DeltaReleSC<DeltaRMineleSCendcap)
147  {
148  DeltaRMineleSCendcap = DeltaReleSC;
149  nearestSCendcap = &*scIt;
150  iscRefendcap = iSC;
151  }
152  }
154  // if(eleIt->isEB()) assert(DeltaRMineleSCbarrel < DeltaRMineleSCendcap);
155  //else assert(DeltaRMineleSCbarrel > DeltaRMineleSCendcap);
156  if(eleIt->isEB() && DeltaRMineleSCbarrel > DeltaRMineleSCendcap){
157  edm::LogError("ElectronRecalibAssociator") << "EB electron, but nearest SC is in EE";;
158  continue;
159  }
160 
161  if(eleIt->isEB() && nearestSCbarrel){
162  pOutEleCore->push_back(*eleIt->core()); // clone the old core and add to the collection of new cores
163  reco::GsfElectronCoreRef newEleCoreRef(rEleCore, idxEleCore++); // reference to the new electron core in the new collection
164  reco::GsfElectronCore & newEleCore = pOutEleCore->back(); // pick the clone
165  //newEleCore.setGsfTrack(eleIt->gsfTrack()); // set the gsf track (not needed since it is not changed)
166  reco::SuperClusterRef scRef(reco::SuperClusterRef(superClusterEBHandle, iscRef)); // Reference to the new SC
167 #ifndef CMSSW_5_3_X
168  newEleCore.setParentSuperCluster(scRef); // mustache
169 #endif
170  newEleCore.setSuperCluster(scRef); // let's check this! if it is possible to recreate the pfSC
171 
172  pOutEle->push_back(reco::GsfElectron(*eleIt,newEleCoreRef));
173  reco::GsfElectron& newEle = pOutEle->back();
174 
175  //-- first possibility: set the new p4SC using refined SC
178  eleIt->p4Error(reco::GsfElectron::P4_FROM_SUPER_CLUSTER), false); //*newEle.superCluster()->energy()/eleIt->superCluster()->energy());
179 
180  //-- second possibility: set the new p4SC using mustache SC
181  //newEle.setP4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER, eleIt->p4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER)*newEle.parentSuperCluster()->energy()/eleIt->parentSuperCluster()->energy(), eleIt->p4Error(reco::GsfElectron::P4_FROM_SUPER_CLUSTER), false);
182 
183  //-- update the correctedEcalEnergy
184  newEle.setCorrectedEcalEnergy(eleIt->ecalEnergy()*(scRef->energy()/eleIt->p4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER).energy()));
185  newEle.setCorrectedEcalEnergyError(eleIt->ecalEnergyError()*(scRef->energy()/eleIt->ecalEnergy()));
186 
187  } else if(!(eleIt->isEB()) && nearestSCendcap)
188  {
189  pOutEleCore->push_back(*eleIt->core()); // clone the old core and add to the collection of new cores
190  reco::GsfElectronCoreRef newEleCoreRef(rEleCore, idxEleCore++); // reference to the new electron core in the new collection
191  reco::GsfElectronCore & newEleCore = pOutEleCore->back(); // pick the clone
192  //newEleCore.setGsfTrack(eleIt->gsfTrack()); // set the gsf track (not needed since it is not changed)
193  reco::SuperClusterRef scRef(reco::SuperClusterRef(superClusterEEHandle, iscRefendcap)); // Reference to the new SC
194 #ifndef CMSSW_5_3_X
195  newEleCore.setParentSuperCluster(scRef); // mustache
196 #endif
197  newEleCore.setSuperCluster(scRef); // let's check this! if it is possible to recreate the pfSC
198 
199  pOutEle->push_back(reco::GsfElectron(*eleIt,newEleCoreRef));
200  reco::GsfElectron& newEle = pOutEle->back();
201 
202  //-- first possibility: set the new p4SC using refined SC
205  eleIt->p4Error(reco::GsfElectron::P4_FROM_SUPER_CLUSTER), false); //*newEle.superCluster()->energy()/eleIt->superCluster()->energy());
206 
207  //-- second possibility: set the new p4SC using mustache SC
208  //newEle.setP4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER, eleIt->p4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER)*newEle.parentSuperCluster()->energy()/eleIt->parentSuperCluster()->energy(), eleIt->p4Error(reco::GsfElectron::P4_FROM_SUPER_CLUSTER), false);
209 
210  //-- update the correctedEcalEnergy
211  newEle.setCorrectedEcalEnergy(eleIt->ecalEnergy()*(scRef->energy()/eleIt->p4(reco::GsfElectron::P4_FROM_SUPER_CLUSTER).energy()));
212  newEle.setCorrectedEcalEnergyError(eleIt->ecalEnergyError()*(scRef->energy()/eleIt->ecalEnergy()));
213  }else{
214  edm::LogError("Failed SC association") << "No SC to be associated to the electron";
215  }
216  }
217 
218 
219 
220 #ifdef DEBUG
221  std::cout << "Filled new electrons " << pOutEle->size() << std::endl;
222  std::cout << "Filled new electronsCore " << pOutEleCore->size() << std::endl;
223  // std::cout << "Filled new endcapSC " << pOutNewEndcapSC->size() << std::endl;
224 #endif
225 
226  // put result into the Event
227 
228  e.put(std::move(pOutEle));
229  e.put(std::move(pOutEleCore));
230 
231  // e.put(std::move(pOutNewEndcapSC));
232 
233 }
234 
T getParameter(std::string const &) const
void setP4(P4Kind kind, const LorentzVector &p4, float p4Error, bool setCandidate)
Definition: GsfElectron.cc:199
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:169
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
ElectronRecalibSuperClusterAssociator(const edm::ParameterSet &conf)
void setSuperCluster(const SuperClusterRef &scl)
void setCorrectedEcalEnergyError(float newEnergyError)
Definition: GsfElectron.cc:179
void setParentSuperCluster(const SuperClusterRef &scl)
T sqrt(T t)
Definition: SSEVec.h:18
std::vector< GsfElectronCore > GsfElectronCoreCollection
RefProd< PROD > getRefBeforePut()
Definition: Event.h:156
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
void setCorrectedEcalEnergy(float newEnergy)
Definition: GsfElectron.cc:182
fixed size matrix
HLT enums.
void produce(edm::Event &e, const edm::EventSetup &c) override
def move(src, dest)
Definition: eostools.py:510