]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCEsdWriterComponent.cxx
removing deprecated functionality
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCEsdWriterComponent.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   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
24 */
25
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
32 #include <cassert>
33 #include "AliHLTTPCEsdWriterComponent.h"
34 #include "AliESDEvent.h"
35 #include "AliESDtrack.h"
36 #include "AliCDBEntry.h"
37 #include "AliCDBManager.h"
38 #include "TTree.h"
39 #include "TList.h"
40 #include "AliHLTTPCTrack.h"
41 #include "AliHLTTPCTrackArray.h"
42 #include "AliHLTTPCTrackletDataFormat.h"
43 #include "AliHLTTPCDefinitions.h"
44 #include "AliHLTTPCTransform.h"
45 #include "AliHLTExternalTrackParam.h"
46 #include "AliHLTGlobalBarrelTrack.h"
47 #include "AliHLTTrackMCLabel.h"
48 #include "TGeoGlobalMagField.h"
49
50 #include <vector>
51
52 /** ROOT macro for the implementation of ROOT specific class methods */
53 ClassImp(AliHLTTPCEsdWriterComponent)
54
55 AliHLTTPCEsdWriterComponent::AliHLTTPCEsdWriterComponent()
56   :
57   fSolenoidBz(0)
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
66 AliHLTTPCEsdWriterComponent::~AliHLTTPCEsdWriterComponent()
67 {
68   // see header file for class documentation
69 }
70
71 AliHLTTPCEsdWriterComponent::AliWriter::AliWriter()
72   :
73   fTree(NULL),
74   fESD(NULL),
75   fBase(new AliHLTTPCEsdWriterComponent)
76 {
77   // see header file for class documentation
78   // or
79   // refer to README to build package
80   // or
81   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
82 }
83
84 AliHLTTPCEsdWriterComponent::AliWriter::~AliWriter()
85 {
86   // see header file for class documentation
87   if (fBase) delete fBase;
88   fBase=NULL;
89 }
90
91 void AliHLTTPCEsdWriterComponent::AliWriter::GetInputDataTypes(AliHLTComponentDataTypeList& list)
92 {
93   // see header file for class documentation
94   list.push_back(AliHLTTPCDefinitions::fgkTrackSegmentsDataType);
95   list.push_back(AliHLTTPCDefinitions::fgkTracksDataType);
96   list.push_back( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC );
97 }
98
99 int AliHLTTPCEsdWriterComponent::AliWriter::InitWriter()
100 {
101   // see header file for class documentation
102   int iResult=0;
103   fESD = new AliESDEvent;
104   if (fESD) {
105     fESD->CreateStdContent();
106     fTree = new TTree("esdTree", "Tree with HLT ESD objects");
107     if (fTree) {
108       fTree->SetDirectory(0);
109       fESD->WriteToTree(fTree);
110     }
111   }
112   if (fTree==NULL) {
113     iResult=-ENOMEM;
114   }
115
116   if (iResult>=0) {
117     iResult=fBase->Reconfigure(NULL, NULL);
118   }
119
120   return iResult;
121 }
122
123 int AliHLTTPCEsdWriterComponent::AliWriter::CloseWriter()
124 {
125   // see header file for class documentation
126   int iResult=0;
127   if (fTree) {
128     // the esd structure is written to the user info and is
129     // needed in te ReadFromTree method to read all objects correctly
130     if (fESD) fTree->GetUserInfo()->Add(fESD);
131     WriteObject(kAliHLTVoidEventID, fTree);
132     fTree->GetUserInfo()->Clear();
133     TTree* pTree=fTree;
134     fTree=NULL;
135     delete pTree;
136   } else {
137     HLTWarning("not initialized");
138   }
139
140   if (fESD) {
141     delete fESD;
142   }
143   iResult=AliHLTRootFileWriterComponent::CloseWriter();
144   return iResult;
145 }
146
147 int AliHLTTPCEsdWriterComponent::AliWriter::DumpEvent( const AliHLTComponentEventData& evtData,
148                                             const AliHLTComponentBlockData* blocks, 
149                                             AliHLTComponentTriggerData& /*trigData*/ )
150 {
151   // see header file for class documentation
152   int iResult=0;
153   TTree* pTree=fTree;
154   assert(fBase);
155   if (pTree && fBase) {
156     if (fESD) {
157       AliESDEvent* pESD=fESD;
158
159       pESD->Reset(); 
160       pESD->SetMagneticField(GetBz());
161       iResult=fBase->ProcessBlocks(pTree, pESD, blocks, (int)evtData.fBlockCnt);
162
163     } else {
164       iResult=-ENOMEM;
165     }
166   }
167   return iResult;
168 }
169
170 int AliHLTTPCEsdWriterComponent::AliWriter::ScanArgument(int argc, const char** argv)
171 {
172   // see header file for class documentation
173   int iResult=AliHLTRootFileWriterComponent::ScanArgument(argc, argv);
174   return iResult;
175 }
176
177 int AliHLTTPCEsdWriterComponent::ProcessBlocks(TTree* pTree, AliESDEvent* pESD,
178                                                const AliHLTComponentBlockData* blocks,
179                                                int nBlocks, int* pMinSlice,
180                                                int* pMaxSlice)
181 {
182   // see header file for class documentation
183
184   int iResult=0;
185   int iAddedDataBlocks=0;
186   
187   if (pESD && blocks) {
188       const AliHLTComponentBlockData* iter = NULL;
189       int bIsTrackSegs=0;
190
191       // first read MC information (if present)
192
193       std::map<int,int> mcLabels;
194
195       for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
196         iter = blocks+ndx;
197         if(iter->fDataType == (kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC) ) {
198           AliHLTTrackMCData* dataPtr = ( AliHLTTrackMCData* )( iter->fPtr );
199           for( unsigned int il=0; il<dataPtr->fCount; il++ ){
200             AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
201             mcLabels[lab.fTrackID] = lab.fMCLabel;
202           }
203         }
204       }
205
206
207       // do the conversion of tracks
208
209       for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
210         iter = blocks+ndx;
211         
212         if( iter->fDataType == ( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC ) ){   
213           AliHLTTracksData* dataPtr = ( AliHLTTracksData* ) iter->fPtr;
214           int nTracks = dataPtr->fCount;
215           AliHLTExternalTrackParam* currOutTrack = dataPtr->fTracklets;
216
217           for( int itr=0; itr<nTracks; itr++ ){
218             AliHLTGlobalBarrelTrack t(*currOutTrack);
219             Float_t points[4] = {currOutTrack->fX, currOutTrack->fY, currOutTrack->fLastX, currOutTrack->fLastY };
220             
221             Int_t mcLabel = -1;
222             if( mcLabels.find(currOutTrack->fTrackID)!=mcLabels.end() )
223               mcLabel = mcLabels[currOutTrack->fTrackID];
224             
225             t.SetLabel( mcLabel );
226             
227             AliESDtrack iotrack;
228             iotrack.UpdateTrackParams( &t,AliESDtrack::kTPCin);
229             iotrack.SetTPCPoints(points);
230             pESD->AddTrack(&iotrack);
231             unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + currOutTrack->fNPoints * sizeof( unsigned int );
232             currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
233           }
234           iAddedDataBlocks++;
235         }
236
237         
238         if ( (bIsTrackSegs=(iter->fDataType == AliHLTTPCDefinitions::fgkTrackSegmentsDataType))==1 ||
239              iter->fDataType == AliHLTTPCDefinitions::fgkTracksDataType ) {
240           Int_t minslice=AliHLTTPCDefinitions::GetMinSliceNr(iter->fSpecification);
241           Int_t maxslice=AliHLTTPCDefinitions::GetMaxSliceNr(iter->fSpecification);
242           if (bIsTrackSegs==0) {
243             // slice parameter and data specification ignored, tracks already in global coordinates
244             minslice=-1;
245             maxslice=-1;
246             if (pMinSlice) *pMinSlice=0;
247             if (pMaxSlice) *pMaxSlice=AliHLTTPCTransform::GetNSlice()-1;
248           } else {
249             if (pMinSlice && (*pMinSlice==-1 || *pMinSlice>minslice)) *pMinSlice=minslice;
250             if (pMaxSlice && (*pMaxSlice==-1 || *pMaxSlice<maxslice)) *pMaxSlice=maxslice;
251           }
252           //HLTDebug("dataspec %#x minslice %d", iter->fSpecification, minslice);
253           if (minslice >=-1 && minslice<AliHLTTPCTransform::GetNSlice()) {
254             if (minslice!=maxslice) {
255               HLTWarning("data from multiple sectors in one block: "
256                          "possible mismatch in treatment of local coordinate system");
257             }
258             AliHLTTPCTrackArray tracks;
259             AliHLTTPCTrackletData* inPtr = (AliHLTTPCTrackletData*) iter->fPtr;     
260             HLTDebug("reading block %d (slice %d): %d tracklets", ndx, minslice, inPtr->fTrackletCnt);
261             if ((iResult=tracks.FillTracksChecked(inPtr->fTracklets, inPtr->fTrackletCnt, iter->fSize, minslice, 0/*don't rotate*/))>=0) {
262               if ((iResult=Tracks2ESD(&tracks, pESD ))>=0) {
263                 iAddedDataBlocks++;
264               }
265             }
266           } else {
267             HLTError("invalid sector number");
268             iResult=-EBADF;
269           }
270         }      
271       }
272
273       if (iAddedDataBlocks>0 && pTree) {
274         pTree->Fill();
275       }
276   
277   } else {
278     iResult=-EINVAL;
279   }
280   if (iResult>=0) iResult=iAddedDataBlocks;
281   return iResult;
282 }
283
284
285 int AliHLTTPCEsdWriterComponent::Tracks2ESD(AliHLTTPCTrackArray* pTracks, AliESDEvent* pESD )
286 {
287   // see header file for class documentation
288   int iResult=0;
289
290   if (pTracks && pESD) {    
291  
292     for (int i=0; i<pTracks->GetNTracks() && iResult>=0; i++) {
293       AliHLTTPCTrack* pTrack=(*pTracks)[i];
294       if (pTrack) {
295         
296         if( pTrack->Convert2AliKalmanTrack() ){   
297           HLTError("conversion to AliKalmanTrack failed for track %d of %d", i, pTracks->GetNTracks()); 
298           continue;
299         }
300
301         Float_t points[4] = {pTrack->GetFirstPointX(), pTrack->GetFirstPointY(), pTrack->GetLastPointX(), pTrack->GetLastPointY() };
302
303         if(pTrack->GetSector() == -1){ // Set first and last points for global tracks
304           Double_t s = TMath::Sin( pTrack->GetAlpha() );
305           Double_t c = TMath::Cos( pTrack->GetAlpha() );
306           points[0] =  pTrack->GetFirstPointX()*c + pTrack->GetFirstPointY()*s;
307           points[1] = -pTrack->GetFirstPointX()*s + pTrack->GetFirstPointY()*c;   
308           points[2] =  pTrack->GetLastPointX() *c + pTrack->GetLastPointY() *s;
309           points[3] = -pTrack->GetLastPointX() *s + pTrack->GetLastPointY() *c;   
310         }
311
312         AliESDtrack iotrack;
313         iotrack.UpdateTrackParams(pTrack,AliESDtrack::kTPCin);
314         iotrack.SetTPCPoints(points);
315
316         pESD->AddTrack(&iotrack);
317       } else {
318         HLTError("internal mismatch in array");
319         iResult=-EFAULT;
320       }
321     }
322     
323   } else {
324     iResult=-EINVAL;
325   }
326   return iResult;
327 }
328
329 int AliHLTTPCEsdWriterComponent::Configure(const char* arguments)
330 {
331   // see header file for class documentation
332   int iResult=0;
333   if (!arguments) return iResult;
334
335   // Check field
336   if (!TGeoGlobalMagField::Instance()) {
337     HLTError("magnetic field not initialized, please set up TGeoGlobalMagField and AliMagF");
338     return -ENODEV;
339   }
340
341   TString allArgs=arguments;
342   TString argument;
343   int bMissingParam=0;
344
345   TObjArray* pTokens=allArgs.Tokenize(" ");
346   if (pTokens) {
347     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
348       argument=((TObjString*)pTokens->At(i))->GetString();
349       if (argument.IsNull()) continue;
350       
351       if (argument.CompareTo("-solenoidBz")==0) {
352         if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
353         HLTWarning("parameter -solenoidBz deprecated, magnetic field handled by global AliMagF object and TGeoGlobalMagField");
354         continue;
355       } else {
356         HLTError("unknown argument %s", argument.Data());
357         iResult=-EINVAL;
358         break;
359       }
360     }
361     delete pTokens;
362   }
363   if (bMissingParam) {
364     HLTError("missing parameter for argument %s", argument.Data());
365     iResult=-EINVAL;
366   }
367
368   return iResult;
369 }
370
371 int AliHLTTPCEsdWriterComponent::Reconfigure(const char* cdbEntry, const char* chainId)
372 {
373   // see header file for class documentation
374   int iResult=0;
375   const char* path=NULL;
376   const char* defaultNotify="";
377   if (cdbEntry) {
378     path=cdbEntry;
379     defaultNotify=" (default)";
380   }
381   if (path) {
382     HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
383     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
384     if (pEntry) {
385       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
386       if (pString) {
387         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
388         iResult=Configure(pString->GetString().Data());
389       } else {
390         HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
391       }
392     } else {
393       HLTError("can not fetch object \"%s\" from CDB", path);
394     }
395   }
396   
397   return iResult;
398 }
399
400 AliHLTTPCEsdWriterComponent::AliConverter::AliConverter()
401   :
402   fESD(NULL),
403   fBase(new AliHLTTPCEsdWriterComponent),
404   fWriteTree(0)
405 {
406   // see header file for class documentation
407   // or
408   // refer to README to build package
409   // or
410   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
411 }
412
413 AliHLTTPCEsdWriterComponent::AliConverter::~AliConverter()
414 {
415   // see header file for class documentation
416   if (fBase) delete fBase;
417   fBase=NULL;
418
419   if (fESD) delete fESD;
420   fESD=NULL;
421 }
422
423 void AliHLTTPCEsdWriterComponent::AliConverter::GetInputDataTypes(AliHLTComponentDataTypeList& list)
424 {
425   // see header file for class documentation
426   list.push_back(AliHLTTPCDefinitions::fgkTrackSegmentsDataType);
427   list.push_back(AliHLTTPCDefinitions::fgkTracksDataType);
428   list.push_back( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC );
429 }
430
431 AliHLTComponentDataType AliHLTTPCEsdWriterComponent::AliConverter::GetOutputDataType()
432 {
433   // see header file for class documentation
434   return kAliHLTDataTypeESDTree;
435 }
436
437 void AliHLTTPCEsdWriterComponent::AliConverter::GetOutputDataSize(unsigned long& constBase, double& inputMultiplier)
438 {
439   // see header file for class documentation
440   constBase=2000000;
441   inputMultiplier=10.0;
442 }
443
444 int AliHLTTPCEsdWriterComponent::AliConverter::DoInit(int argc, const char** argv)
445 {
446   // see header file for class documentation
447   int iResult=0;
448   TString argument="";
449   int bMissingParam=0;
450   for (int i=0; i<argc && iResult>=0; i++) {
451     argument=argv[i];
452     if (argument.IsNull()) continue;
453
454     // -notree
455     if (argument.CompareTo("-notree")==0) {
456       fWriteTree=0;
457
458       // -tree
459     } else if (argument.CompareTo("-tree")==0) {
460       fWriteTree=1;
461
462       // -solenoidBz
463     } else if (argument.CompareTo("-solenoidBz")==0) {
464       HLTWarning("parameter -solenoidBz deprecated, magnetic field handled by global AliMagF object and TGeoGlobalMagField");
465     } else {
466       HLTError("unknown argument %s", argument.Data());
467       break;
468     }
469   }
470   if (bMissingParam) {
471     HLTError("missing parameter for argument %s", argument.Data());
472     iResult=-EINVAL;
473   }
474
475   if (iResult>=0) {
476     iResult=fBase->Reconfigure(NULL, NULL);
477   }
478
479   return iResult;
480 }
481
482 int AliHLTTPCEsdWriterComponent::AliConverter::DoDeinit()
483 {
484   // see header file for class documentation
485   return 0;
486 }
487
488 int AliHLTTPCEsdWriterComponent::AliConverter::DoEvent(const AliHLTComponentEventData& evtData, 
489                                                        const AliHLTComponentBlockData* blocks, 
490                                                        AliHLTComponentTriggerData& /*trigData*/,
491                                                        AliHLTUInt8_t* /*outputPtr*/, 
492                                                        AliHLTUInt32_t& size,
493                                                        AliHLTComponentBlockDataList& /*outputBlocks*/ )
494 {
495   // see header file for class documentation
496   int iResult=0;
497   // no direct writing to the output buffer
498   size=0;
499
500   assert(fBase);
501   if (!fESD) {
502     fESD = new AliESDEvent;
503     if (fESD) {
504       fESD->CreateStdContent();
505     } else {
506       iResult=-ENOMEM;
507     }
508   }
509
510   AliESDEvent* pESD = fESD;
511
512   if (pESD && fBase) {
513   
514     TTree* pTree = NULL;
515     // TODO: Matthias 06.12.2007
516     // Tried to write the ESD directly instead to a tree, but this did not work
517     // out. Information in the ESD is different, needs investigation.
518     
519     if (fWriteTree)
520       pTree = new TTree("esdTree", "Tree with HLT ESD objects");
521  
522     if (pTree) {
523       pTree->SetDirectory(0);
524     }
525
526     pESD->Reset(); 
527     pESD->SetMagneticField(GetBz());
528     if ((iResult=fBase->ProcessBlocks(pTree, pESD, blocks, (int)evtData.fBlockCnt))>0) {
529       // TODO: set the specification correctly
530       if (pTree) {
531         // the esd structure is written to the user info and is
532         // needed in te ReadFromTree method to read all objects correctly
533         pTree->GetUserInfo()->Add(pESD);
534         pESD->WriteToTree(pTree);
535         iResult=PushBack(pTree, kAliHLTDataTypeESDTree|kAliHLTDataOriginTPC, 0);
536       } else {
537         iResult=PushBack(pESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginTPC, 0);
538       }
539     }
540     if (pTree) {
541       // clear user info list to prevent objects from being deleted
542       pTree->GetUserInfo()->Clear();
543       delete pTree;
544     }
545   }
546   return iResult;
547 }
548