Fork me on GitHub

source: git/classes/SortableObject.h@ 952bbbc

Last change on this file since 952bbbc was 341014c, checked in by Pavel Demin <pavel-demin@…>, 6 years ago

apply .clang-format to all .h, .cc and .cpp files

  • Property mode set to 100644
File size: 6.0 KB
Line 
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/** \class SortableObject
23 *
24 * \author P. Demin - UCL, Louvain-la-Neuve
25 *
26 */
27
28#include "TObject.h"
29#include "TRef.h"
30#include "TRefArray.h"
31
32#include "TMath.h"
33
34//---------------------------------------------------------------------------
35
36class CompBase
37{
38public:
39 virtual ~CompBase() {}
40 virtual Bool_t IsSortable(const TObject *) const { return kTRUE; }
41 virtual Int_t Compare(const TObject *obj1, const TObject *obj2) const = 0;
42};
43
44//---------------------------------------------------------------------------
45
46class SortableObject: public TObject
47{
48public:
49 Bool_t IsSortable() const { return GetCompare() ? GetCompare()->IsSortable(this) : kFALSE; }
50 Int_t Compare(const TObject *obj) const { return GetCompare()->Compare(this, obj); }
51
52 virtual const CompBase *GetCompare() const = 0;
53
54 ClassDef(SortableObject, 1)
55};
56
57//---------------------------------------------------------------------------
58// Standard Comparison Criteria: E, ET, PT, DeltaR
59//---------------------------------------------------------------------------
60
61template <typename T>
62class CompE: public CompBase
63{
64 CompE() {}
65
66public:
67 static CompE *Instance()
68 {
69 static CompE single;
70 return &single;
71 }
72
73 Int_t Compare(const TObject *obj1, const TObject *obj2) const
74 {
75 const T *t1 = static_cast<const T *>(obj1);
76 const T *t2 = static_cast<const T *>(obj2);
77 if(t1->E > t2->E)
78 return -1;
79 else if(t1->E < t2->E)
80 return 1;
81 else
82 return 0;
83 }
84};
85
86//---------------------------------------------------------------------------
87
88template <typename T>
89class CompPT: public CompBase
90{
91 CompPT() {}
92
93public:
94 static CompPT *Instance()
95 {
96 static CompPT single;
97 return &single;
98 }
99
100 Int_t Compare(const TObject *obj1, const TObject *obj2) const
101 {
102 const T *t1 = static_cast<const T *>(obj1);
103 const T *t2 = static_cast<const T *>(obj2);
104 if(t1->PT > t2->PT)
105 return -1;
106 else if(t1->PT < t2->PT)
107 return 1;
108 else
109 return 0;
110 }
111};
112
113//---------------------------------------------------------------------------
114
115template <typename T>
116class CompMomentumPt: public CompBase
117{
118 CompMomentumPt() {}
119
120public:
121 static CompMomentumPt *Instance()
122 {
123 static CompMomentumPt single;
124 return &single;
125 }
126
127 Int_t Compare(const TObject *obj1, const TObject *obj2) const
128 {
129 const T *t1 = static_cast<const T *>(obj1);
130 const T *t2 = static_cast<const T *>(obj2);
131 if(t1->Momentum.Pt() > t2->Momentum.Pt())
132 return -1;
133 else if(t1->Momentum.Pt() < t2->Momentum.Pt())
134 return 1;
135 else
136 return 0;
137 }
138};
139
140//---------------------------------------------------------------------------
141
142template <typename T>
143class CompET: public CompBase
144{
145 CompET() {}
146
147public:
148 static CompET *Instance()
149 {
150 static CompET single;
151 return &single;
152 }
153
154 Int_t Compare(const TObject *obj1, const TObject *obj2) const
155 {
156 const T *t1 = static_cast<const T *>(obj1);
157 const T *t2 = static_cast<const T *>(obj2);
158 if(t1->ET > t2->ET)
159 return -1;
160 else if(t1->ET < t2->ET)
161 return 1;
162 else
163 return 0;
164 }
165};
166
167//---------------------------------------------------------------------------
168
169template <typename T>
170class CompSumPT2: public CompBase
171{
172 CompSumPT2() {}
173
174public:
175 static CompSumPT2 *Instance()
176 {
177 static CompSumPT2 single;
178 return &single;
179 }
180
181 Int_t Compare(const TObject *obj1, const TObject *obj2) const
182 {
183 const T *t1 = static_cast<const T *>(obj1);
184 const T *t2 = static_cast<const T *>(obj2);
185 if(t1->SumPT2 > t2->SumPT2)
186 return -1;
187 else if(t1->SumPT2 < t2->SumPT2)
188 return 1;
189 else
190 return 0;
191 }
192};
193
194//---------------------------------------------------------------------------
195
196template <typename T1, typename T2>
197class CompDeltaR: public CompBase
198{
199 CompDeltaR(const T2 *obj = 0) :
200 fObj(obj) {}
201
202 Double_t DeltaPhi(Double_t phi1, Double_t phi2)
203 {
204 Double_t phi = TMath::Abs(phi1 - phi2);
205 return (phi <= TMath::Pi()) ? phi : (2.0 * TMath::Pi()) - phi;
206 }
207
208 Double_t Sqr(Double_t x) { return x * x; }
209
210 Double_t SumSqr(Double_t a, Double_t b)
211 {
212 Double_t aAbs = TMath::Abs(a);
213 Double_t bAbs = TMath::Abs(b);
214 if(aAbs > bAbs)
215 return aAbs * TMath::Sqrt(1.0 + Sqr(bAbs / aAbs));
216 else
217 return (bAbs == 0) ? 0.0 : bAbs * TMath::Sqrt(1.0 + Sqr(aAbs / bAbs));
218 };
219
220 const T2 *fObj;
221
222public:
223 static CompDeltaR *Instance(const T2 *obj = 0)
224 {
225 static CompDeltaR single(obj);
226 return &single;
227 }
228
229 void SetObject(const T2 *obj) { fObj = obj; }
230
231 Int_t Compare(const TObject *obj1, const TObject *obj2) const
232 {
233 Double_t eta[3], phi[3], deltaR[2];
234 const T1 *t1 = static_cast<const T1 *>(obj1);
235 const T1 *t2 = static_cast<const T1 *>(obj2);
236
237 eta[0] = fObj->Eta;
238 phi[0] = fObj->Phi;
239
240 eta[1] = t1->Eta;
241 phi[1] = t1->Phi;
242
243 eta[2] = t2->Eta;
244 phi[2] = t2->Phi;
245
246 deltaR[0] = SumSqr(TMath::Abs(eta[0] - eta[1]), DeltaPhi(phi[0], phi[1]));
247 deltaR[1] = SumSqr(TMath::Abs(eta[0] - eta[2]), DeltaPhi(phi[0], phi[2]));
248
249 if(deltaR[0] < deltaR[1])
250 return -1;
251 else if(deltaR[0] > deltaR[1])
252 return 1;
253 else
254 return 0;
255 }
256};
257
258#endif // SortableObject_h
Note: See TracBrowser for help on using the repository browser.