stablejet is hosted by Hepforge, IPPP Durham
StableJet
ClusterAnalysisHighPt.cc
1 
2 #include <climits>
3 
4 #include "ClusterAnalysisHighPt.h"
5 
6 #include "matchOneToOne.hh"
7 
8 namespace stab {
9 
11 void ClusterAnalysisHighPt::initialize(const fastjet::ClusterSequence &cs)
12 {
13 
14  // clear the class
15  clear();
16 
17  // how many particles do we have
18  m_nEvent = cs.n_particles();
19 
20  // check if there is room in our maps and vectors
21  if ( m_nEvent > m_nEventSize ) {
22  m_nEventSize = m_nEvent;
23  m_event.resize(m_nEvent);
24  m_partMap.resize(m_nEvent);
25  m_cpStab.resize(m_nEvent);
26  }
27 
28  // zero the cp stability
29  for ( unsigned i=0; i != m_nEvent; i++ )
30  m_cpStab[0] = 0.;
31 
32  // fill the initial event into m_event
33  const Event & allJets = cs.jets();
34  for ( unsigned jp=0; jp != m_nEvent; jp++ ) {
35  const int jpHistIndex = allJets[jp].cluster_sequence_history_index();
36  assert(jpHistIndex >= 0);
37  m_event[jp] = allJets[jp];
38  assert( m_event[jp].pt() > 0. );
39  }
40 
41 
42  // perform initial clustering
43  const Event & cluJets = fastjet::sorted_by_pt(cs.exclusive_jets(m_dCut));
44 
45  // make sure there is room in m_jets and the like
46  m_nJets=cluJets.size();
47  m_nJetsInCycle = m_nJets;
48  if ( m_nJetSize < m_nJets ) {
49  m_jets.resize(m_nJets);
50  m_jetCycleMap.resize(m_nJets);
51  }
52 
53  const std::vector<int> & histMap = cs.particle_jet_indices(cluJets);
54  assert(m_nEvent == histMap.size());
55  for ( unsigned p=0; p != m_nEvent; p++ ) {
56  const int index = histMap[p];
57  if ( index >= 0 && index < 2 ) m_partMap[p] = index;
58  else if ( m_useJunkJet && index > 1 ) m_partMap[p] = 2U;
59  else {
60  m_partMap[p] = UINT_MAX;
61  }
62  }
63 
64 }
65 
66 
68 void ClusterAnalysisHighPt::fill(const fastjet::ClusterSequence &cs)
69 {
70 
71  // get the clustered jets
72  const Event & cluJets = fastjet::sorted_by_pt(cs.exclusive_jets(m_dCut));
73  m_nJetsInCycle = cluJets.size();
74 
75  // make sure we don't exceed the number of jets seen thus far
76  if ( m_nJetsInCycle > m_nJetSize ) {
77  m_nJetSize = m_nJetsInCycle;
78  m_jets.resize(m_nJetSize);
79  m_jetCycleMap.resize(m_nJetSize);
80  }
81 
82  // map cycle's jets to m_jets indices
83  for ( unsigned j=0; j != m_nJetsInCycle; j++ )
84  m_jetCycleMap[j]=0U;
85 
86  // match one-to-one
87  std::vector<int> matchIndex;
88  deltaRCalc calc;
89  matchOneToOne(cluJets,m_jets,m_nJetsInCycle,m_nJets,calc,&matchIndex);
90 
91  for ( unsigned j=0; j != m_nJetsInCycle; j++ ) {
92  const int index = matchIndex[j];
93  if ( index >=0 ) m_jetCycleMap[j] = index;
94  else { // we have encountered a new jet
95  if ( m_nJetSize == m_nJets ) m_jets.resize(++m_nJetSize);
96  m_jets[m_nJets] = cluJets[j];
97  m_jetCycleMap[j] = m_nJets++;
98  }
99  }
100 
101  // fill particle maps and histograms
102  std::vector<unsigned> partMap(m_nEvent);
103  const std::vector<int> & histMap = cs.particle_jet_indices(cluJets);
104  assert(m_nEvent == histMap.size());
105  for ( unsigned p=0; p != m_nEvent; p++ ) {
106  const int index = histMap[p];
107  if ( index >= 0 && index < 2 ) partMap[p] = index;
108  else if ( m_useJunkJet && index > 1 ) partMap[p] = 2U;
109  else partMap[p] = UINT_MAX;
110  }
111  m_partMapVec.push_back(partMap);
112 
113  // increment m_nCycle
114  m_nCycle++;
115 
116  assert( m_nJetsInCycle );
117 }
118 
120 {
121  m_nCycle=0U;
122 
123  // clear event
124  for ( unsigned p=0; p != m_nEventSize; p++ ) {
125  m_event[p] = PseudoJet();
126  m_partMap[p] = 0U;
127  }
128  m_nEvent = 0U;
129  m_partMapVec.clear();
130 
131  for ( unsigned j=0; j != m_nJetSize; j++ ) {
132  m_jets[j] = PseudoJet();
133  m_jetCycleMap[j] = 0U;
134  }
135  m_nJets = 0U;
136 
137 }
138 
139 
140 double ClusterAnalysisHighPt::doStability(const ClusteringComparisonIndexCP& index, const StabilityCalcMethod method)
141 {
142  double stab=0.;
143 
144  switch(method) {
145  case PAIRWISE:
146  stab = pairwiseStability(index);
147  break;
148  case EXHAUSTIVE:
149  stab = exhaustiveStability(index);
150  break;
151  case UNSMEARED:
152  stab = unsmearedStability(index);
153  break;
154  case RESAMPLED:
155  stab = resampledStability(index);
156  break;
157  }
158 
159  return stab;
160 }
161 
162 
163 double ClusterAnalysisHighPt::pairwiseStability(const ClusteringComparisonIndexCP &index)
164 {
165 
166  long double stab=0.L;
167 
168  for ( unsigned c=0; c != m_nCycle-1U; c++ ) {
169  std::vector<unsigned> & clustMap1 = m_partMapVec[c];
170  std::vector<unsigned> & clustMap2 = m_partMapVec[c+1U];
171  const double temp = index.index(&m_event[0],&clustMap1[0],&clustMap2[0],m_nEvent,&m_cpStab[0]);
172  stab += temp;
173  }
174 
175  // normalize the constituent perspective stability measures
176  for ( unsigned i=0; i != m_nEvent; i++ )
177  m_cpStab[i] /= (m_nCycle-1U);
178 
179  stab /= (m_nCycle-1);
180  //std::cout << "stab=" << stab << std::endl;
181 
182  return stab;
183 
184 }
185 
186 double ClusterAnalysisHighPt::exhaustiveStability(const ClusteringComparisonIndexCP &index)
187 {
188 
189  double stab=0.;
190 
191  for ( unsigned c1=0; c1 != m_nCycle-1U; c1++ ) {
192  std::vector<unsigned> & clustMap1 = m_partMapVec[c1];
193  for ( unsigned c2=c1+1; c2 != m_nCycle; c2++ ) {
194  std::vector<unsigned> & clustMap2 = m_partMapVec[c2];
195  stab += index.index(&m_event[0],&clustMap1[0],&clustMap2[0],m_nEvent,&m_cpStab[0]);
196  }
197  }
198 
199  // normalize the constituent perspective stability measures
200  for ( unsigned i=0; i != m_nEvent; i++ )
201  m_cpStab[i] /= (double)m_nCycle*((double)m_nCycle-1.)/2.;
202 
203  stab /= (double)m_nCycle*((double)m_nCycle-1.)/2.;
204  return stab;
205 }
206 
207 double ClusterAnalysisHighPt::unsmearedStability(const ClusteringComparisonIndexCP &index)
208 {
209  double stab=0.;
210 
211  for ( unsigned c=0; c != m_nCycle; c++ ) {
212  std::vector<unsigned> & clustMap = m_partMapVec[c];
213  stab += index.index(&m_event[0],&m_partMap[0],&clustMap[0],m_nEvent,&m_cpStab[0]);
214  }
215 
216  // normalize the constituent perspective stability measures
217  for ( unsigned i=0; i != m_nEvent; i++ )
218  m_cpStab[i] /= m_nCycle;
219 
220  stab /= m_nCycle;
221  return stab;
222 }
223 
224 double ClusterAnalysisHighPt::resampledStability(const ClusteringComparisonIndexCP &index)
225 {
226  return 0.;
227 }
228 
230 {
231 
232  // construct weights
233  const unsigned weightSize = m_nJets*m_nEvent;
234  double weights[weightSize];
235 
236  for ( unsigned i=0; i != weightSize; i++ )
237  weights[i] = 0.0;
238 
239  // fill weights as probability of initial particle to go into a final jet
240  // ** this could use some optimization **
241  for ( unsigned i=0; i != m_nJets; i++ ) {
242  for ( unsigned j=0; j != m_nEvent; j++ ) {
243  unsigned count = 0;
244  for ( unsigned k=0; k != m_nCycle; k++ )
245  if ( m_partMapVec[k][j] == i ) count++;
246  weights[i*m_nEvent+j] = (double)count/(double)m_nCycle;
247  assert( weights[i*m_nEvent+j] >= 0. );
248  }
249  }
250 
251  bool lastFinalJetIsUnclustered=false;
252  const unsigned lenJetFuzziness = m_nJets;
253  double jetFuzziness[lenJetFuzziness];
254 
255  return fuzzyCalc.calculate(m_event, m_jets, weights, m_nJets, m_nEvent, lastFinalJetIsUnclustered,
256  jetFuzziness, lenJetFuzziness);
257 
258  return 0.;
259 }
260 
261 } // end stab namespace
void clear()
clear the contents
void fill(const fastjet::ClusterSequence &)
Add results of latest clustering to stability consideration.
double doStability(const ClusteringComparisonIndexCP &, const StabilityCalcMethod method=PAIRWISE)
perform the stability calculation
double doFuzziness(const AbsFuzzinessCalculator &)
perform the fuzziness calculation
void initialize(const fastjet::ClusterSequence &)
Initialize internal data from first clustering results.
virtual double index(const PseudoJet *inputParticles, const unsigned *clustering1, const unsigned *clustering2, unsigned size, double *constitStab) const =0