CMS 3D CMS Logo

PFBlock.cc
Go to the documentation of this file.
4 
5 #include <iomanip>
6 #include <sstream>
7 
8 using namespace std;
9 using namespace reco;
10 
11 void PFBlock::addElement(PFBlockElement* element) {
12  element->setIndex(elements_.size());
13  element->lock();
14  elements_.push_back(element->clone());
15 }
16 
17 void PFBlock::bookLinkData() {}
18 
19 void PFBlock::setLink(unsigned i1, unsigned i2, double Dist, LinkData& linkData, LinkTest test) const {
20  assert(test < LINKTEST_ALL);
21 
22  unsigned index = 0;
23  bool ok = matrix2vector(i1, i2, index);
24 
25  if (ok) {
26  //ignore the -1, -1 pair
27  if (Dist > -0.5) {
28  Link& l = linkData[index];
29  l.distance = Dist;
30  l.test |= (1 << test);
31  } else //delete if existing
32  {
33  LinkData::iterator it = linkData.find(index);
34  if (it != linkData.end())
35  linkData.erase(it);
36  }
37 
38  } else {
39  assert(0);
40  }
41 }
42 
43 // void PFBlock::lock(unsigned i, LinkData& linkData ) const {
44 
45 // assert( linkData.size() == linkDataSize() );
46 
47 // for(unsigned j=0; j<elements_.size(); j++) {
48 
49 // if(i==j) continue;
50 
51 // unsigned index = 0;
52 // bool ok = matrix2vector(i,j, index);
53 // if(ok)
54 // linkData[index] = -1;
55 // else
56 // assert(0);
57 // }
58 // }
59 
60 void PFBlock::associatedElements(unsigned i,
61  const LinkData& linkData,
62  multimap<double, unsigned>& sortedAssociates,
64  LinkTest test) const {
65  sortedAssociates.clear();
66 
67  // i is too large
68  if (i > elements_.size())
69  return;
70  // assert(i>=0); i >= 0, since i is unsigned
71 
72  for (unsigned ie = 0; ie < elements_.size(); ie++) {
73  // considered element itself
74  if (ie == i) {
75  continue;
76  }
77  // not the right type
78  if (type != PFBlockElement::NONE && elements_[ie].type() != type) {
79  continue;
80  }
81 
82  // Order the elements by increasing distance !
83 
84  unsigned index = 0;
85  if (!matrix2vector(i, ie, index))
86  continue;
87 
88  double c2 = -1;
89  LinkData::const_iterator it = linkData.find(index);
90  if (it != linkData.end() && (((1 << test) & it->second.test) != 0 || (test == LINKTEST_ALL)))
91  c2 = it->second.distance;
92 
93  // not associated
94  if (c2 < 0) {
95  continue;
96  }
97 
98  sortedAssociates.insert(pair<double, unsigned>(c2, ie));
99  }
100 }
101 
102 bool PFBlock::matrix2vector(unsigned iindex, unsigned jindex, unsigned& index) const {
103  unsigned size = elements_.size();
104  if (iindex == jindex || iindex >= size || jindex >= size) {
105  return false;
106  }
107 
108  if (iindex > jindex)
109  std::swap(iindex, jindex);
110 
111  index = jindex - iindex - 1;
112 
113  if (iindex > 0) {
114  index += iindex * size;
115  unsigned missing = iindex * (iindex + 1) / 2;
116  index -= missing;
117  }
118 
119  return true;
120 }
121 
122 double PFBlock::dist(unsigned ie1, unsigned ie2, const LinkData& linkData) const {
123  double Dist = -1;
124 
125  unsigned index = 0;
126  if (!matrix2vector(ie1, ie2, index))
127  return Dist;
128  LinkData::const_iterator it = linkData.find(index);
129  if (it != linkData.end())
130  Dist = it->second.distance;
131 
132  return Dist;
133 }
134 
135 ostream& reco::operator<<(ostream& out, const reco::PFBlock& block) {
136  if (!out)
137  return out;
139  out << "\t--- PFBlock --- " << endl;
140  out << "\tnumber of elements: " << elements.size() << endl;
141 
142  // Build element label (string) : elid from type, layer and occurence number
143  // use stringstream instead of sprintf to concatenate string and integer into string
144 
145  vector<string> elid;
146  string s;
147  stringstream ss;
148  int iel = 0;
149  int iTK = 0;
150  int iGSF = 0;
151  int iBREM = 0;
152  int iPS1 = 0;
153  int iPS2 = 0;
154  int iEE = 0;
155  int iEB = 0;
156  int iHE = 0;
157  int iHB = 0;
158  int iHFEM = 0;
159  int iHFHAD = 0;
160  int iSC = 0;
161  int iHO = 0;
162 
163  // for each element in turn
164  std::vector<bool> toPrint(elements.size(), static_cast<bool>(true));
165  for (unsigned ie = 0; ie < elements.size(); ie++) {
166  PFBlockElement::Type type = elements[ie].type();
167  std::multimap<double, unsigned> ecalElems;
168  switch (type) {
169  case PFBlockElement::TRACK:
170  iTK++;
171  ss << "TK" << iTK;
172  break;
173  case PFBlockElement::GSF:
174  iGSF++;
175  ss << "GSF" << iGSF;
176  break;
177  case PFBlockElement::BREM:
178  block.associatedElements(
179  ie, block.linkData(), ecalElems, reco::PFBlockElement::ECAL, reco::PFBlock::LINKTEST_ALL);
180  iBREM++;
181  if (!ecalElems.empty()) {
182  ss << "BR" << iBREM;
183  } else {
184  toPrint[ie] = false;
185  }
186  break;
187  case PFBlockElement::SC:
188  iSC++;
189  ss << "SC" << iSC;
190  break;
191  default: {
192  PFClusterRef clusterref = elements[ie].clusterRef();
193  int layer = clusterref->layer();
194  switch (layer) {
195  case PFLayer::PS1:
196  iPS1++;
197  ss << "PV" << iPS1;
198  break;
199  case PFLayer::PS2:
200  iPS2++;
201  ss << "PH" << iPS2;
202  break;
204  iEE++;
205  ss << "EE" << iEE;
206  break;
208  iEB++;
209  ss << "EB" << iEB;
210  break;
212  iHE++;
213  ss << "HE" << iHE;
214  break;
216  iHB++;
217  ss << "HB" << iHB;
218  break;
220  iHO++;
221  ss << "HO" << iHO;
222  break;
223  case PFLayer::HF_EM:
224  iHFEM++;
225  ss << "FE" << iHFEM;
226  break;
227  case PFLayer::HF_HAD:
228  iHFHAD++;
229  ss << "FH" << iHFHAD;
230  break;
231  default:
232  iel++;
233  ss << "??" << iel;
234  break;
235  }
236  break;
237  }
238  }
239  s = ss.str();
240  elid.push_back(s);
241  // clear stringstream
242  ss.str("");
243 
244  if (toPrint[ie])
245  out << "\t" << s << " " << elements[ie] << endl;
246  }
247 
248  out << endl;
249 
250  int width = 6;
251  if (!block.linkData().empty()) {
252  out << endl << "\tlink data (distance x 1000): " << endl;
253  out << setiosflags(ios::right);
254  out << "\t" << setw(width) << " ";
255  for (unsigned ie = 0; ie < elid.size(); ie++)
256  if (toPrint[ie])
257  out << setw(width) << elid[ie];
258  out << endl;
259  out << setiosflags(ios::fixed);
260  out << setprecision(1);
261 
262  for (unsigned i = 0; i < block.elements().size(); i++) {
263  if (!toPrint[i])
264  continue;
265  out << "\t";
266  out << setw(width) << elid[i];
267  for (unsigned j = 0; j < block.elements().size(); j++) {
268  if (!toPrint[j])
269  continue;
270  double Dist = block.dist(i, j, block.linkData()); //,PFBlock::LINKTEST_ALL);
271 
272  // out<<setw(width)<< Dist*1000.;
273  if (Dist > -0.5)
274  out << setw(width) << Dist * 1000.;
275  else
276  out << setw(width) << " ";
277  }
278  out << endl;
279  }
280 
281  out << endl << "\tlink data (distance x 1000) for tracking links : " << endl;
282  out << setiosflags(ios::right);
283  out << "\t" << setw(width) << " ";
284  for (unsigned ie = 0; ie < elid.size(); ie++)
285  if (toPrint[ie] &&
286  (block.elements()[ie].type() == PFBlockElement::TRACK || block.elements()[ie].type() == PFBlockElement::GSF))
287  out << setw(width) << elid[ie];
288  out << endl;
289  out << setiosflags(ios::fixed);
290  out << setprecision(1);
291 
292  for (unsigned i = 0; i < block.elements().size(); i++) {
293  if (!toPrint[i] ||
294  (block.elements()[i].type() != PFBlockElement::TRACK && block.elements()[i].type() != PFBlockElement::GSF))
295  continue;
296  out << "\t";
297  out << setw(width) << elid[i];
298  for (unsigned j = 0; j < block.elements().size(); j++) {
299  if (!toPrint[j] ||
300  (block.elements()[j].type() != PFBlockElement::TRACK && block.elements()[j].type() != PFBlockElement::GSF))
301  continue;
302  double Dist = block.dist(i, j, block.linkData()); //,PFBlock::LINKTEST_ALL);
303 
304  // out<<setw(width)<< Dist*1000.;
305  if (Dist > -0.5)
306  out << setw(width) << Dist * 1000.;
307  else
308  out << setw(width) << " ";
309  }
310  out << endl;
311  }
312 
313  out << setprecision(3);
314  out << resetiosflags(ios::right | ios::fixed);
315 
316  } else {
317  out << "\tno links." << endl;
318  }
319 
320  return out;
321 }
322 
323 unsigned PFBlock::linkDataSize() const {
324  unsigned n = elements_.size();
325 
326  // number of possible undirected links between n elements.
327  // reflective links impossible.
328 
329  return n * (n - 1) / 2;
330 }
size
Write out results.
Abstract base class for a PFBlock element (track, cluster...)
std::map< unsigned int, Link > LinkData
Definition: PFBlock.h:39
assert(be >=bs)
missing
Definition: combine.py:5
virtual PFBlockElement * clone() const =0
necessary to have the edm::OwnVector<PFBlockElement> working
std::ostream & operator<<(std::ostream &, BeamSpot beam)
Definition: BeamSpot.cc:66
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void setIndex(unsigned index)
set index
void lock()
lock element
fixed size matrix
Definition: TkAlStyle.h:43
Block of elements.
Definition: PFBlock.h:26