CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoJets/JetProducers/plugins/CompoundJetProducer.cc

Go to the documentation of this file.
00001 
00002 #include "FWCore/Framework/interface/MakerMacros.h"
00003 #include "RecoJets/JetProducers/plugins/CompoundJetProducer.h"
00004 #include "FWCore/Utilities/interface/Exception.h"
00005 #include "RecoJets/JetProducers/interface/JetSpecific.h"
00006 
00007 using namespace std;
00008 using namespace reco;
00009 using namespace edm;
00010 using namespace cms;
00011 
00012 namespace {
00013   const bool debug = false;
00014 
00015 }
00016 
00017 
00018 CompoundJetProducer::CompoundJetProducer(edm::ParameterSet const& conf):
00019   VirtualJetProducer( conf )
00020 {
00021 
00022   produces<reco::BasicJetCollection>();
00023   // the subjet collections are set through the config file in the "jetCollInstanceName" field.
00024 }
00025 
00026 
00027 void CompoundJetProducer::inputTowers( ) 
00028 {
00029   fjCompoundJets_.clear();
00030   VirtualJetProducer::inputTowers();
00031 }
00032 
00033 void CompoundJetProducer::output(edm::Event & iEvent, edm::EventSetup const& iSetup)
00034 {
00035   // Write jets and constitutents. Will use fjJets_. 
00036   switch( jetTypeE ) {
00037   case JetType::CaloJet :
00038     writeCompoundJets<reco::CaloJet>( iEvent, iSetup );
00039     break;
00040   case JetType::PFJet :
00041     writeCompoundJets<reco::PFJet>( iEvent, iSetup );
00042     break;
00043   case JetType::GenJet :
00044     writeCompoundJets<reco::GenJet>( iEvent, iSetup );
00045     break;
00046   case JetType::BasicJet :
00047     writeCompoundJets<reco::BasicJet>( iEvent, iSetup );
00048     break;
00049   default:
00050     throw cms::Exception("InvalidInput") << "invalid jet type in CompoundJetProducer\n";
00051     break;
00052   };
00053 
00054 }
00055 
00057 template< class T>
00058 void CompoundJetProducer::writeCompoundJets(  edm::Event & iEvent, edm::EventSetup const& iSetup)
00059 {
00060 
00061   // get a list of output jets
00062   std::auto_ptr<reco::BasicJetCollection>  jetCollection( new reco::BasicJetCollection() );
00063   // get a list of output subjets
00064   std::auto_ptr<std::vector<T> >  subjetCollection( new std::vector<T>() );
00065 
00066   // This will store the handle for the subjets after we write them
00067   edm::OrphanHandle< std::vector<T> > subjetHandleAfterPut;
00068   // this is the mapping of subjet to hard jet
00069   std::vector< std::vector<int> > indices;
00070   // this is the list of hardjet 4-momenta
00071   std::vector<math::XYZTLorentzVector> p4_hardJets;
00072   // this is the hardjet areas
00073   std::vector<double> area_hardJets;
00074 
00075 
00076   // Loop over the hard jets
00077   std::vector<CompoundPseudoJet>::const_iterator it = fjCompoundJets_.begin(),
00078     iEnd = fjCompoundJets_.end(),
00079     iBegin = fjCompoundJets_.begin();
00080   indices.resize( fjCompoundJets_.size() );
00081   for ( ; it != iEnd; ++it ) {
00082     int jetIndex = it - iBegin;
00083     fastjet::PseudoJet localJet = it->hardJet();
00084     // Get the 4-vector for the hard jet
00085     p4_hardJets.push_back( math::XYZTLorentzVector(localJet.px(), localJet.py(), localJet.pz(), localJet.e() ));
00086     area_hardJets.push_back( it->hardJetArea() );
00087 
00088     // create the subjet list
00089     std::vector<CompoundPseudoSubJet>::const_iterator itSubJetBegin = it->subjets().begin(),
00090       itSubJet = itSubJetBegin, itSubJetEnd = it->subjets().end();
00091     for (; itSubJet != itSubJetEnd; ++itSubJet ){
00092 
00093       fastjet::PseudoJet subjet = itSubJet->subjet();
00094       math::XYZTLorentzVector p4Subjet(subjet.px(), subjet.py(), subjet.pz(), subjet.e() );
00095       reco::Particle::Point point(0,0,0);
00096 
00097       // This will hold ptr's to the subjets
00098       std::vector<reco::CandidatePtr> subjetConstituents;
00099 
00100       // Get the transient subjet constituents from fastjet
00101       std::vector<int> const & subjetFastjetConstituentIndices = itSubJet->constituents();
00102       std::vector<int>::const_iterator fastSubIt = subjetFastjetConstituentIndices.begin(),
00103         transConstEnd = subjetFastjetConstituentIndices.end();
00104       for ( ; fastSubIt != transConstEnd; ++fastSubIt ) {
00105         // Add a ptr to this constituent
00106         if ( *fastSubIt < static_cast<int>(inputs_.size()) ) 
00107           subjetConstituents.push_back( inputs_[*fastSubIt] );
00108       }
00109 
00110       // This holds the subjet-to-hardjet mapping
00111       indices[jetIndex].push_back( subjetCollection->size() );      
00112 
00113 
00114       // Add the concrete subjet type to the subjet list to write to event record
00115       T jet;
00116       reco::writeSpecific( jet, p4Subjet, point, subjetConstituents, iSetup);
00117       jet.setJetArea( itSubJet->subjetArea() );
00118       subjetCollection->push_back( jet );
00119 
00120     }
00121   }
00122   // put subjets into event record
00123   subjetHandleAfterPut = iEvent.put( subjetCollection, jetCollInstanceName_ );
00124   
00125   
00126   // Now create the hard jets with ptr's to the subjets as constituents
00127   std::vector<math::XYZTLorentzVector>::const_iterator ip4 = p4_hardJets.begin(),
00128     ip4Begin = p4_hardJets.begin(),
00129     ip4End = p4_hardJets.end();
00130 
00131   for ( ; ip4 != ip4End; ++ip4 ) {
00132     int p4_index = ip4 - ip4Begin;
00133     std::vector<int> & ind = indices[p4_index];
00134     std::vector<reco::CandidatePtr> i_hardJetConstituents;
00135     // Add the subjets to the hard jet
00136     for( std::vector<int>::const_iterator isub = ind.begin();
00137          isub != ind.end(); ++isub ) {
00138       reco::CandidatePtr candPtr( subjetHandleAfterPut, *isub, false );
00139       i_hardJetConstituents.push_back( candPtr );
00140     }   
00141     reco::Particle::Point point(0,0,0);
00142     reco::BasicJet toput( *ip4, point, i_hardJetConstituents);
00143     toput.setJetArea( area_hardJets[ip4 - ip4Begin] );
00144     jetCollection->push_back( toput );
00145   }
00146   
00147   // put hard jets into event record
00148   iEvent.put( jetCollection);
00149 
00150 }