CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends
PFBlockAlgo Class Reference

Particle Flow Algorithm. More...

#include <PFBlockAlgo.h>

Public Types

typedef std::vector< std::unique_ptr< reco::PFBlockElement > > ElementList
 
typedef std::array< std::pair< unsigned int, unsigned int >, reco::PFBlockElement::kNBETypesElementRanges
 

Public Member Functions

void buildElements (const edm::Event &)
 
reco::PFBlockCollection findBlocks ()
 build blocks More...
 
 PFBlockAlgo ()
 
void setDebug (bool debug)
 sets debug printout flag More...
 
void setImporters (const std::vector< edm::ParameterSet > &, edm::ConsumesCollector &)
 
void setLinkers (const std::vector< edm::ParameterSet > &)
 
void updateEventSetup (const edm::EventSetup &)
 
 ~PFBlockAlgo ()
 

Private Member Functions

void link (const reco::PFBlockElement *el1, const reco::PFBlockElement *el2, double &dist) const
 check whether 2 elements are linked. Returns distance More...
 
void packLinks (reco::PFBlock &block, const std::unordered_map< std::pair< unsigned int, unsigned int >, double > &links) const
 

Private Attributes

bool debug_
 if true, debug printouts activated More...
 
ElementList elements_
 
const std::unordered_map< std::string, reco::PFBlockElement::TypeelementTypes_
 
std::vector< std::unique_ptr< BlockElementImporterBase > > importers_
 
std::vector< std::unique_ptr< KDTreeLinkerBase > > kdtrees_
 
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
 
unsigned int linkTestSquare_ [reco::PFBlockElement::kNBETypes][reco::PFBlockElement::kNBETypes]
 
ElementRanges ranges_
 
bool useHO_
 

Friends

std::ostream & operator<< (std::ostream &, const PFBlockAlgo &)
 

Detailed Description

Particle Flow Algorithm.

Author
Colin Bernet (rewrite/refactor by L. Gray)
Date
January 2006 (April 2014)

Definition at line 34 of file PFBlockAlgo.h.

Member Typedef Documentation

typedef std::vector<std::unique_ptr<reco::PFBlockElement> > PFBlockAlgo::ElementList

Definition at line 37 of file PFBlockAlgo.h.

typedef std::array<std::pair<unsigned int, unsigned int>, reco::PFBlockElement::kNBETypes> PFBlockAlgo::ElementRanges

Definition at line 39 of file PFBlockAlgo.h.

Constructor & Destructor Documentation

PFBlockAlgo::PFBlockAlgo ( )

Definition at line 71 of file PFBlockAlgo.cc.

References ECAL, HCAL, DigiToRawDM_cff::HO, and INIT_ENTRY.

72  : debug_(false),
73  elementTypes_({INIT_ENTRY(PFBlockElement::TRACK),
74  INIT_ENTRY(PFBlockElement::PS1),
75  INIT_ENTRY(PFBlockElement::PS2),
78  INIT_ENTRY(PFBlockElement::GSF),
79  INIT_ENTRY(PFBlockElement::BREM),
80  INIT_ENTRY(PFBlockElement::HFEM),
81  INIT_ENTRY(PFBlockElement::HFHAD),
82  INIT_ENTRY(PFBlockElement::SC),
84  INIT_ENTRY(PFBlockElement::HGCAL)}) {}
#define INIT_ENTRY(name)
Definition: PFBlockAlgo.cc:19
const std::unordered_map< std::string, reco::PFBlockElement::Type > elementTypes_
Definition: PFBlockAlgo.h:82
bool debug_
if true, debug printouts activated
Definition: PFBlockAlgo.h:75
PFBlockAlgo::~PFBlockAlgo ( )

Definition at line 134 of file PFBlockAlgo.cc.

References gather_cfg::cout, debug_, and elements_.

134  {
135 #ifdef PFLOW_DEBUG
136  if (debug_)
137  cout << "~PFBlockAlgo - number of remaining elements: " << elements_.size() << endl;
138 #endif
139 }
ElementList elements_
Definition: PFBlockAlgo.h:71
bool debug_
if true, debug printouts activated
Definition: PFBlockAlgo.h:75

Member Function Documentation

void PFBlockAlgo::buildElements ( const edm::Event evt)

Definition at line 286 of file PFBlockAlgo.cc.

References a, b, elements_, mps_fire::i, importers_, kdtrees_, and ranges_.

Referenced by PFBlockProducer::produce().

