00001 #ifndef HepMCCandAlgos_MCTruthPairSelector_h 00002 #define HepMCCandAlgos_MCTruthPairSelector_h 00003 /* \class MCTruthPairSelector 00004 * 00005 * \author Luca Lista, INFN 00006 * 00007 */ 00008 00009 #include <set> 00010 #include "DataFormats/Candidate/interface/Candidate.h" 00011 00012 namespace helpers { 00013 template<typename T> 00014 struct MCTruthPairSelector { 00015 MCTruthPairSelector( bool checkCharge = false ) : 00016 checkCharge_( checkCharge ) { } 00017 template<typename I> 00018 MCTruthPairSelector( const I & begin, const I & end, bool checkCharge = false ) : 00019 checkCharge_( checkCharge ) { 00020 for( I i = begin; i != end; ++i ) 00021 matchIds_.insert( abs( * i ) ); 00022 } 00023 bool operator()( const T & c, const reco::Candidate & mc ) const { 00024 if ( mc.status() != 1 ) return false; 00025 if ( checkCharge_ && c.charge() != mc.charge() ) return false; 00026 if ( matchIds_.size() == 0 ) return true; 00027 return matchIds_.find( abs( mc.pdgId() ) ) != matchIds_.end(); 00028 } 00029 private: 00030 std::set<int> matchIds_; 00031 bool checkCharge_; 00032 }; 00033 } 00034 00035 #include "PhysicsTools/UtilAlgos/interface/ParameterAdapter.h" 00036 #include <algorithm> 00037 #include <string> 00038 #include <vector> 00039 00040 namespace reco { 00041 namespace modules { 00042 00043 template<typename T> 00044 struct ParameterAdapter<helpers::MCTruthPairSelector<T> > { 00045 static helpers::MCTruthPairSelector<T> make( const edm::ParameterSet & cfg ) { 00046 using namespace std; 00047 const string matchPDGId( "matchPDGId" ); 00048 const string checkCharge( "checkCharge" ); 00049 bool ck = false; 00050 vector<string> bools = cfg.template getParameterNamesForType<bool>(); 00051 bool found = find( bools.begin(), bools.end(), checkCharge ) != bools.end(); 00052 if (found) ck = cfg.template getParameter<bool>( checkCharge ); 00053 typedef vector<int> vint; 00054 vector<string> ints = cfg.template getParameterNamesForType<vint>(); 00055 found = find( ints.begin(), ints.end(), matchPDGId ) != ints.end(); 00056 if ( found ) { 00057 vint ids = cfg.template getParameter<vint>( matchPDGId ); 00058 return helpers::MCTruthPairSelector<T>( ids.begin(), ids.end(), ck ); 00059 } else { 00060 return helpers::MCTruthPairSelector<T>( ck ); 00061 } 00062 } 00063 }; 00064 00065 } 00066 } 00067 00068 #endif