Fork me on GitHub

source: svn/trunk/classes/SortableObject.h@ 1377

Last change on this file since 1377 was 895, checked in by Pavel Demin, 12 years ago

sort all objects in TreeWriter

File size: 4.7 KB
Line 
1#ifndef SortableObject_h
2#define SortableObject_h
3
4/** \class SortableObject
5 *
6 *
7 * $Date: 2008-06-04 13:57:26 $
8 * $Revision: 1.1 $
9 *
10 *
11 * \author P. Demin - UCL, Louvain-la-Neuve
12 *
13 */
14
15#include "TRef.h"
16#include "TObject.h"
17#include "TRefArray.h"
18
19#include "TMath.h"
20
21//---------------------------------------------------------------------------
22
23class CompBase
24{
25public:
26 virtual ~CompBase() { }
27 virtual Bool_t IsSortable(const TObject *obj) const { return kTRUE; }
28 virtual Int_t Compare(const TObject *obj1, const TObject *obj2) const = 0;
29};
30
31//---------------------------------------------------------------------------
32
33class SortableObject: public TObject
34{
35public:
36
37 Bool_t IsSortable() const { return GetCompare() ? GetCompare()->IsSortable(this) : kFALSE; }
38 Int_t Compare(const TObject *obj) const { return GetCompare()->Compare(this, obj); }
39
40 virtual const CompBase *GetCompare() const = 0;
41
42 ClassDef(SortableObject, 1)
43};
44
45//---------------------------------------------------------------------------
46// Standard Comparison Criteria: E, ET, PT, DeltaR
47//---------------------------------------------------------------------------
48
49template <typename T>
50class CompE: public CompBase
51{
52 CompE() {}
53public:
54 static CompE *Instance()
55 {
56 static CompE single;
57 return &single;
58 }
59
60 Int_t Compare(const TObject *obj1, const TObject *obj2) const
61 {
62 const T *t1 = static_cast<const T*>(obj1);
63 const T *t2 = static_cast<const T*>(obj2);
64 if(t1->E > t2->E)
65 return -1;
66 else if(t1->E < t2->E)
67 return 1;
68 else
69 return 0;
70 }
71};
72
73//---------------------------------------------------------------------------
74
75template <typename T>
76class CompPT: public CompBase
77{
78 CompPT() {}
79public:
80 static CompPT *Instance()
81 {
82 static CompPT single;
83 return &single;
84 }
85
86 Int_t Compare(const TObject *obj1, const TObject *obj2) const
87 {
88 const T *t1 = static_cast<const T*>(obj1);
89 const T *t2 = static_cast<const T*>(obj2);
90 if(t1->PT > t2->PT)
91 return -1;
92 else if(t1->PT < t2->PT)
93 return 1;
94 else
95 return 0;
96 }
97};
98
99//---------------------------------------------------------------------------
100
101template <typename T>
102class CompMomentumPt: public CompBase
103{
104 CompMomentumPt() {}
105public:
106 static CompMomentumPt *Instance()
107 {
108 static CompMomentumPt single;
109 return &single;
110 }
111
112 Int_t Compare(const TObject *obj1, const TObject *obj2) const
113 {
114 const T *t1 = static_cast<const T*>(obj1);
115 const T *t2 = static_cast<const T*>(obj2);
116 if(t1->Momentum.Pt() > t2->Momentum.Pt())
117 return -1;
118 else if(t1->Momentum.Pt() < t2->Momentum.Pt())
119 return 1;
120 else
121 return 0;
122 }
123};
124
125//---------------------------------------------------------------------------
126
127template <typename T>
128class CompET: public CompBase
129{
130 CompET() {}
131public:
132 static CompET *Instance()
133 {
134 static CompET single;
135 return &single;
136 }
137
138 Int_t Compare(const TObject *obj1, const TObject *obj2) const
139 {
140 const T *t1 = static_cast<const T*>(obj1);
141 const T *t2 = static_cast<const T*>(obj2);
142 if(t1->ET > t2->ET)
143 return -1;
144 else if(t1->ET < t2->ET)
145 return 1;
146 else
147 return 0;
148 }
149};
150
151//---------------------------------------------------------------------------
152
153template <typename T1, typename T2>
154class CompDeltaR: public CompBase
155{
156 CompDeltaR(const T2 *obj = 0) : fObj(obj) {}
157
158 Double_t DeltaPhi(Double_t phi1, Double_t phi2)
159 {
160 Double_t phi = TMath::Abs(phi1 - phi2);
161 return (phi <= TMath::Pi()) ? phi : (2.0*TMath::Pi()) - phi;
162 }
163
164 Double_t Sqr(Double_t x) { return x*x; }
165
166 Double_t SumSqr(Double_t a, Double_t b)
167 {
168 Double_t aAbs = TMath::Abs(a);
169 Double_t bAbs = TMath::Abs(b);
170 if(aAbs > bAbs) return aAbs * TMath::Sqrt(1.0 + Sqr(bAbs / aAbs));
171 else return (bAbs == 0) ? 0.0 : bAbs * TMath::Sqrt(1.0 + Sqr(aAbs / bAbs));
172 };
173
174 const T2 *fObj;
175
176public:
177 static CompDeltaR *Instance(const T2 *obj = 0)
178 {
179 static CompDeltaR single(obj);
180 return &single;
181 }
182
183 void SetObject(const T2 *obj) { fObj = obj; }
184
185 Int_t Compare(const TObject *obj1, const TObject *obj2) const
186 {
187 Double_t eta[3], phi[3], deltaR[2];
188 const T1 *t1 = static_cast<const T1*>(obj1);
189 const T1 *t2 = static_cast<const T1*>(obj2);
190
191 eta[0] = fObj->Eta;
192 phi[0] = fObj->Phi;
193
194 eta[1] = t1->Eta;
195 phi[1] = t1->Phi;
196
197 eta[2] = t2->Eta;
198 phi[2] = t2->Phi;
199
200 deltaR[0] = SumSqr(TMath::Abs(eta[0] - eta[1]), DeltaPhi(phi[0], phi[1]));
201 deltaR[1] = SumSqr(TMath::Abs(eta[0] - eta[2]), DeltaPhi(phi[0], phi[2]));
202
203 if(deltaR[0] < deltaR[1])
204 return -1;
205 else if(deltaR[0] > deltaR[1])
206 return 1;
207 else
208 return 0;
209 }
210};
211
212#endif // SortableObject_h
213
214
Note: See TracBrowser for help on using the repository browser.