CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TopProjectorAlgo.h
Go to the documentation of this file.
1 #ifndef CommonTools_ParticleFlow_TopProjectorAlgo
2 #define CommonTools_ParticleFlow_TopProjectorAlgo
3 
4 // system include files
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 
9 // user include files
12 
15 
17 
19 
21 
22 
30 #include <iostream>
31 
32 
33 namespace pf2pat {
34 
35 template< class Top, class Bottom>
37 
38  public:
39 
40  typedef std::vector<Top> TopCollection;
42  typedef std::vector<Bottom> BottomCollection;
45 
47 
49 
50 
51  BottomCollection produce( const TopHandle& topHandle,
52  const BottomHandle& bottomHandle,
53  const edm::EventBase& iEvent );
54 
55 
56  private:
57 
61  void
63  reco::CandidatePtrVector& ancestors,
64  const edm::ProductID& ancestorsID,
65  const edm::EventBase& iEvent ) const;
66 
70  void maskAncestors( const reco::CandidatePtrVector& ancestors,
71  std::vector<bool>& masked ) const;
72 
73 
74  void processCollection( const edm::Handle< std::vector<Top> >& handle,
75  const edm::Handle< std::vector<Bottom> >& allPFCandidates ,
76  std::vector<bool>& masked,
77  const char* objectName,
78  const edm::EventBase& iEvent ) const;
79 
80  void printAncestors( const reco::CandidatePtrVector& ancestors,
81  const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const;
82 
83 
85  bool verbose_;
86 
87  //COLIN does not seem to be needed
89 
90 };
91 
92 
93 
94 
95 template< class Top, class Bottom>
97 
98  verbose_ = iConfig.getUntrackedParameter<bool>("verbose",false);
99  name_ = iConfig.getUntrackedParameter<std::string>("name","No Name");
100 }
101 
102 
103 template< class Top, class Bottom >
104 std::vector<Bottom> TopProjectorAlgo< Top, Bottom >::produce( const edm::Handle< std::vector<Top> >& tops,
105  const edm::Handle< std::vector<Bottom> >& bottoms,
106  const edm::EventBase& iEvent ) {
107 
108  edm::ProductID topsID = tops.id();
109  edm::ProductID bottomsID = bottoms.id();
110 
111  cout<<"produce "<<tops.id()<<" "<<bottoms.id()<<endl;
112 
113  //COLIN had to comment all verbose stuff cause EventBase::getProvenance does not exist...
114 /* if(verbose_) { */
115 /* const Provenance& topProv = iEvent.getProvenance(tops.id()); */
116 /* const Provenance& bottomProv = iEvent.getProvenance(bottoms.id()); */
117 
118 /* cout<<"Top projector: event "<<iEvent.id().event()<<endl; */
119 /* cout<<"Inputs --------------------"<<endl; */
120 /* cout<<"Top : " */
121 /* <<tops.id()<<"\t"<<tops->size()<<endl */
122 /* <<topProv.branchDescription()<<endl */
123 /* <<"Bottom : " */
124 /* <<bottoms.id()<<"\t"<<bottoms->size()<<endl */
125 /* <<bottomProv.branchDescription()<<endl; */
126 /* } */
127 
128 
129  // output collection of objects,
130  // selected from the Bottom collection
131 
132 /* auto_ptr< BottomCollection > */
133 /* pBottomOutput( new BottomCollection ); */
134  BottomCollection bottomOutput;
135  bottomOutput.reserve( bottoms->size() );
136 
137  //COLIN to be cleaned up lated.
138  BottomCollection* pBottomOutput = &bottomOutput;
139 
140  // mask for each bottom object.
141  // at the beginning, all bottom objects are unmasked.
142  vector<bool> masked( bottoms->size(), false);
143 
144  processCollection( tops, bottoms, masked, name_.c_str(), iEvent );
145 
146  const BottomCollection& inCands = *bottoms;
147 
148  if(verbose_)
149  cout<<" Remaining candidates in the bottom collection ------ "<<endl;
150 
151  for(unsigned i=0; i<inCands.size(); i++) {
152 
153  if(masked[i]) {
154  if(verbose_)
155  cout<<"X "<<i<<" "<< *(inCands[i].get())<<endl;
156  continue;
157  }
158  else {
159  if(verbose_)
160  cout<<"O "<<i<<" "<< *(inCands[i].get())<<endl;
161 
162  pBottomOutput->push_back( inCands[i] );
163  BottomPtr motherPtr( bottoms, i );
164  pBottomOutput->back().setSourceCandidatePtr(motherPtr);
165  }
166  }
167 
168  return bottomOutput;
169 }
170 
171 
172 
173 template< class Top, class Bottom >
175  const edm::Handle< std::vector<Bottom> >& bottoms ,
176  std::vector<bool>& masked,
177  const char* objectName,
178  const edm::EventBase& iEvent) const {
179 
180  if( tops.isValid() && bottoms.isValid() ) {
181  const std::vector<Top>& topCollection = *tops;
182 
183  if(verbose_)
184  std::cout<<" processing: "<<objectName
185  <<" size = "<<topCollection.size()<<std::endl;
186 
187  for(unsigned i=0; i<topCollection.size(); i++) {
188 
189 
190  edm::Ptr<Top> ptr( tops, i);
191  reco::CandidatePtr basePtr( ptr );
192 
193 
194  reco::CandidatePtrVector ancestors;
195  ptrToAncestor( basePtr,
196  ancestors,
197  bottoms.id(),
198  iEvent );
199 
200  if(verbose_) {
201 /* std::cout<<"\t"<<objectName<<" "<<i */
202 /* <<" pt,eta,phi = " */
203 /* <<basePtr->pt()<<"," */
204 /* <<basePtr->eta()<<"," */
205 /* <<basePtr->phi()<<std::endl; */
206 
207  std::cout<<"\t"<<topCollection[i]<<std::endl;
208  printAncestors( ancestors, bottoms );
209  }
210 
211  maskAncestors( ancestors, masked );
212  }
213  }
214 
215 }
216 
217 
218 template< class Top, class Bottom >
220  const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const {
221 
222 
223  std::vector<Bottom> pfs = *allPFCandidates;
224 
225  for(unsigned i=0; i<ancestors.size(); i++) {
226 
227  edm::ProductID id = ancestors[i].id();
228  assert( id == allPFCandidates.id() );
229 
230  unsigned index = ancestors[i].key();
231  assert( index < pfs.size() );
232 
233  std::cout<<"\t\t"<<pfs[index]<<std::endl;
234  }
235 }
236 
237 
238 
239 template< class Top, class Bottom >
240 void
242  reco::CandidatePtrVector& ancestors,
243  const edm::ProductID& ancestorsID,
244  const edm::EventBase& iEvent) const {
245 
246 
247 
248  unsigned nSources = candPtr->numberOfSourceCandidatePtrs();
249 
250 /* if(verbose_) { */
251 /* const Provenance& hereProv = iEvent.getProvenance(candPtr.id()); */
252 
253 /* cout<<"going down from "<<candPtr.id() */
254 /* <<"/"<<candPtr.key()<<" #mothers "<<nSources */
255 /* <<" ancestor id "<<ancestorsID<<endl */
256 /* <<hereProv.branchDescription()<<endl; */
257 /* } */
258 
259  for(unsigned i=0; i<nSources; i++) {
260 
261  CandidatePtr mother = candPtr->sourceCandidatePtr(i);
262 /* if( verbose_ ) { */
263 /* const Provenance& motherProv = iEvent.getProvenance(mother.id()); */
264 /* cout<<" mother id "<<mother.id()<<endl */
265 /* <<motherProv<<endl; */
266 /* } */
267  if( mother.id() != ancestorsID ) {
268  // the mother is not yet at lowest level
269  ptrToAncestor( mother, ancestors, ancestorsID, iEvent );
270  }
271  else {
272  // adding mother to the list of ancestors
273  ancestors.push_back( mother );
274  }
275  }
276 }
277 
278 
279 
280 
281 template< class Top, class Bottom >
283  std::vector<bool>& masked ) const {
284 
285  for(unsigned i=0; i<ancestors.size(); i++) {
286  unsigned index = ancestors[i].key();
287  assert( index<masked.size() );
288 
289  // if(verbose_) {
290  // ProductID id = ancestors[i].id();
291  // cout<<"\tmasking "<<index<<", ancestor "<<id<<"/"<<index<<endl;
292  // }
293  masked[index] = true;
294  }
295 }
296 
297 }
298 #endif
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
edm::Ptr< Bottom > BottomPtr
size_type size() const
Size of the RefVector.
Definition: PtrVectorBase.h:74
tuple topCollection
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:141
assert(m_qm.get())
BottomCollection produce(const TopHandle &topHandle, const BottomHandle &bottomHandle, const edm::EventBase &iEvent)
TopProjectorAlgo(const edm::ParameterSet &)
edm::Handle< std::vector< Top > > TopHandle
ProductID id() const
Accessor for product ID.
Definition: PtrVectorBase.h:59
std::vector< Top > TopCollection
int iEvent
Definition: GenABIO.cc:230
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::EventBase &iEvent) const
tuple handle
Definition: patZpeak.py:22
void printAncestors(const reco::CandidatePtrVector &ancestors, const edm::Handle< std::vector< Bottom > > &allPFCandidates) const
edm::Ptr< Candidate > CandidatePtr
persistent reference to an object in a collection of Candidate objects
Definition: CandidateFwd.h:25
void maskAncestors(const reco::CandidatePtrVector &ancestors, std::vector< bool > &masked) const
std::vector< Bottom > BottomCollection
tuple cout
Definition: gather_cfg.py:145
edm::Handle< std::vector< Bottom > > BottomHandle
ProductIndex id() const
Definition: ProductID.h:38
void ptrToAncestor(reco::CandidatePtr candRef, reco::CandidatePtrVector &ancestors, const edm::ProductID &ancestorsID, const edm::EventBase &iEvent) const