286  {
287  // import block elements as defined in python configuration
288  ranges_.fill(std::make_pair(0, 0));
289  elements_.clear();
290  for (const auto& importer : importers_) {
291  importer->importToBlock(evt, elements_);
292  }
293 
294  std::sort(elements_.begin(), elements_.end(), [](const auto& a, const auto& b) { return a->type() < b->type(); });
295 
296  // list is now partitioned, so mark the boundaries so we can efficiently skip chunks
297  unsigned current_type = (!elements_.empty() ? elements_[0]->type() : 0);
298  unsigned last_type = (!elements_.empty() ? elements_.back()->type() : 0);
299  ranges_[current_type].first = 0;
300  ranges_[last_type].second = elements_.size() - 1;
301  for (size_t i = 0; i < elements_.size(); ++i) {
302  const auto the_type = elements_[i]->type();
303  if (the_type != current_type) {
304  ranges_[the_type].first = i;
305  ranges_[current_type].second = i - 1;
306  current_type = the_type;
307  }
308  }
309  // -------------- Loop over block elements ---------------------
310 
311  // Here we provide to all KDTree linkers the collections to link.
312  // Glowinski & Gouzevitch
313 
314  for (ElementList::iterator it = elements_.begin(); it != elements_.end(); ++it) {
315  for (const auto& kdtree : kdtrees_) {
316  if ((*it)->type() == kdtree->targetType()) {
317  kdtree->insertTargetElt(it->get());
318  }
319  if ((*it)->type() == kdtree->fieldType()) {
320  kdtree->insertFieldClusterElt(it->get());
321  }
322  }
323  }
324  //std::cout << "(new) imported: " << elements_.size() << " elements!" << std::endl;
325 }
ElementList elements_
Definition: PFBlockAlgo.h:71
std::vector< std::unique_ptr< KDTreeLinkerBase > > kdtrees_
Definition: PFBlockAlgo.h:86
ElementRanges ranges_
Definition: PFBlockAlgo.h:72
std::vector< std::unique_ptr< BlockElementImporterBase > > importers_
Definition: PFBlockAlgo.h:80
double b
Definition: hdecay.h:118
double a
Definition: hdecay.h:119
reco::PFBlockCollection PFBlockAlgo::findBlocks ( )

build blocks

Definition at line 141 of file PFBlockAlgo.cc.

References gather_cfg::blocks, elements_, mps_fire::i, dqmiolumiharvest::j, kdtrees_, crabWrapper::key, relativeConstraints::keys, electronStore::links, linkTests_, linkTestSquare_, min(), p1, p2, packLinks(), FastTimerService_cff::range, and ranges_.

Referenced by PFBlockProducer::produce().

141  {
142  // Glowinski & Gouzevitch
143  for (const auto& kdtree : kdtrees_) {
144  kdtree->process();
145  }
146  // !Glowinski & Gouzevitch
148  // the blocks have not been passed to the event, and need to be cleared
149  blocks.reserve(elements_.size());
150 
151  QuickUnion qu(elements_.size());
152  const auto elem_size = elements_.size();
153  for (unsigned i = 0; i < elem_size; ++i) {
154  for (unsigned j = 0; j < elem_size; ++j) {
155  if (qu.connected(i, j) || j == i)
156  continue;
157  if (!linkTests_[linkTestSquare_[elements_[i]->type()][elements_[j]->type()]]) {
158  j = ranges_[elements_[j]->type()].second;
159  continue;
160  }
161  auto p1(elements_[i].get()), p2(elements_[j].get());
162  const PFBlockElement::Type type1 = p1->type();
163  const PFBlockElement::Type type2 = p2->type();
164  const unsigned index = linkTestSquare_[type1][type2];
165  if (linkTests_[index]->linkPrefilter(p1, p2)) {
166  const double dist = linkTests_[index]->testLink(p1, p2);
167  // compute linking info if it is possible
168  if (dist > -0.5) {
169  qu.unite(i, j);
170  }
171  }
172  }
173  }
174 
175  std::unordered_multimap<unsigned, unsigned> blocksmap(elements_.size());
176  std::vector<unsigned> keys;
177  keys.reserve(elements_.size());
178  for (unsigned i = 0; i < elements_.size(); ++i) {
179  unsigned key = i;
180  while (key != qu.find(key))
181  key = qu.find(key); // make sure we always find the root node...
182  auto pos = std::lower_bound(keys.begin(), keys.end(), key);
183  if (pos == keys.end() || *pos != key) {
184  keys.insert(pos, key);
185  }
186  blocksmap.emplace(key, i);
187  }
188 
189  for (auto key : keys) {
190  blocks.push_back(reco::PFBlock());
191  auto range = blocksmap.equal_range(key);
192  auto& the_block = blocks.back();
193  ElementList::value_type::pointer p1(elements_[range.first->second].get());
194  the_block.addElement(p1);
195  const unsigned block_size = blocksmap.count(key) + 1;
196  //reserve up to 1M or 8MB; pay rehash cost for more
197  std::unordered_map<std::pair<unsigned int, unsigned int>, double> links(min(1000000u, block_size * block_size));
198  auto itr = range.first;
199  ++itr;
200  for (; itr != range.second; ++itr) {
201  ElementList::value_type::pointer p2(elements_[itr->second].get());
202  const PFBlockElement::Type type1 = p1->type();
203  const PFBlockElement::Type type2 = p2->type();
204  the_block.addElement(p2);
205  const unsigned index = linkTestSquare_[type1][type2];
206  if (nullptr != linkTests_[index]) {
207  const double dist = linkTests_[index]->testLink(p1, p2);
208  links.emplace(std::make_pair(p1->index(), p2->index()), dist);
209  }
210  }
211  packLinks(the_block, links);
212  }
213 
214  elements_.clear();
215 
216  return blocks;
217 }
type
Definition: HCALResponse.h:21
ElementList elements_
Definition: PFBlockAlgo.h:71
std::vector< std::unique_ptr< KDTreeLinkerBase > > kdtrees_
Definition: PFBlockAlgo.h:86
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< PFBlock > PFBlockCollection
collection of PFBlock objects
Definition: PFBlockFwd.h:10
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:219
ElementRanges ranges_
Definition: PFBlockAlgo.h:72
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
Definition: PFBlockAlgo.h:83
unsigned int linkTestSquare_[reco::PFBlockElement::kNBETypes][reco::PFBlockElement::kNBETypes]
Definition: PFBlockAlgo.h:84
double p1[4]
Definition: TauolaWrapper.h:89
Block of elements.
Definition: PFBlock.h:26
void PFBlockAlgo::link ( const reco::PFBlockElement el1,
const reco::PFBlockElement el2,
double &  dist 
) const
inlineprivate

