Changed output detector to kAliHLTDataOriginOut
[u/mrichter/AliRoot.git] / HLT / global / AliHLTGlobalEsdConverterComponent.cxx
CommitLineData
a1408c4b 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"
093b64dc 35#include "AliHLTTrackMCLabel.h"
6117ef63 36#include "AliHLTCTPData.h"
a1408c4b 37#include "AliESDEvent.h"
38#include "AliESDtrack.h"
39#include "AliCDBEntry.h"
40#include "AliCDBManager.h"
18ada816 41#include "AliPID.h"
a1408c4b 42#include "TTree.h"
43#include "TList.h"
9a9467ed 44#include "AliHLTESDCaloClusterMaker.h"
45#include "AliHLTCaloClusterDataStruct.h"
46#include "AliHLTCaloClusterReader.h"
47#include "AliESDCaloCluster.h"
a1408c4b 48
49/** ROOT macro for the implementation of ROOT specific class methods */
50ClassImp(AliHLTGlobalEsdConverterComponent)
51
52AliHLTGlobalEsdConverterComponent::AliHLTGlobalEsdConverterComponent()
53 : AliHLTProcessor()
a1408c4b 54 , fWriteTree(0)
093b64dc 55 , fVerbosity(0)
8b7a0f3c 56 , fESD(NULL)
a8714ffa 57 , fSolenoidBz(-5.00668)
a1408c4b 58{
59 // see header file for class documentation
60 // or
61 // refer to README to build package
62 // or
63 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
64}
65
66AliHLTGlobalEsdConverterComponent::~AliHLTGlobalEsdConverterComponent()
67{
68 // see header file for class documentation
69 if (fESD) delete fESD;
70 fESD=NULL;
71}
72
73int AliHLTGlobalEsdConverterComponent::Configure(const char* arguments)
74{
75 // see header file for class documentation
76 int iResult=0;
77 if (!arguments) return iResult;
78
79 TString allArgs=arguments;
80 TString argument;
81 int bMissingParam=0;
82
83 TObjArray* pTokens=allArgs.Tokenize(" ");
84 if (pTokens) {
85 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
fe0af255 86 argument=((TObjString*)pTokens->At(i))->GetString();
a1408c4b 87 if (argument.IsNull()) continue;
88
89 if (argument.CompareTo("-solenoidBz")==0) {
90 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
91 HLTInfo("Magnetic Field set to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
92 fSolenoidBz=((TObjString*)pTokens->At(i))->GetString().Atof();
93 continue;
94 } else {
95 HLTError("unknown argument %s", argument.Data());
96 iResult=-EINVAL;
97 break;
98 }
99 }
100 delete pTokens;
101 }
102 if (bMissingParam) {
103 HLTError("missing parameter for argument %s", argument.Data());
104 iResult=-EINVAL;
105 }
106
107 return iResult;
108}
109
110int AliHLTGlobalEsdConverterComponent::Reconfigure(const char* cdbEntry, const char* chainId)
111{
112 // see header file for class documentation
113 int iResult=0;
114 const char* path=kAliHLTCDBSolenoidBz;
115 const char* defaultNotify="";
116 if (cdbEntry) {
117 path=cdbEntry;
118 defaultNotify=" (default)";
119 }
120 if (path) {
121 HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
122 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
123 if (pEntry) {
124 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
125 if (pString) {
126 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
127 iResult=Configure(pString->GetString().Data());
128 } else {
129 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
130 }
131 } else {
132 HLTError("can not fetch object \"%s\" from CDB", path);
133 }
134 }
135
136 return iResult;
137}
138
139void AliHLTGlobalEsdConverterComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
140{
141 // see header file for class documentation
142 list.push_back(kAliHLTDataTypeTrack);
143 list.push_back(kAliHLTDataTypeTrackMC);
9a9467ed 144 list.push_back(kAliHLTDataTypeCaloCluster);
0973c527 145 list.push_back(kAliHLTDataTypedEdx );
7f167a74 146 list.push_back(kAliHLTDataTypeESDVertex );
a1408c4b 147}
148
149AliHLTComponentDataType AliHLTGlobalEsdConverterComponent::GetOutputDataType()
150{
151 // see header file for class documentation
7a9de6d1 152 return kAliHLTDataTypeESDObject|kAliHLTDataOriginOut;
a1408c4b 153}
154
155void AliHLTGlobalEsdConverterComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
156{
157 // see header file for class documentation
158 constBase=2000000;
159 inputMultiplier=10.0;
160}
161
162int AliHLTGlobalEsdConverterComponent::DoInit(int argc, const char** argv)
163{
164 // see header file for class documentation
165 int iResult=0;
166 TString argument="";
167 int bMissingParam=0;
168 for (int i=0; i<argc && iResult>=0; i++) {
169 argument=argv[i];
170 if (argument.IsNull()) continue;
171
172 // -notree
173 if (argument.CompareTo("-notree")==0) {
174 fWriteTree=0;
175
176 // -tree
177 } else if (argument.CompareTo("-tree")==0) {
178 fWriteTree=1;
a1408c4b 179 } else {
180 HLTError("unknown argument %s", argument.Data());
181 break;
182 }
183 }
184 if (bMissingParam) {
185 HLTError("missing parameter for argument %s", argument.Data());
186 iResult=-EINVAL;
187 }
188
189 if (iResult>=0) {
190 iResult=Reconfigure(NULL, NULL);
191 }
192
193 if (iResult>=0) {
194 fESD = new AliESDEvent;
195 if (fESD) {
196 fESD->CreateStdContent();
197 } else {
198 iResult=-ENOMEM;
199 }
6117ef63 200
201 SetupCTPData();
a1408c4b 202 }
203
204 return iResult;
205}
206
207int AliHLTGlobalEsdConverterComponent::DoDeinit()
208{
209 // see header file for class documentation
210 if (fESD) delete fESD;
211 fESD=NULL;
212
213 return 0;
214}
215
216int AliHLTGlobalEsdConverterComponent::DoEvent(const AliHLTComponentEventData& /*evtData*/,
6117ef63 217 AliHLTComponentTriggerData& trigData)
a1408c4b 218{
219 // see header file for class documentation
220 int iResult=0;
221 if (!fESD) return -ENODEV;
222
223 AliESDEvent* pESD = fESD;
224
225 pESD->Reset();
226 pESD->SetMagneticField(fSolenoidBz);
6117ef63 227 pESD->SetRunNumber(GetRunNo());
6700298e 228 pESD->SetPeriodNumber(GetPeriodNumber());
229 pESD->SetOrbitNumber(GetOrbitNumber());
230 pESD->SetBunchCrossNumber(GetBunchCrossNumber());
231 pESD->SetTimeStamp(GetTimeStamp());
a1408c4b 232
6117ef63 233 const AliHLTCTPData* pCTPData=CTPData();
234 if (pCTPData) {
235 AliHLTUInt64_t mask=pCTPData->ActiveTriggers(trigData);
236 for (int index=0; index<gkNCTPTriggerClasses; index++) {
237 if ((mask&((AliHLTUInt64_t)0x1<<index)) == 0) continue;
238 pESD->SetTriggerClass(pCTPData->Name(index), index);
239 }
240 pESD->SetTriggerMask(mask);
241 }
242
a1408c4b 243 TTree* pTree = NULL;
244 if (fWriteTree)
245 pTree = new TTree("esdTree", "Tree with HLT ESD objects");
246
247 if (pTree) {
248 pTree->SetDirectory(0);
249 }
250
81509fd3 251 if ((iResult=ProcessBlocks(pTree, pESD))>=0) {
a1408c4b 252 // TODO: set the specification correctly
253 if (pTree) {
254 // the esd structure is written to the user info and is
255 // needed in te ReadFromTree method to read all objects correctly
256 pTree->GetUserInfo()->Add(pESD);
257 pESD->WriteToTree(pTree);
7a9de6d1 258 iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginOut, 0);
a1408c4b 259 } else {
7a9de6d1 260 iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOut, 0);
a1408c4b 261 }
262 }
263 if (pTree) {
264 // clear user info list to prevent objects from being deleted
265 pTree->GetUserInfo()->Clear();
266 delete pTree;
267 }
268 return iResult;
269}
270
271int AliHLTGlobalEsdConverterComponent::ProcessBlocks(TTree* pTree, AliESDEvent* pESD)
272{
273 // see header file for class documentation
274
275 int iResult=0;
276 int iAddedDataBlocks=0;
277
278 // Barrel tracking
279
280 // in the first attempt this component reads the TPC tracks and updates in the
281 // second step from the ITS tracks
093b64dc 282
283
284 // first read MC information (if present)
285 std::map<int,int> mcLabels;
286
287 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC);
288 pBlock!=NULL; pBlock=GetNextInputBlock()) {
289 AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
290 if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
291 for( unsigned int il=0; il<dataPtr->fCount; il++ ){
292 AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
293 mcLabels[lab.fTrackID] = lab.fMCLabel;
294 }
295 } else {
296 HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information",
297 DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification,
298 dataPtr->fCount, pBlock->fSize);
299 }
300 }
301
0973c527 302 // read dEdx information (if present)
303
304 AliHLTFloat32_t *dEdxTPC = 0;
305 Int_t ndEdxTPC = 0;
306 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypedEdx|kAliHLTDataOriginTPC);
307 pBlock!=NULL; pBlock=GetNextInputBlock()) {
308 dEdxTPC = reinterpret_cast<AliHLTFloat32_t*>( pBlock->fPtr );
309 ndEdxTPC = pBlock->fSize / sizeof(AliHLTFloat32_t);
310 break;
311 }
312
cd60a73f 313 // convert the TPC tracks to ESD tracks
a1408c4b 314 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginTPC);
315 pBlock!=NULL; pBlock=GetNextInputBlock()) {
316 vector<AliHLTGlobalBarrelTrack> tracks;
81509fd3 317 if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>=0) {
a1408c4b 318 for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
319 element!=tracks.end(); element++) {
320 Float_t points[4] = {
321 element->GetX(),
322 element->GetY(),
323 element->GetLastPointX(),
324 element->GetLastPointY()
325 };
093b64dc 326
327 Int_t mcLabel = -1;
328 if( mcLabels.find(element->TrackID())!=mcLabels.end() )
329 mcLabel = mcLabels[element->TrackID()];
330 element->SetLabel( mcLabel );
331
a1408c4b 332 AliESDtrack iotrack;
550ecaab 333
334 // for the moment, the number of clusters are not set when processing the
335 // kTPCin update, only at kTPCout
336 // there ar emainly three parameters updated for kTPCout
337 // number of clusters
338 // chi2
339 // pid signal
340 // The first one can be updated already at that stage here, while the two others
341 // eventually require to update from the ITS tracks before. The exact scheme
342 // needs to be checked
a1408c4b 343 iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCin);
550ecaab 344 iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCout);
a1408c4b 345 iotrack.SetTPCPoints(points);
0973c527 346 if( element->TrackID()<ndEdxTPC ){
7e32fedf 347 iotrack.SetTPCsignal( dEdxTPC[element->TrackID()], 0, 0 );
348 //AliTPCseed s;
349 //s.Set( element->GetX(), element->GetAlpha(),
350 //element->GetParameter(), element->GetCovariance() );
351 //s.SetdEdx( dEdxTPC[element->TrackID()] );
352 //s.CookPID();
353 //iotrack.SetTPCpid(s.TPCrPIDs() );
0973c527 354 } else {
355 if( dEdxTPC ) HLTWarning("Wrong number of dEdx TPC labels");
356 }
a1408c4b 357 pESD->AddTrack(&iotrack);
093b64dc 358 if (fVerbosity>0) element->Print();
a1408c4b 359 }
093b64dc 360 HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
a1408c4b 361 iAddedDataBlocks++;
362 } else if (iResult<0) {
093b64dc 363 HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d",
364 DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
a1408c4b 365 }
366 }
367
7f167a74 368
369 // Get ITS SPD vertex
370
371 for ( const TObject *iter = GetFirstInputObject(kAliHLTDataTypeESDVertex|kAliHLTDataOriginITS); iter != NULL; iter = GetNextInputObject() ) {
372 AliESDVertex *vtx = dynamic_cast<AliESDVertex*>(const_cast<TObject*>( iter ) );
373 pESD->SetPrimaryVertexSPD( vtx );
374 }
375
cd60a73f 376 // now update ESD tracks with the ITS info
af3250a3 377 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginITS);
cd60a73f 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++;
a1408c4b 389 }
cd60a73f 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 );
a1408c4b 398 }
cd60a73f 399 }
400 }
18ada816 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 }
9a9467ed 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 }
a1408c4b 448
4d5ee3db 449
a1408c4b 450 if (iAddedDataBlocks>0 && pTree) {
cd60a73f 451 pTree->Fill();
a1408c4b 452 }
453
454 if (iResult>=0) iResult=iAddedDataBlocks;
455 return iResult;
456}