deltas type modified, getdeltas added.
[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"
9334b5cb 48#include "AliHLTVertexer.h"
7e32fedf 49//#include "AliTPCseed.h"
a1408c4b 50
51/** ROOT macro for the implementation of ROOT specific class methods */
52ClassImp(AliHLTGlobalEsdConverterComponent)
53
54AliHLTGlobalEsdConverterComponent::AliHLTGlobalEsdConverterComponent()
55 : AliHLTProcessor()
a1408c4b 56 , fWriteTree(0)
093b64dc 57 , fVerbosity(0)
8b7a0f3c 58 , fESD(NULL)
a8714ffa 59 , fSolenoidBz(-5.00668)
36d0f159 60 , fFillVtxConstrainedTracks( 0 )
a1408c4b 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
69AliHLTGlobalEsdConverterComponent::~AliHLTGlobalEsdConverterComponent()
70{
71 // see header file for class documentation
72 if (fESD) delete fESD;
73 fESD=NULL;
74}
75
76int 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;
36d0f159 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;
a1408c4b 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
119int 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
148void AliHLTGlobalEsdConverterComponent::GetInputDataTypes(AliHLTComponentDataTypeList& list)
149{
150 // see header file for class documentation
151 list.push_back(kAliHLTDataTypeTrack);
152 list.push_back(kAliHLTDataTypeTrackMC);
9a9467ed 153 list.push_back(kAliHLTDataTypeCaloCluster);
0973c527 154 list.push_back(kAliHLTDataTypedEdx );
a1408c4b 155}
156
157AliHLTComponentDataType AliHLTGlobalEsdConverterComponent::GetOutputDataType()
158{
159 // see header file for class documentation
7a9de6d1 160 return kAliHLTDataTypeESDObject|kAliHLTDataOriginOut;
a1408c4b 161}
162
163void AliHLTGlobalEsdConverterComponent::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
164{
165 // see header file for class documentation
166 constBase=2000000;
167 inputMultiplier=10.0;
168}
169
170int 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 }
6117ef63 209
210 SetupCTPData();
a1408c4b 211 }
212
213 return iResult;
214}
215
216int AliHLTGlobalEsdConverterComponent::DoDeinit()
217{
218 // see header file for class documentation
219 if (fESD) delete fESD;
220 fESD=NULL;
221
222 return 0;
223}
224
225int AliHLTGlobalEsdConverterComponent::DoEvent(const AliHLTComponentEventData& /*evtData*/,
6117ef63 226 AliHLTComponentTriggerData& trigData)
a1408c4b 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);
6117ef63 236 pESD->SetRunNumber(GetRunNo());
6700298e 237 pESD->SetPeriodNumber(GetPeriodNumber());
238 pESD->SetOrbitNumber(GetOrbitNumber());
239 pESD->SetBunchCrossNumber(GetBunchCrossNumber());
240 pESD->SetTimeStamp(GetTimeStamp());
a1408c4b 241
6117ef63 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
a1408c4b 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
81509fd3 260 if ((iResult=ProcessBlocks(pTree, pESD))>=0) {
a1408c4b 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);
7a9de6d1 267 iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginOut, 0);
a1408c4b 268 } else {
7a9de6d1 269 iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOut, 0);
a1408c4b 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
280int 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
093b64dc 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
0973c527 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
cd60a73f 322 // convert the TPC tracks to ESD tracks
a1408c4b 323 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginTPC);
324 pBlock!=NULL; pBlock=GetNextInputBlock()) {
325 vector<AliHLTGlobalBarrelTrack> tracks;
81509fd3 326 if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>=0) {
a1408c4b 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 };
093b64dc 335
336 Int_t mcLabel = -1;
337 if( mcLabels.find(element->TrackID())!=mcLabels.end() )
338 mcLabel = mcLabels[element->TrackID()];
339 element->SetLabel( mcLabel );
340
a1408c4b 341 AliESDtrack iotrack;
550ecaab 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
a1408c4b 352 iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCin);
550ecaab 353 iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTPCout);
a1408c4b 354 iotrack.SetTPCPoints(points);
0973c527 355 if( element->TrackID()<ndEdxTPC ){
7e32fedf 356 iotrack.SetTPCsignal( dEdxTPC[element->TrackID()], 0, 0 );
357 //AliTPCseed s;
358 //s.Set( element->GetX(), element->GetAlpha(),
359 //element->GetParameter(), element->GetCovariance() );
360 //s.SetdEdx( dEdxTPC[element->TrackID()] );
361 //s.CookPID();
362 //iotrack.SetTPCpid(s.TPCrPIDs() );
0973c527 363 } else {
364 if( dEdxTPC ) HLTWarning("Wrong number of dEdx TPC labels");
365 }
a1408c4b 366 pESD->AddTrack(&iotrack);
093b64dc 367 if (fVerbosity>0) element->Print();
a1408c4b 368 }
093b64dc 369 HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
a1408c4b 370 iAddedDataBlocks++;
371 } else if (iResult<0) {
093b64dc 372 HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d",
373 DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
a1408c4b 374 }
375 }
376
cd60a73f 377 // now update ESD tracks with the ITS info
af3250a3 378 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack|kAliHLTDataOriginITS);
cd60a73f 379 pBlock!=NULL; pBlock=GetNextInputBlock()) {
380 vector<AliHLTGlobalBarrelTrack> tracks;
381 if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
382 for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
383 element!=tracks.end(); element++) {
384 int ncl=0;
385 const UInt_t* pointsArray=element->GetPoints();
386 for( unsigned il=0; il<element->GetNumberOfPoints(); il++ ){
387 // TODO: check what needs to be done with the clusters
388 if( pointsArray[il]<~(UInt_t)0 ) {/*tITS.SetClusterIndex(ncl, tr.fClusterIds[il]);*/}
389 ncl++;
a1408c4b 390 }
cd60a73f 391 //tITS.SetNumberOfClusters( ncl );
392 int tpcID=element->TrackID();
393 // the ITS tracker assigns the TPC track used as seed for a certain track to
394 // the trackID
395 if( tpcID<0 || tpcID>=pESD->GetNumberOfTracks()) continue;
396
397 AliESDtrack *tESD = pESD->GetTrack( tpcID );
398 if( tESD ) tESD->UpdateTrackParams( &(*element), AliESDtrack::kITSin );
a1408c4b 399 }
cd60a73f 400 }
401 }
18ada816 402
403 // convert the HLT TRD tracks to ESD tracks
404 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrack | kAliHLTDataOriginTRD);
405 pBlock!=NULL; pBlock=GetNextInputBlock()) {
406 vector<AliHLTGlobalBarrelTrack> tracks;
407 if ((iResult=AliHLTGlobalBarrelTrack::ConvertTrackDataArray(reinterpret_cast<const AliHLTTracksData*>(pBlock->fPtr), pBlock->fSize, tracks))>0) {
408 for (vector<AliHLTGlobalBarrelTrack>::iterator element=tracks.begin();
409 element!=tracks.end(); element++) {
410
411 Double_t TRDpid[AliPID::kSPECIES], eProb(0.2), restProb((1-eProb)/(AliPID::kSPECIES-1)); //eprob(element->GetTRDpid...);
412 for(Int_t i=0; i<AliPID::kSPECIES; i++){
413 switch(i){
414 case AliPID::kElectron: TRDpid[AliPID::kElectron]=eProb; break;
415 default: TRDpid[i]=restProb; break;
416 }
417 }
418
419 AliESDtrack iotrack;
420 iotrack.UpdateTrackParams(&(*element),AliESDtrack::kTRDin);
421 iotrack.SetTRDpid(TRDpid);
422
423 pESD->AddTrack(&iotrack);
424 if (fVerbosity>0) element->Print();
425 }
426 HLTInfo("converted %d track(s) to AliESDtrack and added to ESD", tracks.size());
427 iAddedDataBlocks++;
428 } else if (iResult<0) {
429 HLTError("can not extract tracks from data block of type %s (specification %08x) of size %d: error %d",
430 DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, pBlock->fSize, iResult);
431 }
432 }
9a9467ed 433 for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeCaloCluster | kAliHLTDataOriginPHOS); pBlock!=NULL; pBlock=GetNextInputBlock())
434 {
435 AliHLTCaloClusterHeaderStruct *caloClusterHeaderPtr = reinterpret_cast<AliHLTCaloClusterHeaderStruct*>(pBlock->fPtr);
436
437 HLTDebug("%d HLT clusters from spec: 0x%X", caloClusterHeaderPtr->fNClusters, pBlock->fSpecification);
438
439 AliHLTCaloClusterReader reader;
440 reader.SetMemory(caloClusterHeaderPtr);
441
442 AliHLTESDCaloClusterMaker clusterMaker;
443
444 int nClusters = clusterMaker.FillESD(pESD, caloClusterHeaderPtr);
445
446 HLTInfo("converted %d cluster(s) to AliESDCaloCluster and added to ESD", nClusters);
447 iAddedDataBlocks++;
448 }
a1408c4b 449
450 // primary vertex & V0's
9334b5cb 451
452 AliHLTVertexer vertexer;
453 vertexer.SetESD( pESD );
454 vertexer.FindPrimaryVertex();
455 vertexer.FindV0s();
36d0f159 456
457 // relate the tracks to vertex
458 if( fFillVtxConstrainedTracks ){
459 for( Int_t i = 0; i<pESD->GetNumberOfTracks(); i++){
460 if( !vertexer.TrackInfos()[i].fPrimUsedFlag ) continue;
461 pESD->GetTrack(i)->RelateToVertex( pESD->GetPrimaryVertex(), fESD->GetMagneticField(),5. );
462 }
463 }
9334b5cb 464
a1408c4b 465 if (iAddedDataBlocks>0 && pTree) {
cd60a73f 466 pTree->Fill();
a1408c4b 467 }
468
469 if (iResult>=0) iResult=iAddedDataBlocks;
470 return iResult;
471}