check whether 2 elements are linked. Returns distance

Definition at line 263 of file PFBlockAlgo.cc.

References constexpr, gather_cfg::cout, debug_, reco::PFBlockElement::kNBETypes, linkTests_, SiStripPI::max, min(), and reco::PFBlockElement::type().

Referenced by packLinks().

263  {
264  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
265  dist = -1.0;
266  const PFBlockElement::Type type1 = el1->type();
267  const PFBlockElement::Type type2 = el2->type();
268  const unsigned index = rowsize * std::max(type1, type2) + std::min(type1, type2);
269  if (debug_) {
270  std::cout << " PFBlockAlgo links type1 " << type1 << " type2 " << type2 << std::endl;
271  }
272 
273  // index is always checked in the preFilter above, no need to check here
274  dist = linkTests_[index]->testLink(el1, el2);
275 }
Type type() const
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
Definition: PFBlockAlgo.h:83
bool debug_
if true, debug printouts activated
Definition: PFBlockAlgo.h:75
#define constexpr
void PFBlockAlgo::packLinks ( reco::PFBlock block,
const std::unordered_map< std::pair< unsigned int, unsigned int >, double > &  links 
) const
private

compute missing links in the blocks (the recursive procedure does not build all links)

Definition at line 219 of file PFBlockAlgo.cc.

References reco::PFBlock::bookLinkData(), constexpr, reco::PFBlock::elements(), testProducerWithPsetDescEmpty_cfi::i1, testProducerWithPsetDescEmpty_cfi::i2, reco::PFBlockElement::kNBETypes, link(), reco::PFBlock::linkData(), electronStore::links, linkTests_, reco::PFBlock::setLink(), and edm::OwnVector< T, P >::size().

Referenced by findBlocks().

