CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FwdPtrCollectionFilter.h
Go to the documentation of this file.
1 #ifndef CommonTools_UtilAlgos_FwdPtrCollectionFilter_h
2 #define CommonTools_UtilAlgos_FwdPtrCollectionFilter_h
3 
4 
22 #include <vector>
23 
24 namespace edm {
25 
26  template < class T, class S, class H = ProductFromFwdPtrFactory<T> >
28  public :
29  explicit FwdPtrCollectionFilter() {}
30 
31  explicit FwdPtrCollectionFilter( edm::ParameterSet const & params ) :
32  srcToken_( consumes< std::vector< edm::FwdPtr<T> > >( params.getParameter<edm::InputTag>("src") ) ),
33  srcViewToken_( mayConsume< edm::View<T> >( params.getParameter<edm::InputTag>("src") ) ),
35  selector_( params )
36  {
37  if ( params.exists("filter") ) {
38  filter_ = params.getParameter<bool>("filter");
39  }
40  if ( params.exists("makeClones") ) {
41  makeClones_ = params.getParameter<bool>("makeClones");
42  }
43 
44  produces< std::vector< edm::FwdPtr<T> > > ();
45  if ( makeClones_ ) {
46  produces< std::vector<T> > ();
47  }
48  }
49 
51 
52  virtual bool filter(edm::Event & iEvent, const edm::EventSetup& iSetup){
53 
54  std::auto_ptr< std::vector< edm::FwdPtr<T> > > pOutput ( new std::vector<edm::FwdPtr<T> > );
55 
56  std::auto_ptr< std::vector<T> > pClones ( new std::vector<T> );
57 
58 
60  edm::Handle< edm::View<T> > hSrcAsView;
61  bool foundAsFwdPtr = iEvent.getByToken( srcToken_, hSrcAsFwdPtr );
62  if ( !foundAsFwdPtr ) {
63  iEvent.getByToken( srcViewToken_, hSrcAsView );
64  }
65 
66  // First try to access as a View<T>. If not a View<T>, look as a vector<FwdPtr<T> >
67  if ( !foundAsFwdPtr ) {
68  for ( typename edm::View<T>::const_iterator ibegin = hSrcAsView->begin(),
69  iend = hSrcAsView->end(),
70  i = ibegin; i!= iend; ++i ) {
71  if ( selector_( *i ) ) {
72  pOutput->push_back( edm::FwdPtr<T>( hSrcAsView->ptrAt( i - ibegin ), hSrcAsView->ptrAt( i - ibegin ) ) );
73  if ( makeClones_ ) {
74  H factory;
75  T outclone = factory( pOutput->back() );
76  pClones->push_back( outclone );
77  }
78  }
79  }
80  } else {
81  for ( typename std::vector<edm::FwdPtr<T> >::const_iterator ibegin = hSrcAsFwdPtr->begin(),
82  iend = hSrcAsFwdPtr->end(),
83  i = ibegin; i!= iend; ++i ) {
84  if ( selector_( **i ) ) {
85  pOutput->push_back( *i );
86  if ( makeClones_ ) {
87  H factory;
88  T outclone = factory( pOutput->back() );
89  pClones->push_back( outclone );
90  }
91  }
92  }
93 
94  }
95 
96  bool pass = pOutput->size() > 0;
97  iEvent.put( pOutput );
98  if ( makeClones_ )
99  iEvent.put( pClones );
100  if ( filter_ )
101  return pass;
102  else
103  return true;
104 
105  }
106 
107  protected :
110  bool filter_;
113  };
114 }
115 
116 #endif
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
FwdPtrCollectionFilter(edm::ParameterSet const &params)
bool exists(std::string const &parameterName) const
checks if a parameter exists
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
edm::EDGetTokenT< edm::View< T > > srcViewToken_
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup)
Selects a list of FwdPtr&#39;s to a product T (templated) that satisfy a method S(T) (templated). Can also handle input as View&lt;T&gt;. Can also have a factory class to create new instances of clones if desired.
edm::EDGetTokenT< std::vector< edm::FwdPtr< T > > > srcToken_
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
volatile std::atomic< bool > shutdown_flag false
long double T
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)