CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/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 
00073   // Loop over the hard jets
00074   std::vector<CompoundPseudoJet>::const_iterator it = fjCompoundJets_.begin(),
00075     iEnd = fjCompoundJets_.end(),
00076     iBegin = fjCompoundJets_.begin();
00077   indices.resize( fjCompoundJets_.size() );
00078   for ( ; it != iEnd; ++it ) {
00079     int jetIndex = it - iBegin;
00080     fastjet::PseudoJet localJet = it->hardJet();
00081     // Get the 4-vector for the hard jet
00082     p4_hardJets.push_back( math::XYZTLorentzVector(localJet.px(), localJet.py(), localJet.pz(), localJet.e() ));
00083 
00084     // create the subjet list
00085     std::vector<CompoundPseudoSubJet>::const_iterator itSubJetBegin = it->subjets().begin(),
00086       itSubJet = itSubJetBegin, itSubJetEnd = it->subjets().end();
00087     for (; itSubJet != itSubJetEnd; ++itSubJet ){
00088 
00089       fastjet::PseudoJet subjet = itSubJet->subjet();
00090       math::XYZTLorentzVector p4Subjet(subjet.px(), subjet.py(), subjet.pz(), subjet.e() );
00091       reco::Particle::Point point(0,0,0);
00092 
00093       // This will hold ptr's to the subjets
00094       std::vector<reco::CandidatePtr> subjetConstituents;
00095 
00096       // Get the transient subjet constituents from fastjet
00097       std::vector<int> const & subjetFastjetConstituentIndices = itSubJet->constituents();
00098       std::vector<int>::const_iterator fastSubIt = subjetFastjetConstituentIndices.begin(),
00099         transConstBegin = subjetFastjetConstituentIndices.begin(),
00100         transConstEnd = subjetFastjetConstituentIndices.end();
00101       for ( ; fastSubIt != transConstEnd; ++fastSubIt ) {
00102         // Add a ptr to this constituent
00103         if ( *fastSubIt < static_cast<int>(inputs_.size()) ) 
00104           subjetConstituents.push_back( inputs_[*fastSubIt] );
00105       }
00106 
00107       // This holds the subjet-to-hardjet mapping
00108       indices[jetIndex].push_back( subjetCollection->size() );      
00109 
00110 
00111       // Add the concrete subjet type to the subjet list to write to event record
00112       T jet;
00113       reco::writeSpecific( jet, p4Subjet, point, subjetConstituents, iSetup);
00114       subjetCollection->push_back( jet );
00115 
00116     }
00117   }
00118   // put subjets into event record
00119   subjetHandleAfterPut = iEvent.put( subjetCollection, jetCollInstanceName_ );
00120   
00121   
00122   // Now create the hard jets with ptr's to the subjets as constituents
00123   std::vector<math::XYZTLorentzVector>::const_iterator ip4 = p4_hardJets.begin(),
00124     ip4Begin = p4_hardJets.begin(),
00125     ip4End = p4_hardJets.end();
00126 
00127   for ( ; ip4 != ip4End; ++ip4 ) {
00128     int p4_index = ip4 - ip4Begin;
00129     std::vector<int> & ind = indices[p4_index];
00130     std::vector<reco::CandidatePtr> i_hardJetConstituents;
00131     // Add the subjets to the hard jet
00132     for( std::vector<int>::const_iterator isub = ind.begin();
00133          isub != ind.end(); ++isub ) {
00134       reco::CandidatePtr candPtr( subjetHandleAfterPut, *isub, false );
00135       i_hardJetConstituents.push_back( candPtr );
00136     }   
00137     reco::Particle::Point point(0,0,0);
00138     jetCollection->push_back( reco::BasicJet( *ip4, point, i_hardJetConstituents) );
00139   }
00140   
00141   // put hard jets into event record
00142   iEvent.put( jetCollection);
00143 
00144 }