CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
189  // for each element in turn
190  std::vector<bool> toPrint(elements.size(),static_cast<bool>(true));
191  for(unsigned ie=0; ie<elements.size(); ie++) {
192 
193  PFBlockElement::Type type = elements[ie].type();
194  std::multimap<double, unsigned> ecalElems;
195  switch(type){
196  case PFBlockElement::TRACK:
197  iTK++;
198  ss << "TK" << iTK;
199  break;
200  case PFBlockElement::GSF:
201  iGSF++;
202  ss << "GSF" << iGSF;
203  break;
204  case PFBlockElement::BREM:
205  block.associatedElements( ie, block.linkData(),
206  ecalElems ,
209  iBREM++;
210  if ( ecalElems.size() ) {
211  ss << "BR" << iBREM;
212  } else {
213  toPrint[ie] = false;
214  }
215  break;
216  case PFBlockElement::SC:
217  iSC++;
218  ss << "SC" << iSC;
219  break;
220  default:{
221  PFClusterRef clusterref = elements[ie].clusterRef();
222  int layer = clusterref->layer();
223  switch (layer){
224  case PFLayer::PS1:
225  iPS1++;
226  ss << "PV" << iPS1;
227  break;
228  case PFLayer::PS2:
229  iPS2++;
230  ss << "PH" << iPS2;
231  break;
233  iEE++;
234  ss << "EE" << iEE;
235  break;
237  iEB++;
238  ss << "EB" << iEB;
239  break;
241  iHE++;
242  ss << "HE" << iHE;
243  break;
245  iHB++;
246  ss << "HB" << iHB;
247  break;
248  case PFLayer::HF_EM:
249  iHFEM++;
250  ss << "FE" << iHFEM;
251  break;
252  case PFLayer::HF_HAD:
253  iHFHAD++;
254  ss << "FH" << iHFHAD;
255  break;
256  default:
257  iel++;
258  ss << "??" << iel;
259  break;
260  }
261  break;
262  }
263  }
264  s = ss.str();
265  elid.push_back( s );
266  // clear stringstream
267  ss.str("");
268 
269  if ( toPrint[ie] ) out<<"\t"<< s <<" "<<elements[ie] <<endl;
270  }
271 
272  out<<endl;
273 
274  int width = 6;
275  if( !block.linkData().empty() ) {
276  out<<endl<<"\tlink data (distance x 1000): "<<endl;
277  out<<setiosflags(ios::right);
278  out<<"\t" << setw(width) << " ";
279  for(unsigned ie=0; ie<elid.size(); ie++)
280  if ( toPrint[ie] ) out <<setw(width)<< elid[ie];
281  out<<endl;
282  out<<setiosflags(ios::fixed);
283  out<<setprecision(1);
284 
285  for(unsigned i=0; i<block.elements_.size(); i++) {
286  if ( !toPrint[i] ) continue;
287  out<<"\t";
288  out <<setw(width) << elid[i];
289  for(unsigned j=0; j<block.elements_.size(); j++) {
290  if ( !toPrint[j] ) continue;
291  double Dist = block.dist(i,j, block.linkData());//,PFBlock::LINKTEST_ALL);
292 
293  // out<<setw(width)<< Dist*1000.;
294  if (Dist > -0.5) out<<setw(width)<< Dist*1000.;
295  else out <<setw(width)<< " ";
296  }
297  out<<endl;
298  }
299 
300 
301  out<<endl<<"\tlink data (distance x 1000) for tracking links : "<<endl;
302  out<<setiosflags(ios::right);
303  out<<"\t" << setw(width) << " ";
304  for(unsigned ie=0; ie<elid.size(); ie++)
305  if ( toPrint[ie] &&
306  ( block.elements_[ie].type() == PFBlockElement::TRACK ||
307  block.elements_[ie].type() == PFBlockElement::GSF ))
308  out <<setw(width)<< elid[ie];
309  out<<endl;
310  out<<setiosflags(ios::fixed);
311  out<<setprecision(1);
312 
313  for(unsigned i=0; i<block.elements_.size(); i++) {
314  if ( !toPrint[i] ||
315  (block.elements_[i].type() != PFBlockElement::TRACK &&
316  block.elements_[i].type() != PFBlockElement::GSF )) continue;
317  out<<"\t";
318  out <<setw(width) << elid[i];
319  for(unsigned j=0; j<block.elements_.size(); j++) {
320  if ( !toPrint[j] ||
321  (block.elements_[j].type() != PFBlockElement::TRACK &&
322  block.elements_[j].type() != PFBlockElement::GSF )) continue;
323  double Dist = block.dist(i,j, block.linkData());//,PFBlock::LINKTEST_ALL);
324 
325  // out<<setw(width)<< Dist*1000.;
326  if (Dist > -0.5) out<<setw(width)<< Dist*1000.;
327  else out <<setw(width)<< " ";
328  }
329  out<<endl;
330  }
331 
332  out<<setprecision(3);
333  out<<resetiosflags(ios::right|ios::fixed);
334 
335  }
336  else {
337  out<<"\tno links."<<endl;
338  }
339 
340  return out;
341 }
342 
343 
344 unsigned PFBlock::linkDataSize() const {
345  unsigned n = elements_.size();
346 
347  // number of possible undirected links between n elements.
348  // reflective links impossible.
349 
350  return n*(n-1)/2;
351 }
type
Definition: HCALResponse.h:22
dictionary missing
Definition: combine.py:4
Abstract base class for a PFBlock element (track, cluster...)
int i
Definition: DBlmapReader.cc:9
std::map< unsigned int, Link > LinkData
Definition: PFBlock.h:46
size_type size() const
Definition: OwnVector.h:262
list elements
Definition: asciidump.py:414
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&lt;PFBlockElement&gt; working
std::ostream & operator<<(std::ostream &, BeamSpot beam)
Definition: BeamSpot.cc:72
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void setIndex(unsigned index)
set index
int j
Definition: DBlmapReader.cc:9
void lock()
lock element
tuple out
Definition: dbtoconf.py:99
edm::OwnVector< reco::PFBlockElement > elements_
all elements
Definition: PFBlock.h:132
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
double dist(unsigned ie1, unsigned ie2, const LinkData &linkData, LinkTest test) const
Definition: PFBlock.h:94
string s
Definition: asciidump.py:422
tuple size
Write out results.
Block of elements.
Definition: PFBlock.h:30