- workaround for copying and merging of ESDs: The HLTOUT contains ESDs for every...
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCEsdWriterComponent.cxx
CommitLineData
3cde846d 1// @(#) $Id$
2
c5123824 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//**************************************************************************
2a083ac4 18
3cde846d 19/** @file AliHLTTPCEsdWriterComponent.cxx
20 @author Matthias Richter
21 @date
22 @brief Writer component to store tracks of the HLT TPC conformal
23 mapping tracker in the AliESD format
c5123824 24*/
3cde846d 25
f32b83e1 26// see header file for class documentation
27// or
28// refer to README to build package
29// or
30// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
31
37513ae4 32#include <cassert>
3cde846d 33#include "AliHLTTPCEsdWriterComponent.h"
af885e0f 34#include "AliESDEvent.h"
d523f52c 35#include "AliESDtrack.h"
90c37647 36#include "AliCDBEntry.h"
37#include "AliCDBManager.h"
3cde846d 38#include "TTree.h"
62ff1e23 39#include "TList.h"
3cde846d 40#include "AliHLTTPCTrack.h"
41#include "AliHLTTPCTrackArray.h"
42#include "AliHLTTPCTrackletDataFormat.h"
43#include "AliHLTTPCDefinitions.h"
37513ae4 44#include "AliHLTTPCTransform.h"
3cde846d 45
3cde846d 46/** ROOT macro for the implementation of ROOT specific class methods */
47ClassImp(AliHLTTPCEsdWriterComponent)
48
49AliHLTTPCEsdWriterComponent::AliHLTTPCEsdWriterComponent()
90c37647 50 :
51 fSolenoidBz(0)
37513ae4 52{
53 // see header file for class documentation
54 // or
55 // refer to README to build package
56 // or
57 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58}
59
60AliHLTTPCEsdWriterComponent::~AliHLTTPCEsdWriterComponent()
61{
62 // see header file for class documentation
63}
64
65AliHLTTPCEsdWriterComponent::AliWriter::AliWriter()
3cde846d 66 :
67 fTree(NULL),
37513ae4 68 fESD(NULL),
69 fBase(new AliHLTTPCEsdWriterComponent)
3cde846d 70{
2a083ac4 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
3cde846d 76}
77
37513ae4 78AliHLTTPCEsdWriterComponent::AliWriter::~AliWriter()
79{
80 // see header file for class documentation
81 if (fBase) delete fBase;
82 fBase=NULL;
83}
84
85void AliHLTTPCEsdWriterComponent::AliWriter::GetInputDataTypes(AliHLTComponentDataTypeList& list)
3cde846d 86{
2a083ac4 87 // see header file for class documentation
37513ae4 88 list.push_back(AliHLTTPCDefinitions::fgkTrackSegmentsDataType);
8f471af0 89 list.push_back(AliHLTTPCDefinitions::fgkTracksDataType);
3cde846d 90}
91
37513ae4 92int AliHLTTPCEsdWriterComponent::AliWriter::InitWriter()
3cde846d 93{
2a083ac4 94 // see header file for class documentation
3cde846d 95 int iResult=0;
af885e0f 96 fESD = new AliESDEvent;
3cde846d 97 if (fESD) {
76e61c6e 98 fESD->CreateStdContent();
3cde846d 99 fTree = new TTree("esdTree", "Tree with HLT ESD objects");
100 if (fTree) {
68f62aaa 101 fTree->SetDirectory(0);
af885e0f 102 fESD->WriteToTree(fTree);
3cde846d 103 }
3cde846d 104 }
105 if (fTree==NULL) {
106 iResult=-ENOMEM;
107 }
90c37647 108
109 if (iResult>=0) {
110 iResult=fBase->Reconfigure(NULL, NULL);
111 }
112
3cde846d 113 return iResult;
114}
115
37513ae4 116int AliHLTTPCEsdWriterComponent::AliWriter::CloseWriter()
3cde846d 117{
2a083ac4 118 // see header file for class documentation
3cde846d 119 int iResult=0;
120 if (fTree) {
62ff1e23 121 // the esd structure is written to the user info and is
122 // needed in te ReadFromTree method to read all objects correctly
123 if (fESD) fTree->GetUserInfo()->Add(fESD);
3cde846d 124 WriteObject(kAliHLTVoidEventID, fTree);
62ff1e23 125 fTree->GetUserInfo()->Clear();
3cde846d 126 TTree* pTree=fTree;
127 fTree=NULL;
128 delete pTree;
129 } else {
130 HLTWarning("not initialized");
131 }
62ff1e23 132
133 if (fESD) {
134 delete fESD;
135 }
2a083ac4 136 iResult=AliHLTRootFileWriterComponent::CloseWriter();
137 return iResult;
3cde846d 138}
139
37513ae4 140int AliHLTTPCEsdWriterComponent::AliWriter::DumpEvent( const AliHLTComponentEventData& evtData,
3cde846d 141 const AliHLTComponentBlockData* blocks,
5d2abf3b 142 AliHLTComponentTriggerData& /*trigData*/ )
3cde846d 143{
2a083ac4 144 // see header file for class documentation
3cde846d 145 int iResult=0;
146 TTree* pTree=fTree;
37513ae4 147 assert(fBase);
148 if (pTree && fBase) {
3cde846d 149 if (fESD) {
af885e0f 150 AliESDEvent* pESD=fESD;
3cde846d 151
37513ae4 152 iResult=fBase->ProcessBlocks(pTree, pESD, blocks, (int)evtData.fBlockCnt);
153
154 } else {
155 iResult=-ENOMEM;
156 }
157 }
158 return iResult;
159}
160
161int AliHLTTPCEsdWriterComponent::AliWriter::ScanArgument(int argc, const char** argv)
162{
163 // see header file for class documentation
164 int iResult=AliHLTRootFileWriterComponent::ScanArgument(argc, argv);
165 return iResult;
166}
167
168int AliHLTTPCEsdWriterComponent::ProcessBlocks(TTree* pTree, AliESDEvent* pESD,
169 const AliHLTComponentBlockData* blocks,
170 int nBlocks, int* pMinSlice,
171 int* pMaxSlice)
172{
173 // see header file for class documentation
174 int iResult=0;
a978c0d5 175 if (pESD && blocks) {
90c37647 176 pESD->SetMagneticField(fSolenoidBz);
3cde846d 177 const AliHLTComponentBlockData* iter = NULL;
3cde846d 178 AliHLTTPCTrackletData* inPtr=NULL;
de554e07 179 int bIsTrackSegs=0;
3cde846d 180
37513ae4 181 for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
3cde846d 182 iter = blocks+ndx;
96bda103 183 if ( (bIsTrackSegs=(iter->fDataType == AliHLTTPCDefinitions::fgkTrackSegmentsDataType))==1 ||
184 iter->fDataType == AliHLTTPCDefinitions::fgkTracksDataType ) {
4fdaad1e 185 Int_t minslice=AliHLTTPCDefinitions::GetMinSliceNr(iter->fSpecification);
186 Int_t maxslice=AliHLTTPCDefinitions::GetMaxSliceNr(iter->fSpecification);
de554e07 187 if (bIsTrackSegs==0) {
188 // slice parameter and data specification ignored, tracks already in global coordinates
b09663cd 189 minslice=-1;
190 maxslice=-1;
37513ae4 191 if (pMinSlice) *pMinSlice=0;
192 if (pMaxSlice) *pMaxSlice=AliHLTTPCTransform::GetNSlice()-1;
193 } else {
194 if (pMinSlice && (*pMinSlice==-1 || *pMinSlice>minslice)) *pMinSlice=minslice;
195 if (pMaxSlice && (*pMaxSlice==-1 || *pMaxSlice<maxslice)) *pMaxSlice=maxslice;
de554e07 196 }
4fdaad1e 197 //HLTDebug("dataspec %#x minslice %d", iter->fSpecification, minslice);
37513ae4 198 if (minslice >=-1 && minslice<AliHLTTPCTransform::GetNSlice()) {
4fdaad1e 199 if (minslice!=maxslice) {
200 HLTWarning("data from multiple sectors in one block: "
c5123824 201 "possible mismatch in treatment of local coordinate system");
4fdaad1e 202 }
203 AliHLTTPCTrackArray tracks;
204 inPtr=(AliHLTTPCTrackletData*)iter->fPtr;
205 HLTDebug("reading block %d (slice %d): %d tracklets", ndx, minslice, inPtr->fTrackletCnt);
206 tracks.FillTracks(inPtr->fTrackletCnt, inPtr->fTracklets, minslice, 0/*don't rotate*/);
207 if ((iResult=Tracks2ESD(&tracks, pESD))>=0) {
3cde846d 208 }
209 } else {
4fdaad1e 210 HLTError("invalid sector number");
211 iResult=-EBADF;
3cde846d 212 }
213 }
214 }
a978c0d5 215 if (iResult>=0 && pTree) {
4fdaad1e 216 pTree->Fill();
217 }
3cde846d 218
37513ae4 219 pESD->Reset();
220
221 } else {
222 iResult=-EINVAL;
3cde846d 223 }
224 return iResult;
225}
226
af885e0f 227int AliHLTTPCEsdWriterComponent::Tracks2ESD(AliHLTTPCTrackArray* pTracks, AliESDEvent* pESD)
3cde846d 228{
2a083ac4 229 // see header file for class documentation
3cde846d 230 int iResult=0;
231 if (pTracks && pESD) {
232 HLTDebug("converting %d tracks from track array", pTracks->GetNTracks());
233 for (int i=0; i<pTracks->GetNTracks() && iResult>=0; i++) {
234 AliHLTTPCTrack* pTrack=(*pTracks)[i];
235 if (pTrack) {
04dbc9e4 236 //HLTDebug("convert track %d", i);
237 //pTrack->Print();
3cde846d 238 int iLocal=pTrack->Convert2AliKalmanTrack();
239 if (iLocal>=0) {
240 AliESDtrack iotrack;
241 iotrack.UpdateTrackParams(pTrack,AliESDtrack::kTPCin);
242 iotrack.SetTPCPoints(pTrack->GetPoints());
243 pESD->AddTrack(&iotrack);
244 } else {
245 HLTError("conversion to AliKalmanTrack failed for track %d of %d", i, pTracks->GetNTracks());
246 }
247 } else {
c5123824 248 HLTError("internal mismatch in array");
3cde846d 249 iResult=-EFAULT;
250 }
251 }
252
253 } else {
254 iResult=-EINVAL;
255 }
256 return iResult;
257}
37513ae4 258
90c37647 259int AliHLTTPCEsdWriterComponent::Configure(const char* arguments)
260{
261 // see header file for class documentation
262 int iResult=0;
263 if (!arguments) return iResult;
264
265 TString allArgs=arguments;
266 TString argument;
267 int bMissingParam=0;
268
269 TObjArray* pTokens=allArgs.Tokenize(" ");
270 if (pTokens) {
271 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
272 argument=((TObjString*)pTokens->At(i))->GetString();
273 if (argument.IsNull()) continue;
274
275 if (argument.CompareTo("-solenoidBz")==0) {
276 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
277 HLTInfo("Magnetic Field set to: %s", ((TObjString*)pTokens->At(i))->GetString().Data());
278 fSolenoidBz=((TObjString*)pTokens->At(i))->GetString().Atof();
279 continue;
280 } else {
281 HLTError("unknown argument %s", argument.Data());
282 iResult=-EINVAL;
283 break;
284 }
285 }
286 delete pTokens;
287 }
288 if (bMissingParam) {
289 HLTError("missing parameter for argument %s", argument.Data());
290 iResult=-EINVAL;
291 }
292
293 return iResult;
294}
295
296int AliHLTTPCEsdWriterComponent::Reconfigure(const char* cdbEntry, const char* chainId)
297{
298 // see header file for class documentation
299 int iResult=0;
300 const char* path="HLT/ConfigHLT/SolenoidBz";
301 const char* defaultNotify="";
302 if (cdbEntry) {
303 path=cdbEntry;
304 defaultNotify=" (default)";
305 }
306 if (path) {
307 HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
308 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
309 if (pEntry) {
310 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
311 if (pString) {
312 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
313 iResult=Configure(pString->GetString().Data());
314 } else {
315 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
316 }
317 } else {
318 HLTError("can not fetch object \"%s\" from CDB", path);
319 }
320 }
321
322 return iResult;
323}
324
37513ae4 325AliHLTTPCEsdWriterComponent::AliConverter::AliConverter()
326 :
a978c0d5 327 fBase(new AliHLTTPCEsdWriterComponent),
328 fWriteTree(1)
37513ae4 329{
330 // see header file for class documentation
331 // or
332 // refer to README to build package
333 // or
334 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
335}
336
337AliHLTTPCEsdWriterComponent::AliConverter::~AliConverter()
338{
339 // see header file for class documentation
340 if (fBase) delete fBase;
341 fBase=NULL;
342}
343
344void AliHLTTPCEsdWriterComponent::AliConverter::GetInputDataTypes(AliHLTComponentDataTypeList& list)
345{
346 // see header file for class documentation
347 list.push_back(AliHLTTPCDefinitions::fgkTrackSegmentsDataType);
8f471af0 348 list.push_back(AliHLTTPCDefinitions::fgkTracksDataType);
37513ae4 349}
350
351AliHLTComponentDataType AliHLTTPCEsdWriterComponent::AliConverter::GetOutputDataType()
352{
353 // see header file for class documentation
354 return kAliHLTDataTypeESDTree;
355}
356
357void AliHLTTPCEsdWriterComponent::AliConverter::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
358{
359 // see header file for class documentation
62ff1e23 360 constBase=2000000;
361 inputMultiplier=10.0;
37513ae4 362}
363
364int AliHLTTPCEsdWriterComponent::AliConverter::DoInit(int argc, const char** argv)
365{
366 // see header file for class documentation
a978c0d5 367 int iResult=0;
368 TString argument="";
369 int bMissingParam=0;
370 for (int i=0; i<argc && iResult>=0; i++) {
371 argument=argv[i];
372 if (argument.IsNull()) continue;
373
374 // -notree
375 if (argument.CompareTo("-notree")==0) {
376 fWriteTree=0;
377
378 // -tree
379 } else if (argument.CompareTo("-tree")==0) {
380 fWriteTree=1;
381
382 } else {
383 HLTError("unknown argument %s", argument.Data());
384 break;
385 }
386 }
387 if (bMissingParam) {
388 HLTError("missing parameter for argument %s", argument.Data());
389 iResult=-EINVAL;
390 }
391
90c37647 392 if (iResult>=0) {
393 iResult=fBase->Reconfigure(NULL, NULL);
394 }
395
a978c0d5 396 return iResult;
37513ae4 397}
398
399int AliHLTTPCEsdWriterComponent::AliConverter::DoDeinit()
400{
401 // see header file for class documentation
402 return 0;
403}
404
405int AliHLTTPCEsdWriterComponent::AliConverter::DoEvent(const AliHLTComponentEventData& evtData,
406 const AliHLTComponentBlockData* blocks,
407 AliHLTComponentTriggerData& /*trigData*/,
408 AliHLTUInt8_t* /*outputPtr*/,
409 AliHLTUInt32_t& /*size*/,
410 AliHLTComponentBlockDataList& /*outputBlocks*/ )
411{
412 // see header file for class documentation
413 int iResult=0;
414 assert(fBase);
415 AliESDEvent* pESD = new AliESDEvent;
416 if (pESD && fBase) {
417 pESD->CreateStdContent();
a978c0d5 418 TTree* pTree = NULL;
419 // TODO: Matthias 06.12.2007
420 // Tried to write the ESD directly instead to a tree, but this did not work
421 // out. Information in the ESD is different, needs investigation.
422 if (fWriteTree)
423 pTree = new TTree("esdTree", "Tree with HLT ESD objects");
37513ae4 424 if (pTree) {
68f62aaa 425 pTree->SetDirectory(0);
37513ae4 426 pESD->WriteToTree(pTree);
a978c0d5 427 }
37513ae4 428
a978c0d5 429 if ((iResult=fBase->ProcessBlocks(pTree, pESD, blocks, (int)evtData.fBlockCnt))>=0) {
37513ae4 430 // TODO: set the specification correctly
62ff1e23 431 if (pTree) {
432 // the esd structure is written to the user info and is
433 // needed in te ReadFromTree method to read all objects correctly
434 pTree->GetUserInfo()->Add(pESD);
37513ae4 435 iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC, 0);
62ff1e23 436 } else {
a978c0d5 437 iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginTPC, 0);
62ff1e23 438 }
37513ae4 439 }
62ff1e23 440 if (pTree) {
441 // clear user info list to prevent objects from being deleted
442 pTree->GetUserInfo()->Clear();
a978c0d5 443 delete pTree;
62ff1e23 444 }
a978c0d5 445
37513ae4 446 delete pESD;
447 }
448 return iResult;
449}
450