CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/EgammaAnalysis/PhotonIDProducers/src/PiZeroDiscriminatorProducer.cc

Go to the documentation of this file.
00001 // system include files
00002 #include <vector>
00003 
00004 // user include files
00005 
00006 #include "DataFormats/Common/interface/Handle.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 
00009 // Reconstruction Classes
00010 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00011 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00012 
00013 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00014 #include <fstream>
00015 #include <sstream>
00016 
00017 #include "EgammaAnalysis/PhotonIDProducers/interface/PiZeroDiscriminatorProducer.h"
00018 
00019 // Class for Cluster Shape Algorithm
00020 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00021 #include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
00022 
00023 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00024 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
00025 #include "DataFormats/EgammaReco/interface/PreshowerClusterShape.h"
00026 #include "DataFormats/EgammaCandidates/interface/PhotonPi0DiscriminatorAssociation.h"
00027 
00028 // to compute on-the-fly cluster shapes
00029 //#include "RecoEcal/EgammaCoreTools/interface/EcalClusterLazyTools.h"
00030 #include "FWCore/Framework/interface/ESHandle.h"
00031 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h"
00032 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00033 #include "Geometry/CaloTopology/interface/CaloTopology.h"
00034 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00035 #include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
00036 
00037 
00038 using namespace std;
00039 using namespace reco;
00040 using namespace edm;
00042 
00043 PiZeroDiscriminatorProducer::PiZeroDiscriminatorProducer(const ParameterSet& ps) {
00044   // use configuration file to setup input/output collection names
00045 
00046   preshClusterShapeCollectionX_ = ps.getParameter<std::string>("preshClusterShapeCollectionX");
00047   preshClusterShapeCollectionY_ = ps.getParameter<std::string>("preshClusterShapeCollectionY");
00048   preshClusterShapeProducer_   = ps.getParameter<std::string>("preshClusterShapeProducer");
00049 
00050   photonCorrCollectionProducer_ = ps.getParameter<string>("corrPhoProducer");
00051   correctedPhotonCollection_ = ps.getParameter<string>("correctedPhotonCollection");
00052 
00053   barrelRecHitCollection_ = ps.getParameter<edm::InputTag>("barrelRecHitCollection");
00054   endcapRecHitCollection_ = ps.getParameter<edm::InputTag>("endcapRecHitCollection");
00055 
00056   EScorr_ = ps.getParameter<int>("EScorr");
00057 
00058   preshNst_ = ps.getParameter<int>("preshPi0Nstrip");
00059  
00060   preshStripECut_ = ps.getParameter<double>("preshStripEnergyCut");
00061  
00062   w0_ = ps.getParameter<double>("w0");
00063 
00064   PhotonPi0DiscriminatorAssociationMap_ = ps.getParameter<string>("Pi0Association");
00065 
00066   string debugString = ps.getParameter<string>("debugLevel");
00067 
00068   if      (debugString == "DEBUG")   debugL_pi0 = EndcapPiZeroDiscriminatorAlgo::pDEBUG;
00069   else if (debugString == "INFO")    debugL_pi0 = EndcapPiZeroDiscriminatorAlgo::pINFO;
00070   else                               debugL_pi0 = EndcapPiZeroDiscriminatorAlgo::pERROR;
00071 
00072   string tmpPath = ps.getUntrackedParameter<string>("pathToWeightFiles","RecoEcal/EgammaClusterProducers/data/");
00073   
00074   presh_pi0_algo = new EndcapPiZeroDiscriminatorAlgo(preshStripECut_, preshNst_, tmpPath.c_str(), debugL_pi0); 
00075 
00076   produces< PhotonPi0DiscriminatorAssociationMap >(PhotonPi0DiscriminatorAssociationMap_);
00077 
00078  
00079   nEvt_ = 0;
00080 
00081 }
00082 
00083 
00084 PiZeroDiscriminatorProducer::~PiZeroDiscriminatorProducer() {
00085    delete presh_pi0_algo;
00086 }
00087 
00088 
00089 void PiZeroDiscriminatorProducer::produce(Event& evt, const EventSetup& es) {
00090 
00091   ostringstream ostr; // use this stream for all messages in produce
00092 
00093   if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG )
00094        cout << "\n PiZeroDiscriminatorProducer: .......  Event " << evt.id() << " with Number = " <<  nEvt_+1
00095             << " is analyzing ....... " << endl << endl;
00096 
00097   // Get ES clusters in X plane
00098   Handle<reco::PreshowerClusterShapeCollection> pPreshowerShapeClustersX;
00099   evt.getByLabel(preshClusterShapeProducer_, preshClusterShapeCollectionX_, pPreshowerShapeClustersX);
00100   const reco::PreshowerClusterShapeCollection *clustersX = pPreshowerShapeClustersX.product();
00101   if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00102     cout << "\n PiZeroDiscriminatorProducer: pPreshowerShapeClustersX->size() = " << clustersX->size() << endl;
00103   } 
00104   // Get ES clusters in Y plane
00105   Handle<reco::PreshowerClusterShapeCollection> pPreshowerShapeClustersY;
00106   evt.getByLabel(preshClusterShapeProducer_, preshClusterShapeCollectionY_, pPreshowerShapeClustersY);
00107   const reco::PreshowerClusterShapeCollection *clustersY = pPreshowerShapeClustersY.product();
00108   if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) { 
00109     cout << "\n PiZeroDiscriminatorProducer: pPreshowerShapeClustersY->size() = " << clustersY->size() << endl;
00110   }
00111   auto_ptr<PhotonPi0DiscriminatorAssociationMap> Pi0Assocs_p(new PhotonPi0DiscriminatorAssociationMap);
00112 
00113   Handle< EcalRecHitCollection > pEBRecHits;
00114   evt.getByLabel( barrelRecHitCollection_, pEBRecHits );
00115   const EcalRecHitCollection *ebRecHits = pEBRecHits.product();
00116 
00117   Handle< EcalRecHitCollection > pEERecHits;
00118   evt.getByLabel( endcapRecHitCollection_, pEERecHits );
00119   const EcalRecHitCollection *eeRecHits = pEERecHits.product();
00120 
00121   ESHandle<CaloGeometry> pGeometry;
00122   es.get<CaloGeometryRecord>().get(pGeometry);
00123   const CaloGeometry *geometry = pGeometry.product();
00124 
00125   ESHandle<CaloTopology> pTopology;
00126   es.get<CaloTopologyRecord>().get(pTopology);
00127   const CaloTopology *topology = pTopology.product();
00128 
00129   //make cycle over Photon Collection
00130   int Photon_index  = 0;
00131   Handle<PhotonCollection> correctedPhotonHandle; 
00132   evt.getByLabel(photonCorrCollectionProducer_, correctedPhotonCollection_ , correctedPhotonHandle);
00133   const PhotonCollection corrPhoCollection = *(correctedPhotonHandle.product());
00134   if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00135     cout << " PiZeroDiscriminatorProducer: Photon Collection size : " << corrPhoCollection.size() << endl;
00136   }
00137 
00138   for( PhotonCollection::const_iterator  iPho = corrPhoCollection.begin(); iPho != corrPhoCollection.end(); iPho++) {
00139 
00140       float Phot_En   = iPho->energy();  
00141       float Phot_Et   = Phot_En*sin(2*atan(exp(-iPho->eta())));
00142       float Phot_eta  = iPho->eta();
00143       float Phot_phi  = iPho->phi();
00144       float Phot_R9   = iPho->r9();
00145       
00146       if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00147          cout << " PiZeroDiscriminatorProducer: Photon index : " << Photon_index 
00148                            << " with Energy = " <<  Phot_En
00149                            << " Et = " << Phot_Et
00150                            << " ETA = " << Phot_eta
00151                            << " PHI = " << Phot_phi 
00152                            << " R9 = " << Phot_R9 << endl;
00153       }
00154       SuperClusterRef it_super = iPho->superCluster();
00155 
00156       float SC_En   = it_super->energy();  
00157       float SC_Et   = SC_En*sin(2*atan(exp(-it_super->eta())));
00158       float SC_eta  = it_super->eta();
00159       float SC_phi  = it_super->phi();
00160 
00161       if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00162         cout << " PiZeroDiscriminatorProducer: superE = " << SC_En
00163                   << " superEt = " << SC_Et 
00164                   << " superETA = " << SC_eta
00165                   << " superPHI = " << SC_phi << endl;
00166       }                    
00167 
00168       //  New way to get ClusterShape info
00169       // Find the entry in the map corresponding to the seed BasicCluster of the SuperCluster
00170       // DetId id = it_super->seed()->hitsAndFractions()[0].first;
00171 
00172       // get on-the-fly the cluster shapes
00173 //      EcalClusterLazyTools lazyTool( evt, es, barrelRecHitCollection_, endcapRecHitCollection_ );
00174 
00175       float nnoutput = -1.;
00176       if(fabs(SC_eta) >= 1.65 && fabs(SC_eta) <= 2.5) {  //  Use Preshower region only
00177           const GlobalPoint pointSC(it_super->x(),it_super->y(),it_super->z()); // get the centroid of the SC
00178           if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) cout << "SC centroind = " << pointSC << endl;
00179           double SC_seed_energy = it_super->seed()->energy();
00180 
00181           const CaloClusterPtr  seed = it_super->seed();
00182 
00183           EcalClusterTools::eMax( *seed, ebRecHits );
00184 
00185           double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, eeRecHits );
00186           double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, eeRecHits, topology );
00187           double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, eeRecHits, topology );
00188 
00189           if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00190             cout << "PiZeroDiscriminatorProducer: ( SeedBC_energy, E1, E3x3, E5x5) = " 
00191                  <<  SC_seed_energy << " "
00192                  <<  SC_seed_Shape_E1 <<  " "
00193                  <<  SC_seed_Shape_E3x3 <<  " "
00194                  <<  SC_seed_Shape_E5x5 << endl;
00195           }
00196                      
00197 // Get the Preshower 2-planes energy vectors associated with the given SC
00198           vector<float> vout_stripE1;
00199           vector<float> vout_stripE2;
00200           for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersX->begin();
00201                                                        esClus !=clustersX->end(); esClus++) {
00202              SuperClusterRef sc_ref = esClus->superCluster();
00203              float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) + 
00204                              (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
00205              if(dR < 0.01 ) {
00206 
00207                vout_stripE1 = esClus->getStripEnergies();
00208                
00209              }
00210           }
00211           for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersY->begin();
00212                                                        esClus !=clustersY->end(); esClus++) {
00213             SuperClusterRef sc_ref = esClus->superCluster();
00214             float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) + 
00215                              (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
00216              if(dR < 0.01 ) {
00217              
00218                vout_stripE2 = esClus->getStripEnergies();
00219                
00220             }  
00221           }
00222 
00223           if(vout_stripE1.size() == 0 || vout_stripE2.size() == 0 ) {
00224             cout  << " PiZeroDiscriminatorProducer: Attention!!!!!  Not Valid ES NN input Variables Return NNout = -1" << endl;
00225             Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,Photon_index), nnoutput);
00226             continue;
00227           }
00228 
00229           if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00230             cout  << "PiZeroDiscriminatorProducer : vout_stripE1.size = " << vout_stripE1.size() 
00231                   << " vout_stripE2.size = " << vout_stripE2.size() << endl;
00232             cout  << "PiZeroDiscriminatorProducer : ES_input_vector = " ;
00233             for(int k1=0;k1<11;k1++) {
00234               cout  << vout_stripE1[k1] << " " ;
00235             }
00236             for(int k1=0;k1<11;k1++) {
00237               cout  << vout_stripE2[k1] << " " ;
00238             }
00239             cout  << endl;
00240           }
00241           
00242           bool valid_NNinput = presh_pi0_algo->calculateNNInputVariables(vout_stripE1, vout_stripE2,
00243                                                  SC_seed_Shape_E1, SC_seed_Shape_E3x3, SC_seed_Shape_E5x5, EScorr_);
00244 
00245           if(!valid_NNinput) {
00246             cout  << " PiZeroDiscriminatorProducer: Attention!!!!!  Not Valid ES NN input Variables Return NNout = -1" << endl;
00247             Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,Photon_index), nnoutput);
00248             continue;
00249           }
00250 
00251           float* nn_input_var = presh_pi0_algo->get_input_vector();
00252 
00253           if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00254             cout  << " PiZeroDiscriminatorProducer: NN_ESEndcap_input_vector+Et+Eta+Phi+R9 = " ;
00255             for(int k1=0;k1<25;k1++) {
00256               cout  << nn_input_var[k1] << " " ;
00257             }
00258             cout  << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9  << endl;
00259           }  
00260 
00261           nnoutput = presh_pi0_algo->GetNNOutput(SC_Et);
00262 
00263           if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00264                cout << " PiZeroDiscriminatorProducer: Event : " <<  evt.id()
00265                     << " SC id = " << Photon_index
00266                     << " with Pt = " << SC_Et
00267                     << " eta = " << SC_eta
00268                     << " phi = " << SC_phi
00269                     << " contains: " << it_super->clustersSize() << " BCs "
00270                     << " has NNout = " <<  nnoutput << endl;
00271          }
00272          
00273          Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,Photon_index), nnoutput); 
00274          
00275       } else if((fabs(SC_eta) <= 1.4442) || (fabs(SC_eta) < 1.65 && fabs(SC_eta) >= 1.566) || fabs(SC_eta) >= 2.5) {
00276 
00277          const CaloClusterPtr seed = it_super->seed();
00278           
00279          double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, ebRecHits );
00280          double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, ebRecHits, topology );
00281          double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, ebRecHits, topology );
00282          double SC_seed_Shape_E2 = EcalClusterTools::e2nd( *seed, ebRecHits );
00283 
00284          std::vector<float> vCov = EcalClusterTools::covariances( *seed, ebRecHits , topology, geometry, w0_ );
00285 
00286          double SC_seed_Shape_cEE = vCov[0];
00287          double SC_seed_Shape_cEP = vCov[1];
00288          double SC_seed_Shape_cPP = vCov[2];
00289 
00290          double SC_seed_Shape_E2x2 = EcalClusterTools::e2x2( *seed, ebRecHits, topology );
00291          double SC_seed_Shape_E3x2 = EcalClusterTools::e3x2( *seed, ebRecHits, topology );
00292 
00293          double SC_seed_Shape_E3x2r = 0.0;
00294          double SC_seed_Shape_ELeft = EcalClusterTools::eLeft( *seed, ebRecHits, topology );
00295          double SC_seed_Shape_ERight = EcalClusterTools::eRight( *seed, ebRecHits, topology );
00296          double SC_seed_Shape_ETop = EcalClusterTools::eTop( *seed, ebRecHits, topology );
00297          double SC_seed_Shape_EBottom = EcalClusterTools::eBottom( *seed, ebRecHits, topology );
00298 
00299          double DA = SC_seed_Shape_E2x2 - SC_seed_Shape_E2 - SC_seed_Shape_E1;
00300 
00301          if(SC_seed_Shape_E2==SC_seed_Shape_ETop || SC_seed_Shape_E2==SC_seed_Shape_EBottom) {
00302            if( SC_seed_Shape_ELeft > SC_seed_Shape_ERight ) {
00303              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ELeft)/(0.25+SC_seed_Shape_ELeft);
00304            } else {
00305              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ERight)/(0.25+SC_seed_Shape_ERight);
00306            }
00307 
00308          } else if(SC_seed_Shape_E2==SC_seed_Shape_ELeft || SC_seed_Shape_E2==SC_seed_Shape_ERight) {
00309 
00310            if( SC_seed_Shape_ETop > SC_seed_Shape_EBottom ) {
00311              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ETop)/(0.25+SC_seed_Shape_ETop);
00312            } else {
00313              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_EBottom)/(0.25+SC_seed_Shape_EBottom);
00314            }
00315 
00316          }
00317                    
00318          double SC_seed_Shape_xcog = EcalClusterTools::eRight( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Left( *seed, ebRecHits, topology );
00319          double SC_seed_Shape_ycog = EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology );
00320 
00321 
00322          if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00323             cout << "PiZeroDiscriminatorProduce: lazyTool  (E1,E3x3,E5x5,E2,cEE,cEP,cPP,E2x2,E3x2_E3x2r,Xcog,Ycog,E2x5Bottom,E2x5Top,Et,Eta,PhiR9) = ( " 
00324                  <<   SC_seed_Shape_E1 << " "
00325                  <<   SC_seed_Shape_E3x3 << " "
00326                  <<   SC_seed_Shape_E5x5 << " " 
00327                  <<   SC_seed_Shape_E2 << " " 
00328                  <<   SC_seed_Shape_cEE <<  " "
00329                  <<   SC_seed_Shape_cEP << " "      
00330                  <<   SC_seed_Shape_cPP << " " 
00331                  <<   SC_seed_Shape_E2x2 <<  " "
00332                  <<   SC_seed_Shape_E3x2 << " " 
00333                  <<   SC_seed_Shape_E3x2r << " " 
00334                  <<   SC_seed_Shape_xcog <<  " "
00335                  <<   SC_seed_Shape_ycog << " "
00336                  <<   EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology ) << " "
00337                  <<   EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) << " "
00338                  <<   SC_Et << " "
00339                  <<   SC_eta << " "
00340                  <<   SC_phi << " "                
00341                  <<   Phot_R9 << " )" << endl;
00342          }    
00343 
00344          float SC_et = it_super->energy()*sin(2*atan(exp(-it_super->eta())));
00345 
00346          presh_pi0_algo->calculateBarrelNNInputVariables(SC_et, SC_seed_Shape_E1, SC_seed_Shape_E3x3,
00347                                               SC_seed_Shape_E5x5, SC_seed_Shape_E2,
00348                                               SC_seed_Shape_cEE, SC_seed_Shape_cEP,
00349                                               SC_seed_Shape_cPP, SC_seed_Shape_E2x2,
00350                                               SC_seed_Shape_E3x2, SC_seed_Shape_E3x2r,
00351                                               SC_seed_Shape_xcog, SC_seed_Shape_ycog);
00352 
00353          float* nn_input_var = presh_pi0_algo->get_input_vector();
00354 
00355          if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00356            cout  << " PiZeroDiscriminatorProducer : NN_barrel_nonESEndcap_variables+Et+Eta+Phi+R9 = " ;
00357            for(int k3=0;k3<12;k3++) {
00358              cout  << nn_input_var[k3] << " " ;
00359            }
00360            cout  << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9 << endl;
00361   
00362          }
00363            
00364          nnoutput = presh_pi0_algo->GetBarrelNNOutput(SC_et);
00365          
00366 
00367          if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) {
00368            cout << "PiZeroDiscriminatorProducer : Event : " <<  evt.id()
00369                     << " SC id = " << Photon_index
00370                     << " with Pt = " << SC_Et
00371                     << " eta = " << SC_eta
00372                     << " phi = " << SC_phi
00373                     << " contains: " << it_super->clustersSize() << " BCs "
00374                     << " has NNout = " <<  nnoutput
00375                     << endl;
00376          }
00377          
00378          Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,Photon_index), nnoutput);
00379       } else { Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,Photon_index), -1.);}
00380       Photon_index++;
00381   } // end of cycle over Photons
00382   
00383   evt.put(Pi0Assocs_p,PhotonPi0DiscriminatorAssociationMap_);
00384   if ( debugL_pi0 <= EndcapPiZeroDiscriminatorAlgo::pDEBUG ) cout << "PiZeroDiscriminatorProducer: PhotonPi0DiscriminatorAssociationMap added to the event" << endl;
00385 
00386   nEvt_++;
00387 
00388   LogDebug("PiZeroDiscriminatorDebug") << ostr.str();
00389 
00390 
00391 }