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 
140 
141 
142  // Access the collection to
143  // be masked by the other ones
144  BottomHandle bottoms;
145  iEvent.getByLabel( inputTagBottom_, bottoms );
146 
147 /* if( !bottoms.isValid() ) { */
148 /* std::ostringstream err; */
149 /* err<<"The bottom collection must be supplied."<<std::endl */
150 /* <<"It is now set to : "<<inputTagBottom_<<std::endl; */
151 /* edm::LogError("PFPAT")<<err.str(); */
152 /* throw cms::Exception( "MissingProduct", err.str()); */
153 /* } */
154 
155 
156  if(verbose_) {
157  const edm::Provenance& topProv = iEvent.getProvenance(tops.id());
158  const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id());
159 
160  std::cout<<"Top projector: event "<<iEvent.id().event()<<std::endl;
161  std::cout<<"Inputs --------------------"<<std::endl;
162  std::cout<<"Top : "
163  <<tops.id()<<"\t"<<tops->size()<<std::endl
164  <<topProv.branchDescription()<<std::endl
165  <<"Bottom : "
166  <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl
167  <<bottomProv.branchDescription()<<std::endl;
168  }
169 
170 
171  // output collection of objects,
172  // selected from the Bottom collection
173 
174  std::auto_ptr< BottomCollection >
175  pBottomOutput( new BottomCollection );
176 
177  // mask for each bottom object.
178  // at the beginning, all bottom objects are unmasked.
179  std::vector<bool> masked( bottoms->size(), false);
180 
181  if( enable_ )
182  processCollection( tops, bottoms, masked, name_.c_str(), iEvent );
183 
184  const BottomCollection& inCands = *bottoms;
185 
186  if(verbose_)
187  std::cout<<" Remaining candidates in the bottom collection ------ "<<std::endl;
188 
189  for(unsigned i=0; i<inCands.size(); i++) {
190 
191  if(masked[i]) {
192  if(verbose_)
193  std::cout<<"X "<<i<<" "<<inCands[i]<<std::endl;
194  continue;
195  }
196  else {
197  if(verbose_)
198  std::cout<<"O "<<i<<" "<<inCands[i]<<std::endl;
199 
200  pBottomOutput->push_back( inCands[i] );
201  BottomPtr motherPtr( bottoms, i );
202  pBottomOutput->back().setSourceCandidatePtr(motherPtr);
203  }
204  }
205 
206  iEvent.put( pBottomOutput );
207 }
208 
209 
210 
211 template< class Top, class Bottom >
212 void TopProjector< Top, Bottom >::processCollection( const edm::Handle< std::vector<Top> >& tops,
213  const edm::Handle< std::vector<Bottom> >& bottoms ,
214  std::vector<bool>& masked,
215  const char* objectName,
216  const edm::Event& iEvent) const {
217 
218  if( tops.isValid() && bottoms.isValid() ) {
219  const std::vector<Top>& topCollection = *tops;
220 
221  if(verbose_)
222  std::cout<<"******* TopProjector "<<objectName
223  <<" size = "<<topCollection.size()<<" ******** "<<std::endl;
224 
225  for(unsigned i=0; i<topCollection.size(); i++) {
226 
227 
228  edm::Ptr<Top> ptr( tops, i);
229  reco::CandidatePtr basePtr( ptr );
230 
231 
232  reco::CandidatePtrVector ancestors;
233  ptrToAncestor( basePtr,
234  ancestors,
235  bottoms.id(),
236  iEvent );
237 
238  if(verbose_) {
239 /* std::cout<<"\t"<<objectName<<" "<<i */
240 /* <<" pt,eta,phi = " */
241 /* <<basePtr->pt()<<"," */
242 /* <<basePtr->eta()<<"," */
243 /* <<basePtr->phi()<<std::endl; */
244 
245  std::cout<<"\t"<<topCollection[i]<<std::endl;
246  printAncestors( ancestors, bottoms );
247  }
248 
249  maskAncestors( ancestors, masked );
250  }
251  }
252 
253 }
254 
255 
256 template< class Top, class Bottom >
258  const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const {
259 
260 
261  std::vector<Bottom> pfs = *allPFCandidates;
262 
263  for(unsigned i=0; i<ancestors.size(); i++) {
264 
265  edm::ProductID id = ancestors[i].id();
266  assert( id == allPFCandidates.id() );
267 
268  unsigned index = ancestors[i].key();
269  assert( index < pfs.size() );
270 
271  std::cout<<"\t\t"<<pfs[index]<<std::endl;
272  }
273 }
274 
275 
276 
277 template< class Top, class Bottom >
278 void
280  reco::CandidatePtrVector& ancestors,
281  const edm::ProductID& ancestorsID,
282  const edm::Event& iEvent) const {
283 
284 
285  unsigned nSources = candPtr->numberOfSourceCandidatePtrs();
286 
287  if(verbose_) {
288  const edm::Provenance& hereProv = iEvent.getProvenance(candPtr.id());
289 
290  std::cout<<"going down from "<<candPtr.id()
291  <<"/"<<candPtr.key()<<" #mothers "<<nSources
292  <<" ancestor id "<<ancestorsID<<std::endl
293  <<hereProv.branchDescription()<<std::endl;
294  }
295 
296  for(unsigned i=0; i<nSources; i++) {
297 
298  reco::CandidatePtr mother = candPtr->sourceCandidatePtr(i);
299  if( verbose_ ) {
300 /* const Provenance& motherProv = iEvent.getProvenance(mother.id()); */
301  std::cout<<" mother id "<<mother.id()<<std::endl;
302  }
303  if( mother.id() != ancestorsID ) {
304  // the mother is not yet at lowest level
305  ptrToAncestor( mother, ancestors, ancestorsID, iEvent );
306  }
307  else {
308  // adding mother to the list of ancestors
309  ancestors.push_back( mother );
310  }
311  }
312 }
313 
314 
315 
316 
317 template< class Top, class Bottom >
319  std::vector<bool>& masked ) const {
320 
321  for(unsigned i=0; i<ancestors.size(); i++) {
322  unsigned index = ancestors[i].key();
323  assert( index<masked.size() );
324 
325  // if(verbose_) {
326  // ProductID id = ancestors[i].id();
327  // std::cout<<"\tmasking "<<index<<", ancestor "<<id<<"/"<<index<<std::endl;
328  // }
329  masked[index] = true;
330  }
331 }
332 
333 
334 #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:85
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:164
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:356
void ptrToAncestor(reco::CandidatePtr candRef, reco::CandidatePtrVector &ancestors, const edm::ProductID &ancestorsID, const edm::Event &iEvent) const
Definition: TopProjector.h:279
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:212
key_type key() const
Definition: Ptr.h:169
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:121
edm::Ptr< Bottom > BottomPtr
Definition: TopProjector.h:45
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:60
void maskAncestors(const reco::CandidatePtrVector &ancestors, std::vector< bool > &masked) const
Definition: TopProjector.h:318
void printAncestors(const reco::CandidatePtrVector &ancestors, const edm::Handle< std::vector< Bottom > > &allPFCandidates) const
Definition: TopProjector.h:257