CMS 3D CMS Logo

ConvertedPhotonProducer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <memory>
00004 
00005 // Framework
00006 #include "FWCore/Framework/interface/Event.h"
00007 #include "FWCore/Framework/interface/EventSetup.h"
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Utilities/interface/Exception.h"
00012 //
00013 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00014 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00015 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
00016 
00017 //
00018 #include "DataFormats/EgammaTrackReco/interface/TrackCaloClusterAssociation.h"
00019 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00020 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
00021 #include "DataFormats/EgammaCandidates/interface/Conversion.h"
00022 #include "DataFormats/EgammaCandidates/interface/ConversionFwd.h"
00023 #include "DataFormats/TrackReco/interface/Track.h"
00024 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00025 #include "DataFormats/VertexReco/interface/Vertex.h"
00026 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00027 //
00028 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00029 //
00030 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00031 //
00032 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionTrackEcalImpactPoint.h"
00033 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionTrackPairFinder.h"
00034 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionVertexFinder.h"
00035 #include "RecoEgamma/EgammaPhotonProducers/interface/ConvertedPhotonProducer.h"
00036 //
00037 #include "RecoTracker/Record/interface/CkfComponentsRecord.h"
00038 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00039 //
00040 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00041 #include "TrackingTools/TransientTrack/interface/TrackTransientTrack.h"
00042 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
00043 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00044 
00045 ConvertedPhotonProducer::ConvertedPhotonProducer(const edm::ParameterSet& config) : 
00046 
00047   conf_(config), 
00048   theTrackPairFinder_(0), 
00049   theVertexFinder_(0), 
00050   theEcalImpactPositionFinder_(0)
00051 
00052 {
00053 
00054 
00055   
00056   LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer CTOR " << "\n";
00057   
00058   
00059   
00060   // use onfiguration file to setup input collection names
00061   
00062   //bcProducer_             = conf_.getParameter<std::string>("bcProducer");
00063   bcBarrelCollection_     = conf_.getParameter<edm::InputTag>("bcBarrelCollection");
00064   bcEndcapCollection_     = conf_.getParameter<edm::InputTag>("bcEndcapCollection");
00065   
00066   scHybridBarrelProducer_       = conf_.getParameter<edm::InputTag>("scHybridBarrelProducer");
00067   scIslandEndcapProducer_       = conf_.getParameter<edm::InputTag>("scIslandEndcapProducer");
00068   
00069   //  scHybridBarrelCollection_     = conf_.getParameter<std::string>("scHybridBarrelCollection");
00070   // scIslandEndcapCollection_     = conf_.getParameter<std::string>("scIslandEndcapCollection");
00071   
00072 
00073   conversionOITrackProducer_ = conf_.getParameter<std::string>("conversionOITrackProducer");
00074   conversionIOTrackProducer_ = conf_.getParameter<std::string>("conversionIOTrackProducer");
00075   
00076   outInTrackSCAssociationCollection_ = conf_.getParameter<std::string>("outInTrackSCAssociation");
00077   inOutTrackSCAssociationCollection_ = conf_.getParameter<std::string>("inOutTrackSCAssociation");
00078   
00079    
00080   // use onfiguration file to setup output collection names
00081   ConvertedPhotonCollection_     = conf_.getParameter<std::string>("convertedPhotonCollection");
00082   
00083   
00084   // Register the product
00085   produces< reco::ConversionCollection >(ConvertedPhotonCollection_);
00086   
00087   // instantiate the Track Pair Finder algorithm
00088   theTrackPairFinder_ = new ConversionTrackPairFinder ();
00089   // instantiate the Vertex Finder algorithm
00090   theVertexFinder_ = new ConversionVertexFinder ();
00091 
00092 
00093   // Inizilize my global event counter
00094   nEvt_=0;
00095 
00096 
00097   theEcalImpactPositionFinder_ =0;
00098   
00099 }
00100 
00101 ConvertedPhotonProducer::~ConvertedPhotonProducer() {
00102   
00103   
00104   delete theTrackPairFinder_;
00105   delete theVertexFinder_;
00106 
00107 
00108 }
00109 
00110 void  ConvertedPhotonProducer::endRun (edm::Run& r, edm::EventSetup const & theEventSetup) {
00111   delete theEcalImpactPositionFinder_; 
00112 
00113 }
00114 
00115 
00116 void  ConvertedPhotonProducer::beginRun (edm::Run& r, edm::EventSetup const & theEventSetup) {
00117  
00118 
00119   //get magnetic field
00120   edm::LogInfo("ConvertedPhotonProducer") << " get magnetic field" << "\n";
00121   theEventSetup.get<IdealMagneticFieldRecord>().get(theMF_);  
00122   
00123   // instantiate the algorithm for finding the position of the track extrapolation at the Ecal front face
00124   theEcalImpactPositionFinder_ = new   ConversionTrackEcalImpactPoint ( &(*theMF_) );
00125   
00126 
00127 }
00128 
00129 
00130 
00131 
00132 void  ConvertedPhotonProducer::endJob () {
00133   
00134   edm::LogInfo("ConvertedPhotonProducer") << " Analyzed " << nEvt_  << "\n";
00135   LogDebug("ConvertedPhotonProducer") << "ConvertedPhotonProducer::endJob Analyzed " << nEvt_ << " events " << "\n";
00136   
00137   
00138 }
00139 
00140 
00141 
00142 void ConvertedPhotonProducer::produce(edm::Event& theEvent, const edm::EventSetup& theEventSetup) {
00143   
00144   using namespace edm;
00145   nEvt_++;  
00146 
00147   LogDebug("ConvertedPhotonProducer")   << "ConvertedPhotonProduce::produce event number " <<   theEvent.id() << " Global counter " << nEvt_ << "\n";
00148   
00149   //
00150   // create empty output collections
00151   //
00152   // Converted photon candidates
00153   reco::ConversionCollection outputConvPhotonCollection;
00154   std::auto_ptr<reco::ConversionCollection> outputConvPhotonCollection_p(new reco::ConversionCollection);
00155 
00156   
00157   // Get the Super Cluster collection in the Barrel
00158   bool validBarrelSCHandle=true;
00159   edm::Handle<edm::View<reco::CaloCluster> > scBarrelHandle;
00160   theEvent.getByLabel(scHybridBarrelProducer_,scBarrelHandle);
00161   if (!scBarrelHandle.isValid()) {
00162     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product "<<scHybridBarrelProducer_.label();
00163     validBarrelSCHandle=false;
00164   }
00165    
00166   // Get the Super Cluster collection in the Endcap
00167   bool validEndcapSCHandle=true;
00168   edm::Handle<edm::View<reco::CaloCluster> > scEndcapHandle;
00169   theEvent.getByLabel(scIslandEndcapProducer_,scEndcapHandle);
00170   if (!scEndcapHandle.isValid()) {
00171     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product "<<scIslandEndcapProducer_.label();
00172     validEndcapSCHandle=false;
00173   }
00174   
00175     
00177   bool validTrackInputs=true;
00178   Handle<reco::TrackCollection> outInTrkHandle;
00179   theEvent.getByLabel(conversionOITrackProducer_,  outInTrkHandle);
00180   if (!outInTrkHandle.isValid()) {
00181     std::cout << "Error! Can't get the conversionOITrack " << "\n";
00182     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the conversionOITrack " << "\n";
00183     validTrackInputs=false;
00184   }
00185   LogDebug("ConvertedPhotonProducer")<< "ConvertedPhotonProducer  outInTrack collection size " << (*outInTrkHandle).size() << "\n";
00186   
00187    
00189   Handle<reco::TrackCaloClusterPtrAssociation> outInTrkSCAssocHandle;
00190   theEvent.getByLabel( conversionOITrackProducer_, outInTrackSCAssociationCollection_, outInTrkSCAssocHandle);
00191   if (!outInTrkSCAssocHandle.isValid()) {
00192     std::cout << "Error! Can't get the product " <<  outInTrackSCAssociationCollection_.c_str() <<"\n";
00193     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product " <<  outInTrackSCAssociationCollection_.c_str() <<"\n";
00194     validTrackInputs=false;
00195   }
00196 
00198   Handle<reco::TrackCollection> inOutTrkHandle;
00199   theEvent.getByLabel(conversionIOTrackProducer_, inOutTrkHandle);
00200   if (!inOutTrkHandle.isValid()) {
00201     std::cout << "Error! Can't get the conversionIOTrack " << "\n";
00202     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the conversionIOTrack " << "\n";
00203     validTrackInputs=false;
00204   }
00205   LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer inOutTrack collection size " << (*inOutTrkHandle).size() << "\n";
00206 
00207   
00209   Handle<reco::TrackCaloClusterPtrAssociation> inOutTrkSCAssocHandle;
00210   theEvent.getByLabel( conversionIOTrackProducer_, inOutTrackSCAssociationCollection_, inOutTrkSCAssocHandle);
00211   if (!inOutTrkSCAssocHandle.isValid()) {
00212     std::cout << "Error! Can't get the product " <<  inOutTrackSCAssociationCollection_.c_str() <<"\n";
00213     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product " <<  inOutTrackSCAssociationCollection_.c_str() <<"\n";
00214     validTrackInputs=false;
00215   }
00216   
00217 
00218   // Get the basic cluster collection in the Barrel 
00219   bool validBarrelBCHandle=true;
00220   edm::Handle<edm::View<reco::CaloCluster> > bcBarrelHandle;
00221   theEvent.getByLabel( bcBarrelCollection_, bcBarrelHandle);
00222   if (!bcBarrelHandle.isValid()) {
00223     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product "<<bcBarrelCollection_.label();
00224      validBarrelBCHandle=false;
00225   }
00226 
00227     
00228   // Get the basic cluster collection in the Endcap 
00229   bool validEndcapBCHandle=true;
00230   edm::Handle<edm::View<reco::CaloCluster> > bcEndcapHandle;
00231   theEvent.getByLabel( bcEndcapCollection_, bcEndcapHandle);
00232   if (!bcEndcapHandle.isValid()) {
00233     edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the product "<<bcEndcapCollection_.label();
00234     validEndcapBCHandle=true;
00235   }
00236  
00237   
00238   // Transform Track into TransientTrack (needed by the Vertex fitter)
00239   edm::ESHandle<TransientTrackBuilder> theTransientTrackBuilder;
00240   theEventSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theTransientTrackBuilder);
00241 
00242 
00243   if (  validTrackInputs ) {
00244     //do the conversion:
00245     std::vector<reco::TransientTrack> t_outInTrk = ( *theTransientTrackBuilder ).build(outInTrkHandle );
00246     std::vector<reco::TransientTrack> t_inOutTrk = ( *theTransientTrackBuilder ).build(inOutTrkHandle );
00247     
00248     
00250     //  std::map<std::vector<reco::TransientTrack>, const reco::SuperCluster*> allPairs;
00251     std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr> allPairs;
00252     allPairs = theTrackPairFinder_->run(t_outInTrk, outInTrkHandle, outInTrkSCAssocHandle, t_inOutTrk, inOutTrkHandle, inOutTrkSCAssocHandle  );
00253     LogDebug("ConvertedPhotonProducer")  << "ConvertedPhotonProducer  allPairs.size " << allPairs.size() << "\n";      
00254     
00255     buildCollections(scBarrelHandle, bcBarrelHandle, allPairs, outputConvPhotonCollection);
00256     buildCollections(scEndcapHandle, bcEndcapHandle, allPairs, outputConvPhotonCollection);
00257   }
00258   
00259   // put the product in the event
00260   outputConvPhotonCollection_p->assign(outputConvPhotonCollection.begin(),outputConvPhotonCollection.end());
00261   LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Putting in the event    converted photon candidates " << (*outputConvPhotonCollection_p).size() << "\n";  
00262   theEvent.put( outputConvPhotonCollection_p, ConvertedPhotonCollection_);
00263 
00264 
00265 
00266   
00267 }
00268 
00269 
00270 void ConvertedPhotonProducer::buildCollections (  const edm::Handle<edm::View<reco::CaloCluster> > & scHandle,
00271                                                   const edm::Handle<edm::View<reco::CaloCluster> > & bcHandle,
00272                                                   std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr>& allPairs,
00273                                                   reco::ConversionCollection & outputConvPhotonCollection)
00274 
00275 {
00276 
00277   //  Loop over SC in the barrel and reconstruct converted photons
00278   int myCands=0;
00279   reco::CaloClusterPtrVector scPtrVec;
00280   for (unsigned i = 0; i < scHandle->size(); ++i ) {
00281 
00282     reco::CaloClusterPtr aClus= scHandle->ptrAt(i);
00283 
00284     std::vector<edm::Ref<reco::TrackCollection> > trackPairRef;
00285     LogDebug("ConvertedPhotonProducer") << "ConvertedPhotonProducer SC energy " << aClus->energy() << " eta " <<  aClus->eta() << " phi " <<  aClus->phi() << "\n";
00286 
00287     
00289     const reco::Particle::Point  vtx( 0, 0, 0 );
00290     reco::Vertex  theConversionVertex;
00291    
00292     math::XYZVector direction =aClus->position() - vtx;
00293     math::XYZVector momentum = direction.unit() * aClus->energy();
00294     const reco::Particle::LorentzVector  p4(momentum.x(), momentum.y(), momentum.z(), aClus->energy() );
00295     
00296     int nFound=0;    
00297     if ( allPairs.size() ) {
00298 
00299       nFound=0;
00300 
00301 
00302       for (  std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr>::const_iterator iPair= allPairs.begin(); iPair!= allPairs.end(); ++iPair ) {
00303         scPtrVec.clear();
00304        
00305         reco::CaloClusterPtr caloPtr=iPair->second;
00306         if ( !( aClus == caloPtr ) ) continue;
00307             
00308         scPtrVec.push_back(aClus);     
00309         nFound++;
00310         
00311 
00312         const string metname = "ConvertedPhotons|ConvertedPhotonProducer";
00313         if ( (iPair->first).size()  > 1 ) {
00314           try{
00315             
00316             TransientVertex trVtx=theVertexFinder_->run(iPair->first); 
00317             theConversionVertex= trVtx;
00318             
00319           }
00320           catch ( cms::Exception& e ) {
00321             std::cout << " cms::Exception caught in ConvertedPhotonProducer::produce" << "\n" ;
00322             edm::LogWarning(metname) << "cms::Exception caught in ConvertedPhotonProducer::produce\n"
00323                                      << e.explainSelf();
00324             
00325           }
00326           
00327         }
00328         
00329 
00330         std::vector<math::XYZPoint> trkPositionAtEcal = theEcalImpactPositionFinder_->find(  iPair->first, bcHandle );
00331         std::vector<reco::CaloClusterPtr>  matchingBC = theEcalImpactPositionFinder_->matchingBC();
00332         
00333 
00334         /*
00335         for ( unsigned int i=0; i< matchingBC.size(); ++i) {
00336           if (  matchingBC[i].isNull() )  std::cout << " This ref to BC is null: skipping " <<  "\n";
00337           else 
00338             std::cout << " BC energy " << matchingBC[i]->energy() <<  "\n";
00339         }
00340         */
00341 
00342 
00344         trackPairRef.clear();
00345 
00346         
00347         for ( std::vector<reco::TransientTrack>::const_iterator iTk=(iPair->first).begin(); iTk!= (iPair->first).end(); ++iTk) {
00348           LogDebug("ConvertedPhotonProducer")  << "  ConvertedPhotonProducer Transient Tracks in the pair  charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << "\n";  
00349           
00350           const reco::TrackTransientTrack* ttt = dynamic_cast<const reco::TrackTransientTrack*>(iTk->basicTransientTrack());
00351           reco::TrackRef myTkRef= ttt->persistentTrackRef(); 
00352           
00353           LogDebug("ConvertedPhotonProducer")  << " ConvertedPhotonProducer Ref to Rec Tracks in the pair  charge " << myTkRef->charge() << " Num of RecHits " << myTkRef->recHitsSize() << " inner momentum " << myTkRef->innerMomentum() << "\n";  
00354           
00355           
00356           trackPairRef.push_back(myTkRef);
00357           
00358         }
00359         
00360         
00361         
00362         LogDebug("ConvertedPhotonProducer")  << " ConvertedPhotonProducer SC energy " <<  aClus->energy() << "\n";
00363         LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer photon p4 " << p4  << "\n";
00364         LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer vtx " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
00365         if( theConversionVertex.isValid() ) {
00366           
00367           LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer theConversionVertex " << theConversionVertex.position().x() << " " << theConversionVertex.position().y() << " " << theConversionVertex.position().z() << "\n";
00368           
00369         }
00370         LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer trackPairRef  " << trackPairRef.size() <<  "\n";
00371 
00372         
00373         reco::Conversion  newCandidate(scPtrVec,  trackPairRef, trkPositionAtEcal, theConversionVertex, matchingBC);
00374         outputConvPhotonCollection.push_back(newCandidate);
00375         
00376         
00377         
00378         myCands++;
00379         LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Put the ConvertedPhotonCollection a candidate in the Barrel " << "\n";
00380         
00381       }
00382       
00383     }
00384      
00385 
00386     if (  allPairs.size() ==0 || nFound ==0) {
00387       LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer GOLDEN PHOTON ?? Zero Tracks " <<  "\n";  
00388       LogDebug("ConvertedPhotonProducer")  << " ConvertedPhotonProducer SC energy " <<  aClus->energy() << "\n";
00389       LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer photon p4 " << p4  << "\n";
00390       LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer vtx " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
00391       LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer trackPairRef  " << trackPairRef.size() <<  "\n";
00392       
00393       std::vector<math::XYZPoint> trkPositionAtEcal;
00394       std::vector<reco::CaloClusterPtr> matchingBC;
00395 
00396       scPtrVec.clear();
00397       scPtrVec.push_back(aClus);     
00398       reco::Conversion  newCandidate(scPtrVec,  trackPairRef, trkPositionAtEcal, theConversionVertex, matchingBC);
00399       outputConvPhotonCollection.push_back(newCandidate);
00400 
00401      
00402       if ( newCandidate.conversionVertex().isValid() ) 
00403         LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer theConversionVertex " <<  newCandidate.conversionVertex().position().x() << " " <<  newCandidate.conversionVertex().position().y() << " " <<  newCandidate.conversionVertex().position().z() << "\n";
00404       
00405 
00406      
00407       
00408       myCands++;
00409       LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Put the ConvertedPhotonCollection a candidate in the Barrel " << "\n";
00410       
00411     }
00412 
00413     
00414   
00415 
00416     
00417   }
00418   
00419 
00420 
00421 
00422 
00423 }
00424 
00425 
00426 

Generated on Tue Jun 9 17:43:27 2009 for CMSSW by  doxygen 1.5.4