CMS 3D CMS Logo

ElectronRegressionEnergyProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 
4 // user include files
6 
12 
15 
25 
26 //
27 // class declaration
28 //
29 
31 public:
34 
35 private:
36  bool filter(edm::Event&, const edm::EventSetup&) override;
37 
38  // ----------member data ---------------------------
41 
44 
47 
49 
52 
55 
57 
60 
63 };
64 
66  printDebug_ = iConfig.getUntrackedParameter<bool>("printDebug", false);
67  electronToken_ = consumes<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("electronTag"));
68 
69  regressionInputFile_ = iConfig.getParameter<std::string>("regressionInputFile");
70  energyRegressionType_ = iConfig.getParameter<uint32_t>("energyRegressionType");
71 
72  nameEnergyReg_ = iConfig.getParameter<std::string>("nameEnergyReg");
73  nameEnergyErrorReg_ = iConfig.getParameter<std::string>("nameEnergyErrorReg");
74 
75  recHitCollectionEBToken_ = consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("recHitCollectionEB"));
76  recHitCollectionEEToken_ = consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("recHitCollectionEE"));
77 
78  hVertexToken_ = consumes<reco::VertexCollection>(edm::InputTag("offlinePrimaryVertices"));
79  hRhoKt6PFJetsToken_ = consumes<double>(edm::InputTag("kt6PFJets", "rho"));
80 
83 
84  produces<edm::ValueMap<double> >(nameEnergyReg_);
85  produces<edm::ValueMap<double> >(nameEnergyErrorReg_);
86 
88 
89  //set regression type
91  if (energyRegressionType_ == 1)
93  else if (energyRegressionType_ == 2)
95  else if (energyRegressionType_ == 3)
97  else if (energyRegressionType_ == 4)
99 
100  //load weights and initialize
102 
103  geomInitialized_ = false;
104 }
105 
107 
108 // ------------ method called on each new Event ------------
111 
112  if (!geomInitialized_) {
113  ecalTopology_ = &(iSetup.getData(ecalTopoToken_));
114  caloGeometry_ = &(iSetup.getData(caloGeomToken_));
115  geomInitialized_ = true;
116  }
117 
118  std::unique_ptr<edm::ValueMap<double> > regrEnergyMap(new edm::ValueMap<double>());
119  edm::ValueMap<double>::Filler energyFiller(*regrEnergyMap);
120 
121  std::unique_ptr<edm::ValueMap<double> > regrEnergyErrorMap(new edm::ValueMap<double>());
122  edm::ValueMap<double>::Filler energyErrorFiller(*regrEnergyErrorMap);
123 
125  iEvent.getByToken(electronToken_, egCollection);
126  const reco::GsfElectronCollection egCandidates = (*egCollection.product());
127 
128  std::vector<double> energyValues;
129  std::vector<double> energyErrorValues;
130  energyValues.reserve(egCollection->size());
131  energyErrorValues.reserve(egCollection->size());
132  //
133  //**************************************************************************
134  // Rechits
135  //**************************************************************************
138  iEvent.getByToken(recHitCollectionEBToken_, pEBRecHits);
139  iEvent.getByToken(recHitCollectionEEToken_, pEERecHits);
140 
141  //**************************************************************************
142  //Get Number of Vertices
143  //**************************************************************************
145  iEvent.getByToken(hVertexToken_, hVertexProduct);
146  const reco::VertexCollection inVertices = *(hVertexProduct.product());
147 
148  // loop through all vertices
149  Int_t nvertices = 0;
150  for (reco::VertexCollection::const_iterator inV = inVertices.begin(); inV != inVertices.end(); ++inV) {
151  // pass these vertex cuts
152  if (inV->ndof() >= 4 && inV->position().Rho() <= 2.0 && fabs(inV->z()) <= 24.0) {
153  nvertices++;
154  }
155  }
156 
157  //**************************************************************************
158  //Get Rho
159  //**************************************************************************
160  double rho = 0;
161  edm::Handle<double> hRhoKt6PFJets;
162  iEvent.getByToken(hRhoKt6PFJetsToken_, hRhoKt6PFJets);
163  rho = (*hRhoKt6PFJets);
164 
165  for (reco::GsfElectronCollection::const_iterator egIter = egCandidates.begin(); egIter != egCandidates.end();
166  ++egIter) {
167  const EcalRecHitCollection* recHits = nullptr;
168  if (egIter->isEB())
169  recHits = pEBRecHits.product();
170  else
171  recHits = pEERecHits.product();
172 
173  SuperClusterHelper mySCHelper(&(*egIter), recHits, ecalTopology_, caloGeometry_);
174 
175  double energy = regressionEvaluator->calculateRegressionEnergy(&(*egIter), mySCHelper, rho, nvertices, printDebug_);
176 
177  double error =
178  regressionEvaluator->calculateRegressionEnergyUncertainty(&(*egIter), mySCHelper, rho, nvertices, printDebug_);
179 
180  energyValues.push_back(energy);
181  energyErrorValues.push_back(error);
182  }
183 
184  energyFiller.insert(egCollection, energyValues.begin(), energyValues.end());
185  energyFiller.fill();
186 
187  energyErrorFiller.insert(egCollection, energyErrorValues.begin(), energyErrorValues.end());
188  energyErrorFiller.fill();
189 
190  iEvent.put(std::move(regrEnergyMap), nameEnergyReg_);
191  iEvent.put(std::move(regrEnergyErrorMap), nameEnergyErrorReg_);
192 
193  return true;
194 }
195 
196 //define this as a plug-in
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
ElectronEnergyRegressionEvaluate * regressionEvaluator
T const * product() const
Definition: Handle.h:70
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
double calculateRegressionEnergy(const reco::GsfElectron *ele, SuperClusterHelper &mySCHelper, double rho, double nvertices, bool printDebug=false)
std::vector< Vertex > VertexCollection
collection of Vertex objects
Definition: VertexFwd.h:9
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEEToken_
assert(be >=bs)
std::vector< GsfElectron > GsfElectronCollection
collection of GsfElectron objects
T getUntrackedParameter(std::string const &, T const &) const
edm::ESGetToken< CaloTopology, CaloTopologyRecord > ecalTopoToken_
int iEvent
Definition: GenABIO.cc:224
ElectronRegressionEnergyProducer(const edm::ParameterSet &)
bool filter(edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< reco::VertexCollection > hVertexToken_
void initialize(std::string weightsFile, ElectronEnergyRegressionEvaluate::ElectronEnergyRegressionType type)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEBToken_
double calculateRegressionEnergyUncertainty(const reco::GsfElectron *ele, SuperClusterHelper &mySCHelper, double rho, double nvertices, bool printDebug=false)
edm::EDGetTokenT< reco::GsfElectronCollection > electronToken_
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > caloGeomToken_
def move(src, dest)
Definition: eostools.py:511