]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/HWCFemulator/AliHLTTPCHWClusterMerger.h
bug fix for outer sectors, extra interface
[u/mrichter/AliRoot.git] / HLT / TPCLib / HWCFemulator / AliHLTTPCHWClusterMerger.h
1 //-*- Mode: C++ -*-
2 // $Id$
3
4 #ifndef ALIHLTTPCHWCLUSTERMERGER_H
5 #define ALIHLTTPCHWCLUSTERMERGER_H
6 //* This file is property of and copyright by the ALICE HLT Project        * 
7 //* ALICE Experiment at CERN, All rights reserved.                         *
8 //* See cxx source for full Copyright notice                               *
9
10 //  @file   AliHLTTPCHWClusterMerger.h
11 //  @author Matthias Richter, Sergey Gorbunov
12 //  @date   2011-11-25
13 //  @brief  Merger class for HLT TPC Hardware clusters
14 //          Handles merging of branch border clusters
15
16 #include "AliHLTTPCRawCluster.h"
17 #include "AliHLTTPCClusterMCData.h"
18 #include "AliHLTLogging.h"
19 #include <vector>
20 #include "TObject.h"
21
22 /**
23  * @class AliHLTTPCHWClusterMerger
24  *
25  * @ingroup alihlt_base
26  */
27 class AliHLTTPCHWClusterMerger : public AliHLTLogging
28 {
29  public:
30   /// standard constructor
31   AliHLTTPCHWClusterMerger();
32   /// destructor
33   ~AliHLTTPCHWClusterMerger();
34
35   void Init();
36
37   //////////////////////////////////////////////////////////////////////////////////////////////////////////
38   //////////////////////////////////////////////////////////////////////////////////////////////////////////
39
40   /// check if a cluster is a candidate for merging
41   template<typename T> 
42   bool CheckCandidate(int slice, int partition, const T& c) {
43     return CheckCandidate(slice, partition, c.GetPadRow(), c.GetPad(), c.GetTime(), c.GetSigmaY2() );
44   }
45
46   /// check if a cluster is a candidate for merging
47   bool CheckCandidate(int slice,
48                       int partition,
49                       int partitionrow, // local row in partition
50                       float pad,
51                       float time,
52                       float sigmaPad2) ;
53
54   /// cache cluster for later merging
55   template<typename T> 
56   int AddCandidate(int slice,
57                    int partition,
58                    AliHLTUInt32_t id,
59                    const T& c) {
60     return AddCandidate(slice,
61                         partition,
62                         c.GetPadRow(),
63                         c.GetPad(),
64                         c.GetTime(),
65                         c.GetSigmaY2(),
66                         c.GetSigmaZ2(),
67                         c.GetCharge(),
68                         c.GetQMax(),
69                         id
70                         );
71   }
72
73   // cache cluster for later merging
74   template<typename T> 
75   int AddCandidate(int slice,
76                    int partition,
77                    AliHLTUInt32_t id,
78                    const T& c,
79                    const AliHLTTPCClusterMCLabel &mc) {
80     return AddCandidate(slice,
81                         partition,
82                         c.GetPadRow(),
83                         c.GetPad(),
84                         c.GetTime(),
85                         c.GetSigmaY2(),
86                         c.GetSigmaZ2(),
87                         c.GetCharge(),
88                         c.GetQMax(),
89                         id,
90                         mc
91                         );
92   }
93
94   /// cache cluster for later merging
95   int AddCandidate(int slice,
96                    int partition,
97                    short partitionrow, // local row in the partition
98                    float pad,
99                    float time,
100                    float sigmaY2,
101                    float sigmaZ2,
102                    unsigned short charge,
103                    unsigned short qmax,
104                    AliHLTUInt32_t id=~AliHLTUInt32_t(0),
105                    const AliHLTTPCClusterMCLabel &mc=AliHLTTPCClusterMCLabel()
106                    );
107
108   /// merge clusters
109   int Merge();
110
111   /// cleanup
112   void Clear();
113
114   //////////////////////////////////////////////////////////////////////////////////////////////////////////
115   //////////////////////////////////////////////////////////////////////////////////////////////////////////
116
117   /// helper class to store relevant data for a cluster at border
118   struct AliBorderRecord {
119     AliHLTInt64_t fClusterRecordID;
120     AliHLTUInt32_t fTimeBin;    
121   };
122  
123   
124   /// helper class to store relevant data for a cluster candidate
125   class AliClusterRecord {
126   public:
127     AliClusterRecord()
128       : fSlice(-1), fPartition(-1), fBorder(-1), fMergedFlag(-1), fId(~AliHLTUInt32_t(0)), fCluster(), fMC() {}
129     AliClusterRecord(int slice, int partition, int border,int merged, AliHLTUInt32_t id, const AliHLTTPCRawCluster &cluster)
130       : fSlice(slice), fPartition(partition), fBorder(border), fMergedFlag(merged), fId(id), fCluster(cluster), fMC() {}
131     AliClusterRecord(int slice, int partition, int border,int merged, AliHLTUInt32_t id, const AliHLTTPCRawCluster &cluster, const AliHLTTPCClusterMCLabel &mc)
132       : fSlice(slice), fPartition(partition), fBorder(border), fMergedFlag(merged), fId(id), fCluster(cluster), fMC(mc) {}
133
134     AliClusterRecord(const AliClusterRecord& other)
135       : fSlice(other.fSlice), fPartition(other.fPartition), fBorder(other.fBorder), fMergedFlag(other.fMergedFlag), fId(other.fId), fCluster(other.fCluster), fMC(other.fMC) {}
136     AliClusterRecord& operator=(const AliClusterRecord& other) {
137       if (this==&other) return *this;
138       this->~AliClusterRecord();
139       new (this) AliClusterRecord(other);
140       return *this;
141     }
142
143     ~AliClusterRecord() {}    
144     
145     AliClusterRecord& operator=(const AliHLTTPCRawCluster& c) {
146       fCluster=c;
147       return *this;
148     }
149
150     int IsMergedTo() const { return fMergedFlag; }
151     int GetSlice() const {return fSlice;}
152     int GetBorder() const {return fBorder;}
153     int GetPartition() const {return fPartition;}
154     AliHLTUInt32_t GetId() const {return fId;}
155     operator AliHLTTPCRawCluster() const {return fCluster;}
156     const AliHLTTPCRawCluster& GetCluster() const {return fCluster;}
157     const AliHLTTPCClusterMCLabel& GetMCLabel() const {return fMC;}
158     void SetMergedTo( int ind ){ fMergedFlag = ind;}
159     AliHLTTPCRawCluster &Cluster(){ return fCluster; }
160     AliHLTTPCClusterMCLabel& MCLabel(){ return fMC; }
161   private:
162     int fSlice; //!
163     int fPartition; //!
164     int fBorder; //!
165     int fMergedFlag; //!
166     AliHLTUInt32_t fId; //!
167     AliHLTTPCRawCluster fCluster; //!
168     AliHLTTPCClusterMCLabel fMC; //!
169   };
170
171   //////////////////////////////////////////////////////////////////////////////////////////////////////////
172   //////////////////////////////////////////////////////////////////////////////////////////////////////////
173
174   /// iterator class to access merged and remaining clusters
175   class iterator {
176   public:
177     iterator() : fArray(NULL), fIter() {}
178     iterator(vector<AliClusterRecord>* pArray) : fArray(pArray), fIter() {if (fArray) fIter=fArray->begin();}
179     iterator(const iterator& other) : fArray(other.fArray), fIter(other.fIter) {}
180     iterator& operator=(const iterator& other) {
181       if (this==&other) return *this;
182       fArray=other.fArray; fIter=other.fIter; return *this;
183     }
184     ~iterator() {}
185
186     AliClusterRecord operator*() {return *fIter;}
187
188     // prefix operators
189     iterator& operator++() {
190       if (!fArray || fIter==fArray->end()) return *this;
191       while ((++fIter)!=fArray->end()) {
192         if ( fIter->IsMergedTo()<0 ) break;     
193       }      
194       return *this;
195     }
196     iterator& operator--() {
197       if (!fArray) return *this;
198       while (fIter!=fArray->begin()) {
199         --fIter;
200         if ( fIter->IsMergedTo()<0 ) break;     
201       }
202       return *this;
203     }
204
205     // postfix operators
206     iterator operator++(int) {iterator i(*this); this->operator++(); return i;}
207     iterator operator--(int) {iterator i(*this); this->operator--(); return i;}
208
209     iterator& operator+=(int step) {
210       if (!fArray) return *this;
211       while ((fIter)!=fArray->end() && step-->0) {++fIter;}
212       return *this;
213     }
214
215     bool operator==(const iterator& other) {
216       return (other.fArray!=NULL && fArray!=NULL && other.fIter==fIter);
217     }
218
219     bool operator!=(const iterator& other) {
220       return (other.fArray!=NULL && fArray!=NULL && other.fIter!=fIter);
221     }
222
223   protected:
224   private:
225     vector<AliClusterRecord>* fArray; //!
226     vector<AliClusterRecord>::iterator fIter; //!
227   };
228
229   //////////////////////////////////////////////////////////////////////////////////////////////////////////
230   //////////////////////////////////////////////////////////////////////////////////////////////////////////
231
232   /// iterator function, start iteration
233   iterator& begin() {
234     fIter.~iterator();
235     new (&fIter) iterator(&fClusters);
236     fEnd=fIter;  fEnd+=fClusters.size();    
237     // skip empty (merged) clusters
238     while (fIter!=fEnd && ( (*fIter).IsMergedTo()>=0) ) {
239       fIter++;
240     }
241     return fIter;
242   }
243
244   /// iterator function, end marker
245   iterator& end() {
246     return fEnd;
247   }
248
249   const vector<AliHLTTPCHWClusterMerger::AliClusterRecord> &GetRecords(){ return fClusters; }
250   static const int GetNSlices(){ return fkNSlices; }
251   int GetNBorders() const { return fNBorders; }
252   int GetBorderNClusters( int ib ) const { return fBorderNClusters[ib]; }
253   int GetBorderFirstCluster( int ib ) const { return fBorderFirstCluster[ib]; }
254   const AliHLTTPCHWClusterMerger::AliBorderRecord *GetBorderClusters() const { return fBorderClusters;}
255
256   //////////////////////////////////////////////////////////////////////////////////////////////////////////
257   //////////////////////////////////////////////////////////////////////////////////////////////////////////
258
259  protected:
260
261  private:
262   /// copy constructor
263   AliHLTTPCHWClusterMerger(const AliHLTTPCHWClusterMerger&);
264   /// assignment operator
265   AliHLTTPCHWClusterMerger& operator=(const AliHLTTPCHWClusterMerger&);
266
267   int FillIndex();
268   static bool CompareTime( const AliBorderRecord &b1, const AliBorderRecord &b2){
269     return b1.fTimeBin > b2.fTimeBin;
270   }
271  
272   static bool CompareMCWeights(const AliHLTTPCClusterMCWeight &a, const AliHLTTPCClusterMCWeight &b){
273     return a.fWeight > b.fWeight;
274   }
275   static bool CompareMCLabels(const AliHLTTPCClusterMCWeight &a, const AliHLTTPCClusterMCWeight &b){
276     return a.fMCID < b.fMCID;
277   }
278
279   AliHLTInt16_t *fMapping;//!
280   int fNRows;//!
281   int fNRowPads;//!
282   int fNBorders;//!
283   AliHLTFloat32_t *fBorders; //!
284   int *fBorderNClusters; //!
285   int *fBorderFirstCluster; //!
286   AliBorderRecord *fBorderClusters;
287   int fBorderNClustersTotal; //!
288
289   vector<AliClusterRecord> fClusters; //! array of candidates
290   vector<AliHLTUInt32_t> fRemovedClusterIds; //! array of removed clusters by id
291   iterator fIter; //!
292   iterator fEnd; //!
293   static const int fkMergeWidth = 3;
294   static const int fkNSlices = 36;
295   static const int fkMergeTimeWindow = 3;
296   ClassDef(AliHLTTPCHWClusterMerger, 0)
297 };
298
299 #endif //ALIHLTTPCHWCLUSTERMERGER_H