CMS 3D CMS Logo

PiZeroDiscriminatorProducer.cc
Go to the documentation of this file.
1 // system include files
2 #include <vector>
3 
4 // user include files
5 
8 
9 // Reconstruction Classes
12 
14 #include <fstream>
15 #include <sstream>
16 
18 
19 // Class for Cluster Shape Algorithm
22 
25 
26 // to compute on-the-fly cluster shapes
27 //#include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
34 
35 
36 using namespace std;
37 using namespace reco;
38 using namespace edm;
40 
42  // use configuration file to setup input/output collection names
43 
44  pPreshowerShapeClustersXToken_ = consumes<PreshowerClusterShapeCollection>(edm::InputTag(ps.getParameter<std::string>("preshClusterShapeProducer"), ps.getParameter<std::string>("preshClusterShapeCollectionX")));
45  pPreshowerShapeClustersYToken_ = consumes<PreshowerClusterShapeCollection>(edm::InputTag(ps.getParameter<std::string>("preshClusterShapeProducer"), ps.getParameter<std::string>("preshClusterShapeCollectionY")));
46 
47  correctedPhotonToken_ = consumes<PhotonCollection>(edm::InputTag(ps.getParameter<string>("corrPhoProducer"), ps.getParameter<string>("correctedPhotonCollection")));
48 
49  barrelRecHitCollection_ = ps.getParameter<edm::InputTag>("barrelRecHitCollection");
50  barrelRecHitCollectionToken_ = consumes<EcalRecHitCollection>(barrelRecHitCollection_);
51  endcapRecHitCollection_ = ps.getParameter<edm::InputTag>("endcapRecHitCollection");
52  endcapRecHitCollectionToken_ = consumes<EcalRecHitCollection>(endcapRecHitCollection_);
53 
54  EScorr_ = ps.getParameter<int>("EScorr");
55 
56  preshNst_ = ps.getParameter<int>("preshPi0Nstrip");
57 
58  preshStripECut_ = ps.getParameter<double>("preshStripEnergyCut");
59 
60  w0_ = ps.getParameter<double>("w0");
61 
62  PhotonPi0DiscriminatorAssociationMap_ = ps.getParameter<string>("Pi0Association");
63 
64  string debugString = ps.getParameter<string>("debugLevel");
65 
66  if (debugString == "DEBUG") debugL_pi0 = pDEBUG;
67  else if (debugString == "INFO") debugL_pi0 = pINFO;
68  else debugL_pi0 = pERROR;
69 
70  string tmpPath = ps.getUntrackedParameter<string>("pathToWeightFiles","RecoEcal/EgammaClusterProducers/data/");
71 
72  presh_pi0_algo = new EndcapPiZeroDiscriminatorAlgo(preshStripECut_, preshNst_, tmpPath);
73 
74  produces< PhotonPi0DiscriminatorAssociationMap >(PhotonPi0DiscriminatorAssociationMap_);
75 
76 
77  nEvt_ = 0;
78 
79 }
80 
81 
83  delete presh_pi0_algo;
84 }
85 
86 
88 
89  ostringstream ostr; // use this stream for all messages in produce
90 
91  if ( debugL_pi0 <= pDEBUG )
92  cout << "\n PiZeroDiscriminatorProducer: ....... Event " << evt.id() << " with Number = " << nEvt_+1
93  << " is analyzing ....... " << endl << endl;
94 
95  // Get ES clusters in X plane
96  Handle<reco::PreshowerClusterShapeCollection> pPreshowerShapeClustersX;
97  evt.getByToken(pPreshowerShapeClustersXToken_, pPreshowerShapeClustersX);
98  const reco::PreshowerClusterShapeCollection *clustersX = pPreshowerShapeClustersX.product();
99  if ( debugL_pi0 <= pDEBUG ) {
100  cout << "\n PiZeroDiscriminatorProducer: pPreshowerShapeClustersX->size() = " << clustersX->size() << endl;
101  }
102  // Get ES clusters in Y plane
103  Handle<reco::PreshowerClusterShapeCollection> pPreshowerShapeClustersY;
104  evt.getByToken(pPreshowerShapeClustersYToken_, pPreshowerShapeClustersY);
105  const reco::PreshowerClusterShapeCollection *clustersY = pPreshowerShapeClustersY.product();
106  if ( debugL_pi0 <= pDEBUG ) {
107  cout << "\n PiZeroDiscriminatorProducer: pPreshowerShapeClustersY->size() = " << clustersY->size() << endl;
108  }
109 
111  evt.getByToken( barrelRecHitCollectionToken_, pEBRecHits );
112  const EcalRecHitCollection *ebRecHits = pEBRecHits.product();
113 
115  evt.getByToken( endcapRecHitCollectionToken_, pEERecHits );
116  const EcalRecHitCollection *eeRecHits = pEERecHits.product();
117 
118  ESHandle<CaloGeometry> pGeometry;
119  es.get<CaloGeometryRecord>().get(pGeometry);
120  const CaloGeometry *geometry = pGeometry.product();
121 
122  ESHandle<CaloTopology> pTopology;
123  es.get<CaloTopologyRecord>().get(pTopology);
124  const CaloTopology *topology = pTopology.product();
125 
126  //make cycle over Photon Collection
127  Handle<PhotonCollection> correctedPhotonHandle;
128  evt.getByToken(correctedPhotonToken_, correctedPhotonHandle);
129  const PhotonCollection corrPhoCollection = *(correctedPhotonHandle.product());
130  if ( debugL_pi0 <= pDEBUG ) {
131  cout << " PiZeroDiscriminatorProducer: Photon Collection size : " << corrPhoCollection.size() << endl;
132  }
133 
134  unique_ptr<PhotonPi0DiscriminatorAssociationMap> Pi0Assocs_p(new PhotonPi0DiscriminatorAssociationMap(correctedPhotonHandle));
135 
136  for( PhotonCollection::const_iterator iPho = corrPhoCollection.begin(); iPho != corrPhoCollection.end(); iPho++) {
137 
138  float Phot_En = iPho->energy();
139  float Phot_Et = Phot_En*sin(2*atan(exp(-iPho->eta())));
140  float Phot_eta = iPho->eta();
141  float Phot_phi = iPho->phi();
142  float Phot_R9 = iPho->r9();
143 
144  if ( debugL_pi0 <= pDEBUG ) {
145  cout << " PiZeroDiscriminatorProducer: Photon index : " << iPho - corrPhoCollection.begin()
146  << " with Energy = " << Phot_En
147  << " Et = " << Phot_Et
148  << " ETA = " << Phot_eta
149  << " PHI = " << Phot_phi
150  << " R9 = " << Phot_R9 << endl;
151  }
152  SuperClusterRef it_super = iPho->superCluster();
153 
154  float SC_En = it_super->energy();
155  float SC_Et = SC_En*sin(2*atan(exp(-it_super->eta())));
156  float SC_eta = it_super->eta();
157  float SC_phi = it_super->phi();
158 
159  if ( debugL_pi0 <= pDEBUG ) {
160  cout << " PiZeroDiscriminatorProducer: superE = " << SC_En
161  << " superEt = " << SC_Et
162  << " superETA = " << SC_eta
163  << " superPHI = " << SC_phi << endl;
164  }
165 
166  // New way to get ClusterShape info
167  // Find the entry in the map corresponding to the seed BasicCluster of the SuperCluster
168  // DetId id = it_super->seed()->hitsAndFractions()[0].first;
169 
170  // get on-the-fly the cluster shapes
171 // EcalClusterLazyTools lazyTool( evt, es, barrelRecHitCollection_, endcapRecHitCollection_ );
172 
173  float nnoutput = -1.;
174  if(fabs(SC_eta) >= 1.65 && fabs(SC_eta) <= 2.5) { // Use Preshower region only
175  const GlobalPoint pointSC(it_super->x(),it_super->y(),it_super->z()); // get the centroid of the SC
176  if ( debugL_pi0 <= pDEBUG ) cout << "SC centroind = " << pointSC << endl;
177  double SC_seed_energy = it_super->seed()->energy();
178 
179  const CaloClusterPtr seed = it_super->seed();
180 
181  EcalClusterTools::eMax( *seed, ebRecHits );
182 
183  double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, eeRecHits );
184  double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, eeRecHits, topology );
185  double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, eeRecHits, topology );
186 
187  if ( debugL_pi0 <= pDEBUG ) {
188  cout << "PiZeroDiscriminatorProducer: ( SeedBC_energy, E1, E3x3, E5x5) = "
189  << SC_seed_energy << " "
190  << SC_seed_Shape_E1 << " "
191  << SC_seed_Shape_E3x3 << " "
192  << SC_seed_Shape_E5x5 << endl;
193  }
194 
195 // Get the Preshower 2-planes energy vectors associated with the given SC
196  vector<float> vout_stripE1;
197  vector<float> vout_stripE2;
198  for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersX->begin();
199  esClus !=clustersX->end(); esClus++) {
200  SuperClusterRef sc_ref = esClus->superCluster();
201  float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) +
202  (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
203  if(dR < 0.01 ) {
204 
205  vout_stripE1 = esClus->getStripEnergies();
206 
207  }
208  }
209  for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersY->begin();
210  esClus !=clustersY->end(); esClus++) {
211  SuperClusterRef sc_ref = esClus->superCluster();
212  float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) +
213  (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
214  if(dR < 0.01 ) {
215 
216  vout_stripE2 = esClus->getStripEnergies();
217 
218  }
219  }
220 
221  if(vout_stripE1.empty() || vout_stripE2.empty() ) {
222  if ( debugL_pi0 <= pDEBUG )
223  cout << " PiZeroDiscriminatorProducer: Attention!!!!! Not Valid ES NN input Variables Return NNout = -1" << endl;
224  Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
225  continue;
226  }
227 
228  if ( debugL_pi0 <= pDEBUG ) {
229  cout << "PiZeroDiscriminatorProducer : vout_stripE1.size = " << vout_stripE1.size()
230  << " vout_stripE2.size = " << vout_stripE2.size() << endl;
231  cout << "PiZeroDiscriminatorProducer : ES_input_vector = " ;
232  for(int k1=0;k1<11;k1++) {
233  cout << vout_stripE1[k1] << " " ;
234  }
235  for(int k1=0;k1<11;k1++) {
236  cout << vout_stripE2[k1] << " " ;
237  }
238  cout << endl;
239  }
240 
241  bool valid_NNinput = presh_pi0_algo->calculateNNInputVariables(vout_stripE1, vout_stripE2,
242  SC_seed_Shape_E1, SC_seed_Shape_E3x3, SC_seed_Shape_E5x5, EScorr_);
243 
244  if(!valid_NNinput) {
245  if ( debugL_pi0 <= pDEBUG )
246  cout << " PiZeroDiscriminatorProducer: Attention!!!!! Not Valid ES NN input Variables Return NNout = -1" << endl;
247  Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
248  continue;
249  }
250 
251  if ( debugL_pi0 <= pDEBUG ) {
252  cout << " PiZeroDiscriminatorProducer: NN_ESEndcap_input_vector+Et+Eta+Phi+R9 = " ;
253  for(auto v: presh_pi0_algo->get_input_vector()) {
254  cout << v << " " ;
255  }
256  cout << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9 << endl;
257  }
258 
259  nnoutput = presh_pi0_algo->GetNNOutput(SC_Et);
260 
261  if ( debugL_pi0 <= pDEBUG ) {
262  cout << " PiZeroDiscriminatorProducer: Event : " << evt.id()
263  << " SC id = " << iPho - corrPhoCollection.begin()
264  << " with Pt = " << SC_Et
265  << " eta = " << SC_eta
266  << " phi = " << SC_phi
267  << " contains: " << it_super->clustersSize() << " BCs "
268  << " has NNout = " << nnoutput << endl;
269  }
270 
271  Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
272 
273  } else if((fabs(SC_eta) <= 1.4442) || (fabs(SC_eta) < 1.65 && fabs(SC_eta) >= 1.566) || fabs(SC_eta) >= 2.5) {
274 
275  const CaloClusterPtr seed = it_super->seed();
276 
277  double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, ebRecHits );
278  double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, ebRecHits, topology );
279  double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, ebRecHits, topology );
280  double SC_seed_Shape_E2 = EcalClusterTools::e2nd( *seed, ebRecHits );
281 
282  std::vector<float> vCov = EcalClusterTools::covariances( *seed, ebRecHits , topology, geometry, w0_ );
283 
284  double SC_seed_Shape_cEE = vCov[0];
285  double SC_seed_Shape_cEP = vCov[1];
286  double SC_seed_Shape_cPP = vCov[2];
287 
288  double SC_seed_Shape_E2x2 = EcalClusterTools::e2x2( *seed, ebRecHits, topology );
289  double SC_seed_Shape_E3x2 = EcalClusterTools::e3x2( *seed, ebRecHits, topology );
290 
291  double SC_seed_Shape_E3x2r = 0.0;
292  double SC_seed_Shape_ELeft = EcalClusterTools::eLeft( *seed, ebRecHits, topology );
293  double SC_seed_Shape_ERight = EcalClusterTools::eRight( *seed, ebRecHits, topology );
294  double SC_seed_Shape_ETop = EcalClusterTools::eTop( *seed, ebRecHits, topology );
295  double SC_seed_Shape_EBottom = EcalClusterTools::eBottom( *seed, ebRecHits, topology );
296 
297  double DA = SC_seed_Shape_E2x2 - SC_seed_Shape_E2 - SC_seed_Shape_E1;
298 
299  if(SC_seed_Shape_E2==SC_seed_Shape_ETop || SC_seed_Shape_E2==SC_seed_Shape_EBottom) {
300  if( SC_seed_Shape_ELeft > SC_seed_Shape_ERight ) {
301  SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ELeft)/(0.25+SC_seed_Shape_ELeft);
302  } else {
303  SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ERight)/(0.25+SC_seed_Shape_ERight);
304  }
305 
306  } else if(SC_seed_Shape_E2==SC_seed_Shape_ELeft || SC_seed_Shape_E2==SC_seed_Shape_ERight) {
307 
308  if( SC_seed_Shape_ETop > SC_seed_Shape_EBottom ) {
309  SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ETop)/(0.25+SC_seed_Shape_ETop);
310  } else {
311  SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_EBottom)/(0.25+SC_seed_Shape_EBottom);
312  }
313 
314  }
315 
316  double SC_seed_Shape_xcog = EcalClusterTools::eRight( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Left( *seed, ebRecHits, topology );
317  double SC_seed_Shape_ycog = EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology );
318 
319 
320  if ( debugL_pi0 <= pDEBUG ) {
321  cout << "PiZeroDiscriminatorProduce: lazyTool (E1,E3x3,E5x5,E2,cEE,cEP,cPP,E2x2,E3x2_E3x2r,Xcog,Ycog,E2x5Bottom,E2x5Top,Et,Eta,PhiR9) = ( "
322  << SC_seed_Shape_E1 << " "
323  << SC_seed_Shape_E3x3 << " "
324  << SC_seed_Shape_E5x5 << " "
325  << SC_seed_Shape_E2 << " "
326  << SC_seed_Shape_cEE << " "
327  << SC_seed_Shape_cEP << " "
328  << SC_seed_Shape_cPP << " "
329  << SC_seed_Shape_E2x2 << " "
330  << SC_seed_Shape_E3x2 << " "
331  << SC_seed_Shape_E3x2r << " "
332  << SC_seed_Shape_xcog << " "
333  << SC_seed_Shape_ycog << " "
334  << EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology ) << " "
335  << EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) << " "
336  << SC_Et << " "
337  << SC_eta << " "
338  << SC_phi << " "
339  << Phot_R9 << " )" << endl;
340  }
341 
342  float SC_et = it_super->energy()*sin(2*atan(exp(-it_super->eta())));
343 
344  presh_pi0_algo->calculateBarrelNNInputVariables(SC_et, SC_seed_Shape_E1, SC_seed_Shape_E3x3,
345  SC_seed_Shape_E5x5, SC_seed_Shape_E2,
346  SC_seed_Shape_cEE, SC_seed_Shape_cEP,
347  SC_seed_Shape_cPP, SC_seed_Shape_E2x2,
348  SC_seed_Shape_E3x2, SC_seed_Shape_E3x2r,
349  SC_seed_Shape_xcog, SC_seed_Shape_ycog);
350 
351  if ( debugL_pi0 <= pDEBUG ) {
352  cout << " PiZeroDiscriminatorProducer : NN_barrel_nonESEndcap_variables+Et+Eta+Phi+R9 = " ;
353  for(auto v: presh_pi0_algo->get_input_vector()) {
354  cout << v << " " ;
355  }
356  cout << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9 << endl;
357 
358  }
359 
360  nnoutput = presh_pi0_algo->GetBarrelNNOutput(SC_et);
361 
362 
363  if ( debugL_pi0 <= pDEBUG ) {
364  cout << "PiZeroDiscriminatorProducer : Event : " << evt.id()
365  << " SC id = " << iPho - corrPhoCollection.begin()
366  << " with Pt = " << SC_Et
367  << " eta = " << SC_eta
368  << " phi = " << SC_phi
369  << " contains: " << it_super->clustersSize() << " BCs "
370  << " has NNout = " << nnoutput
371  << endl;
372  }
373 
374  Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
375  } else { Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), -1.);}
376  } // end of cycle over Photons
377 
378  evt.put(std::move(Pi0Assocs_p),PhotonPi0DiscriminatorAssociationMap_);
379  if ( debugL_pi0 <= pDEBUG ) cout << "PiZeroDiscriminatorProducer: PhotonPi0DiscriminatorAssociationMap added to the event" << endl;
380 
381  nEvt_++;
382 
383  LogDebug("PiZeroDiscriminatorDebug") << ostr.str();
384 
385 
386 }
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
CaloTopology const * topology(0)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
edm::AssociationMap< edm::OneToValue< std::vector< reco::Photon >, float > > PhotonPi0DiscriminatorAssociationMap
T sqrt(T t)
Definition: SSEVec.h:18
std::vector< PreshowerClusterShape > PreshowerClusterShapeCollection
collection of PreshowerClusterShape objects
T const * product() const
Definition: Handle.h:81
void produce(edm::Event &evt, const edm::EventSetup &es) override
std::vector< Photon > PhotonCollection
collectin of Photon objects
Definition: PhotonFwd.h:9
edm::EventID id() const
Definition: EventBase.h:60
fixed size matrix
HLT enums.
T get() const
Definition: EventSetup.h:63
PiZeroDiscriminatorProducer(const edm::ParameterSet &ps)
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510