CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TopProjector.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PFCandProducer_TopProjector_
2 #define PhysicsTools_PFCandProducer_TopProjector_
3 
4 // system include files
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 
9 // user include files
13 
16 
18 
22 
24 
25 
33 #include <iostream>
34 
35 
36 template< class Top, class Bottom>
37 class TopProjector : public edm::EDProducer {
38 
39  public:
40 
41  typedef std::vector<Top> TopCollection;
43  typedef std::vector<Bottom> BottomCollection;
46 
48 
50 
51 
52  void produce(edm::Event&, const edm::EventSetup&);
53 
54 
55  private:
56 
60  void
62  reco::CandidatePtrVector& ancestors,
63  const edm::ProductID& ancestorsID,
64  const edm::Event& iEvent ) const;
65 
69  void maskAncestors( const reco::CandidatePtrVector& ancestors,
70  std::vector<bool>& masked ) const;
71 
72 
73  void processCollection( const edm::Handle< std::vector<Top> >& handle,
74  const edm::Handle< std::vector<Bottom> >& allPFCandidates ,
75  std::vector<bool>& masked,
76  const char* objectName,
77  const edm::Event& iEvent ) const;
78 
79  void printAncestors( const reco::CandidatePtrVector& ancestors,
80  const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const;
81 
82 
84  bool enable_;
85 
87  bool verbose_;
88 
90  std::string name_;
91 
94 
97 };
98 
99 
100 
101 
102 template< class Top, class Bottom>
104  enable_(iConfig.getParameter<bool>("enable")) {
105 
106  verbose_ = iConfig.getUntrackedParameter<bool>("verbose",false);
107  name_ = iConfig.getUntrackedParameter<std::string>("name","No Name");
108  inputTagTop_ = iConfig.getParameter<edm::InputTag>("topCollection");
109  inputTagBottom_ = iConfig.getParameter<edm::InputTag>("bottomCollection");
110 
111  // will produce a collection of the unmasked candidates in the
112  // bottom collection
113  produces< std::vector<Bottom> >();
114 }
115 
116 
117 template< class Top, class Bottom >
119  const edm::EventSetup& iSetup) {
120 
121  if( verbose_)
122  std::cout<<"Event -------------------- "<<iEvent.id().event()<<std::endl;
123 
124  // get the various collections
125 
126  // Access the masking collection
127  TopHandle tops;
128  iEvent.getByLabel( inputTagTop_, tops );
129 
130 
131 /* if( !tops.isValid() ) { */
132 /* std::ostringstream err; */
133 /* err<<"The top collection must be supplied."<<std::endl */
134 /* <<"It is now set to : "<<inputTagTop_<<std::endl; */
135 /* edm::LogError("PFPAT")<<err.str(); */
136 /* throw cms::Exception( "MissingProduct", err.str()); */
137 /* } */
138 
139  edm::ProductID topsID = tops.id();
140 
141 
142 
143  // Access the collection to
144  // be masked by the other ones
145  BottomHandle bottoms;
146  iEvent.getByLabel( inputTagBottom_, bottoms );
147 
148 /* if( !bottoms.isValid() ) { */
149 /* std::ostringstream err; */
150 /* err<<"The bottom collection must be supplied."<<std::endl */
151 /* <<"It is now set to : "<<inputTagBottom_<<std::endl; */
152 /* edm::LogError("PFPAT")<<err.str(); */
153 /* throw cms::Exception( "MissingProduct", err.str()); */
154 /* } */
155 
156  edm::ProductID bottomsID = bottoms.id();
157 
158 
159  if(verbose_) {
160  const edm::Provenance& topProv = iEvent.getProvenance(tops.id());
161  const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id());
162 
163  std::cout<<"Top projector: event "<<iEvent.id().event()<<std::endl;
164  std::cout<<"Inputs --------------------"<<std::endl;
165  std::cout<<"Top : "
166  <<tops.id()<<"\t"<<tops->size()<<std::endl
167  <<topProv.branchDescription()<<std::endl
168  <<"Bottom : "
169  <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl
170  <<bottomProv.branchDescription()<<std::endl;
171  }
172 
173 
174  // output collection of objects,
175  // selected from the Bottom collection
176 
177  std::auto_ptr< BottomCollection >
178  pBottomOutput( new BottomCollection );
179 
180  // mask for each bottom object.
181  // at the beginning, all bottom objects are unmasked.
182  std::vector<bool> masked( bottoms->size(), false);
183 
184  if( enable_ )
185  processCollection( tops, bottoms, masked, name_.c_str(), iEvent );
186 
187  const BottomCollection& inCands = *bottoms;
188 
189  if(verbose_)
190  std::cout<<" Remaining candidates in the bottom collection ------ "<<std::endl;
191 
192  for(unsigned i=0; i<inCands.size(); i++) {
193 
194  if(masked[i]) {
195  if(verbose_)
196  std::cout<<"X "<<i<<" "<<inCands[i]<<std::endl;
197  continue;
198  }
199  else {
200  if(verbose_)
201  std::cout<<"O "<<i<<" "<<inCands[i]<<std::endl;
202 
203  pBottomOutput->push_back( inCands[i] );
204  BottomPtr motherPtr( bottoms, i );
205  pBottomOutput->back().setSourceCandidatePtr(motherPtr);
206  }
207  }
208 
209  iEvent.put( pBottomOutput );
210 }
211 
212 
213 
214 template< class Top, class Bottom >
215 void TopProjector< Top, Bottom >::processCollection( const edm::Handle< std::vector<Top> >& tops,
216  const edm::Handle< std::vector<Bottom> >& bottoms ,
217  std::vector<bool>& masked,
218  const char* objectName,
219  const edm::Event& iEvent) const {
220 
221  if( tops.isValid() && bottoms.isValid() ) {
222  const std::vector<Top>& topCollection = *tops;
223 
224  if(verbose_)
225  std::cout<<"******* TopProjector "<<objectName
226  <<" size = "<<topCollection.size()<<" ******** "<<std::endl;
227 
228  for(unsigned i=0; i<topCollection.size(); i++) {
229 
230 
231  edm::Ptr<Top> ptr( tops, i);
232  reco::CandidatePtr basePtr( ptr );
233 
234 
235  reco::CandidatePtrVector ancestors;
236  ptrToAncestor( basePtr,
237  ancestors,
238  bottoms.id(),
239  iEvent );
240 
241  if(verbose_) {
242 /* std::cout<<"\t"<<objectName<<" "<<i */
243 /* <<" pt,eta,phi = " */
244 /* <<basePtr->pt()<<"," */
245 /* <<basePtr->eta()<<"," */
246 /* <<basePtr->phi()<<std::endl; */
247 
248  std::cout<<"\t"<<topCollection[i]<<std::endl;
249  printAncestors( ancestors, bottoms );
250  }
251 
252  maskAncestors( ancestors, masked );
253  }
254  }
255 
256 }
257 
258 
259 template< class Top, class Bottom >
261  const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const {
262 
263 
264  std::vector<Bottom> pfs = *allPFCandidates;
265 
266  for(unsigned i=0; i<ancestors.size(); i++) {
267 
268  edm::ProductID id = ancestors[i].id();
269  assert( id == allPFCandidates.id() );
270 
271  unsigned index = ancestors[i].key();
272  assert( index < pfs.size() );
273 
274  std::cout<<"\t\t"<<pfs[index]<<std::endl;
275  }
276 }
277 
278 
279 
280 template< class Top, class Bottom >
281 void
283  reco::CandidatePtrVector& ancestors,
284  const edm::ProductID& ancestorsID,
285  const edm::Event& iEvent) const {
286 
287 
288  unsigned nSources = candPtr->numberOfSourceCandidatePtrs();
289 
290  if(verbose_) {
291  const edm::Provenance& hereProv = iEvent.getProvenance(candPtr.id());
292 
293  std::cout<<"going down from "<<candPtr.id()
294  <<"/"<<candPtr.key()<<" #mothers "<<nSources
295  <<" ancestor id "<<ancestorsID<<std::endl
296  <<hereProv.branchDescription()<<std::endl;
297  }
298 
299  for(unsigned i=0; i<nSources; i++) {
300 
301  reco::CandidatePtr mother = candPtr->sourceCandidatePtr(i);
302  if( verbose_ ) {
303 /* const Provenance& motherProv = iEvent.getProvenance(mother.id()); */
304  std::cout<<" mother id "<<mother.id()<<std::endl;
305  }
306  if( mother.id() != ancestorsID ) {
307  // the mother is not yet at lowest level
308  ptrToAncestor( mother, ancestors, ancestorsID, iEvent );
309  }
310  else {
311  // adding mother to the list of ancestors
312  ancestors.push_back( mother );
313  }
314  }
315 }
316 
317 
318 
319 
320 template< class Top, class Bottom >
322  std::vector<bool>& masked ) const {
323 
324  for(unsigned i=0; i<ancestors.size(); i++) {
325  unsigned index = ancestors[i].key();
326  assert( index<masked.size() );
327 
328  // if(verbose_) {
329  // ProductID id = ancestors[i].id();
330  // std::cout<<"\tmasking "<<index<<", ancestor "<<id<<"/"<<index<<std::endl;
331  // }
332  masked[index] = true;
333  }
334 }
335 
336 
337 #endif
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:44
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
edm::Handle< std::vector< Top > > TopHandle
Definition: TopProjector.h:42
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:73
tuple topCollection
std::vector< Bottom > BottomCollection
Definition: TopProjector.h:43
std::vector< Top > TopCollection
Definition: TopProjector.h:41
ProductID id() const
Definition: HandleBase.cc:15
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:137
edm::InputTag inputTagTop_
input tag for the top (masking) collection
Definition: TopProjector.h:93
bool verbose_
verbose ?
Definition: TopProjector.h:87
ProductID id() const
Accessor for product ID.
Definition: PtrVectorBase.h:56
bool enable_
enable? if not, all candidates in the bottom collection are copied to the output collection ...
Definition: TopProjector.h:84
int iEvent
Definition: GenABIO.cc:243
edm::Handle< std::vector< Bottom > > BottomHandle
Definition: TopProjector.h:44
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:169
TopProjector(const edm::ParameterSet &)
Definition: TopProjector.h:103
tuple handle
Definition: patZpeak.py:22
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
void ptrToAncestor(reco::CandidatePtr candRef, reco::CandidatePtrVector &ancestors, const edm::ProductID &ancestorsID, const edm::Event &iEvent) const
Definition: TopProjector.h:282
void processCollection(const edm::Handle< std::vector< Top > > &handle, const edm::Handle< std::vector< Bottom > > &allPFCandidates, std::vector< bool > &masked, const char *objectName, const edm::Event &iEvent) const
Definition: TopProjector.h:215
key_type key() const
Definition: Ptr.h:174
BranchDescription const & branchDescription() const
Definition: Provenance.h:46
edm::InputTag inputTagBottom_
input tag for the masked collection.
Definition: TopProjector.h:96
std::string name_
name of the top projection
Definition: TopProjector.h:90
edm::EventID id() const
Definition: EventBase.h:56
void produce(edm::Event &, const edm::EventSetup &)
Definition: TopProjector.h:118
tuple cout
Definition: gather_cfg.py:41
edm::Ptr< Bottom > BottomPtr
Definition: TopProjector.h:45
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:61
void maskAncestors(const reco::CandidatePtrVector &ancestors, std::vector< bool > &masked) const
Definition: TopProjector.h:321
void printAncestors(const reco::CandidatePtrVector &ancestors, const edm::Handle< std::vector< Bottom > > &allPFCandidates) const
Definition: TopProjector.h:260