CMS 3D CMS Logo

Qjets.cc
Go to the documentation of this file.
2 
3 using namespace std;
4 
5 void Qjets::SetRandSeed(unsigned int seed){
6  _rand_seed_set = true;
7  _seed = seed;
8 }
9 
10 bool Qjets::JetUnmerged(int num) const{
11  return _merged_jets.find(num) == _merged_jets.end();
12 }
13 
14 bool Qjets::JetsUnmerged(const JetDistance& jd) const{
15  return JetUnmerged(jd.j1) && JetUnmerged(jd.j2);
16 }
17 
19  vector< pair<JetDistance, double> > popped_distances;
20  double norm(0.);
21  JetDistance ret;
22  ret.j1 = -1;
23  ret.j2 = -1;
24  ret.dij = -1.;
25  bool dmin_set(false);
26  double dmin(0.);
27 
28  while(!_distances.empty()){
29  JetDistance dist = _distances.top();
30  _distances.pop();
31  if(JetsUnmerged(dist)){
32  if(!dmin_set){
33  dmin = dist.dij;
34  dmin_set = true;
35  }
36  double weight = exp(-_rigidity * (dist.dij-dmin) /dmin);
37  popped_distances.push_back(make_pair(dist,weight));
38  norm += weight;
39  if(weight/norm < _truncation_fctr)
40  break;
41  }
42  }
43 
44  double rand(Rand()), tot_weight(0.);
45  for(vector<pair<JetDistance, double> >::iterator it = popped_distances.begin(); it != popped_distances.end(); it++){
46  tot_weight += (*it).second/norm;
47  if(tot_weight >= rand){
48  ret = (*it).first;
49  omega *= ((*it).second);
50  break;
51  }
52  }
53 
54  // repopulate in reverse (maybe quicker?)
55  for(vector<pair<JetDistance, double> >::reverse_iterator it = popped_distances.rbegin(); it != popped_distances.rend(); it++)
56  if(JetsUnmerged((*it).first))
57  _distances.push((*it).first);
58 
59  return ret;
60 }
61 
62 void Qjets::Cluster(fastjet::ClusterSequence & cs){
63  omega = 1.;
64  QjetsBaseExtras * extras = new QjetsBaseExtras();
65  computeDCut(cs);
66 
67  // Populate all the distances
68  ComputeAllDistances(cs.jets());
69  JetDistance jd = GetNextDistance();
70 
71  while(!_distances.empty() && jd.dij != -1.){
72  if(!Prune(jd,cs)){
73  // _merged_jets.push_back(jd.j1);
74  // _merged_jets.push_back(jd.j2);
75  _merged_jets[jd.j1] = true;
76  _merged_jets[jd.j2] = true;
77 
78  int new_jet=-1;
79  cs.plugin_record_ij_recombination(jd.j1, jd.j2, 1., new_jet);
80  if(!JetUnmerged(new_jet))
81  edm::LogError("QJets Clustering") << "Problem with FastJet::plugin_record_ij_recombination";
82  ComputeNewDistanceMeasures(cs,new_jet);
83  } else {
84  double j1pt = cs.jets()[jd.j1].perp();
85  double j2pt = cs.jets()[jd.j2].perp();
86  if(j1pt>j2pt){
87  // _merged_jets.push_back(jd.j2);
88  _merged_jets[jd.j2] = true;
89  cs.plugin_record_iB_recombination(jd.j2, 1.);
90  } else {
91  // _merged_jets.push_back(jd.j1);
92  _merged_jets[jd.j1] = true;
93  cs.plugin_record_iB_recombination(jd.j1, 1.);
94  }
95  }
96  jd = GetNextDistance();
97  }
98 
99  extras->_wij = omega;
100  cs.plugin_associate_extras(extras);
101 
102  // merge remaining jets with beam
103  int num_merged_final(0);
104  for(unsigned int i = 0 ; i < cs.jets().size(); i++)
105  if(JetUnmerged(i)){
106  num_merged_final++;
107  cs.plugin_record_iB_recombination(i,1.);
108  }
109 
110  if(!(num_merged_final < 2))
111  edm::LogError("QJets Clustering") << "More than 1 jet remains after reclustering";
112 }
113 
114 void Qjets::computeDCut(fastjet::ClusterSequence & cs){
115  // assume all jets in cs form a single jet. compute mass and pt
116  fastjet::PseudoJet sum(0.,0.,0.,0.);
117  for(vector<fastjet::PseudoJet>::const_iterator it = cs.jets().begin(); it != cs.jets().end(); it++)
118  sum += (*it);
119  _dcut = 2. * _dcut_fctr * sum.m()/sum.perp();
120 }
121 
122 bool Qjets::Prune(JetDistance& jd,fastjet::ClusterSequence & cs){
123  double pt1 = cs.jets()[jd.j1].perp();
124  double pt2 = cs.jets()[jd.j2].perp();
125  fastjet::PseudoJet sum_jet = cs.jets()[jd.j1]+cs.jets()[jd.j2];
126  double sum_pt = sum_jet.perp();
127  double z = min(pt1,pt2)/sum_pt;
128  double d2 = cs.jets()[jd.j1].plain_distance(cs.jets()[jd.j2]);
129  return (d2 > _dcut*_dcut) && (z < _zcut);
130 }
131 
132 void Qjets::ComputeAllDistances(const vector<fastjet::PseudoJet>& inp){
133  for(unsigned int i = 0 ; i < inp.size()-1; i++){
134  // jet-jet distances
135  for(unsigned int j = i+1 ; j < inp.size(); j++){
136  JetDistance jd;
137  jd.j1 = i;
138  jd.j2 = j;
139  if(jd.j1 != jd.j2){
140  jd.dij = d_ij(inp[i],inp[j]);
141  _distances.push(jd);
142  }
143  }
144  }
145 }
146 
147 void Qjets::ComputeNewDistanceMeasures(fastjet::ClusterSequence & cs, unsigned int new_jet){
148  // jet-jet distances
149  for(unsigned int i = 0; i < cs.jets().size(); i++)
150  if(JetUnmerged(i) && i != new_jet){
151  JetDistance jd;
152  jd.j1 = new_jet;
153  jd.j2 = i;
154  jd.dij = d_ij(cs.jets()[jd.j1], cs.jets()[jd.j2]);
155  _distances.push(jd);
156  }
157 }
158 
159 double Qjets::d_ij(const fastjet::PseudoJet& v1,const fastjet::PseudoJet& v2) const{
160  double p1 = v1.perp();
161  double p2 = v2.perp();
162  double ret = pow(min(p1,p2),_exp_min) * pow(max(p1,p2),_exp_max) * v1.squared_distance(v2);
163  if(edm::isNotFinite(ret))
164  edm::LogError("QJets Clustering") << "d_ij is not finite";
165  return ret;
166 }
167 
168 double Qjets::Rand(){
169  double ret = 0.;
170  //if(_rand_seed_set)
171  // ret = rand_r(&_seed)/(double)RAND_MAX;
172  //else
173  //ret = rand()/(double)RAND_MAX;
174  ret = _rnEngine->flat();
175  return ret;
176 }
void ComputeNewDistanceMeasures(fastjet::ClusterSequence &cs, unsigned int new_jet)
Definition: Qjets.cc:147
void Cluster(fastjet::ClusterSequence &cs)
Definition: Qjets.cc:62
double _wij
Definition: Qjets.h:77
bool JetUnmerged(int num) const
Definition: Qjets.cc:10
void SetRandSeed(unsigned int seed)
Definition: Qjets.cc:5
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
unique_ptr< ClusterSequence > cs
double dij
Definition: Qjets.h:17
bool Prune(JetDistance &jd, fastjet::ClusterSequence &cs)
Definition: Qjets.cc:122
int j2
Definition: Qjets.h:19
Definition: weight.py:1
void computeDCut(fastjet::ClusterSequence &cs)
Definition: Qjets.cc:114
JetDistance GetNextDistance()
Definition: Qjets.cc:18
T min(T a, T b)
Definition: MathUtil.h:58
double p2[4]
Definition: TauolaWrapper.h:90
double Rand()
Definition: Qjets.cc:168
double d_ij(const fastjet::PseudoJet &v1, const fastjet::PseudoJet &v2) const
Definition: Qjets.cc:159
void ComputeAllDistances(const std::vector< fastjet::PseudoJet > &inp)
Definition: Qjets.cc:132
double p1[4]
Definition: TauolaWrapper.h:89
Signal rand(Signal arg)
Definition: vlib.cc:442
int j1
Definition: Qjets.h:18
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
bool JetsUnmerged(const JetDistance &jd) const
Definition: Qjets.cc:14