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