]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/global/AliFlatESDEvent.cxx
92708988685fd6305f64233eba79cdd7c62bbcfa
[u/mrichter/AliRoot.git] / HLT / global / AliFlatESDEvent.cxx
1 /* $Id$ */
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Author: The ALICE Off-line Project.                                    *
7  * Contributors are mentioned in the code where appropriate.              *
8  *                                                                        *
9  * Permission to use, copy, modify and distribute this software and its   *
10  * documentation strictly for non-commercial purposes is hereby granted   *
11  * without fee, provided that the above copyright notice appears in all   *
12  * copies and that both the copyright notice and this permission notice   *
13  * appear in the supporting documentation. The authors make no claims     *
14  * about the suitability of this software for any purpose. It is          *
15  * provided "as is" without express or implied warranty.                  *
16  **************************************************************************/
17
18 /**
19  * >> Flat structure representing an ESD <<
20  *
21  * To be used in the online and offline calibration schema.
22  *
23  * Class provides interface methods for 
24  *   - Filling from AliESDEvent, but also from HLT (to be added)
25  *   - Getter methods
26  *
27  * In the online case, the structure can be directly written into a shared 
28  * memory, in the offline case, the size has to be estimated first.
29  *
30  * 
31  * Primary Authors : Sergey Gorbunov, Jochen Thaeder, Chiara Zampolli
32  *
33  * ************************************************************************
34  * Offline usage:
35  *
36  *  ...
37  *  AliESDEvent* esd = ....;
38  *  Bool_t useESDFriends = kTRUE;
39  *
40  *  // -- Book memory for AliFlatESDEvent
41  *  Byte_t *mem = new Byte_t[AliFlatESDEvent::EstimateSize(esd, useESDFriends)];
42  *  AliFlatESDEvent *flatEsd = reinterpret_cast<AliFlatESDEvent*>(mem);
43  *
44  *  // -- Fill AliFlatESDEvent
45  *  flatEsd->Fill(esd, useESDFriends);  
46  *  ...
47  *
48  **************************************************************************/
49
50 #include "AliESDEvent.h"
51 #include "AliESDtrack.h"
52 #include "AliESDfriend.h"
53 #include "AliESDv0.h"
54
55 #include "AliFlatESDEvent.h"
56 #include "AliFlatESDTrack.h"
57 #include "AliFlatTPCCluster.h"
58 #include "AliFlatExternalTrackParam.h"
59 #include "Riostream.h"
60 #include "AliFlatESDVertex.h"
61
62 // _______________________________________________________________________________________________________
63 AliFlatESDEvent::AliFlatESDEvent() :
64   // Default constructor
65   fPrimaryVertexMask(0),
66   fNTracks(0),
67   fTracksPointer(0),
68   fNV0s(0),
69   fV0Pointer(0),
70   fSize(0),
71   fContent() 
72 {
73 }
74
75 // _______________________________________________________________________________________________________
76 void AliFlatESDEvent::Reinitialize()
77 {
78 //
79 // Initializing function
80 //
81 // to be called after event is received via reinterpret_cast from memory
82   
83   
84         new (this) AliFlatESDEvent(AliFlatESDReinitialize);
85                 AliFlatESDVertex* vertexSPD = const_cast<AliFlatESDVertex*>(GetPrimaryVertexSPD());
86                 if (vertexSPD ) {vertexSPD->Reinitialize(); }
87                 AliFlatESDVertex* vertexTracks = const_cast<AliFlatESDVertex*>(GetPrimaryVertexTracks());
88                 if (vertexTracks ) { vertexTracks->Reinitialize(); }
89                 AliFlatESDTrack* track = GetTracks();
90                 
91                 for (Int_t i=0; i<GetNumberOfTracks(); i++)
92                 {
93                         track->Reinitialize();
94                         track= track->GetNextTrack();
95                 }
96         AliFlatESDV0* v0 = GetV0stmp();
97         for (Int_t i=0; i<GetNumberOfV0s(); i++)
98         {
99                         v0->Reinitialize();
100                         v0++;
101                 }
102         }
103
104 /*
105 AliFlatESDEvent::AliFlatESDEvent(const AliFlatESDEvent& ev) :
106   // Default constructor
107   fPrimaryVertexMask(ev.fPrimaryVertexMask),
108   fNTracks(ev.fNTracks),
109   fTracksPointer(ev.fTracksPointer),
110   fNV0s(ev.fNV0s),
111   fV0Pointer(ev.fV0Pointer),
112   fSize(ev.fSize),
113   fContent() 
114 {
115 }
116 */
117 // _______________________________________________________________________________________________________
118 AliFlatESDEvent::AliFlatESDEvent(AliESDEvent *esd) :
119   // Constructor
120   fPrimaryVertexMask(0),
121   fNTracks(0),
122   fTracksPointer(0),
123   fNV0s(0),
124   fV0Pointer(0),
125   fSize(0),
126   fContent() 
127
128   Fill(esd);
129 }
130
131 // _______________________________________________________________________________________________________
132 AliFlatESDEvent::AliFlatESDEvent(AliESDEvent *esd, Bool_t useESDFriends) :
133   // Constructor
134   fPrimaryVertexMask(0),
135   fNTracks(0),
136   fTracksPointer(0),
137   fNV0s(0),
138   fV0Pointer(0),
139   fSize(0),
140   fContent() 
141
142   Fill(esd, useESDFriends);
143 }
144
145 // _______________________________________________________________________________________________________
146 AliFlatESDEvent::~AliFlatESDEvent() 
147 {
148   // Destructor
149 }
150
151 // _______________________________________________________________________________________________________
152   ULong64_t AliFlatESDEvent::EstimateSize(AliESDEvent *esd, Bool_t useESDFriends, Bool_t fillV0s) 
153 {
154   // Estimate upper limit of the object size
155   // -> Added objects have to be added here as well
156   
157   ULong64_t size = sizeof(AliFlatESDEvent);
158   size += 2 * sizeof( AliFlatESDVertex );
159   size += esd->GetNumberOfTracks() * AliFlatESDTrack::EstimateSize(useESDFriends);
160   if( fillV0s ) size += esd->GetNumberOfV0s()*sizeof(AliFlatESDV0);
161   return size;
162 }
163
164 void AliFlatESDEvent::FillPrimaryVertices( const AliESDVertex *vertexSPD,
165                                             const AliESDVertex *vertexTracks )
166 {
167   // fill primary vertices
168   
169   fPrimaryVertexMask = 0;
170   fSize = 0;
171
172   Byte_t flag = 0x1;
173   FillPrimaryVertex(vertexSPD, flag);
174
175   flag = 0x2;
176   FillPrimaryVertex(vertexTracks, flag);
177
178   fTracksPointer = fSize;
179   fV0Pointer = fSize;
180 }
181
182 void AliFlatESDEvent::FillPrimaryVertex(const AliESDVertex *v, Byte_t flag) 
183 {
184   
185   // Fill primary vertex parameters
186
187   if (!v) return;
188
189   AliFlatESDVertex *vtx = reinterpret_cast<AliFlatESDVertex*> (fContent + fSize);
190   new(vtx) AliFlatESDVertex;
191   vtx->Set( *v );    
192   fPrimaryVertexMask |= flag;
193   fSize += sizeof(AliFlatESDVertex);
194 }
195
196
197 Int_t AliFlatESDEvent::FillNextTrack( const AliESDtrack* esdTrack, AliESDfriendTrack* friendTrack)
198 {
199   // fill next track
200
201   AliFlatESDTrack * flatTrack = reinterpret_cast<AliFlatESDTrack*>(fContent+fSize);
202   new(flatTrack) AliFlatESDTrack;
203   flatTrack->Fill(esdTrack, friendTrack);
204   fSize += flatTrack->GetSize();
205   ++fNTracks;
206   return 0;
207 }
208
209 void AliFlatESDEvent::Reset()
210 {
211   fSize = 0;
212   fNTracks=0;
213   fNV0s = 0;  
214   fPrimaryVertexMask = 0;
215   fTracksPointer = 0;  
216   fV0Pointer = 0; 
217 }
218
219 // _______________________________________________________________________________________________________
220 Int_t AliFlatESDEvent::Fill(const AliESDEvent *esd, const Bool_t useESDFriends, const Bool_t fillV0s )
221 {
222   // Fill flat ESD event from normal ALiESDEvent
223   // - Fill tracks + friends (if requested)
224   // -> Added objects have to be added here as well
225
226   Reset();
227   
228   FillPrimaryVertices( esd->GetPrimaryVertexSPD(), esd->GetPrimaryVertexTracks() );
229
230   // -- Get ESD friends
231   // -------------------------------------------------------
232   Bool_t connectESDFriends = useESDFriends;
233   AliESDfriend* esdFriend  = NULL;
234
235    if (connectESDFriends) {
236     esdFriend = dynamic_cast<AliESDfriend*>(esd->FindListObject("AliESDfriend"));  
237     if (!esdFriend) {
238       connectESDFriends = kFALSE;
239       Printf("WARNING: friends not available, cluster information will not be included");
240     }
241     else 
242       Printf("INFO: friends are available, cluster information will be included");
243   }
244
245   // -- Track loop, fill AliFlatESDTrack sub structure
246   // -------------------------------------------------------
247   for (Int_t idxTrack = 0; idxTrack < esd->GetNumberOfTracks(); ++idxTrack) {
248     AliESDtrack       *esdTrack    = esd->GetTrack(idxTrack);
249     AliESDfriendTrack *friendTrack = NULL;
250
251     if (esdTrack) {
252       if (connectESDFriends){
253         friendTrack = esdFriend->GetTrack(idxTrack);
254       }
255       FillNextTrack( esdTrack, friendTrack);
256     }
257   }
258
259   // Fill V0s
260   
261   fV0Pointer = fSize;
262   fNV0s = 0;
263   if( fillV0s ){
264     for( int i=0; i < esd->GetNumberOfV0s(); i++){
265       AliESDv0 *esdV0 = esd->GetV0( i );
266       AliFlatESDV0 *v0 = GetNextV0Pointer();
267       if( !v0 ) continue;
268       v0->fNegTrackID = esdV0->GetNindex();
269       v0->fPosTrackID = esdV0->GetNindex();
270       StoreLastV0();      
271     }
272   }
273
274   return 0;
275 }
276
277 UInt_t AliFlatESDEvent::CountBits(Byte_t field, UInt_t mask) const {
278   // Count bits in field
279   UInt_t count = 0; 
280   UInt_t reg = 0x0; 
281   
282   reg |= field;   
283   reg &= mask;
284   
285   for (count = 0; reg; count++)
286     reg &= reg - 1; 
287
288   return count;
289 }