]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/global/AliFlatESDEvent.cxx
add special constructors for restoring the vtable ptr
[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 AliFlatESDEvent::AliFlatESDEvent(Bool_t)
77 {
78   //special constructor, used to restore the vtable pointer
79   //uses the special dummy constructors of contained objects
80   AliFlatESDVertex* vertexSPD = const_cast<AliFlatESDVertex*>(GetPrimaryVertexSPD());
81   if (vertexSPD ) { new (vertexSPD) AliFlatESDVertex(1); }
82   AliFlatESDVertex* vertexTracks = const_cast<AliFlatESDVertex*>(GetPrimaryVertexTracks());
83   if (vertexTracks ) { new (vertexTracks) AliFlatESDVertex(1); }
84   AliFlatESDTrack* track = GetTracks();
85   for (Int_t i=0; i<GetNumberOfTracks(); i++)
86   {
87     new (track) AliFlatESDTrack(1);
88     track += track->GetSize();
89   }
90   AliFlatESDV0* v0 = GetV0s();
91   for (Int_t i=0; i<GetNumberOfV0s(); i++)
92   {
93     new (v0) AliFlatESDV0(1);
94     v0 += sizeof(AliFlatESDV0);
95   }
96 }
97
98 /*
99 AliFlatESDEvent::AliFlatESDEvent(const AliFlatESDEvent& ev) :
100   // Default constructor
101   fPrimaryVertexMask(ev.fPrimaryVertexMask),
102   fNTracks(ev.fNTracks),
103   fTracksPointer(ev.fTracksPointer),
104   fNV0s(ev.fNV0s),
105   fV0Pointer(ev.fV0Pointer),
106   fSize(ev.fSize),
107   fContent() 
108 {
109 }
110 */
111 // _______________________________________________________________________________________________________
112 AliFlatESDEvent::AliFlatESDEvent(AliESDEvent *esd) :
113   // Constructor
114   fPrimaryVertexMask(0),
115   fNTracks(0),
116   fTracksPointer(0),
117   fNV0s(0),
118   fV0Pointer(0),
119   fSize(0),
120   fContent() 
121
122   Fill(esd);
123 }
124
125 // _______________________________________________________________________________________________________
126 AliFlatESDEvent::AliFlatESDEvent(AliESDEvent *esd, Bool_t useESDFriends) :
127   // Constructor
128   fPrimaryVertexMask(0),
129   fNTracks(0),
130   fTracksPointer(0),
131   fNV0s(0),
132   fV0Pointer(0),
133   fSize(0),
134   fContent() 
135
136   Fill(esd, useESDFriends);
137 }
138
139 // _______________________________________________________________________________________________________
140 AliFlatESDEvent::~AliFlatESDEvent() 
141 {
142   // Destructor
143 }
144
145 // _______________________________________________________________________________________________________
146   ULong64_t AliFlatESDEvent::EstimateSize(AliESDEvent *esd, Bool_t useESDFriends, Bool_t fillV0s) 
147 {
148   // Estimate upper limit of the object size
149   // -> Added objects have to be added here as well
150   
151   ULong64_t size = sizeof(AliFlatESDEvent);
152   size += 2 * sizeof( AliFlatESDVertex );
153   size += esd->GetNumberOfTracks() * AliFlatESDTrack::EstimateSize(useESDFriends);
154   if( fillV0s ) size += esd->GetNumberOfV0s()*sizeof(AliFlatESDV0);
155   return size;
156 }
157
158 void AliFlatESDEvent::FillPrimaryVertices( const AliESDVertex *vertexSPD,
159                                             const AliESDVertex *vertexTracks )
160 {
161   // fill primary vertices
162   
163   fPrimaryVertexMask = 0;
164   fSize = 0;
165
166   Byte_t flag = 0x1;
167   FillPrimaryVertex(vertexSPD, flag);
168
169   flag = 0x2;
170   FillPrimaryVertex(vertexTracks, flag);
171
172   fTracksPointer = fSize;
173   fV0Pointer = fSize;
174 }
175
176 void AliFlatESDEvent::FillPrimaryVertex(const AliESDVertex *v, Byte_t flag) 
177 {
178   
179   // Fill primary vertex parameters
180
181   if (!v) return;
182
183   AliFlatESDVertex *vtx = reinterpret_cast<AliFlatESDVertex*> (fContent + fSize);
184   new(vtx) AliFlatESDVertex;
185   vtx->Set( *v );    
186   fPrimaryVertexMask |= flag;
187   fSize += sizeof(AliFlatESDVertex);
188 }
189
190
191 Int_t AliFlatESDEvent::FillNextTrack( const AliESDtrack* esdTrack, AliESDfriendTrack* friendTrack)
192 {
193   // fill next track
194
195   AliFlatESDTrack * flatTrack = reinterpret_cast<AliFlatESDTrack*>(fContent+fSize);
196   new(flatTrack) AliFlatESDTrack;
197   flatTrack->Fill(esdTrack, friendTrack);
198   fSize += flatTrack->GetSize();
199   ++fNTracks;
200   return 0;
201 }
202
203 void AliFlatESDEvent::Reset()
204 {
205   fSize = 0;
206   fNTracks=0;
207   fNV0s = 0;  
208   fPrimaryVertexMask = 0;
209   fTracksPointer = 0;  
210   fV0Pointer = 0; 
211 }
212
213 // _______________________________________________________________________________________________________
214 Int_t AliFlatESDEvent::Fill(const AliESDEvent *esd, const Bool_t useESDFriends, const Bool_t fillV0s )
215 {
216   // Fill flat ESD event from normal ALiESDEvent
217   // - Fill tracks + friends (if requested)
218   // -> Added objects have to be added here as well
219
220   Reset();
221   
222   FillPrimaryVertices( esd->GetPrimaryVertexSPD(), esd->GetPrimaryVertexTracks() );
223
224   // -- Get ESD friends
225   // -------------------------------------------------------
226   Bool_t connectESDFriends = useESDFriends;
227   AliESDfriend* esdFriend  = NULL;
228
229    if (connectESDFriends) {
230     esdFriend = dynamic_cast<AliESDfriend*>(esd->FindListObject("AliESDfriend"));  
231     if (!esdFriend) {
232       connectESDFriends = kFALSE;
233       Printf("WARNING: friends not available, cluster information will not be included");
234     }
235     else 
236       Printf("INFO: friends are available, cluster information will be included");
237   }
238
239   // -- Track loop, fill AliFlatESDTrack sub structure
240   // -------------------------------------------------------
241   for (Int_t idxTrack = 0; idxTrack < esd->GetNumberOfTracks(); ++idxTrack) {
242     AliESDtrack       *esdTrack    = esd->GetTrack(idxTrack);
243     AliESDfriendTrack *friendTrack = NULL;
244
245     if (esdTrack) {
246       if (connectESDFriends){
247         friendTrack = esdFriend->GetTrack(idxTrack);
248       }
249       FillNextTrack( esdTrack, friendTrack);
250     }
251   }
252
253   // Fill V0s
254   
255   fV0Pointer = fSize;
256   fNV0s = 0;
257   if( fillV0s ){
258     for( int i=0; i < esd->GetNumberOfV0s(); i++){
259       AliESDv0 *esdV0 = esd->GetV0( i );
260       AliFlatESDV0 *v0 = GetNextV0Pointer();
261       if( !v0 ) continue;
262       v0->fNegTrackID = esdV0->GetNindex();
263       v0->fPosTrackID = esdV0->GetNindex();
264       StoreLastV0();      
265     }
266   }
267
268   return 0;
269 }
270
271 UInt_t AliFlatESDEvent::CountBits(Byte_t field, UInt_t mask) const {
272   // Count bits in field
273   UInt_t count = 0; 
274   UInt_t reg = 0x0; 
275   
276   reg |= field;   
277   reg &= mask;
278   
279   for (count = 0; reg; count++)
280     reg &= reg - 1; 
281
282   return count;
283 }