CMS 3D CMS Logo

PFBlockAlgo.cc
Go to the documentation of this file.
7 
8 #include <algorithm>
9 #include <iostream>
10 #include <array>
11 #include <iterator>
12 #include <sstream>
13 #include <type_traits>
14 #include <utility>
15 
16 using namespace std;
17 using namespace reco;
18 
19 #define INIT_ENTRY(name) {#name,name}
20 
21 namespace {
22  class QuickUnion{
23  std::vector<unsigned> id_;
24  std::vector<unsigned> size_;
25  int count_;
26 
27  public:
28  QuickUnion(const unsigned NBranches) {
29  count_ = NBranches;
30  id_.resize(NBranches);
31  size_.resize(NBranches);
32  for( unsigned i = 0; i < NBranches; ++i ) {
33  id_[i] = i;
34  size_[i] = 1;
35  }
36  }
37 
38  int count() const { return count_; }
39 
40  unsigned find(unsigned p) {
41  while( p != id_[p] ) {
42  id_[p] = id_[id_[p]];
43  p = id_[p];
44  }
45  return p;
46  }
47 
48  bool connected(unsigned p, unsigned q) { return find(p) == find(q); }
49 
50  void unite(unsigned p, unsigned q) {
51  unsigned rootP = find(p);
52  unsigned rootQ = find(q);
53  id_[p] = q;
54 
55  if(size_[rootP] < size_[rootQ] ) {
56  id_[rootP] = rootQ; size_[rootQ] += size_[rootP];
57  } else {
58  id_[rootQ] = rootP; size_[rootP] += size_[rootQ];
59  }
60  --count_;
61  }
62  };
63 }
64 
65 
66 //for debug only
67 //#define PFLOW_DEBUG
68 
70  debug_(false),
71  elementTypes_( {
72  INIT_ENTRY(PFBlockElement::TRACK),
73  INIT_ENTRY(PFBlockElement::PS1),
74  INIT_ENTRY(PFBlockElement::PS2),
77  INIT_ENTRY(PFBlockElement::GSF),
78  INIT_ENTRY(PFBlockElement::BREM),
79  INIT_ENTRY(PFBlockElement::HFEM),
80  INIT_ENTRY(PFBlockElement::HFHAD),
81  INIT_ENTRY(PFBlockElement::SC),
83  INIT_ENTRY(PFBlockElement::HGCAL)
84  } ) {}
85 
86 void PFBlockAlgo::setLinkers(const std::vector<edm::ParameterSet>& confs) {
87  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
88  for( unsigned i = 0; i < rowsize; ++i ) {
89  for( unsigned j = 0; j < rowsize; ++j ) {
90 
91  linkTestSquare_[i][j] = 0;
92  }
93  }
94  linkTests_.resize(rowsize*rowsize);
95  const std::string prefix("PFBlockElement::");
96  const std::string pfx_kdtree("KDTree");
97  for( const auto& conf : confs ) {
98  const std::string& linkerName =
99  conf.getParameter<std::string>("linkerName");
100  const std::string& linkTypeStr =
101  conf.getParameter<std::string>("linkType");
102  size_t split = linkTypeStr.find(':');
103  if( split == std::string::npos ) {
104  throw cms::Exception("MalformedLinkType")
105  << "\"" << linkTypeStr << "\" is not a valid link type definition."
106  << " This string should have the form \"linkFrom:linkTo\"";
107  }
108  std::string link1(prefix+linkTypeStr.substr(0,split));
109  std::string link2(prefix+linkTypeStr.substr(split+1,std::string::npos));
110  if( !(elementTypes_.count(link1) && elementTypes_.count(link2) ) ) {
111  throw cms::Exception("InvalidBlockElementType")
112  << "One of \"" << link1 << "\" or \"" << link2
113  << "\" are invalid block element types!";
114  }
115  const PFBlockElement::Type type1 = elementTypes_.at(link1);
116  const PFBlockElement::Type type2 = elementTypes_.at(link2);
117  const unsigned index = rowsize*std::max(type1,type2)+std::min(type1,type2);
118  linkTests_[index].reset(BlockElementLinkerFactory::get()->create(linkerName,conf));
119  linkTestSquare_[type1][type2] = index;
120  linkTestSquare_[type2][type1] = index;
121  // setup KDtree if requested
122  const bool useKDTree = conf.getParameter<bool>("useKDTree");
123  if( useKDTree ) {
124  kdtrees_.emplace_back( KDTreeLinkerFactory::get()->create(pfx_kdtree+
125  linkerName) );
126  kdtrees_.back()->setTargetType(std::min(type1,type2));
127  kdtrees_.back()->setFieldType(std::max(type1,type2));
128  }
129  }
130 }
131 
132 void PFBlockAlgo::setImporters(const std::vector<edm::ParameterSet>& confs,
133  edm::ConsumesCollector& sumes) {
134  importers_.reserve(confs.size());
135  for( const auto& conf : confs ) {
136  const std::string& importerName =
137  conf.getParameter<std::string>("importerName");
138  importers_.emplace_back(BlockElementImporterFactory::get()->create(importerName,conf,sumes));
139  }
140 }
141 
143 
144 #ifdef PFLOW_DEBUG
145  if(debug_)
146  cout<<"~PFBlockAlgo - number of remaining elements: "
147  <<elements_.size()<<endl;
148 #endif
149 }
150 
152  // Glowinski & Gouzevitch
153  for( const auto& kdtree : kdtrees_ ) {
154  kdtree->process();
155  }
156  // !Glowinski & Gouzevitch
158  // the blocks have not been passed to the event, and need to be cleared
159  blocks.reserve(elements_.size());
160 
161  QuickUnion qu(elements_.size());
162  const auto elem_size = elements_.size();
163  for( unsigned i = 0; i < elem_size; ++i ) {
164  for( unsigned j = 0; j < elem_size; ++j ) {
165  if( qu.connected(i,j) || j == i ) continue;
166  if( !linkTests_[linkTestSquare_[elements_[i]->type()][elements_[j]->type()]] ) {
167  j = ranges_[elements_[j]->type()].second;
168  continue;
169  }
170  auto p1(elements_[i].get()), p2(elements_[j].get());
171  const PFBlockElement::Type type1 = p1->type();
172  const PFBlockElement::Type type2 = p2->type();
173  const unsigned index = linkTestSquare_[type1][type2];
174  if( linkTests_[index]->linkPrefilter(p1,p2) ) {
175  const double dist = linkTests_[index]->testLink(p1,p2);
176  // compute linking info if it is possible
177  if( dist > -0.5 ) {
178  qu.unite(i,j);
179  }
180  }
181  }
182  }
183 
184  std::unordered_multimap<unsigned,unsigned> blocksmap(elements_.size());
185  std::vector<unsigned> keys;
186  keys.reserve(elements_.size());
187  for( unsigned i = 0; i < elements_.size(); ++i ) {
188  unsigned key = i;
189  while( key != qu.find(key) ) key = qu.find(key); // make sure we always find the root node...
190  auto pos = std::lower_bound(keys.begin(),keys.end(),key);
191  if( pos == keys.end() || *pos != key ) {
192  keys.insert(pos,key);
193  }
194  blocksmap.emplace(key,i);
195  }
196 
197  for( auto key : keys ) {
198  blocks.push_back( reco::PFBlock() );
199  auto range = blocksmap.equal_range(key);
200  auto& the_block = blocks.back();
201  ElementList::value_type::pointer p1(elements_[range.first->second].get());
202  the_block.addElement(p1);
203  const unsigned block_size = blocksmap.count(key) + 1;
204  //reserve up to 1M or 8MB; pay rehash cost for more
205  std::unordered_map<std::pair<unsigned int,unsigned int>, double > links(min(1000000u,block_size*block_size));
206  auto itr = range.first;
207  ++itr;
208  for( ; itr != range.second; ++itr ) {
209  ElementList::value_type::pointer p2(elements_[itr->second].get());
210  const PFBlockElement::Type type1 = p1->type();
211  const PFBlockElement::Type type2 = p2->type();
212  the_block.addElement(p2);
213  const unsigned index = linkTestSquare_[type1][type2];
214  if( nullptr != linkTests_[index] ) {
215  const double dist = linkTests_[index]->testLink(p1,p2);
216  links.emplace( std::make_pair(p1->index(), p2->index()), dist );
217  }
218  }
219  packLinks( the_block, links );
220  }
221 
222  elements_.clear();
223 
224  return blocks;
225 }
226 
227 void
229  const std::unordered_map<std::pair<unsigned int,unsigned int>,double>& links ) const {
230  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
231 
233 
234  block.bookLinkData();
235  unsigned elsize = els.size();
236  //First Loop: update all link data
237  for( unsigned i1=0; i1<elsize; ++i1 ) {
238  for( unsigned i2=0; i2<i1; ++i2 ) {
239 
240  // no reflexive link
241  //if( i1==i2 ) continue;
242 
243  double dist = -1;
244 
245  bool linked = false;
246 
247  // are these elements already linked ?
248  // this can be optimized
249  const auto link_itr = links.find(std::make_pair(i2,i1));
250  if( link_itr != links.end() ) {
251  dist = link_itr->second;
252  linked = true;
253  }
254 
255  if(!linked) {
256  const PFBlockElement::Type type1 = els[i1].type();
257  const PFBlockElement::Type type2 = els[i2].type();
258  const auto minmax = std::minmax(type1,type2);
259  const unsigned index = rowsize*minmax.second + minmax.first;
260  bool bTestLink = ( nullptr == linkTests_[index] ? false : linkTests_[index]->linkPrefilter(&(els[i1]),&(els[i2])) );
261  if (bTestLink) link( & els[i1], & els[i2], dist);
262  }
263 
264  //loading link data according to link test used: RECHIT
265  //block.setLink( i1, i2, chi2, block.linkData() );
266  block.setLink( i1, i2, dist, block.linkData() );
267  }
268  }
269 
270 }
271 
272 inline void
274  const reco::PFBlockElement* el2,
275  double& dist) const {
276  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
277  dist=-1.0;
278  const PFBlockElement::Type type1 = el1->type();
279  const PFBlockElement::Type type2 = el2->type();
280  const unsigned index = rowsize*std::max(type1,type2) + std::min(type1,type2);
281  if(debug_ ) {
282  std::cout << " PFBlockAlgo links type1 " << type1
283  << " type2 " << type2 << std::endl;
284  }
285 
286  // index is always checked in the preFilter above, no need to check here
287  dist = linkTests_[index]->testLink(el1,el2);
288 }
289 
291  for( auto& importer : importers_ ) {
292  importer->updateEventSetup(es);
293  }
294 }
295 
296 // see plugins/importers and plugins/kdtrees
297 // for the definitions of available block element importers
298 // and kdtree preprocessors
300  // import block elements as defined in python configuration
301  ranges_.fill(std::make_pair(0,0));
302  elements_.clear();
303  for( const auto& importer : importers_ ) {
304  importer->importToBlock(evt,elements_);
305  }
306 
307  std::sort(elements_.begin(),elements_.end(),
308  [](const auto& a, const auto& b) { return a->type() < b->type(); } );
309 
310  // list is now partitioned, so mark the boundaries so we can efficiently skip chunks
311  unsigned current_type = ( !elements_.empty() ? elements_[0]->type() : 0 );
312  unsigned last_type = ( !elements_.empty() ? elements_.back()->type() : 0 );
313  ranges_[current_type].first = 0;
314  ranges_[last_type].second = elements_.size()-1;
315  for( size_t i = 0; i < elements_.size(); ++i ) {
316  const auto the_type = elements_[i]->type();
317  if( the_type != current_type ) {
318  ranges_[the_type].first = i;
319  ranges_[current_type].second = i-1;
320  current_type = the_type;
321  }
322  }
323  // -------------- Loop over block elements ---------------------
324 
325  // Here we provide to all KDTree linkers the collections to link.
326  // Glowinski & Gouzevitch
327 
328  for (ElementList::iterator it = elements_.begin();
329  it != elements_.end(); ++it) {
330  for( const auto& kdtree : kdtrees_ ) {
331  if( (*it)->type() == kdtree->targetType() ) {
332  kdtree->insertTargetElt(it->get());
333  }
334  if( (*it)->type() == kdtree->fieldType() ) {
335  kdtree->insertFieldClusterElt(it->get());
336  }
337  }
338  }
339  //std::cout << "(new) imported: " << elements_.size() << " elements!" << std::endl;
340 }
341 
342 std::ostream& operator<<(std::ostream& out, const PFBlockAlgo& a) {
343  if(! out) return out;
344 
345  out<<"====== Particle Flow Block Algorithm ======= ";
346  out<<endl;
347  out<<"number of unassociated elements : "<<a.elements_.size()<<endl;
348  out<<endl;
349 
350  for(auto const& element : a.elements_) {
351  out<<"\t"<< *element <<endl;
352  }
353 
354  return out;
355 }
356 
357 // a little history, ideas we may want to keep around for later
358  /*
359  // Links between the two preshower layers are not used for now - disable
360  case PFBlockLink::PS1andPS2:
361  {
362  PFClusterRef ps1ref = lowEl->clusterRef();
363  PFClusterRef ps2ref = highEl->clusterRef();
364  assert( !ps1ref.isNull() );
365  assert( !ps2ref.isNull() );
366  // PJ - 14-May-09 : A link by rechit is needed here !
367  // dist = testPS1AndPS2( *ps1ref, *ps2ref );
368  dist = -1.;
369  linktest = PFBlock::LINKTEST_RECHIT;
370  break;
371  }
372  case PFBlockLink::TRACKandPS1:
373  case PFBlockLink::TRACKandPS2:
374  {
375  //cout<<"TRACKandPS"<<endl;
376  PFRecTrackRef trackref = lowEl->trackRefPF();
377  PFClusterRef clusterref = highEl->clusterRef();
378  assert( !trackref.isNull() );
379  assert( !clusterref.isNull() );
380  // PJ - 14-May-09 : A link by rechit is needed here !
381  dist = testTrackAndPS( *trackref, *clusterref );
382  linktest = PFBlock::LINKTEST_RECHIT;
383  break;
384  }
385  // GSF Track/Brem Track and preshower cluster links are not used for now - disable
386  case PFBlockLink::PS1andGSF:
387  case PFBlockLink::PS2andGSF:
388  {
389  PFClusterRef psref = lowEl->clusterRef();
390  assert( !psref.isNull() );
391  const reco::PFBlockElementGsfTrack * GsfEl = dynamic_cast<const reco::PFBlockElementGsfTrack*>(highEl);
392  const PFRecTrack * myTrack = &(GsfEl->GsftrackPF());
393  // PJ - 14-May-09 : A link by rechit is needed here !
394  dist = testTrackAndPS( *myTrack, *psref );
395  linktest = PFBlock::LINKTEST_RECHIT;
396  break;
397  }
398  case PFBlockLink::PS1andBREM:
399  case PFBlockLink::PS2andBREM:
400  {
401  PFClusterRef psref = lowEl->clusterRef();
402  assert( !psref.isNull() );
403  const reco::PFBlockElementBrem * BremEl = dynamic_cast<const reco::PFBlockElementBrem*>(highEl);
404  const PFRecTrack * myTrack = &(BremEl->trackPF());
405  // PJ - 14-May-09 : A link by rechit is needed here !
406  dist = testTrackAndPS( *myTrack, *psref );
407  linktest = PFBlock::LINKTEST_RECHIT;
408  break;
409  }
410  */
type
Definition: HCALResponse.h:21
Abstract base class for a PFBlock element (track, cluster...)
def create(alignables, pedeDump, additionalData, outputFile, config)
Type type() const
friend std::ostream & operator<<(std::ostream &, const PFBlockAlgo &)
Definition: PFBlockAlgo.cc:342
size_type size() const
Definition: OwnVector.h:264
const edm::OwnVector< reco::PFBlockElement > & elements() const
Definition: PFBlock.h:107
const LinkData & linkData() const
Definition: PFBlock.h:112
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
ElementList elements_
Definition: PFBlockAlgo.h:78
void setLink(unsigned i1, unsigned i2, double dist, LinkData &linkData, LinkTest test=LINKTEST_RECHIT) const
Definition: PFBlock.cc:26
Particle Flow Algorithm.
Definition: PFBlockAlgo.h:36
#define INIT_ENTRY(name)
Definition: PFBlockAlgo.cc:19
const std::unordered_map< std::string, reco::PFBlockElement::Type > elementTypes_
Definition: PFBlockAlgo.h:90
std::vector< std::unique_ptr< KDTreeLinkerBase > > kdtrees_
Definition: PFBlockAlgo.h:94
void updateEventSetup(const edm::EventSetup &)
Definition: PFBlockAlgo.cc:290
void setImporters(const std::vector< edm::ParameterSet > &, edm::ConsumesCollector &)
Definition: PFBlockAlgo.cc:132
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< PFBlock > PFBlockCollection
collection of PFBlock objects
Definition: PFBlockFwd.h:11
void bookLinkData()
Definition: PFBlock.cc:20
double p2[4]
Definition: TauolaWrapper.h:90
void packLinks(reco::PFBlock &block, const std::unordered_map< std::pair< unsigned int, unsigned int >, double > &links) const
Definition: PFBlockAlgo.cc:228
ElementRanges ranges_
Definition: PFBlockAlgo.h:79
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
Definition: PFBlockAlgo.h:91
unsigned int linkTestSquare_[reco::PFBlockElement::kNBETypes][reco::PFBlockElement::kNBETypes]
Definition: PFBlockAlgo.h:92
void link(const reco::PFBlockElement *el1, const reco::PFBlockElement *el2, double &dist) const
check whether 2 elements are linked. Returns distance
Definition: PFBlockAlgo.cc:273
std::vector< std::unique_ptr< BlockElementImporterBase > > importers_
Definition: PFBlockAlgo.h:87
double b
Definition: hdecay.h:120
reco::PFBlockCollection findBlocks()
build blocks
Definition: PFBlockAlgo.cc:151
fixed size matrix
double p1[4]
Definition: TauolaWrapper.h:89
double a
Definition: hdecay.h:121
void buildElements(const edm::Event &)
Definition: PFBlockAlgo.cc:299
bool debug_
if true, debug printouts activated
Definition: PFBlockAlgo.h:82
void setLinkers(const std::vector< edm::ParameterSet > &)
Definition: PFBlockAlgo.cc:86
double split
Definition: MVATrainer.cc:139
T get(const Candidate &c)
Definition: component.h:55
#define constexpr
Block of elements.
Definition: PFBlock.h:30