]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/global/AliHLTGlobalEsdConverterComponent.cxx
785b6b7a52602c2afc18863f703ca5c44388fe11
[u/mrichter/AliRoot.git] / HLT / global / AliHLTGlobalEsdConverterComponent.cxx
1 // $Id$
2
3 //**************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project        * 
5 //* ALICE Experiment at CERN, All rights reserved.                         *
6 //*                                                                        *
7 //* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 /** @file   AliHLTGlobalEsdConverterComponent.cxx
20     @author Matthias Richter
21     @date   
22     @brief  Global ESD converter component.
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #include <cassert>
32 #include "AliHLTGlobalEsdConverterComponent.h"
33 #include "AliHLTGlobalBarrelTrack.h"
34 #include "AliHLTExternalTrackParam.h"
35 #include "AliHLTTrackMCLabel.h"
36 #include "AliHLTCTPData.h"
37 #include "AliESDEvent.h"
38 #include "AliESDtrack.h"
39 #include "AliCDBEntry.h"
40 #include "AliCDBManager.h"
41 #include "AliPID.h"
42 #include "TTree.h"
43 #include "TList.h"
44 #include "AliHLTESDCaloClusterMaker.h"
45 #include "AliHLTCaloClusterDataStruct.h"
46 #include "AliHLTCaloClusterReader.h"
47 #include "AliESDCaloCluster.h"
48 #include "AliHLTVertexer.h"
49 #include "AliTPCseed.h"
50
51 /** ROOT macro for the implementation of ROOT specific class methods */
52 ClassImp(AliHLTGlobalEsdConverterComponent)
53
54 AliHLTGlobalEsdConverterComponent::AliHLTGlobalEsdConverterComponent()
55   : AliHLTProcessor()
56   , fWriteTree(0)
57   , fVerbosity(0)
58   , fESD(NULL)
59   , fSolenoidBz(-5.00668)
60   , fFillVtxConstrainedTracks( 0 )
61 {
62   // see header file for class documentation
63   // or
64   // refer to README to build package
65   // or
66   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
67 }
68
69 AliHLTGlobalEsdConverterComponent::~AliHLTGlobalEsdConverterComponent()
70 {
71   // see header file for class documentation
72   if (fESD) delete fESD;
73   fESD=NULL;
74 }
75
76 int AliHLTGlobalEsdConverterComponent::Configure(const char* arguments)
77 {
78   // see header file for class documentation
79   int iResult=0;
80   if (!arguments) return iResult;
81
82   TString allArgs=arguments;
83   TString argument;
84   int bMissingParam=0;
85
86   TObjArray* pTokens=allArgs.Tokenize(" ");
87   if (pTokens) {
88     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
89       argument=((TObjString*)pTokens->At(i))->GetString();
90       if (argument.IsNull()) continue;
91       
92       if (argument.CompareTo("-solenoidBz")==0) {
93         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
94         HLTInfo("Magnetic Field set to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
95         fSolenoidBz=((TObjString*)pTokens->At(i))->GetString().Atof();
96         continue;
97       }
98       else if (argument.CompareTo("-fitTracks2Vertex")==0) {
99         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
100         HLTInfo("Filling of vertex constrained tracks is set set to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
101         fFillVtxConstrainedTracks=((TObjString*)pTokens->At(i))->GetString().Atoi();
102         continue;
103       } else {
104         HLTError("unknown argument %s", argument.Data());
105         iResult=-EINVAL;
106         break;
107       }
108     }
109     delete pTokens;
110   }
111   if (bMissingParam) {
112     HLTError("missing parameter for argument %s", argument.Data());
113     iResult=-EINVAL;
114   }
115
116   return iResult;
117 }
118
119 int AliHLTGlobalEsdConverterComponent::Reconfigure(const char* cdbEntry, const char* chainId)
120 {
121   // see header file for class documentation
122   int iResult=0;
123   const char* path=kAliHLTCDBSolenoidBz;
124   const char* defaultNotify="";
125   if (cdbEntry) {
126     path=cdbEntry;
127     defaultNotify=" (default)";
128   }
129   if (path) {
130     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
131     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
132     if (pEntry) {
133       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
134       if (pString) {
135         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
136         iResult=Configure(pString->GetString().Data());
137       } else {
138         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
139       }
140     } else {
141       HLTError("can not fetch object \"%s\" from CDB", path);
142     }
143   }
144   
145   return iResult;
146 }
147
148 void AliHLTGlobalEsdConverterComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
149 {
150   // see header file for class documentation
151   list.push_back(kAliHLTDataTypeTrack);
152   list.push_back(kAliHLTDataTypeTrackMC);
153   list.push_back(kAliHLTDataTypeCaloCluster);
154   list.push_back(kAliHLTDataTypedEdx );
155 }
156
157 AliHLTComponentDataType AliHLTGlobalEsdConverterComponent::GetOutputDataType()
158 {
159   // see header file for class documentation
160   return kAliHLTDataTypeESDObject|kAliHLTDataOriginOut;
161 }
162
163 void AliHLTGlobalEsdConverterComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
164 {
165   // see header file for class documentation
166   constBase=2000000;
167   inputMultiplier=10.0;
168 }
169
170 int AliHLTGlobalEsdConverterComponent::DoInit(int argc, const char** argv)
171 {
172   // see header file for class documentation
173   int iResult=0;
174   TString argument="";
175   int bMissingParam=0;
176   for (int i=0; i<argc && iResult>=0; i++) {
177     argument=argv[i];
178     if (argument.IsNull()) continue;
179
180     // -notree
181     if (argument.CompareTo("-notree")==0) {
182       fWriteTree=0;
183
184       // -tree
185     } else if (argument.CompareTo("-tree")==0) {
186       fWriteTree=1;
187
188     } else {
189       HLTError("unknown argument %s", argument.Data());
190       break;
191     }
192   }
193   if (bMissingParam) {
194     HLTError("missing parameter for argument %s", argument.Data());
195     iResult=-EINVAL;
196   }
197
198   if (iResult>=0) {
199     iResult=Reconfigure(NULL, NULL);
200   }
201
202   if (iResult>=0) {
203     fESD = new AliESDEvent;
204     if (fESD) {
205       fESD->CreateStdContent();
206     } else {
207       iResult=-ENOMEM;
208     }
209
210     SetupCTPData();
211   }
212
213   return iResult;
214 }
215
216 int AliHLTGlobalEsdConverterComponent::DoDeinit()
217 {
218   // see header file for class documentation
219   if (fESD) delete fESD;
220   fESD=NULL;
221
222   return 0;
223 }
224
225 int AliHLTGlobalEsdConverterComponent::DoEvent(const AliHLTComponentEventData& /*evtData*/, 
226                                                AliHLTComponentTriggerData& trigData)
227 {
228   // see header file for class documentation
229   int iResult=0;
230   if (!fESD) return -ENODEV;
231
232   AliESDEvent* pESD = fESD;
233
234   pESD->Reset(); 
235   pESD->SetMagneticField(fSolenoidBz);
236   pESD->SetRunNumber(GetRunNo());
237   pESD->SetPeriodNumber(GetPeriodNumber());
238   pESD->SetOrbitNumber(GetOrbitNumber());
239   pESD->SetBunchCrossNumber(GetBunchCrossNumber());
240   pESD->SetTimeStamp(GetTimeStamp());
241
242   const AliHLTCTPData* pCTPData=CTPData();
243   if (pCTPData) {
244     AliHLTUInt64_t mask=pCTPData->ActiveTriggers(trigData);
245     for (int index=0; index<gkNCTPTriggerClasses; index++) {
246       if ((mask&((AliHLTUInt64_t)0x1<<index)) == 0) continue;
247       pESD->SetTriggerClass(pCTPData->Name(index), index);
248     }
249     pESD->SetTriggerMask(mask);
250   }
251
252   TTree* pTree = NULL;
253   if (fWriteTree)
254     pTree = new TTree("esdTree", "Tree with HLT ESD objects");
255  
256   if (pTree) {
257     pTree->SetDirectory(0);
258   }
259
260   if ((iResult=ProcessBlocks(pTree, pESD))>=0) {
261     // TODO: set the specification correctly
262     if (pTree) {
263       // the esd structure is written to the user info and is
264       // needed in te ReadFromTree method to read all objects correctly
265       pTree->GetUserInfo()->Add(pESD);
266       pESD->WriteToTree(pTree);
267       iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginOut, 0);
268     } else {
269       iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOut, 0);
270     }
271   }
272   if (pTree) {
273     // clear user info list to prevent objects from being deleted
274     pTree->GetUserInfo()->Clear();
275     delete pTree;
276   }
277   return iResult;
278 }
279
280 int AliHLTGlobalEsdConverterComponent::ProcessBlocks(TTree* pTree, AliESDEvent* pESD)
281 {
282   // see header file for class documentation
283
284   int iResult=0;
285   int iAddedDataBlocks=0;
286
287   // Barrel tracking
288
289   // in the first attempt this component reads the TPC tracks and updates in the
290   // second step from the ITS tracks
291
292
293   // first read MC information (if present)
294   std::map<int,int> mcLabels;
295
296   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC);
297        pBlock!=NULL; pBlock=GetNextInputBlock()) {
298     AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
299     if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
300       for( unsigned int il=0; il<dataPtr->fCount; il++ ){
301         AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
302         mcLabels[lab.fTrackID] = lab.fMCLabel;
303       }
304     } else {
305       HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information", 
306                  DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, 
307                  dataPtr->fCount, pBlock->fSize);
308     }
309   }
310
311   // read dEdx information (if present)
312
313   AliHLTFloat32_t *dEdxTPC = 0; 
314   Int_t ndEdxTPC = 0;
315   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypedEdx|kAliHLTDataOriginTPC);
316        pBlock!=NULL; pBlock=GetNextInputBlock()) {
317     dEdxTPC = reinterpret_cast<AliHLTFloat32_t*>( pBlock->fPtr );
318     ndEdxTPC = pBlock->fSize / sizeof(AliHLTFloat32_t);
319     break;
320   }
321
322   // convert the TPC tracks to ESD tracks
323   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginTPC);
324        pBlock!=NULL; pBlock=GetNextInputBlock()) {
325     vector<AliHLTGlobalBarrelTrack> tracks;
326     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>=0) {
327       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
328            element!=tracks.end(); element++) {
329         Float_t points[4] = {
330           element->GetX(),
331           element->GetY(),
332           element->GetLastPointX(),
333           element->GetLastPointY()
334         };
335
336         Int_t mcLabel = -1;
337         if( mcLabels.find(element->TrackID())!=mcLabels.end() )
338           mcLabel = mcLabels[element->TrackID()];
339         element->SetLabel( mcLabel );
340
341         AliESDtrack iotrack;
342
343         // for the moment, the number of clusters are not set when processing the
344         // kTPCin update, only at kTPCout
345         // there ar emainly three parameters updated for kTPCout
346         //   number of clusters
347         //   chi2
348         //   pid signal
349         // The first one can be updated already at that stage here, while the two others
350         // eventually require to update from the ITS tracks before. The exact scheme
351         // needs to be checked 
352         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCin);
353         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCout);
354         iotrack.SetTPCPoints(points);
355         if( element->TrackID()<ndEdxTPC ){
356           AliTPCseed s;
357           s.Set( element->GetX(), element->GetAlpha(),
358                  element->GetParameter(), element->GetCovariance() );
359           s.SetdEdx( dEdxTPC[element->TrackID()] );
360           s.CookPID();
361           iotrack.SetTPCpid(s.TPCrPIDs() );
362         } else {
363           if( dEdxTPC ) HLTWarning("Wrong number of dEdx TPC labels");
364         }
365         pESD->AddTrack(&iotrack);
366         if (fVerbosity>0) element->Print();
367       }
368       HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
369       iAddedDataBlocks++;
370     } else if (iResult<0) {
371       HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d", 
372                DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
373     }
374   }
375
376   // now update ESD tracks with the ITS info
377   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginITS);
378        pBlock!=NULL; pBlock=GetNextInputBlock()) {
379     vector<AliHLTGlobalBarrelTrack> tracks;
380     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
381       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
382            element!=tracks.end(); element++) {
383         int ncl=0;
384         const UInt_t* pointsArray=element->GetPoints();
385         for( unsigned il=0; il<element->GetNumberOfPoints(); il++ ){
386           // TODO: check what needs to be done with the clusters
387           if( pointsArray[il]<~(UInt_t)0 ) {/*tITS.SetClusterIndex(ncl,  tr.fClusterIds[il]);*/}
388           ncl++;
389         }
390         //tITS.SetNumberOfClusters( ncl );
391         int tpcID=element->TrackID();
392         // the ITS tracker assigns the TPC track used as seed for a certain track to
393         // the trackID
394         if( tpcID<0 || tpcID>=pESD->GetNumberOfTracks()) continue;
395
396         AliESDtrack *tESD = pESD->GetTrack( tpcID );
397         if( tESD ) tESD->UpdateTrackParams( &(*element), AliESDtrack::kITSin );
398       }
399     }
400   }
401
402   // convert the HLT TRD tracks to ESD tracks                        
403   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack | kAliHLTDataOriginTRD);
404        pBlock!=NULL; pBlock=GetNextInputBlock()) {
405     vector<AliHLTGlobalBarrelTrack> tracks;
406     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
407       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
408            element!=tracks.end(); element++) {
409         
410         Double_t TRDpid[AliPID::kSPECIES], eProb(0.2), restProb((1-eProb)/(AliPID::kSPECIES-1)); //eprob(element->GetTRDpid...);
411         for(Int_t i=0; i<AliPID::kSPECIES; i++){
412           switch(i){
413           case AliPID::kElectron: TRDpid[AliPID::kElectron]=eProb; break;
414           default: TRDpid[i]=restProb; break;
415           }
416         }
417         
418         AliESDtrack iotrack;
419         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTRDin);
420         iotrack.SetTRDpid(TRDpid);
421         
422         pESD->AddTrack(&iotrack);
423         if (fVerbosity>0) element->Print();
424       }
425       HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
426       iAddedDataBlocks++;
427     } else if (iResult<0) {
428       HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d", 
429                DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
430     }
431   }
432   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeCaloCluster | kAliHLTDataOriginPHOS); pBlock!=NULL; pBlock=GetNextInputBlock()) 
433     {
434       AliHLTCaloClusterHeaderStruct *caloClusterHeaderPtr = reinterpret_cast<AliHLTCaloClusterHeaderStruct*>(pBlock->fPtr);
435
436       HLTDebug("%d HLT clusters from spec: 0x%X", caloClusterHeaderPtr->fNClusters, pBlock->fSpecification);
437
438       AliHLTCaloClusterReader reader;
439       reader.SetMemory(caloClusterHeaderPtr);
440
441       AliHLTESDCaloClusterMaker clusterMaker;
442
443       int nClusters = clusterMaker.FillESD(pESD, caloClusterHeaderPtr);
444
445       HLTInfo("converted %d cluster(s) to AliESDCaloCluster and added to ESD", nClusters);
446       iAddedDataBlocks++;
447     }
448       
449       // primary vertex & V0's 
450       
451       AliHLTVertexer vertexer;
452       vertexer.SetESD( pESD );
453       vertexer.FindPrimaryVertex();
454       vertexer.FindV0s();
455
456       // relate the tracks to vertex
457       if( fFillVtxConstrainedTracks ){
458         for( Int_t i = 0; i<pESD->GetNumberOfTracks(); i++){
459           if( !vertexer.TrackInfos()[i].fPrimUsedFlag ) continue;
460           pESD->GetTrack(i)->RelateToVertex( pESD->GetPrimaryVertex(), fESD->GetMagneticField(),5. );
461         }
462       }
463       
464       if (iAddedDataBlocks>0 && pTree) {
465        pTree->Fill();
466       }
467   
468   if (iResult>=0) iResult=iAddedDataBlocks;
469   return iResult;
470 }