220  {
221  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
222 
223  const edm::OwnVector<reco::PFBlockElement>& els = block.elements();
224 
225  block.bookLinkData();
226  unsigned elsize = els.size();
227  //First Loop: update all link data
228  for (unsigned i1 = 0; i1 < elsize; ++i1) {
229  for (unsigned i2 = 0; i2 < i1; ++i2) {
230  // no reflexive link
231  //if( i1==i2 ) continue;
232 
233  double dist = -1;
234 
235  bool linked = false;
236 
237  // are these elements already linked ?
238  // this can be optimized
239  const auto link_itr = links.find(std::make_pair(i2, i1));
240  if (link_itr != links.end()) {
241  dist = link_itr->second;
242  linked = true;
243  }
244 
245  if (!linked) {
246  const PFBlockElement::Type type1 = els[i1].type();
247  const PFBlockElement::Type type2 = els[i2].type();
248  const auto minmax = std::minmax(type1, type2);
249  const unsigned index = rowsize * minmax.second + minmax.first;
250  bool bTestLink =
251  (nullptr == linkTests_[index] ? false : linkTests_[index]->linkPrefilter(&(els[i1]), &(els[i2])));
252  if (bTestLink)
253  link(&els[i1], &els[i2], dist);
254  }
255 
256  //loading link data according to link test used: RECHIT
257  //block.setLink( i1, i2, chi2, block.linkData() );
258  block.setLink(i1, i2, dist, block.linkData());
259  }
260  }
261 }
size_type size() const
Definition: OwnVector.h:300
const LinkData & linkData() const
Definition: PFBlock.h:88
void setLink(unsigned i1, unsigned i2, double dist, LinkData &linkData, LinkTest test=LINKTEST_RECHIT) const
Definition: PFBlock.cc:19
void bookLinkData()
Definition: PFBlock.cc:17
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
Definition: PFBlockAlgo.h:83
void link(const reco::PFBlockElement *el1, const reco::PFBlockElement *el2, double &dist) const
check whether 2 elements are linked. Returns distance
Definition: PFBlockAlgo.cc:263
const edm::OwnVector< reco::PFBlockElement > & elements() const
Definition: PFBlock.h:85
#define constexpr
void PFBlockAlgo::setDebug ( bool  debug)
inline

sets debug printout flag

Definition at line 59 of file PFBlockAlgo.h.

References groupFilesInBlocks::block, debug, MainPageGenerator::link, and electronStore::links.

Referenced by PFBlockProducer::PFBlockProducer().

59 { debug_ = debug; }
#define debug
Definition: HDRShower.cc:19
bool debug_
if true, debug printouts activated
Definition: PFBlockAlgo.h:75
void PFBlockAlgo::setImporters ( const std::vector< edm::ParameterSet > &  confs,
edm::ConsumesCollector sumes 
)

Definition at line 126 of file PFBlockAlgo.cc.

References beamerCreator::create(), timingPdfMaker::get, HLT_2018_cff::importerName, importers_, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by PFBlockProducer::PFBlockProducer().

126  {
127  importers_.reserve(confs.size());
128  for (const auto& conf : confs) {
129  const std::string& importerName = conf.getParameter<std::string>("importerName");
130  importers_.emplace_back(BlockElementImporterFactory::get()->create(importerName, conf, sumes));
131  }
132 }
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< std::unique_ptr< BlockElementImporterBase > > importers_
Definition: PFBlockAlgo.h:80
void PFBlockAlgo::setLinkers ( const std::vector< edm::ParameterSet > &  confs)

Definition at line 86 of file PFBlockAlgo.cc.

References constexpr, beamerCreator::create(), elementTypes_, Exception, timingPdfMaker::get, mps_fire::i, dqmiolumiharvest::j, kdtrees_, reco::PFBlockElement::kNBETypes, HLT_2018_cff::linkerName, linkTests_, linkTestSquare_, SiStripPI::max, min(), ZMuMuAnalysisNtupler_cff::prefix, cms::dd::split(), AlCaHLTBitMon_QueryRunRegistry::string, and HLT_2018_cff::useKDTree.

Referenced by PFBlockProducer::PFBlockProducer().

