CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoParticleFlow/PFProducer/plugins/PFEGammaProducer.cc

Go to the documentation of this file.
00001 #include "RecoParticleFlow/PFProducer/plugins/PFEGammaProducer.h"
00002 #include "RecoParticleFlow/PFProducer/interface/PFEGammaAlgo.h"
00003 
00004 #include "FWCore/Framework/interface/ESHandle.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 #include "RecoParticleFlow/PFClusterTools/interface/PFEnergyCalibration.h"
00008 #include "RecoParticleFlow/PFClusterTools/interface/PFEnergyCalibrationHF.h"
00009 #include "RecoParticleFlow/PFClusterTools/interface/PFSCEnergyCalibration.h"
00010 #include "CondFormats/PhysicsToolsObjects/interface/PerformancePayloadFromTFormula.h"
00011 #include "CondFormats/DataRecord/interface/PFCalibrationRcd.h"
00012 #include "CondFormats/DataRecord/interface/GBRWrapperRcd.h"
00013 #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
00014 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00015 #include "DataFormats/ParticleFlowReco/interface/PFRecHitFwd.h"
00016 #include "DataFormats/ParticleFlowReco/interface/PFRecHit.h"
00017 #include "DataFormats/ParticleFlowReco/interface/PFBlockElementSuperClusterFwd.h"
00018 #include "DataFormats/ParticleFlowReco/interface/PFBlockElementSuperCluster.h"
00019 #include "DataFormats/ParticleFlowReco/interface/PFBlockFwd.h"
00020 #include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
00021 #include "DataFormats/Common/interface/RefToPtr.h"
00022 #include <sstream>
00023 
00024 #include "TFile.h"
00025 
00026 using namespace std;
00027 
00028 using namespace boost;
00029 
00030 using namespace edm;
00031 
00032 typedef std::list< reco::PFBlockRef >::iterator IBR;
00033 
00034 
00035 PFEGammaProducer::PFEGammaProducer(const edm::ParameterSet& iConfig) {
00036   
00037 
00038   inputTagBlocks_ 
00039     = iConfig.getParameter<InputTag>("blocks");
00040 
00041   usePhotonReg_
00042     =  iConfig.getParameter<bool>("usePhotonReg");
00043 
00044   useRegressionFromDB_
00045     = iConfig.getParameter<bool>("useRegressionFromDB"); 
00046 
00047 
00048   bool usePFSCEleCalib;
00049   std::vector<double>  calibPFSCEle_Fbrem_barrel; 
00050   std::vector<double>  calibPFSCEle_Fbrem_endcap;
00051   std::vector<double>  calibPFSCEle_barrel;
00052   std::vector<double>  calibPFSCEle_endcap;
00053   usePFSCEleCalib =     iConfig.getParameter<bool>("usePFSCEleCalib");
00054   calibPFSCEle_Fbrem_barrel = iConfig.getParameter<std::vector<double> >("calibPFSCEle_Fbrem_barrel");
00055   calibPFSCEle_Fbrem_endcap = iConfig.getParameter<std::vector<double> >("calibPFSCEle_Fbrem_endcap");
00056   calibPFSCEle_barrel = iConfig.getParameter<std::vector<double> >("calibPFSCEle_barrel");
00057   calibPFSCEle_endcap = iConfig.getParameter<std::vector<double> >("calibPFSCEle_endcap");
00058   boost::shared_ptr<PFSCEnergyCalibration>  
00059     thePFSCEnergyCalibration ( new PFSCEnergyCalibration(calibPFSCEle_Fbrem_barrel,calibPFSCEle_Fbrem_endcap,
00060                                                          calibPFSCEle_barrel,calibPFSCEle_endcap )); 
00061                                
00062   bool useEGammaSupercluster = iConfig.getParameter<bool>("useEGammaSupercluster");
00063   double sumEtEcalIsoForEgammaSC_barrel = iConfig.getParameter<double>("sumEtEcalIsoForEgammaSC_barrel");
00064   double sumEtEcalIsoForEgammaSC_endcap = iConfig.getParameter<double>("sumEtEcalIsoForEgammaSC_endcap");
00065   double coneEcalIsoForEgammaSC = iConfig.getParameter<double>("coneEcalIsoForEgammaSC");
00066   double sumPtTrackIsoForEgammaSC_barrel = iConfig.getParameter<double>("sumPtTrackIsoForEgammaSC_barrel");
00067   double sumPtTrackIsoForEgammaSC_endcap = iConfig.getParameter<double>("sumPtTrackIsoForEgammaSC_endcap");
00068   double coneTrackIsoForEgammaSC = iConfig.getParameter<double>("coneTrackIsoForEgammaSC");
00069   unsigned int nTrackIsoForEgammaSC  = iConfig.getParameter<unsigned int>("nTrackIsoForEgammaSC");
00070 
00071 
00072   // register products
00073   produces<reco::PFCandidateCollection>();
00074   produces<reco::PFCandidateEGammaExtraCollection>();
00075   produces<reco::CaloClusterCollection>("EBEEClusters");
00076   produces<reco::CaloClusterCollection>("ESClusters");
00077   produces<reco::SuperClusterCollection>();
00078   
00079   //PFElectrons Configuration
00080   double mvaEleCut
00081     = iConfig.getParameter<double>("pf_electron_mvaCut");
00082 
00083   
00084   string mvaWeightFileEleID
00085     = iConfig.getParameter<string>("pf_electronID_mvaWeightFile");
00086 
00087   bool applyCrackCorrectionsForElectrons
00088     = iConfig.getParameter<bool>("pf_electronID_crackCorrection");
00089   
00090   string path_mvaWeightFileEleID;
00091 
00092   path_mvaWeightFileEleID = edm::FileInPath ( mvaWeightFileEleID.c_str() ).fullPath();
00093      
00094 
00095   //PFPhoton Configuration
00096 
00097   string path_mvaWeightFileConvID;
00098   string mvaWeightFileConvID;
00099   string path_mvaWeightFileGCorr;
00100   string path_mvaWeightFileLCorr;
00101   string path_X0_Map;
00102   string path_mvaWeightFileRes;
00103   double mvaConvCut=-99.;
00104   double sumPtTrackIsoForPhoton = 99.;
00105   double sumPtTrackIsoSlopeForPhoton = 99.;
00106 
00107 
00108   mvaWeightFileConvID =iConfig.getParameter<string>("pf_convID_mvaWeightFile");
00109   mvaConvCut = iConfig.getParameter<double>("pf_conv_mvaCut");
00110   path_mvaWeightFileConvID = edm::FileInPath ( mvaWeightFileConvID.c_str() ).fullPath();  
00111   sumPtTrackIsoForPhoton = iConfig.getParameter<double>("sumPtTrackIsoForPhoton");
00112   sumPtTrackIsoSlopeForPhoton = iConfig.getParameter<double>("sumPtTrackIsoSlopeForPhoton");
00113 
00114   string X0_Map=iConfig.getParameter<string>("X0_Map");
00115   path_X0_Map = edm::FileInPath( X0_Map.c_str() ).fullPath();
00116 
00117   if(!useRegressionFromDB_) {
00118     string mvaWeightFileLCorr=iConfig.getParameter<string>("pf_locC_mvaWeightFile");
00119     path_mvaWeightFileLCorr = edm::FileInPath( mvaWeightFileLCorr.c_str() ).fullPath();
00120     string mvaWeightFileGCorr=iConfig.getParameter<string>("pf_GlobC_mvaWeightFile");
00121     path_mvaWeightFileGCorr = edm::FileInPath( mvaWeightFileGCorr.c_str() ).fullPath();
00122     string mvaWeightFileRes=iConfig.getParameter<string>("pf_Res_mvaWeightFile");
00123     path_mvaWeightFileRes=edm::FileInPath(mvaWeightFileRes.c_str()).fullPath();
00124 
00125     TFile *fgbr = new TFile(path_mvaWeightFileGCorr.c_str(),"READ");
00126     ReaderGC_  =(const GBRForest*)fgbr->Get("GBRForest");
00127     TFile *fgbr2 = new TFile(path_mvaWeightFileLCorr.c_str(),"READ");
00128     ReaderLC_  = (const GBRForest*)fgbr2->Get("GBRForest");
00129     TFile *fgbr3 = new TFile(path_mvaWeightFileRes.c_str(),"READ");
00130     ReaderRes_  = (const GBRForest*)fgbr3->Get("GBRForest");
00131     LogDebug("PFEGammaProducer")<<"Will set regressions from binary files " <<endl;
00132   }
00133 
00134   edm::ParameterSet iCfgCandConnector 
00135     = iConfig.getParameter<edm::ParameterSet>("iCfgCandConnector");
00136 
00137 
00138   // fToRead =  iConfig.getUntrackedParameter<vector<string> >("toRead");
00139 
00140   useCalibrationsFromDB_
00141     = iConfig.getParameter<bool>("useCalibrationsFromDB");    
00142 
00143   boost::shared_ptr<PFEnergyCalibration> 
00144     calibration( new PFEnergyCalibration() ); 
00145 
00146   int algoType 
00147     = iConfig.getParameter<unsigned>("algoType");
00148   
00149   switch(algoType) {
00150   case 0:
00151     //pfAlgo_.reset( new PFAlgo);
00152     break;
00153    default:
00154     assert(0);
00155   }
00156   
00157   //PFEGamma
00158   setPFEGParameters(mvaEleCut,
00159                               path_mvaWeightFileEleID,
00160                               true,
00161                               thePFSCEnergyCalibration,
00162                               calibration,
00163                               sumEtEcalIsoForEgammaSC_barrel,
00164                               sumEtEcalIsoForEgammaSC_endcap,
00165                               coneEcalIsoForEgammaSC,
00166                               sumPtTrackIsoForEgammaSC_barrel,
00167                               sumPtTrackIsoForEgammaSC_endcap,
00168                               nTrackIsoForEgammaSC,
00169                               coneTrackIsoForEgammaSC,
00170                               applyCrackCorrectionsForElectrons,
00171                               usePFSCEleCalib,
00172                               useEGammaElectrons_,
00173                               useEGammaSupercluster,
00174                               true,
00175                               path_mvaWeightFileConvID,
00176                               mvaConvCut,
00177                               usePhotonReg_,
00178                               path_X0_Map,
00179                               sumPtTrackIsoForPhoton,
00180                               sumPtTrackIsoSlopeForPhoton                             
00181                             );  
00182 
00183   //MIKE: Vertex Parameters
00184   vertices_ = iConfig.getParameter<edm::InputTag>("vertexCollection");
00185 
00186   verbose_ = 
00187     iConfig.getUntrackedParameter<bool>("verbose",false);
00188 
00189 //   bool debug_ = 
00190 //     iConfig.getUntrackedParameter<bool>("debug",false);
00191 
00192 }
00193 
00194 
00195 
00196 PFEGammaProducer::~PFEGammaProducer() {}
00197 
00198 void 
00199 PFEGammaProducer::beginRun(const edm::Run & run, 
00200                      const edm::EventSetup & es) 
00201 {
00202 
00203 
00204   /*
00205   static map<string, PerformanceResult::ResultType> functType;
00206 
00207   functType["PFfa_BARREL"] = PerformanceResult::PFfa_BARREL;
00208   functType["PFfa_ENDCAP"] = PerformanceResult::PFfa_ENDCAP;
00209   functType["PFfb_BARREL"] = PerformanceResult::PFfb_BARREL;
00210   functType["PFfb_ENDCAP"] = PerformanceResult::PFfb_ENDCAP;
00211   functType["PFfc_BARREL"] = PerformanceResult::PFfc_BARREL;
00212   functType["PFfc_ENDCAP"] = PerformanceResult::PFfc_ENDCAP;
00213   functType["PFfaEta_BARREL"] = PerformanceResult::PFfaEta_BARREL;
00214   functType["PFfaEta_ENDCAP"] = PerformanceResult::PFfaEta_ENDCAP;
00215   functType["PFfbEta_BARREL"] = PerformanceResult::PFfbEta_BARREL;
00216   functType["PFfbEta_ENDCAP"] = PerformanceResult::PFfbEta_ENDCAP;
00217   */
00218   
00219   /*
00220   for(vector<string>::const_iterator name = fToRead.begin(); name != fToRead.end(); ++name) {    
00221     
00222     cout << "Function: " << *name << endl;
00223     PerformanceResult::ResultType fType = functType[*name];
00224     pfCalibrations->printFormula(fType);
00225     
00226     // evaluate it @ 10 GeV
00227     float energy = 10.;
00228     
00229     BinningPointByMap point;
00230     point.insert(BinningVariables::JetEt, energy);
00231     
00232     if(pfCalibrations->isInPayload(fType, point)) {
00233       float value = pfCalibrations->getResult(fType, point);
00234       cout << "   Energy before:: " << energy << " after: " << value << endl;
00235     } else cout <<  "outside limits!" << endl;
00236     
00237   }
00238   */
00239   
00240   if(useRegressionFromDB_) {
00241     edm::ESHandle<GBRForest> readerPFLCEB;
00242     edm::ESHandle<GBRForest> readerPFLCEE;    
00243     edm::ESHandle<GBRForest> readerPFGCEB;
00244     edm::ESHandle<GBRForest> readerPFGCEEHR9;
00245     edm::ESHandle<GBRForest> readerPFGCEELR9;
00246     edm::ESHandle<GBRForest> readerPFRes;
00247     es.get<GBRWrapperRcd>().get("PFLCorrectionBar",readerPFLCEB);
00248     ReaderLCEB_=readerPFLCEB.product();
00249     es.get<GBRWrapperRcd>().get("PFLCorrectionEnd",readerPFLCEE);
00250     ReaderLCEE_=readerPFLCEE.product();
00251     es.get<GBRWrapperRcd>().get("PFGCorrectionBar",readerPFGCEB);       
00252     ReaderGCBarrel_=readerPFGCEB.product();
00253     es.get<GBRWrapperRcd>().get("PFGCorrectionEndHighR9",readerPFGCEEHR9);
00254     ReaderGCEndCapHighr9_=readerPFGCEEHR9.product();
00255     es.get<GBRWrapperRcd>().get("PFGCorrectionEndLowR9",readerPFGCEELR9);
00256     ReaderGCEndCapLowr9_=readerPFGCEELR9.product();
00257     es.get<GBRWrapperRcd>().get("PFEcalResolution",readerPFRes);
00258     ReaderEcalRes_=readerPFRes.product();
00259     
00260     /*
00261     LogDebug("PFEGammaProducer")<<"setting regressions from DB "<<endl;
00262     */
00263   } 
00264 
00265 
00266   //pfAlgo_->setPFPhotonRegWeights(ReaderLC_, ReaderGC_, ReaderRes_);
00267   setPFPhotonRegWeights(ReaderLCEB_,ReaderLCEE_,ReaderGCBarrel_,ReaderGCEndCapHighr9_, ReaderGCEndCapLowr9_, ReaderEcalRes_ );
00268     
00269 }
00270 
00271 
00272 void 
00273 PFEGammaProducer::produce(Event& iEvent, 
00274                     const EventSetup& iSetup) {
00275   
00276   LogDebug("PFEGammaProducer")<<"START event: "
00277                         <<iEvent.id().event()
00278                         <<" in run "<<iEvent.id().run()<<endl;
00279   
00280 
00281   // reset output collection
00282   if(egCandidates_.get() )
00283     egCandidates_->clear();
00284   else 
00285     egCandidates_.reset( new reco::PFCandidateCollection );                        
00286 
00287   if(ebeeClusters_.get() )
00288     ebeeClusters_->clear();
00289   else 
00290     ebeeClusters_.reset( new reco::CaloClusterCollection );  
00291 
00292   //printf("ebeeclusters->size() = %i\n",int(ebeeClusters_->size()));
00293   
00294   if(esClusters_.get() )
00295     esClusters_->clear();
00296   else 
00297     esClusters_.reset( new reco::CaloClusterCollection );    
00298   
00299   if(sClusters_.get() )
00300     sClusters_->clear();
00301   else 
00302     sClusters_.reset( new reco::SuperClusterCollection );   
00303   
00304   if(egExtra_.get() )
00305     egExtra_->clear();
00306   else 
00307     egExtra_.reset( new reco::PFCandidateEGammaExtraCollection );  
00308   
00309   // Get The vertices from the event
00310   // and assign dynamic vertex parameters
00311   edm::Handle<reco::VertexCollection> vertices;
00312   bool gotVertices = iEvent.getByLabel(vertices_,vertices);
00313   if(!gotVertices) {
00314     ostringstream err;
00315     err<<"Cannot find vertices for this event.Continuing Without them ";
00316     LogError("PFEGammaProducer")<<err.str()<<endl;
00317   }
00318 
00319   //Assign the PFAlgo Parameters
00320   setPFVertexParameters(useVerticesForNeutral_,vertices.product());
00321 
00322   // get the collection of blocks 
00323 
00324   Handle< reco::PFBlockCollection > blocks;
00325 
00326   LogDebug("PFEGammaProducer")<<"getting blocks"<<endl;
00327   bool found = iEvent.getByLabel( inputTagBlocks_, blocks );  
00328 
00329   if(!found ) {
00330 
00331     ostringstream err;
00332     err<<"cannot find blocks: "<<inputTagBlocks_;
00333     LogError("PFEGammaProducer")<<err.str()<<endl;
00334     
00335     throw cms::Exception( "MissingProduct", err.str());
00336   }
00337 
00338 
00339   
00340   LogDebug("PFEGammaProducer")<<"particle flow is starting"<<endl;
00341 
00342   assert( blocks.isValid() );
00343 
00344   //pfAlgo_->reconstructParticles( blocks );
00345   
00346   if(verbose_) {
00347     ostringstream  str;
00348     //str<<(*pfAlgo_)<<endl;
00349     //    cout << (*pfAlgo_) << endl;
00350     LogInfo("PFEGammaProducer") <<str.str()<<endl;
00351   }  
00352 
00353   // sort elements in three lists:
00354   std::list< reco::PFBlockRef > hcalBlockRefs;
00355   std::list< reco::PFBlockRef > ecalBlockRefs;
00356   std::list< reco::PFBlockRef > hoBlockRefs;
00357   std::list< reco::PFBlockRef > otherBlockRefs;
00358   
00359   for( unsigned i=0; i<blocks->size(); ++i ) {
00360     // reco::PFBlockRef blockref( blockh,i );
00361     //reco::PFBlockRef blockref = createBlockRef( *blocks, i);
00362     reco::PFBlockRef blockref(blocks, i);
00363     
00364     const reco::PFBlock& block = *blockref;
00365     const edm::OwnVector< reco::PFBlockElement >& 
00366       elements = block.elements();
00367    
00368     bool singleEcalOrHcal = false;
00369     if( elements.size() == 1 ){
00370       if( elements[0].type() == reco::PFBlockElement::ECAL ){
00371         ecalBlockRefs.push_back( blockref );
00372         singleEcalOrHcal = true;
00373       }
00374       if( elements[0].type() == reco::PFBlockElement::HCAL ){
00375         hcalBlockRefs.push_back( blockref );
00376         singleEcalOrHcal = true;
00377       }
00378       if( elements[0].type() == reco::PFBlockElement::HO ){
00379         // Single HO elements are likely to be noise. Not considered for now.
00380         hoBlockRefs.push_back( blockref );
00381         singleEcalOrHcal = true;
00382       }
00383     }
00384     
00385     if(!singleEcalOrHcal) {
00386       otherBlockRefs.push_back( blockref );
00387     }
00388   }//loop blocks
00389   
00390 
00391 
00392   // loop on blocks that are not single ecal, 
00393   // and not single hcal and produce unbiased collection of EGamma Candidates
00394 
00395   //printf("loop over blocks\n");
00396   //unsigned nblcks = 0;
00397   for( IBR io = otherBlockRefs.begin(); io!=otherBlockRefs.end(); ++io) {
00398       const reco::PFBlockRef& blockref = *io;
00399       const reco::PFBlock& block = **io;
00400       const edm::OwnVector< reco::PFBlockElement >& elements = block.elements();
00401       // make a copy of the link data, which will be edited.
00402       //PFBlock::LinkData linkData =  block.linkData();
00403       
00404       // keep track of the elements which are still active.
00405       vector<bool>   active( elements.size(), true );  
00406     
00407       //printf("pre  algo: egCandidates size = %i\n",int(egCandidates_->size()));
00408       if (pfeg_->isEGValidCandidate(blockref,active)){        
00409         //printf("getCandidates size = %i\n",int(pfeg_->getCandidates().size()));
00410         egCandidates_->insert(egCandidates_->end(),pfeg_->getCandidates().begin(), pfeg_->getCandidates().end());
00411         egExtra_->insert(egExtra_->end(), pfeg_->getEGExtra().begin(), pfeg_->getEGExtra().end());   
00412       }
00413      // printf("post algo: egCandidates size = %i\n",int(egCandidates_->size()));
00414      
00415      
00416     
00417   }
00418   
00419 //   edm::RefProd<reco::CaloClusterCollection> ebeeClusterProd = iEvent.getRefBeforePut<reco::CaloClusterCollection>("EBEEClusters");
00420 //   edm::RefProd<reco::CaloClusterCollection> esClusterProd = iEvent.getRefBeforePut<reco::CaloClusterCollection>("ESClusters");
00421    edm::RefProd<reco::SuperClusterCollection> sClusterProd = iEvent.getRefBeforePut<reco::SuperClusterCollection>();
00422   
00423   //printf("loop over candidates\n");
00424   //make CaloClusters for Refined SuperClusters
00425   std::vector<std::vector<int> > ebeeidxs(egCandidates_->size());
00426   std::vector<std::vector<int> > esidxs(egCandidates_->size());;
00427   for (unsigned int icand=0; icand<egCandidates_->size(); ++icand) {
00428     reco::PFCandidate &cand = egCandidates_->at(icand);
00429     //reco::PFCandidateEGammaExtra &extra = egExtra_->at(icand);
00430 
00431     //loop over blockelements
00432    // printf("loop over blockelements\n");
00433     for (reco::PFCandidate::ElementsInBlocks::const_iterator it=cand.elementsInBlocks().begin(); it!=cand.elementsInBlocks().end(); ++it) {
00434       const reco::PFBlockElement &element = it->first->elements()[it->second];
00435       if (element.type()==reco::PFBlockElement::ECAL) {    
00436         reco::CaloCluster cluster(*element.clusterRef().get());
00437         ebeeClusters_->push_back(cluster);
00438                 
00439         ebeeidxs[icand].push_back(ebeeClusters_->size()-1);         
00440       }
00441       else if (element.type()==reco::PFBlockElement::PS1 || element.type()==reco::PFBlockElement::PS2) {
00442         reco::CaloCluster cluster(*element.clusterRef().get());
00443         esClusters_->push_back(cluster);
00444         
00445         esidxs[icand].push_back(esClusters_->size()-1);
00446       }
00447     }
00448     
00449   }
00450     
00451   //put cluster products
00452   auto_ptr< reco::CaloClusterCollection >
00453     pOutputEBEEClusters( ebeeClusters_ ); 
00454   edm::OrphanHandle<reco::CaloClusterCollection > ebeeClusterProd=
00455     iEvent.put(pOutputEBEEClusters,"EBEEClusters");        
00456     
00457   auto_ptr< reco::CaloClusterCollection >
00458     pOutputESClusters( esClusters_ ); 
00459   edm::OrphanHandle<reco::CaloClusterCollection > esClusterProd=
00460     iEvent.put(pOutputESClusters,"ESClusters");         
00461     
00462   //loop over sets of clusters to make superclusters
00463   for (unsigned int iclus=0; iclus<egCandidates_->size(); ++iclus) {
00464     reco::PFCandidate &cand = egCandidates_->at(iclus);
00465     reco::PFCandidateEGammaExtra &extra = egExtra_->at(iclus);    
00466 
00467     const std::vector<int> &ebeeidx = ebeeidxs[iclus];
00468     const std::vector<int> &esidx = esidxs[iclus];
00469     reco::CaloClusterPtr seed;
00470     
00471     reco::CaloClusterPtrVector ebeeclusters;
00472     reco::CaloClusterPtrVector esclusters;
00473     
00474     double maxenergy = 0.;
00475     double rawenergy = 0.;
00476     double energy = 0.;
00477 
00478     double posX = 0.;
00479     double posY = 0.;
00480     double posZ = 0.;
00481     for (unsigned int icaloclus=0; icaloclus<ebeeidx.size(); ++icaloclus) {
00482       const reco::CaloCluster &cluster = ebeeClusterProd->at(ebeeidx[icaloclus]);
00483       reco::CaloClusterPtr caloptr(ebeeClusterProd,ebeeidx[icaloclus]);
00484       ebeeclusters.push_back(caloptr);
00485             
00486       rawenergy += cluster.energy();        
00487       energy += cluster.energy();   
00488       
00489       posX += cluster.energy()*cluster.position().x();
00490       posY += cluster.energy()*cluster.position().y();
00491       posZ += cluster.energy()*cluster.position().z();
00492       
00493       if (cluster.energy()>maxenergy) {
00494         maxenergy = cluster.energy();
00495         seed = caloptr;
00496       }
00497       
00498 
00499       
00500     }
00501     
00502     for (unsigned int icaloclus=0; icaloclus<esidx.size(); ++icaloclus) {
00503       const reco::CaloCluster &cluster = esClusterProd->at(esidx[icaloclus]);
00504       
00505       reco::CaloClusterPtr caloptr(esClusterProd,esidx[icaloclus]);
00506       esclusters.push_back(caloptr);
00507       
00508       energy += cluster.energy();        
00509     } 
00510         
00511     posX /= rawenergy;
00512     posY /= rawenergy;
00513     posZ /= rawenergy;
00514     
00515     math::XYZPoint scposition(posX,posY,posZ);
00516     
00517     reco::SuperCluster refinedSC(rawenergy,scposition,seed,ebeeclusters,esclusters);
00518     sClusters_->push_back(refinedSC);
00519     
00520     reco::SuperClusterRef scref(sClusterProd,sClusters_->size()-1);
00521     cand.setSuperClusterRef(scref);
00522     extra.setSuperClusterRef(scref);
00523   }
00524     
00525 // Save the PFEGamma Extra Collection First as to be able to create valid References  
00526   auto_ptr< reco::PFCandidateEGammaExtraCollection >
00527     pOutputEGammaCandidateExtraCollection( egExtra_ );    
00528   const edm::OrphanHandle<reco::PFCandidateEGammaExtraCollection > egammaExtraProd=
00529     iEvent.put(pOutputEGammaCandidateExtraCollection);      
00530   //pfAlgo_->setEGammaExtraRef(egammaExtraProd);
00531    
00532   //final loop over Candidates to set PFCandidateEGammaExtra references
00533   for (unsigned int icand=0; icand<egCandidates_->size(); ++icand) {
00534     reco::PFCandidate &cand = egCandidates_->at(icand);
00535     
00536     reco::PFCandidateEGammaExtraRef extraref(egammaExtraProd,icand);
00537     cand.setPFEGammaExtraRef(extraref);    
00538   }
00539      
00540     
00541   auto_ptr< reco::SuperClusterCollection >
00542     pOutputSClusters( sClusters_ ); 
00543   //edm::OrphanHandle<reco::SuperClusterCollection > sClusterProd=
00544     iEvent.put(pOutputSClusters);    
00545     
00546   // Save the final PFCandidate collection
00547   auto_ptr< reco::PFCandidateCollection > 
00548     pOutputCandidateCollection( egCandidates_ ); 
00549   
00550 
00551   
00552 //   LogDebug("PFEGammaProducer")<<"particle flow: putting products in the event"<<endl;
00553 //   if ( verbose_ ) std::cout <<"particle flow: putting products in the event. Here the full list"<<endl;
00554 //   int nC=0;
00555 //   for( reco::PFCandidateCollection::const_iterator  itCand =  (*pOutputCandidateCollection).begin(); itCand !=  (*pOutputCandidateCollection).end(); itCand++) {
00556 //     nC++;
00557 //       if (verbose_ ) std::cout << nC << ")" << (*itCand).particleId() << std::endl;
00558 // 
00559 //   }
00560 // 
00561 //   // Write in the event
00562    iEvent.put(pOutputCandidateCollection);
00563  
00564 }
00565 
00566 //PFEGammaAlgo: a new method added to set the parameters for electron and photon reconstruction. 
00567 void 
00568 PFEGammaProducer::setPFEGParameters(double mvaEleCut,
00569                            string mvaWeightFileEleID,
00570                            bool usePFElectrons,
00571                            const boost::shared_ptr<PFSCEnergyCalibration>& thePFSCEnergyCalibration,
00572                            const boost::shared_ptr<PFEnergyCalibration>& thePFEnergyCalibration,
00573                            double sumEtEcalIsoForEgammaSC_barrel,
00574                            double sumEtEcalIsoForEgammaSC_endcap,
00575                            double coneEcalIsoForEgammaSC,
00576                            double sumPtTrackIsoForEgammaSC_barrel,
00577                            double sumPtTrackIsoForEgammaSC_endcap,
00578                            unsigned int nTrackIsoForEgammaSC,
00579                            double coneTrackIsoForEgammaSC,
00580                            bool applyCrackCorrections,
00581                            bool usePFSCEleCalib,
00582                            bool useEGElectrons,
00583                            bool useEGammaSupercluster,
00584                            bool usePFPhotons,  
00585                            std::string mvaWeightFileConvID, 
00586                            double mvaConvCut,
00587                            bool useReg,
00588                            std::string X0_Map,
00589                            double sumPtTrackIsoForPhoton,
00590                            double sumPtTrackIsoSlopeForPhoton                      
00591                         ) {
00592   
00593   mvaEleCut_ = mvaEleCut;
00594   usePFElectrons_ = usePFElectrons;
00595   applyCrackCorrectionsElectrons_ = applyCrackCorrections;  
00596   usePFSCEleCalib_ = usePFSCEleCalib;
00597   thePFSCEnergyCalibration_ = thePFSCEnergyCalibration;
00598   useEGElectrons_ = useEGElectrons;
00599   useEGammaSupercluster_ = useEGammaSupercluster;
00600   sumEtEcalIsoForEgammaSC_barrel_ = sumEtEcalIsoForEgammaSC_barrel;
00601   sumEtEcalIsoForEgammaSC_endcap_ = sumEtEcalIsoForEgammaSC_endcap;
00602   coneEcalIsoForEgammaSC_ = coneEcalIsoForEgammaSC;
00603   sumPtTrackIsoForEgammaSC_barrel_ = sumPtTrackIsoForEgammaSC_barrel;
00604   sumPtTrackIsoForEgammaSC_endcap_ = sumPtTrackIsoForEgammaSC_endcap;
00605   coneTrackIsoForEgammaSC_ = coneTrackIsoForEgammaSC;
00606   nTrackIsoForEgammaSC_ = nTrackIsoForEgammaSC;
00607 
00608 
00609   if(!usePFElectrons_) return;
00610   mvaWeightFileEleID_ = mvaWeightFileEleID;
00611   FILE * fileEleID = fopen(mvaWeightFileEleID_.c_str(), "r");
00612   if (fileEleID) {
00613     fclose(fileEleID);
00614   }
00615   else {
00616     string err = "PFAlgo: cannot open weight file '";
00617     err += mvaWeightFileEleID;
00618     err += "'";
00619     throw invalid_argument( err );
00620   }
00621   
00622   usePFPhotons_ = usePFPhotons;
00623 
00624   //for MVA pass PV if there is one in the collection otherwise pass a dummy    
00625   reco::Vertex dummy;  
00626   if(useVertices_)  
00627     {  
00628       dummy = primaryVertex_;  
00629     }  
00630   else { // create a dummy PV  
00631     reco::Vertex::Error e;  
00632     e(0, 0) = 0.0015 * 0.0015;  
00633     e(1, 1) = 0.0015 * 0.0015;  
00634     e(2, 2) = 15. * 15.;  
00635     reco::Vertex::Point p(0, 0, 0);  
00636     dummy = reco::Vertex(p, e, 0, 0, 0);  
00637   }  
00638   // pv=&dummy;  
00639   //if(! usePFPhotons_) return;  
00640   FILE * filePhotonConvID = fopen(mvaWeightFileConvID.c_str(), "r");  
00641   if (filePhotonConvID) {  
00642     fclose(filePhotonConvID);  
00643   }  
00644   else {  
00645     string err = "PFAlgo: cannot open weight file '";  
00646     err += mvaWeightFileConvID;  
00647     err += "'";  
00648     throw invalid_argument( err );  
00649   }  
00650   const reco::Vertex* pv=&dummy;  
00651   pfeg_.reset(new PFEGammaAlgo(mvaEleCut_,mvaWeightFileEleID_,
00652                              thePFSCEnergyCalibration_,
00653                              thePFEnergyCalibration,
00654                              applyCrackCorrectionsElectrons_,
00655                              usePFSCEleCalib_,
00656                              useEGElectrons_,
00657                              useEGammaSupercluster_,
00658                              sumEtEcalIsoForEgammaSC_barrel_,
00659                              sumEtEcalIsoForEgammaSC_endcap_,
00660                              coneEcalIsoForEgammaSC_,
00661                              sumPtTrackIsoForEgammaSC_barrel_,
00662                              sumPtTrackIsoForEgammaSC_endcap_,
00663                              nTrackIsoForEgammaSC_,
00664                              coneTrackIsoForEgammaSC_,
00665                             mvaWeightFileConvID, 
00666                             mvaConvCut, 
00667                             useReg,
00668                             X0_Map,  
00669                             *pv,
00670                             sumPtTrackIsoForPhoton,
00671                             sumPtTrackIsoSlopeForPhoton
00672                             ));
00673   return;  
00674   
00675 //   pfele_= new PFElectronAlgo(mvaEleCut_,mvaWeightFileEleID_,
00676 //                           thePFSCEnergyCalibration_,
00677 //                           thePFEnergyCalibration,
00678 //                           applyCrackCorrectionsElectrons_,
00679 //                           usePFSCEleCalib_,
00680 //                           useEGElectrons_,
00681 //                           useEGammaSupercluster_,
00682 //                           sumEtEcalIsoForEgammaSC_barrel_,
00683 //                           sumEtEcalIsoForEgammaSC_endcap_,
00684 //                           coneEcalIsoForEgammaSC_,
00685 //                           sumPtTrackIsoForEgammaSC_barrel_,
00686 //                           sumPtTrackIsoForEgammaSC_endcap_,
00687 //                           nTrackIsoForEgammaSC_,
00688 //                           coneTrackIsoForEgammaSC_);
00689 }
00690 
00691 /*
00692 void PFAlgo::setPFPhotonRegWeights(
00693                   const GBRForest *LCorrForest,
00694                   const GBRForest *GCorrForest,
00695                   const GBRForest *ResForest
00696                   ) {                                                           
00697   if(usePFPhotons_) 
00698     pfpho_->setGBRForest(LCorrForest, GCorrForest, ResForest);
00699 } 
00700 */
00701 void PFEGammaProducer::setPFPhotonRegWeights(
00702                                    const GBRForest *LCorrForestEB,
00703                                    const GBRForest *LCorrForestEE,
00704                                    const GBRForest *GCorrForestBarrel,
00705                                    const GBRForest *GCorrForestEndcapHr9,
00706                                    const GBRForest *GCorrForestEndcapLr9,                                          const GBRForest *PFEcalResolution
00707                                    ){
00708   
00709   pfeg_->setGBRForest(LCorrForestEB,LCorrForestEE,
00710                        GCorrForestBarrel, GCorrForestEndcapHr9, 
00711                        GCorrForestEndcapLr9, PFEcalResolution);
00712 }
00713 
00714 void
00715 PFEGammaProducer::setPFVertexParameters(bool useVertex,
00716                               const reco::VertexCollection*  primaryVertices) {
00717   useVertices_ = useVertex;
00718 
00719   //Set the vertices for muon cleaning
00720 //  pfmu_->setInputsForCleaning(primaryVertices);
00721 
00722 
00723   //Now find the primary vertex!
00724   //bool primaryVertexFound = false;
00725   int nVtx=primaryVertices->size();
00726   pfeg_->setnPU(nVtx);
00727 //   if(usePFPhotons_){
00728 //     pfpho_->setnPU(nVtx);
00729 //   }
00730   primaryVertex_ = primaryVertices->front();
00731   for (unsigned short i=0 ;i<primaryVertices->size();++i)
00732     {
00733       if(primaryVertices->at(i).isValid()&&(!primaryVertices->at(i).isFake()))
00734         {
00735           primaryVertex_ = primaryVertices->at(i);
00736           //primaryVertexFound = true;
00737           break;
00738         }
00739     }
00740   
00741   pfeg_->setPhotonPrimaryVtx(primaryVertex_ );
00742   
00743 }