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