86  {
87  constexpr unsigned rowsize = reco::PFBlockElement::kNBETypes;
88  for (unsigned i = 0; i < rowsize; ++i) {
89  for (unsigned j = 0; j < rowsize; ++j) {
90  linkTestSquare_[i][j] = 0;
91  }
92  }
93  linkTests_.resize(rowsize * rowsize);
94  const std::string prefix("PFBlockElement::");
95  const std::string pfx_kdtree("KDTree");
96  for (const auto& conf : confs) {
97  const std::string& linkerName = conf.getParameter<std::string>("linkerName");
98  const std::string& linkTypeStr = conf.getParameter<std::string>("linkType");
99  size_t split = linkTypeStr.find(':');
100  if (split == std::string::npos) {
101  throw cms::Exception("MalformedLinkType") << "\"" << linkTypeStr << "\" is not a valid link type definition."
102  << " This string should have the form \"linkFrom:linkTo\"";
103  }
104  std::string link1(prefix + linkTypeStr.substr(0, split));
105  std::string link2(prefix + linkTypeStr.substr(split + 1, std::string::npos));
106  if (!(elementTypes_.count(link1) && elementTypes_.count(link2))) {
107  throw cms::Exception("InvalidBlockElementType")
108  << "One of \"" << link1 << "\" or \"" << link2 << "\" are invalid block element types!";
109  }
110  const PFBlockElement::Type type1 = elementTypes_.at(link1);
111  const PFBlockElement::Type type2 = elementTypes_.at(link2);
112  const unsigned index = rowsize * std::max(type1, type2) + std::min(type1, type2);
113  linkTests_[index] = BlockElementLinkerFactory::get()->create(linkerName, conf);
114  linkTestSquare_[type1][type2] = index;
115  linkTestSquare_[type2][type1] = index;
116  // setup KDtree if requested
117  const bool useKDTree = conf.getParameter<bool>("useKDTree");
118  if (useKDTree) {
119  kdtrees_.emplace_back(KDTreeLinkerFactory::get()->create(pfx_kdtree + linkerName, conf));
120  kdtrees_.back()->setTargetType(std::min(type1, type2));
121  kdtrees_.back()->setFieldType(std::max(type1, type2));
122  } // useKDTree
123  } // loop over confs
124 }
std::vector< std::string_view > split(std::string_view, const char *)
def create(alignables, pedeDump, additionalData, outputFile, config)
std::vector< std::unique_ptr< KDTreeLinkerBase > > kdtrees_
Definition: PFBlockAlgo.h:86
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< std::unique_ptr< BlockElementLinkerBase > > linkTests_
Definition: PFBlockAlgo.h:83
const std::unordered_map< std::string, reco::PFBlockElement::Type > elementTypes_
Definition: PFBlockAlgo.h:82
unsigned int linkTestSquare_[reco::PFBlockElement::kNBETypes][reco::PFBlockElement::kNBETypes]
Definition: PFBlockAlgo.h:84
#define constexpr
void PFBlockAlgo::updateEventSetup ( const edm::EventSetup es)

Definition at line 277 of file PFBlockAlgo.cc.

References importers_.

Referenced by PFBlockProducer::beginLuminosityBlock().

277  {
278  for (auto& importer : importers_) {
279  importer->updateEventSetup(es);
280  }
281 }
std::vector< std::unique_ptr< BlockElementImporterBase > > importers_
Definition: PFBlockAlgo.h:80

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const PFBlockAlgo a 
)
friend

Definition at line 327 of file PFBlockAlgo.cc.

327  {
328  if (!out)
329  return out;
330 
331  out << "====== Particle Flow Block Algorithm ======= ";
332  out << endl;
333  out << "number of unassociated elements : " << a.elements_.size() << endl;
334  out << endl;
335 
336  for (auto const& element : a.elements_) {
337  out << "\t" << *element << endl;
338  }
339 
340  return out;
341 }
ElementList elements_
Definition: PFBlockAlgo.h:71

Member Data Documentation

bool PFBlockAlgo::debug_
private

if true, debug printouts activated

Definition at line 75 of file PFBlockAlgo.h.

Referenced by link(), and ~PFBlockAlgo().

ElementList PFBlockAlgo::elements_
private

Definition at line 71 of file PFBlockAlgo.h.

Referenced by buildElements(), findBlocks(), operator<<(), and ~PFBlockAlgo().

const std::unordered_map<std::string, reco::PFBlockElement::Type> PFBlockAlgo::elementTypes_
private

Definition at line 82 of file PFBlockAlgo.h.

Referenced by setLinkers().

std::vector<std::unique_ptr<BlockElementImporterBase> > PFBlockAlgo::importers_
private

Definition at line 80 of file PFBlockAlgo.h.

Referenced by buildElements(), setImporters(), and updateEventSetup().

std::vector<std::unique_ptr<KDTreeLinkerBase> > PFBlockAlgo::kdtrees_
private

Definition at line 86 of file PFBlockAlgo.h.

Referenced by buildElements(), findBlocks(), and setLinkers().

std::vector<std::unique_ptr<BlockElementLinkerBase> > PFBlockAlgo::linkTests_
private

Definition at line 83 of file PFBlockAlgo.h.

Referenced by findBlocks(), link(), packLinks(), and setLinkers().

unsigned int PFBlockAlgo::linkTestSquare_[reco::PFBlockElement::kNBETypes][reco::PFBlockElement::kNBETypes]
private

Definition at line 84 of file PFBlockAlgo.h.

Referenced by findBlocks(), and setLinkers().

ElementRanges PFBlockAlgo::ranges_
private

Definition at line 72 of file PFBlockAlgo.h.

Referenced by buildElements(), and findBlocks().

bool PFBlockAlgo::useHO_
private

Definition at line 78 of file PFBlockAlgo.h.