]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/global/AliHLTGlobalEsdConverterComponent.cxx
added conversion shell scripts
[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 "AliHLTErrorGuard.h"
38 #include "AliESDEvent.h"
39 #include "AliESDtrack.h"
40 #include "AliESDMuonTrack.h"
41 #include "AliESDMuonCluster.h"
42 #include "AliCDBEntry.h"
43 #include "AliCDBManager.h"
44 #include "AliPID.h"
45 #include "TTree.h"
46 #include "TList.h"
47 #include "TClonesArray.h"
48 #include "TTimeStamp.h"
49 #include "THnSparse.h"
50 #include "AliHLTESDCaloClusterMaker.h"
51 #include "AliHLTCaloClusterDataStruct.h"
52 #include "AliHLTCaloClusterReader.h"
53 #include "AliESDCaloCluster.h"
54 #include "AliESDVZERO.h"
55 #include "AliHLTGlobalVertexerComponent.h"
56 #include "AliHLTVertexFinderBase.h"
57
58 /** ROOT macro for the implementation of ROOT specific class methods */
59 ClassImp(AliHLTGlobalEsdConverterComponent)
60
61 AliHLTGlobalEsdConverterComponent::AliHLTGlobalEsdConverterComponent()
62   : AliHLTProcessor()
63   , fWriteTree(0)
64   , fVerbosity(0)
65   , fESD(NULL)
66   , fSolenoidBz(-5.00668)
67   , fBenchmark("EsdConverter")
68   , fBenchmarkHistosFilename("$PWD/histosBenchmark.root")
69   , fInitialTime(0)
70 {
71   // see header file for class documentation
72   // or
73   // refer to README to build package
74   // or
75   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
76   
77     TFile *f = TFile::Open(fBenchmarkHistosFilename,"READ");
78   if(f!=0x0){
79         TNamed *t = (TNamed*)f->Get("time");
80         if(t!=0x0){
81           fInitialTime = atoi(t->GetTitle());
82         }
83   }
84   
85 }
86
87 AliHLTGlobalEsdConverterComponent::~AliHLTGlobalEsdConverterComponent()
88 {
89   // see header file for class documentation
90   if (fESD) delete fESD;
91   fESD=NULL;
92 }
93
94 int AliHLTGlobalEsdConverterComponent::Configure(const char* arguments)
95 {
96   // see header file for class documentation
97   int iResult=0;
98   if (!arguments) return iResult;
99
100   TString allArgs=arguments;
101   TString argument;
102   int bMissingParam=0;
103
104   TObjArray* pTokens=allArgs.Tokenize(" ");
105   if (pTokens) {
106     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
107       argument=((TObjString*)pTokens->At(i))->String(); 
108       if (argument.IsNull()) continue;
109       
110       if (argument.CompareTo("-solenoidBz")==0) {
111         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
112         HLTWarning("argument -solenoidBz is deprecated, magnetic field set up globally (%f)", GetBz());
113         continue;
114       } else {
115         HLTError("unknown argument %s", argument.Data());
116         iResult=-EINVAL;
117         break;
118       }
119     }
120     delete pTokens;
121   }
122   if (bMissingParam) {
123     HLTError("missing parameter for argument %s", argument.Data());
124     iResult=-EINVAL;
125   }
126
127   return iResult;
128 }
129
130 int AliHLTGlobalEsdConverterComponent::Reconfigure(const char* cdbEntry, const char* chainId)
131 {
132   // see header file for class documentation
133   int iResult=0;
134   const char* path=NULL;
135   const char* defaultNotify="";
136   if (cdbEntry) {
137     path=cdbEntry;
138     defaultNotify=" (default)";
139   }
140   if (path) {
141     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
142     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
143     if (pEntry) {
144       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
145       if (pString) {
146         HLTInfo("received configuration object string: \'%s\'", pString->String().Data());
147         iResult=Configure(pString->String().Data());
148       } else {
149         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
150       }
151     } else {
152       HLTError("can not fetch object \"%s\" from CDB", path);
153     }
154   }
155   
156   return iResult;
157 }
158
159 void AliHLTGlobalEsdConverterComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
160 {
161   // see header file for class documentation
162   list.push_back(kAliHLTDataTypeTrack);
163   list.push_back(kAliHLTDataTypeTrackMC);
164   list.push_back(kAliHLTDataTypeCaloCluster);
165   list.push_back(kAliHLTDataTypedEdx );
166   list.push_back(kAliHLTDataTypeESDVertex );
167   list.push_back(kAliHLTDataTypeESDObject);
168   list.push_back(kAliHLTDataTypeTObject);
169   list.push_back(kAliHLTDataTypeGlobalVertexer);
170   list.push_back(kAliHLTDataTypeV0Finder); // array of track ids for V0s
171   list.push_back(kAliHLTDataTypeKFVertex); // KFVertex object from vertexer
172   list.push_back(kAliHLTDataTypePrimaryFinder); // array of track ids for prim vertex
173   list.push_back(kAliHLTDataTypeESDContent);
174 }
175
176 AliHLTComponentDataType AliHLTGlobalEsdConverterComponent::GetOutputDataType()
177 {
178   // see header file for class documentation
179   return kAliHLTDataTypeESDObject|kAliHLTDataOriginOut;
180 }
181
182 void AliHLTGlobalEsdConverterComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
183 {
184   // see header file for class documentation
185   constBase=2000000;
186   inputMultiplier=10.0;
187 }
188
189 int AliHLTGlobalEsdConverterComponent::DoInit(int argc, const char** argv)
190 {
191   // see header file for class documentation
192   int iResult=0;
193   TString argument="";
194   int bMissingParam=0;
195
196   // default list of skiped ESD objects
197   TString skipObjects=
198     // "AliESDRun,"
199     // "AliESDHeader,"
200     // "AliESDZDC,"
201     "AliESDFMD,"
202     // "AliESDVZERO,"
203     // "AliESDTZERO,"
204     // "TPCVertex,"
205     // "SPDVertex,"
206     // "PrimaryVertex,"
207     // "AliMultiplicity,"
208     // "PHOSTrigger,"
209     // "EMCALTrigger,"
210     // "SPDPileupVertices,"
211     // "TrkPileupVertices,"
212     "Cascades,"
213     "Kinks,"
214     "AliRawDataErrorLogs,"
215     "AliESDACORDE";
216
217   iResult=Reconfigure(NULL, NULL);
218   TString allArgs = "";
219   for ( int i = 0; i < argc; i++ ) {
220     if ( !allArgs.IsNull() ) allArgs += " ";
221     allArgs += argv[i];
222   }
223
224   TObjArray* pTokens=allArgs.Tokenize(" ");
225   if (pTokens) {
226     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
227       argument=((TObjString*)pTokens->At(i))->String(); 
228       if (argument.IsNull()) continue;
229
230       // -notree
231       if (argument.CompareTo("-notree")==0) {
232         fWriteTree=0;
233         
234         // -tree
235       } else if (argument.CompareTo("-tree")==0) {
236         fWriteTree=1;
237       } else if (argument.CompareTo("-solenoidBz")==0) {
238         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
239         HLTInfo("Magnetic Field set to: %s", ((TObjString*)pTokens->At(i))->String().Data());
240         HLTWarning("argument '-solenoidBz' is deprecated, solenoid field initiaized from CDB settings");
241         continue;
242       } else if (argument.Contains("-skipobject=")) {
243         argument.ReplaceAll("-skipobject=", "");
244         skipObjects=argument;
245       } else {
246         HLTError("unknown argument %s", argument.Data());
247         iResult=-EINVAL;
248         break;
249       }
250     }
251   }
252   if (bMissingParam) {
253     HLTError("missing parameter for argument %s", argument.Data());
254     iResult=-EINVAL;
255   }
256
257   fSolenoidBz=GetBz();
258
259   if (iResult>=0) {
260     fESD = new AliESDEvent;
261     if (fESD) {
262       fESD->CreateStdContent();
263
264       // remove some of the objects which are not needed
265       if (fESD->GetList() && !skipObjects.IsNull()) {
266         pTokens=skipObjects.Tokenize(",");
267         if (pTokens) {
268           const char* id=NULL;
269           TIter next(pTokens);
270           TObject* pObject=NULL;
271           while ((pObject=next())!=NULL) {
272             id=((TObjString*)pObject)->String().Data();
273             TObject* pObj=fESD->GetList()->FindObject(id);
274             if (pObj) {
275               HLTDebug("removing object %s", id);
276               fESD->GetList()->Remove(pObj);
277               delete pObj;
278             } else {
279               HLTWarning("failed to remove object '%s' from ESD", id);
280             }
281           }
282           fESD->GetStdContent();
283           delete pTokens;
284         }
285       }
286     } else {
287       iResult=-ENOMEM;
288     }
289
290     SetupCTPData();
291   }
292
293   fBenchmark.SetTimer(0,"total");
294
295   return iResult;
296 }
297
298 int AliHLTGlobalEsdConverterComponent::DoDeinit()
299 {
300   // see header file for class documentation
301   if (fESD) delete fESD;
302   fESD=NULL;
303
304   return 0;
305 }
306
307 int AliHLTGlobalEsdConverterComponent::DoEvent(const AliHLTComponentEventData& /*evtData*/, 
308                                                AliHLTComponentTriggerData& trigData)
309 {
310   // see header file for class documentation
311   int iResult=0;
312   bool benchmark = true;
313   if (!fESD) return -ENODEV;
314
315   if (!IsDataEvent()) return iResult;
316   fBenchmark.StartNewEvent();
317   fBenchmark.Start(0);
318
319   AliESDEvent* pESD = fESD;
320
321   pESD->Reset(); 
322   pESD->SetMagneticField(fSolenoidBz);
323   pESD->SetRunNumber(GetRunNo());
324   pESD->SetPeriodNumber(GetPeriodNumber());
325   pESD->SetOrbitNumber(GetOrbitNumber());
326   pESD->SetBunchCrossNumber(GetBunchCrossNumber());
327   pESD->SetTimeStamp(GetTimeStamp());
328
329   const AliHLTCTPData* pCTPData=CTPData();
330   if (pCTPData) {
331     AliHLTTriggerMask_t mask=pCTPData->ActiveTriggers(trigData);
332     for (int index=0; index<gkNCTPTriggerClasses; index++) {
333       if ((mask&(AliHLTTriggerMask_t(0x1)<<index)) == 0) continue;
334       pESD->SetTriggerClass(pCTPData->Name(index), index);
335     }
336     //first 50 triggers
337     AliHLTTriggerMask_t mask50;
338     mask50.set(); // set all bits
339     mask50 >>= 50; // shift 50 right
340     pESD->SetTriggerMask((mask&mask50).to_ulong());
341     //next 50
342     pESD->SetTriggerMaskNext50((mask>>50).to_ulong());
343   }
344
345   TTree* pTree = NULL;
346   if (fWriteTree)
347     pTree = new TTree("esdTree", "Tree with HLT ESD objects");
348  
349   if (pTree) {
350     pTree->SetDirectory(0);
351   }
352
353   if ((iResult=ProcessBlocks(pTree, pESD))>=0) {
354     // TODO: set the specification correctly
355     if (pTree) {
356       // the esd structure is written to the user info and is
357       // needed in te ReadFromTree method to read all objects correctly
358       pTree->GetUserInfo()->Add(pESD);
359       pESD->WriteToTree(pTree);
360       iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginOut, 0);
361     } else {
362       iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOut, 0);
363     }
364     fBenchmark.AddOutput(GetLastObjectSize());
365   }
366   if (pTree) {
367     // clear user info list to prevent objects from being deleted
368     pTree->GetUserInfo()->Clear();
369     delete pTree;
370   }
371
372   fBenchmark.Stop(0);
373   HLTWarning( fBenchmark.GetStatistics() );
374   
375     
376   
377   
378   
379   
380     if(benchmark){
381         
382         TTimeStamp ts;
383         Double_t time = (Double_t) ts.GetSec() - fInitialTime;
384         Int_t nV0s = pESD->GetNumberOfV0s();
385         Int_t nTracks = pESD->GetNumberOfTracks();
386         
387         
388         Double_t statistics[10]; 
389         TString names[10];
390         fBenchmark.GetStatisticsData(statistics, names);
391           statistics[5] = nTracks;
392           statistics[6] = time;
393           statistics[7] = nV0s;
394           
395           FillBenchmarkHistos( statistics, names);
396   printf("\nTIME:  %.9f \n\n",time);
397           fBenchmark.Reset();
398   
399   }
400   
401   
402   
403   
404
405   return iResult;
406 }
407
408 int AliHLTGlobalEsdConverterComponent::ProcessBlocks(TTree* pTree, AliESDEvent* pESD)
409 {
410   // see header file for class documentation
411
412   int iResult=0;
413   int iAddedDataBlocks=0;
414
415   // check if there is an ESD block in the input and copy its content first to the
416   // ESD
417   const TObject* pEsdObj = GetFirstInputObject(kAliHLTDataTypeESDObject, "AliESDEvent");
418   AliESDEvent* pInputESD=NULL;
419   if (pEsdObj) pInputESD=dynamic_cast<AliESDEvent*>(const_cast<TObject*>(pEsdObj));
420   if (pInputESD) {
421     pInputESD->GetStdContent();
422     *pESD=*pInputESD;
423   }
424   if (GetNextInputObject()!=NULL) {
425     HLTWarning("handling of multiple ESD input objects not implemented, skipping input");
426   }
427
428   // Barrel tracking
429   // tracks are based on the TPC tracks, and only updated from the ITS information
430   // Sequence:
431   // 1) extract MC information for TPC and ITS from specific data blocks and store in
432   //    intermediate vector arrays
433   // 2) extract TPC tracks, update with MC labels if available, the track parameters
434   //    are estimated at the first cluster position
435   // 2.1) propagate to last cluster position and update kTPCout, sets also outer param (fOp)
436   // 2.2) update kTPCin, sets also inner param (fIp) and TPC inner param (fTPCInner)
437   // 2.3) update kTPCrefit using the same parameters at the first cluster position
438   //      HLT has strictly spoking no refit, but we want the flag to be set
439   //      can be changed to be done after all the individual barrel detector parameters
440   //      have been updated by looping over the tracks again
441   // 3) extract ITS tracks, the tracks are actually TPC tracks updated from the ITS
442   //    tracking information
443   // 3.1) TODO 2010-07-12: handle ITS standalone tracks by updating kITSout before kITSin
444   // 3.2) update with kITSin
445   //    TODO 2010-07-12 find out if the kITSrefit has to be set as well
446   // 4) extract TRD tracks and add to ESD
447   //    TODO 2010-07-12 at the moment there is no matching or merging of TPC and TRD tracks
448   // 5) Add Trigger Detectors 
449   //    VZERO, ZDC
450
451   // 1) first read MC information (if present)
452   std::map<int,int> mcLabelsTPC;
453   std::map<int,int> mcLabelsITS;
454
455   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC);
456        pBlock!=NULL; pBlock=GetNextInputBlock()) {
457
458     fBenchmark.AddInput(pBlock->fSize);
459
460     AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
461     if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
462       for( unsigned int il=0; il<dataPtr->fCount; il++ ){
463         AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
464         mcLabelsTPC[lab.fTrackID] = lab.fMCLabel;
465       }
466     } else {
467       HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information", 
468                  DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, 
469                  dataPtr->fCount, pBlock->fSize);
470     }
471   }
472  
473   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginITS);
474        pBlock!=NULL; pBlock=GetNextInputBlock()) {
475
476     fBenchmark.AddInput(pBlock->fSize);
477
478     AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
479     if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
480       for( unsigned int il=0; il<dataPtr->fCount; il++ ){
481         AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
482         mcLabelsITS[lab.fTrackID] = lab.fMCLabel;
483       }
484     } else {
485       HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information", 
486                  DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, 
487                  dataPtr->fCount, pBlock->fSize);
488     }
489   }
490
491
492   // read dEdx information (if present)
493   // TODO 2010-07-12 this needs to be verified
494
495   AliHLTFloat32_t *dEdxTPC = 0; 
496   Int_t ndEdxTPC = 0;
497   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypedEdx|kAliHLTDataOriginTPC);
498        pBlock!=NULL; pBlock=NULL/*GetNextInputBlock() there is only one block*/) {
499     fBenchmark.AddInput(pBlock->fSize);
500     dEdxTPC = reinterpret_cast<AliHLTFloat32_t*>( pBlock->fPtr );
501     ndEdxTPC = pBlock->fSize / (3*sizeof(AliHLTFloat32_t));
502   }
503
504   // 2) convert the TPC tracks to ESD tracks
505   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginTPC);
506        pBlock!=NULL; pBlock=GetNextInputBlock()) {
507     if (pInputESD && pInputESD->GetNumberOfTracks()>0) {
508       HLTWarning("Tracks array already filled from the input esd block, additional filling from TPC tracks block might cause inconsistent content");
509     }
510     fBenchmark.AddInput(pBlock->fSize);
511     vector<AliHLTGlobalBarrelTrack> tracks;
512     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>=0) {
513       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
514            element!=tracks.end(); element++) {
515         Float_t points[4] = {
516           static_cast<Float_t>(element->GetX()),
517           static_cast<Float_t>(element->GetY()),
518           static_cast<Float_t>(element->GetLastPointX()),
519           static_cast<Float_t>(element->GetLastPointY())
520         };
521
522         Int_t mcLabel = -1;
523         if( mcLabelsTPC.find(element->TrackID())!=mcLabelsTPC.end() )
524           mcLabel = mcLabelsTPC[element->TrackID()];
525         element->SetLabel( mcLabel );
526
527         AliESDtrack iotrack;
528
529         // for the moment, the number of clusters are not set when processing the
530         // kTPCin update, only at kTPCout
531         // there ar emainly three parameters updated for kTPCout
532         //   number of clusters
533         //   chi2
534         //   pid signal
535         // The first one can be updated already at that stage here, while the two others
536         // eventually require to update from the ITS tracks before. The exact scheme
537         // needs to be checked
538         iotrack.SetID( element->TrackID() );
539
540         // 2.1 set kTPCout
541         // TODO 2010-07-12 disabled for the moment because of bug
542         // https://savannah.cern.ch/bugs/index.php?69875
543         // the propagation does not work, there is an offset in z
544         // propagate to last cluster and update parameters with flag kTPCout
545         // Note: updating with flag kITSout further down will overwrite the outer
546         // parameter again so this can be done only for ITS standalone tracks, meaning
547         // tracks in the ITS not associated with any TPC track
548         // HLT does not provide such standalone tracking
549         {
550           AliHLTGlobalBarrelTrack outPar(*element);       
551           //outPar.AliExternalTrackParam::PropagateTo( element->GetLastPointX(), fSolenoidBz );
552           const Int_t N=10; // number of steps.
553           const Float_t xRange = element->GetLastPointX() - element->GetX();
554           const Float_t xStep = xRange / N ;
555           for(int i = 1; i <= N; ++i) {
556             if(!outPar.AliExternalTrackParam::PropagateTo(element->GetX() + xStep * i, fSolenoidBz)) break;
557           }
558           outPar.SetLabel(element->GetLabel());
559           iotrack.UpdateTrackParams(&outPar,AliESDtrack::kTPCout);
560         }
561         // 2.2 TPC tracking estimates parameters at first cluster
562         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCin);
563
564         // 2.3 use the same parameters also for kTPCrefit, there is no proper refit in HLT
565         // maybe this can be done later, however also the inner parameter is changed which
566         // is used as a reference point in the display. That point should be at the first
567         // TPC cluster
568         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCrefit);
569         iotrack.SetTPCPoints(points);
570         if( element->TrackID()<ndEdxTPC ){
571           AliHLTFloat32_t *val = &(dEdxTPC[3*element->TrackID()]);
572           iotrack.SetTPCsignal( val[0], val[1], (UChar_t) val[2] ); 
573           //AliTPCseed s;
574           //s.Set( element->GetX(), element->GetAlpha(),
575           //element->GetParameter(), element->GetCovariance() );
576           //s.SetdEdx( val[0] );
577           //s.CookPID();
578           //iotrack.SetTPCpid(s.TPCrPIDs() );
579         } else {
580           if( dEdxTPC ) HLTWarning("Wrong number of dEdx TPC labels");
581         }
582         iotrack.SetLabel(mcLabel);
583         pESD->AddTrack(&iotrack);
584         if (fVerbosity>0) element->Print();
585       }
586       HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
587       iAddedDataBlocks++;
588     } else if (iResult<0) {
589       HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d", 
590                DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
591     }
592   }
593
594
595   // Get ITS SPD vertex
596   for( const AliHLTComponentBlockData *i= GetFirstInputBlock(kAliHLTDataTypeESDVertex|kAliHLTDataOriginITS); i!=NULL; i=GetNextInputBlock() ){
597     fBenchmark.AddInput(i->fSize);
598   }
599
600   for ( const TObject *iter = GetFirstInputObject(kAliHLTDataTypeESDVertex|kAliHLTDataOriginITS); iter != NULL; iter = GetNextInputObject() ) {
601     AliESDVertex *vtx = dynamic_cast<AliESDVertex*>(const_cast<TObject*>( iter ) );
602     pESD->SetPrimaryVertexSPD( vtx );
603   }
604
605   // 3.1. now update ESD tracks with the ITSOut info
606   // updating track parameters with flag kITSout will overwrite parameter set above with flag kTPCout
607   // TODO 2010-07-12 there are some issues with this updating sequence, for the moment update with
608   // flag kITSout is disabled, would require
609   // a) to update kTPCout after kITSout
610   // b) update only for ITS standalone tracks. The HLT ITS tracker does not provide standalone
611   //    tracking, every track is related to a TPC track
612   // Furthermore there seems to be a bug as the data blocks describe track parameters around the
613   // vertex, not at the outer ITS
614   // bug https://savannah.cern.ch/bugs/index.php?69872
615   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginITSOut);
616        pBlock!=NULL; pBlock=GetNextInputBlock()) {
617     fBenchmark.AddInput(pBlock->fSize);
618     vector<AliHLTGlobalBarrelTrack> tracks;
619     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
620       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
621            element!=tracks.end(); element++) {
622         int tpcID=element->TrackID();
623         // the ITS tracker assigns the TPC track used as seed for a certain track to
624         // the trackID
625         if( tpcID<0 || tpcID>=pESD->GetNumberOfTracks()) continue;
626         AliESDtrack *tESD = pESD->GetTrack( tpcID );
627         element->SetLabel(tESD->GetLabel());
628         // 2010-07-12 disabled, see above, bugfix https://savannah.cern.ch/bugs/index.php?69872
629         //if( tESD ) tESD->UpdateTrackParams( &(*element), AliESDtrack::kITSout );
630       }
631     }
632   }
633
634   // 3.2. now update ESD tracks with the ITS info
635   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginITS);
636        pBlock!=NULL; pBlock=GetNextInputBlock()) {
637     fBenchmark.AddInput(pBlock->fSize);
638     vector<AliHLTGlobalBarrelTrack> tracks;
639     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
640       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
641            element!=tracks.end(); element++) {
642         int tpcID=element->TrackID();
643         // the ITS tracker assigns the TPC track used as seed for a certain track to
644         // the trackID
645         if( tpcID<0 || tpcID>=pESD->GetNumberOfTracks()) continue;
646         Int_t mcLabel = -1;
647         if( mcLabelsITS.find(element->TrackID())!=mcLabelsITS.end() )
648           mcLabel = mcLabelsITS[element->TrackID()];
649         AliESDtrack *tESD = pESD->GetTrack( tpcID );
650         if (!tESD) continue;
651         // the labels for the TPC and ITS tracking params can be different, e.g.
652         // there can be a decay. The ITS label should then be the better one, the
653         // TPC label is saved in a member of AliESDtrack
654         if (mcLabel>=0) {
655           // upadte only if the ITS label is available, otherwise keep TPC label
656           element->SetLabel( mcLabel );
657         } else {
658           // bugfix https://savannah.cern.ch/bugs/?69713
659           element->SetLabel( tESD->GetLabel() );          
660         }
661         tESD->UpdateTrackParams( &(*element), AliESDtrack::kITSin );
662
663         // TODO: add a proper refit
664         //tESD->UpdateTrackParams( &(*element), AliESDtrack::kTPCrefit );
665       }
666     }
667   }
668
669   // update with  vertices and vertex-fitted tracks
670   // output of the GlobalVertexerComponent
671   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeGlobalVertexer);
672        pBlock!=NULL; pBlock=GetNextInputBlock()) {
673     fBenchmark.AddInput(pBlock->fSize);   
674     AliHLTGlobalVertexerComponent::FillESD( pESD, reinterpret_cast<AliHLTGlobalVertexerComponent::AliHLTGlobalVertexerData* >(pBlock->fPtr) );
675   }
676
677   // update with  vertices and vertex-fitted tracks
678   // output of PrimaryVertexer and V0Finder components
679   
680   TObject* pBase = (TObject*)GetFirstInputObject(kAliHLTDataTypeKFVertex | kAliHLTDataOriginOut);
681   if (pBase) {
682     AliKFVertex* kfVertex = dynamic_cast<AliKFVertex *>(pBase);
683     if (kfVertex) {
684       const AliHLTComponentBlockData* pP = GetFirstInputBlock(kAliHLTDataTypePrimaryFinder | kAliHLTDataOriginOut);
685       if (pP && pP->fSize && pP->fPtr) {
686         const AliHLTComponentBlockData* pV0 = GetFirstInputBlock(kAliHLTDataTypeV0Finder | kAliHLTDataOriginOut);
687         if (pV0 && pV0->fPtr && pInputESD && pInputESD->GetNumberOfV0s()>0) {
688           const int* v0s = static_cast<const int*>(pV0->fPtr);
689           HLTWarning("V0 array already filled from the input esd block, additional filling from V0 block of %d entries might cause inconsistent content", v0s[0]);
690         }
691         AliHLTVertexFinderBase::FillESD(pESD, kfVertex, pP->fPtr, pV0?pV0->fPtr:NULL);
692       } else
693         HLTWarning("Problem with primary finder's data block");
694     } else {
695       HLTWarning("primary vertex block of wrong type, expecting AliKFVertex instead of %s", pBase->GetName());
696     }
697   } else {
698     // throw an error if there is a V0 data block which can not be handled without
699     // the AliKFVertex object
700     if (GetFirstInputBlock(kAliHLTDataTypeV0Finder | kAliHLTDataOriginOut)!=NULL) {
701       ALIHLTERRORGUARD(1, "missing AliKFVertex object ignoring V0 data block of type %s",
702                        DataType2Text(kAliHLTDataTypeV0Finder|kAliHLTDataOriginOut).c_str());
703     }
704   }
705
706   // Fill DCA parameters for TPC tracks
707   // TODO 2010-07-12 this propagates also the TPC inner param to beamline
708   // sounds not very reasonable
709   // https://savannah.cern.ch/bugs/index.php?69873
710   for (int i=0; i<pESD->GetNumberOfTracks(); i++) {
711     if (!pESD->GetTrack(i) || 
712         !pESD->GetTrack(i)->GetTPCInnerParam() ) continue;
713     pESD->GetTrack(i)->RelateToVertexTPC(pESD->GetPrimaryVertexTracks(), fSolenoidBz, 1000 );    
714   }
715
716   // loop over all tracks and set the TPC refit flag by updating with the
717   // original TPC inner parameter if not yet set
718   // TODO: replace this by a proper refit
719   // code is comented for the moment as it does not fully solve the problems with
720   // the display
721   // - would set the main parameters to the TPC inner wall again, or
722   // - changes the inner param if the parameters are propagated, so we loose the track
723   //   reference point for the display
724   // with the current sequence we have the latter case as the DCA operations above
725   // change the TPC inner parameters
726   /*
727   for (int i=0; i<pESD->GetNumberOfTracks(); i++) {
728     if (!pESD->GetTrack(i) || 
729         !pESD->GetTrack(i)->GetTPCInnerParam() ||
730         pESD->GetTrack(i)->IsOn(AliESDtrack::kTPCrefit)) continue;
731     AliESDtrack* tESD=pESD->GetTrack(i);
732     AliHLTGlobalBarrelTrack inner(*tESD->GetTPCInnerParam());
733     inner.SetLabel(tESD->GetLabel());
734     tESD->UpdateTrackParams(&inner, AliESDtrack::kTPCrefit);
735   }
736   */
737
738   // 4. convert the HLT TRD tracks to ESD tracks                        
739   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack | kAliHLTDataOriginTRD);
740        pBlock!=NULL; pBlock=GetNextInputBlock()) {
741     fBenchmark.AddInput(pBlock->fSize);
742     vector<AliHLTGlobalBarrelTrack> tracks;
743     if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
744       for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
745            element!=tracks.end(); element++) {
746         
747         Double_t TRDpid[AliPID::kSPECIES], eProb(0.2), restProb((1-eProb)/(AliPID::kSPECIES-1)); //eprob(element->GetTRDpid...);
748         for(Int_t i=0; i<AliPID::kSPECIES; i++){
749           switch(i){
750           case AliPID::kElectron: TRDpid[AliPID::kElectron]=eProb; break;
751           default: TRDpid[i]=restProb; break;
752           }
753         }
754         
755         AliESDtrack iotrack;
756         iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTRDout);
757         iotrack.SetStatus(AliESDtrack::kTRDin);
758         iotrack.SetTRDpid(TRDpid);
759         
760         pESD->AddTrack(&iotrack);
761         if (fVerbosity>0) element->Print();
762       }
763       HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
764       iAddedDataBlocks++;
765     } else if (iResult<0) {
766       HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d", 
767                DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
768     }
769   }
770   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeCaloCluster | kAliHLTDataOriginAny); pBlock!=NULL; pBlock=GetNextInputBlock()) 
771     {
772       fBenchmark.AddInput(pBlock->fSize);
773       AliHLTCaloClusterHeaderStruct *caloClusterHeaderPtr = reinterpret_cast<AliHLTCaloClusterHeaderStruct*>(pBlock->fPtr);
774
775       HLTDebug("%d HLT clusters from spec: 0x%X", caloClusterHeaderPtr->fNClusters, pBlock->fSpecification);
776
777       //AliHLTCaloClusterReader reader;
778       //reader.SetMemory(caloClusterHeaderPtr);
779
780       AliHLTESDCaloClusterMaker clusterMaker;
781
782       int nClusters = clusterMaker.FillESD(pESD, caloClusterHeaderPtr);
783      
784       HLTInfo("converted %d cluster(s) to AliESDCaloCluster and added to ESD", nClusters);
785       iAddedDataBlocks++;
786     }
787   
788   // 5) Add Trigger Detectors 
789   //    VZERO, ZDC
790
791   // FIXME: the size of all input blocks can be added in one loop
792   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeESDContent|kAliHLTDataOriginVZERO);
793        pBlock!=NULL; pBlock=GetNextInputBlock()) {
794     fBenchmark.AddInput(pBlock->fSize);
795   }
796
797   for ( const TObject *pObject = GetFirstInputObject(kAliHLTDataTypeESDContent|kAliHLTDataOriginVZERO); 
798         pObject != NULL; pObject = GetNextInputObject() ) {
799     AliESDVZERO *esdVZERO = dynamic_cast<AliESDVZERO*>(const_cast<TObject*>( pObject ) );
800     if (esdVZERO) {
801       pESD->SetVZEROData( esdVZERO );
802       break;
803     } else {
804       ALIHLTERRORGUARD(1, "input object of data type %s is not of class AliESDVZERO",
805                        DataType2Text(kAliHLTDataTypeESDContent|kAliHLTDataOriginVZERO).c_str());
806     }
807   }
808
809   // FIXME: the size of all input blocks can be added in one loop
810   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeESDContent|kAliHLTDataOriginZDC);
811        pBlock!=NULL; pBlock=GetNextInputBlock()) {
812     fBenchmark.AddInput(pBlock->fSize);
813   }
814   for ( const TObject *pObject = GetFirstInputObject(kAliHLTDataTypeESDContent|kAliHLTDataOriginZDC); 
815         pObject != NULL; pObject = GetNextInputObject() ) {
816     AliESDZDC *esdZDC = dynamic_cast<AliESDZDC*>(const_cast<TObject*>( pObject ) );
817     if (esdZDC) {
818 #ifndef HAVE_NOT_ALIZDCRECONSTRUCTOR_r43770
819       pESD->SetZDCData( esdZDC );
820 #else
821       ALIHLTERRORGUARD(1, "Processing of ZDC data requires AliRoot r43770m skipping data block of type %s",
822                        DataType2Text(kAliHLTDataTypeESDContent|kAliHLTDataOriginZDC).c_str());
823 #endif
824       break;
825     } else {
826       ALIHLTERRORGUARD(1, "input object of data type %s is not of class AliESDZDC",
827                        DataType2Text(kAliHLTDataTypeESDContent|kAliHLTDataOriginZDC).c_str());
828     }
829   }
830
831   // Add tracks and clusters from MUON.
832   for( const AliHLTComponentBlockData *i= GetFirstInputBlock(kAliHLTAnyDataType | kAliHLTDataOriginMUON); i!=NULL; i=GetNextInputBlock() ){
833     fBenchmark.AddInput(i->fSize);
834   }
835
836   for (const TObject* obj = GetFirstInputObject(kAliHLTAnyDataType | kAliHLTDataOriginMUON);
837        obj != NULL;
838        obj = GetNextInputObject()
839       )
840   {
841     const TClonesArray* tracklist = NULL;
842     const TClonesArray* clusterlist = NULL;
843     if (obj->IsA() == AliESDEvent::Class())
844     {
845       const AliESDEvent* event = static_cast<const AliESDEvent*>(obj);
846       HLTDebug("Received a MUON ESD with specification: 0x%X", GetSpecification(obj));
847       if (event->GetList() == NULL) continue;
848       tracklist = dynamic_cast<const TClonesArray*>(event->GetList()->FindObject("MuonTracks"));
849       if (tracklist == NULL) continue;
850       clusterlist = dynamic_cast<const TClonesArray*>(event->GetList()->FindObject("MuonClusters"));
851       if (clusterlist == NULL) continue;
852     }
853     else if (obj->IsA() == TClonesArray::Class())
854     {
855       if (!strcmp(obj->GetName(), "MuonTracks")) {
856         tracklist = static_cast<const TClonesArray*>(obj);
857         HLTDebug("Received a MUON TClonesArray of tracks with specification: 0x%X", GetSpecification(obj));
858       } else {
859         clusterlist = static_cast<const TClonesArray*>(obj);
860         HLTDebug("Received a MUON TClonesArray of clusters with specification: 0x%X", GetSpecification(obj));
861       }
862     }
863     else
864     {
865       // Cannot handle this object type.
866       continue;
867     }
868     // copy tracks
869     if (tracklist) {
870       HLTDebug("Received %d MUON tracks.", tracklist->GetEntriesFast());
871       if (tracklist->GetEntriesFast() > 0)
872       {
873         const AliESDMuonTrack* track = dynamic_cast<const AliESDMuonTrack*>(tracklist->UncheckedAt(0));
874         if (track == NULL)
875         {
876           HLTError(Form("%s from MUON does not contain AliESDMuonTrack objects.", obj->ClassName()));
877           continue;
878         }
879       }
880       for (Int_t i = 0; i < tracklist->GetEntriesFast(); ++i)
881       {
882         AliESDMuonTrack* track = pESD->NewMuonTrack();
883         *track = *(static_cast<const AliESDMuonTrack*>(tracklist->UncheckedAt(i)));
884       }
885     }
886     // copy clusters
887     if (clusterlist) {
888       HLTDebug("Received %d MUON clusters.", clusterlist->GetEntriesFast());
889       if (clusterlist->GetEntriesFast() > 0)
890       {
891         const AliESDMuonCluster* cluster = dynamic_cast<const AliESDMuonCluster*>(clusterlist->UncheckedAt(0));
892         if (cluster == NULL)
893         {
894           HLTError(Form("%s from MUON does not contain AliESDMuonCluster objects.", obj->ClassName()));
895           continue;
896         }
897       }
898       for (Int_t i = 0; i < clusterlist->GetEntriesFast(); ++i)
899       {
900         AliESDMuonCluster* cluster = pESD->NewMuonCluster();
901         *cluster = *(static_cast<const AliESDMuonCluster*>(clusterlist->UncheckedAt(i)));
902       }
903     }
904   }
905   
906   if (iAddedDataBlocks>0 && pTree) {
907     pTree->Fill();
908   }
909
910   
911   
912   
913   if (iResult>=0) iResult=iAddedDataBlocks;
914   return iResult;
915 }
916
917
918
919
920 void AliHLTGlobalEsdConverterComponent::FillBenchmarkHistos(Double_t *statistics, TString */*names*/){
921
922
923 //  cout<<"Now writing benchmarks to " <<  fBenchmarkHistosFilename <<endl<<endl;
924     
925   TFile *f = TFile::Open(fBenchmarkHistosFilename,"UPDATE");
926   THnSparseD *s = (THnSparseD*)f->Get("benchmarkInformation");
927   TNamed *t = (TNamed*)f->Get("time");
928         
929   if(!s){
930         HLTWarning( "Benchmark Histograms not available!" );
931         return;
932   }
933   s->Fill(statistics);
934
935   TList histosList;
936   histosList.Add(s);
937   histosList.Add(t);
938   histosList.SaveAs(fBenchmarkHistosFilename);
939   
940 }