SortableObject.h
Go to the documentation of this file.
1 /*
2  * Delphes: a framework for fast simulation of a generic collider experiment
3  * Copyright (C) 2012-2014 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SortableObject_h
20 #define SortableObject_h
21 
22 
33 #include "TRef.h"
34 #include "TObject.h"
35 #include "TRefArray.h"
36 
37 #include "TMath.h"
38 
39 // namespace delphes{
40 
41 //---------------------------------------------------------------------------
42 
43 class CompBase
44 {
45 public:
46  virtual ~CompBase() { }
47  virtual Bool_t IsSortable(const TObject *) const { return kTRUE; }
48  virtual Int_t Compare(const TObject *obj1, const TObject *obj2) const = 0;
49 };
50 
51 //---------------------------------------------------------------------------
52 
53 class SortableObject: public TObject
54 {
55 public:
56 
57  Bool_t IsSortable() const { return GetCompare() ? GetCompare()->IsSortable(this) : kFALSE; }
58  Int_t Compare(const TObject *obj) const { return GetCompare()->Compare(this, obj); }
59 
60  virtual const CompBase *GetCompare() const = 0;
61 
62  ClassDef(SortableObject, 1)
63 };
64 
65 //---------------------------------------------------------------------------
66 // Standard Comparison Criteria: E, ET, PT, DeltaR
67 //---------------------------------------------------------------------------
68 
69 template <typename T>
70 class CompE: public CompBase
71 {
72  CompE() {}
73 public:
74  static CompE *Instance()
75  {
76  static CompE single;
77  return &single;
78  }
79 
80  Int_t Compare(const TObject *obj1, const TObject *obj2) const
81  {
82  auto t1 = static_cast<const T*>(obj1);
83  auto t2 = static_cast<const T*>(obj2);
84  if(t1->E > t2->E)
85  return -1;
86  else if(t1->E < t2->E)
87  return 1;
88  else
89  return 0;
90  }
91 };
92 
93 //---------------------------------------------------------------------------
94 
95 template <typename T>
96 class CompPT: public CompBase
97 {
98  CompPT() {}
99 public:
100  static CompPT *Instance()
101  {
102  static CompPT single;
103  return &single;
104  }
105 
106  Int_t Compare(const TObject *obj1, const TObject *obj2) const
107  {
108  auto t1 = static_cast<const T*>(obj1);
109  auto t2 = static_cast<const T*>(obj2);
110  if(t1->PT > t2->PT)
111  return -1;
112  else if(t1->PT < t2->PT)
113  return 1;
114  else
115  return 0;
116  }
117 };
118 
119 //---------------------------------------------------------------------------
120 
121 template <typename T>
123 {
124  CompMomentumPt() {}
125 public:
127  {
128  static CompMomentumPt single;
129  return &single;
130  }
131 
132  Int_t Compare(const TObject *obj1, const TObject *obj2) const
133  {
134  auto t1 = static_cast<const T*>(obj1);
135  auto t2 = static_cast<const T*>(obj2);
136  if(t1->Momentum.Pt() > t2->Momentum.Pt())
137  return -1;
138  else if(t1->Momentum.Pt() < t2->Momentum.Pt())
139  return 1;
140  else
141  return 0;
142  }
143 };
144 
145 //---------------------------------------------------------------------------
146 
147 template <typename T>
148 class CompET: public CompBase
149 {
150  CompET() {}
151 public:
152  static CompET *Instance()
153  {
154  static CompET single;
155  return &single;
156  }
157 
158  Int_t Compare(const TObject *obj1, const TObject *obj2) const
159  {
160  auto t1 = static_cast<const T*>(obj1);
161  auto t2 = static_cast<const T*>(obj2);
162  if(t1->ET > t2->ET)
163  return -1;
164  else if(t1->ET < t2->ET)
165  return 1;
166  else
167  return 0;
168  }
169 };
170 
171 //---------------------------------------------------------------------------
172 
173 template <typename T1, typename T2>
174 class CompDeltaR: public CompBase
175 {
176  CompDeltaR(const T2 *obj = 0) : fObj(obj) {}
177 
178  Double_t DeltaPhi(Double_t phi1, Double_t phi2)
179  {
180  auto phi = TMath::Abs(phi1 - phi2);
181  return (phi <= TMath::Pi()) ? phi : (2.0*TMath::Pi()) - phi;
182  }
183 
184  Double_t Sqr(Double_t x) { return x*x; }
185 
186  Double_t SumSqr(Double_t a, Double_t b)
187  {
188  auto aAbs = TMath::Abs(a);
189  auto bAbs = TMath::Abs(b);
190  if(aAbs > bAbs) return aAbs * TMath::Sqrt(1.0 + Sqr(bAbs / aAbs));
191  else return (bAbs == 0) ? 0.0 : bAbs * TMath::Sqrt(1.0 + Sqr(aAbs / bAbs));
192  };
193 
194  const T2 *fObj;
195 
196 public:
197  static CompDeltaR *Instance(const T2 *obj = 0)
198  {
199  static CompDeltaR single(obj);
200  return &single;
201  }
202 
203  void SetObject(const T2 *obj) { fObj = obj; }
204 
205  Int_t Compare(const TObject *obj1, const TObject *obj2) const
206  {
207  Double_t eta[3], phi[3], deltaR[2];
208  auto t1 = static_cast<const T1*>(obj1);
209  auto t2 = static_cast<const T1*>(obj2);
210 
211  eta[0] = fObj->Eta;
212  phi[0] = fObj->Phi;
213 
214  eta[1] = t1->Eta;
215  phi[1] = t1->Phi;
216 
217  eta[2] = t2->Eta;
218  phi[2] = t2->Phi;
219 
220  deltaR[0] = SumSqr(TMath::Abs(eta[0] - eta[1]), DeltaPhi(phi[0], phi[1]));
221  deltaR[1] = SumSqr(TMath::Abs(eta[0] - eta[2]), DeltaPhi(phi[0], phi[2]));
222 
223  if(deltaR[0] < deltaR[1])
224  return -1;
225  else if(deltaR[0] > deltaR[1])
226  return 1;
227  else
228  return 0;
229  }
230 };
231 
232 // }
233 
236 #endif // SortableObject_h
237 
238 
static CompE * Instance()
Definition: SortableObject.h:74
virtual Int_t Compare(const TObject *obj1, const TObject *obj2) const =0
Definition: SortableObject.h:43
static CompDeltaR * Instance(const T2 *obj=0)
Definition: SortableObject.h:197
Bool_t IsSortable() const
Definition: SortableObject.h:57
static CompET * Instance()
Definition: SortableObject.h:152
static CompPT * Instance()
Definition: SortableObject.h:100
virtual ~CompBase()
Definition: SortableObject.h:46
Definition: SortableObject.h:148
Int_t Compare(const TObject *obj1, const TObject *obj2) const
Definition: SortableObject.h:106
static CompMomentumPt * Instance()
Definition: SortableObject.h:126
Int_t Compare(const TObject *obj1, const TObject *obj2) const
Definition: SortableObject.h:80
virtual Bool_t IsSortable(const TObject *) const
Definition: SortableObject.h:47
Int_t Compare(const TObject *obj1, const TObject *obj2) const
Definition: SortableObject.h:158
Definition: SortableObject.h:96
Definition: SortableObject.h:70
Int_t Compare(const TObject *obj1, const TObject *obj2) const
Definition: SortableObject.h:205
void SetObject(const T2 *obj)
Definition: SortableObject.h:203
Definition: SortableObject.h:122
Int_t Compare(const TObject *obj1, const TObject *obj2) const
Definition: SortableObject.h:132
constexpr double Pi()
Constant .
Definition: Si.hh:46
Definition: SortableObject.h:53
Definition: SortableObject.h:174
Int_t Compare(const TObject *obj) const
Definition: SortableObject.h:58