CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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 = pDEBUG;
00069   else if (debugString == "INFO")    debugL_pi0 = pINFO;
00070   else                               debugL_pi0 = pERROR;
00071 
00072   string tmpPath = ps.getUntrackedParameter<string>("pathToWeightFiles","RecoEcal/EgammaClusterProducers/data/");
00073   
00074   presh_pi0_algo = new EndcapPiZeroDiscriminatorAlgo(preshStripECut_, preshNst_, tmpPath.c_str()); 
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 <= 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 <= 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 <= 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   Handle<PhotonCollection> correctedPhotonHandle; 
00131   evt.getByLabel(photonCorrCollectionProducer_, correctedPhotonCollection_ , correctedPhotonHandle);
00132   const PhotonCollection corrPhoCollection = *(correctedPhotonHandle.product());
00133   if ( debugL_pi0 <= pDEBUG ) {
00134     cout << " PiZeroDiscriminatorProducer: Photon Collection size : " << corrPhoCollection.size() << endl;
00135   }
00136 
00137   for( PhotonCollection::const_iterator  iPho = corrPhoCollection.begin(); iPho != corrPhoCollection.end(); iPho++) {
00138 
00139       float Phot_En   = iPho->energy();  
00140       float Phot_Et   = Phot_En*sin(2*atan(exp(-iPho->eta())));
00141       float Phot_eta  = iPho->eta();
00142       float Phot_phi  = iPho->phi();
00143       float Phot_R9   = iPho->r9();
00144       
00145       if ( debugL_pi0 <= pDEBUG ) {
00146          cout << " PiZeroDiscriminatorProducer: Photon index : " << iPho - corrPhoCollection.begin() 
00147                            << " with Energy = " <<  Phot_En
00148                            << " Et = " << Phot_Et
00149                            << " ETA = " << Phot_eta
00150                            << " PHI = " << Phot_phi 
00151                            << " R9 = " << Phot_R9 << endl;
00152       }
00153       SuperClusterRef it_super = iPho->superCluster();
00154 
00155       float SC_En   = it_super->energy();  
00156       float SC_Et   = SC_En*sin(2*atan(exp(-it_super->eta())));
00157       float SC_eta  = it_super->eta();
00158       float SC_phi  = it_super->phi();
00159 
00160       if ( debugL_pi0 <= pDEBUG ) {
00161         cout << " PiZeroDiscriminatorProducer: superE = " << SC_En
00162                   << " superEt = " << SC_Et 
00163                   << " superETA = " << SC_eta
00164                   << " superPHI = " << SC_phi << endl;
00165       }                    
00166 
00167       //  New way to get ClusterShape info
00168       // Find the entry in the map corresponding to the seed BasicCluster of the SuperCluster
00169       // DetId id = it_super->seed()->hitsAndFractions()[0].first;
00170 
00171       // get on-the-fly the cluster shapes
00172 //      EcalClusterLazyTools lazyTool( evt, es, barrelRecHitCollection_, endcapRecHitCollection_ );
00173 
00174       float nnoutput = -1.;
00175       if(fabs(SC_eta) >= 1.65 && fabs(SC_eta) <= 2.5) {  //  Use Preshower region only
00176           const GlobalPoint pointSC(it_super->x(),it_super->y(),it_super->z()); // get the centroid of the SC
00177           if ( debugL_pi0 <= pDEBUG ) cout << "SC centroind = " << pointSC << endl;
00178           double SC_seed_energy = it_super->seed()->energy();
00179 
00180           const CaloClusterPtr  seed = it_super->seed();
00181 
00182           EcalClusterTools::eMax( *seed, ebRecHits );
00183 
00184           double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, eeRecHits );
00185           double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, eeRecHits, topology );
00186           double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, eeRecHits, topology );
00187 
00188           if ( debugL_pi0 <= pDEBUG ) {
00189             cout << "PiZeroDiscriminatorProducer: ( SeedBC_energy, E1, E3x3, E5x5) = " 
00190                  <<  SC_seed_energy << " "
00191                  <<  SC_seed_Shape_E1 <<  " "
00192                  <<  SC_seed_Shape_E3x3 <<  " "
00193                  <<  SC_seed_Shape_E5x5 << endl;
00194           }
00195                      
00196 // Get the Preshower 2-planes energy vectors associated with the given SC
00197           vector<float> vout_stripE1;
00198           vector<float> vout_stripE2;
00199           for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersX->begin();
00200                                                        esClus !=clustersX->end(); esClus++) {
00201              SuperClusterRef sc_ref = esClus->superCluster();
00202              float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) + 
00203                              (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
00204              if(dR < 0.01 ) {
00205 
00206                vout_stripE1 = esClus->getStripEnergies();
00207                
00208              }
00209           }
00210           for(reco::PreshowerClusterShapeCollection::const_iterator esClus = clustersY->begin();
00211                                                        esClus !=clustersY->end(); esClus++) {
00212             SuperClusterRef sc_ref = esClus->superCluster();
00213             float dR = sqrt((SC_eta-sc_ref->eta())*(SC_eta-sc_ref->eta()) + 
00214                              (SC_phi-sc_ref->phi())*(SC_phi-sc_ref->phi()));
00215              if(dR < 0.01 ) {
00216              
00217                vout_stripE2 = esClus->getStripEnergies();
00218                
00219             }  
00220           }
00221 
00222           if(vout_stripE1.size() == 0 || vout_stripE2.size() == 0 ) {
00223             if ( debugL_pi0 <= pDEBUG ) 
00224                     cout  << " PiZeroDiscriminatorProducer: Attention!!!!!  Not Valid ES NN input Variables Return NNout = -1" << endl;
00225             Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
00226             continue;
00227           }
00228 
00229           if ( debugL_pi0 <= 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             if ( debugL_pi0 <= pDEBUG ) 
00247                    cout  << " PiZeroDiscriminatorProducer: Attention!!!!!  Not Valid ES NN input Variables Return NNout = -1" << endl;
00248             Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
00249             continue;
00250           }
00251 
00252           float* nn_input_var = presh_pi0_algo->get_input_vector();
00253 
00254           if ( debugL_pi0 <= pDEBUG ) {
00255             cout  << " PiZeroDiscriminatorProducer: NN_ESEndcap_input_vector+Et+Eta+Phi+R9 = " ;
00256             for(int k1=0;k1<25;k1++) {
00257               cout  << nn_input_var[k1] << " " ;
00258             }
00259             cout  << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9  << endl;
00260           }  
00261 
00262           nnoutput = presh_pi0_algo->GetNNOutput(SC_Et);
00263 
00264           if ( debugL_pi0 <= pDEBUG ) {
00265                cout << " PiZeroDiscriminatorProducer: Event : " <<  evt.id()
00266                     << " SC id = " << iPho - corrPhoCollection.begin()
00267                     << " with Pt = " << SC_Et
00268                     << " eta = " << SC_eta
00269                     << " phi = " << SC_phi
00270                     << " contains: " << it_super->clustersSize() << " BCs "
00271                     << " has NNout = " <<  nnoutput << endl;
00272          }
00273          
00274          Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput); 
00275          
00276       } else if((fabs(SC_eta) <= 1.4442) || (fabs(SC_eta) < 1.65 && fabs(SC_eta) >= 1.566) || fabs(SC_eta) >= 2.5) {
00277 
00278          const CaloClusterPtr seed = it_super->seed();
00279           
00280          double SC_seed_Shape_E1 = EcalClusterTools::eMax( *seed, ebRecHits );
00281          double SC_seed_Shape_E3x3 = EcalClusterTools::e3x3( *seed, ebRecHits, topology );
00282          double SC_seed_Shape_E5x5 = EcalClusterTools::e5x5( *seed, ebRecHits, topology );
00283          double SC_seed_Shape_E2 = EcalClusterTools::e2nd( *seed, ebRecHits );
00284 
00285          std::vector<float> vCov = EcalClusterTools::covariances( *seed, ebRecHits , topology, geometry, w0_ );
00286 
00287          double SC_seed_Shape_cEE = vCov[0];
00288          double SC_seed_Shape_cEP = vCov[1];
00289          double SC_seed_Shape_cPP = vCov[2];
00290 
00291          double SC_seed_Shape_E2x2 = EcalClusterTools::e2x2( *seed, ebRecHits, topology );
00292          double SC_seed_Shape_E3x2 = EcalClusterTools::e3x2( *seed, ebRecHits, topology );
00293 
00294          double SC_seed_Shape_E3x2r = 0.0;
00295          double SC_seed_Shape_ELeft = EcalClusterTools::eLeft( *seed, ebRecHits, topology );
00296          double SC_seed_Shape_ERight = EcalClusterTools::eRight( *seed, ebRecHits, topology );
00297          double SC_seed_Shape_ETop = EcalClusterTools::eTop( *seed, ebRecHits, topology );
00298          double SC_seed_Shape_EBottom = EcalClusterTools::eBottom( *seed, ebRecHits, topology );
00299 
00300          double DA = SC_seed_Shape_E2x2 - SC_seed_Shape_E2 - SC_seed_Shape_E1;
00301 
00302          if(SC_seed_Shape_E2==SC_seed_Shape_ETop || SC_seed_Shape_E2==SC_seed_Shape_EBottom) {
00303            if( SC_seed_Shape_ELeft > SC_seed_Shape_ERight ) {
00304              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ELeft)/(0.25+SC_seed_Shape_ELeft);
00305            } else {
00306              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ERight)/(0.25+SC_seed_Shape_ERight);
00307            }
00308 
00309          } else if(SC_seed_Shape_E2==SC_seed_Shape_ELeft || SC_seed_Shape_E2==SC_seed_Shape_ERight) {
00310 
00311            if( SC_seed_Shape_ETop > SC_seed_Shape_EBottom ) {
00312              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_ETop)/(0.25+SC_seed_Shape_ETop);
00313            } else {
00314              SC_seed_Shape_E3x2r = (DA - SC_seed_Shape_EBottom)/(0.25+SC_seed_Shape_EBottom);
00315            }
00316 
00317          }
00318                    
00319          double SC_seed_Shape_xcog = EcalClusterTools::eRight( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Left( *seed, ebRecHits, topology );
00320          double SC_seed_Shape_ycog = EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) - EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology );
00321 
00322 
00323          if ( debugL_pi0 <= pDEBUG ) {
00324             cout << "PiZeroDiscriminatorProduce: lazyTool  (E1,E3x3,E5x5,E2,cEE,cEP,cPP,E2x2,E3x2_E3x2r,Xcog,Ycog,E2x5Bottom,E2x5Top,Et,Eta,PhiR9) = ( " 
00325                  <<   SC_seed_Shape_E1 << " "
00326                  <<   SC_seed_Shape_E3x3 << " "
00327                  <<   SC_seed_Shape_E5x5 << " " 
00328                  <<   SC_seed_Shape_E2 << " " 
00329                  <<   SC_seed_Shape_cEE <<  " "
00330                  <<   SC_seed_Shape_cEP << " "      
00331                  <<   SC_seed_Shape_cPP << " " 
00332                  <<   SC_seed_Shape_E2x2 <<  " "
00333                  <<   SC_seed_Shape_E3x2 << " " 
00334                  <<   SC_seed_Shape_E3x2r << " " 
00335                  <<   SC_seed_Shape_xcog <<  " "
00336                  <<   SC_seed_Shape_ycog << " "
00337                  <<   EcalClusterTools::e2x5Bottom( *seed, ebRecHits, topology ) << " "
00338                  <<   EcalClusterTools::e2x5Top( *seed, ebRecHits, topology ) << " "
00339                  <<   SC_Et << " "
00340                  <<   SC_eta << " "
00341                  <<   SC_phi << " "                
00342                  <<   Phot_R9 << " )" << endl;
00343          }    
00344 
00345          float SC_et = it_super->energy()*sin(2*atan(exp(-it_super->eta())));
00346 
00347          presh_pi0_algo->calculateBarrelNNInputVariables(SC_et, SC_seed_Shape_E1, SC_seed_Shape_E3x3,
00348                                               SC_seed_Shape_E5x5, SC_seed_Shape_E2,
00349                                               SC_seed_Shape_cEE, SC_seed_Shape_cEP,
00350                                               SC_seed_Shape_cPP, SC_seed_Shape_E2x2,
00351                                               SC_seed_Shape_E3x2, SC_seed_Shape_E3x2r,
00352                                               SC_seed_Shape_xcog, SC_seed_Shape_ycog);
00353 
00354          float* nn_input_var = presh_pi0_algo->get_input_vector();
00355 
00356          if ( debugL_pi0 <= pDEBUG ) {
00357            cout  << " PiZeroDiscriminatorProducer : NN_barrel_nonESEndcap_variables+Et+Eta+Phi+R9 = " ;
00358            for(int k3=0;k3<12;k3++) {
00359              cout  << nn_input_var[k3] << " " ;
00360            }
00361            cout  << SC_Et << " " << SC_eta << " " << SC_phi << " " << Phot_R9 << endl;
00362   
00363          }
00364            
00365          nnoutput = presh_pi0_algo->GetBarrelNNOutput(SC_et);
00366          
00367 
00368          if ( debugL_pi0 <= pDEBUG ) {
00369            cout << "PiZeroDiscriminatorProducer : Event : " <<  evt.id()
00370                     << " SC id = " << iPho - corrPhoCollection.begin()
00371                     << " with Pt = " << SC_Et
00372                     << " eta = " << SC_eta
00373                     << " phi = " << SC_phi
00374                     << " contains: " << it_super->clustersSize() << " BCs "
00375                     << " has NNout = " <<  nnoutput
00376                     << endl;
00377          }
00378          
00379          Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), nnoutput);
00380       } else { Pi0Assocs_p->insert(Ref<PhotonCollection>(correctedPhotonHandle,iPho - corrPhoCollection.begin()), -1.);}
00381   } // end of cycle over Photons
00382   
00383   evt.put(Pi0Assocs_p,PhotonPi0DiscriminatorAssociationMap_);
00384   if ( debugL_pi0 <= pDEBUG ) cout << "PiZeroDiscriminatorProducer: PhotonPi0DiscriminatorAssociationMap added to the event" << endl;
00385 
00386   nEvt_++;
00387 
00388   LogDebug("PiZeroDiscriminatorDebug") << ostr.str();
00389 
00390 
00391 }