CMS 3D CMS Logo

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