1 // $Id: AliHLTESDMCEventPublisherComponent.cxx 25452 2008-04-25 21:46:04Z richterm $
3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Jochen Thaeder <thaeder@kip.uni-heidelberg.de> *
8 * for The ALICE HLT Project. *
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 **************************************************************************/
19 /** @file AliHLTESDMCEventPublisherComponent.cxx
20 @author Jochen Thaeder
22 @brief Component for publishing ESD and MC events.
23 @note The class is used in Offline (AliRoot) context
26 #include "AliHLTESDMCEventPublisherComponent.h"
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTESDMCEventPublisherComponent)
37 * ---------------------------------------------------------------------------------
38 * Constructor / Destructor
39 * ---------------------------------------------------------------------------------
42 // #################################################################################
43 AliHLTESDMCEventPublisherComponent::AliHLTESDMCEventPublisherComponent()
45 AliHLTFilePublisher(),
46 fpCurrentFolder(NULL),
47 fpCurrentFileList(NULL),
53 fPublishHLTESD(kFALSE),
65 // see header file for class documentation
67 // refer to README to build package
69 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
71 // Set file to ROOT-File
72 SetIsRawFile( kFALSE );
74 fFolderList.SetOwner();
77 // #################################################################################
78 AliHLTESDMCEventPublisherComponent::~AliHLTESDMCEventPublisherComponent() {
79 // see header file for class documentation
81 // file list and file name list are owner of their objects and
82 // delete all the objects
86 * ---------------------------------------------------------------------------------
87 * Public functions to implement AliHLTComponent's interface.
88 * These functions are required for the registration process
89 * ---------------------------------------------------------------------------------
92 // #################################################################################
93 const char* AliHLTESDMCEventPublisherComponent::GetComponentID() {
94 // see header file for class documentation
95 return "ESDMCEventPublisher";
98 // #################################################################################
99 AliHLTComponent* AliHLTESDMCEventPublisherComponent::Spawn() {
100 // see header file for class documentation
101 return new AliHLTESDMCEventPublisherComponent;
105 * ---------------------------------------------------------------------------------
106 * Protected functions to implement AliHLTComponent's interface.
107 * These functions provide initialization as well as the actual processing
108 * capabilities of the component.
109 * ---------------------------------------------------------------------------------
112 // #################################################################################
113 Int_t AliHLTESDMCEventPublisherComponent::DoInit(int argc, const char** argv) {
114 // see header file for class documentation
117 Int_t bMissingParam=0;
121 fSpecification = kAliHLTVoidDataSpec;
123 // -- Loop over all arguments
124 for ( Int_t iter = 0; iter<argc && iResult>=0; iter++) {
126 if (argument.IsNull()) continue;
128 // -- Check for which type to publish
129 // Can be one, all or some of : ESD, HLTESD and MC
130 if ( !argument.CompareTo("-entrytype") ) {
131 if ((bMissingParam=(++iter>=argc))) break;
133 TString parameter(argv[iter]);
134 parameter.Remove(TString::kLeading, ' ');
136 if ( !parameter.CompareTo("ESD") )
138 else if ( !parameter.CompareTo("HLTESD") )
139 fPublishHLTESD = kTRUE;
140 else if ( !parameter.CompareTo("MC") )
142 else if ( !parameter.CompareTo("MCFAST") ) {
147 HLTError("Wrong parameter %s for argument %s.", parameter.Data(), argument.Data());
152 // -- Data path to reconstructed data
153 else if ( ! argument.CompareTo("-datapath") ) {
154 if ((bMissingParam=(++iter>=argc))) break;
156 TString parameter(argv[iter]);
157 parameter.Remove(TString::kLeading, ' ');
159 fFolderList.Add( new TObjString(parameter) );
162 // -- Data Specification
163 // -- see header file !!!
164 else if ( ! argument.CompareTo("-dataspec") ) {
165 if ((bMissingParam=(++iter>=argc))) break;
167 TString parameter(argv[iter]);
168 parameter.Remove(TString::kLeading, ' ');
170 if ( parameter.IsDigit() )
171 fSpecification = (AliHLTUInt32_t) parameter.Atoi();
172 else if ( parameter.BeginsWith("0x") && parameter.Replace(0,2,"",0).IsHex() )
173 sscanf(parameter.Data(),"%x", &fSpecification);
175 HLTError("Wrong parameter for argument %s, number expected", argument.Data());
180 // -- Argument not known
182 HLTError("Unknown argument %s.", argument.Data());
186 } // for ( Int iter = 0; iter<argc && iResult>=0; iter++) {
188 // -- Check if parameter is missing
189 if ( bMissingParam ) {
190 HLTError("Missing parameter for argument %s.", argument.Data());
194 // -- Check if at least on entrytype was specified
195 if ( fPublishESD||fPublishHLTESD||fPublishMC )
196 AddDataTypesToOutputlist();
198 HLTError("At least one -entrytype argument needs to be specified.");
202 // -- Check if at least one datapath was specified
203 if ( !fFolderList.GetEntries() ) {
204 HLTError("At least one -datapath argument needs to be specified.");
208 // -- Add files according to entry type to event
209 if ( iResult >= 0 ) {
210 iResult = InsertFiles();
213 // -- Calculate event size
215 fMaxSize += sizeof(AliESDEvent);
216 if ( fPublishHLTESD )
217 fMaxSize += sizeof(AliESDEvent);
219 fMaxSize += sizeof(AliMCEvent) + 15000;
221 if ( fEvents.GetSize() == 0) {
222 HLTError("No Files have been added.");
233 // #################################################################################
234 Int_t AliHLTESDMCEventPublisherComponent::DoDeinit() {
235 // see header file for class documentation
256 CloseCurrentFileList();
258 AliHLTFilePublisher::DoDeinit();
263 // #################################################################################
264 void AliHLTESDMCEventPublisherComponent::AddDataTypesToOutputlist() {
265 // see header file for class documentation
267 // add all different data types to the list
269 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline);
270 else if ( fPublishHLTESD )
271 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT);
272 else if( fPublishMC )
273 fOutputDataTypes.push_back(kAliHLTDataTypeMCObject);
276 // #################################################################################
277 Int_t AliHLTESDMCEventPublisherComponent::InsertFiles(){
278 // see header file for class documentation
282 EventFiles* pCurrentFolder = NULL;
284 TObjString *objPath = 0;
285 TIter next(&fFolderList);
287 // Loop over all folders
288 while ( ( objPath = (TObjString*) next() ) && iResult>=0 ) {
290 TString pathAliESDs = objPath->GetString();
291 pathAliESDs.Append("/AliESDs.root");
293 TString pathKinematics = objPath->GetString();
294 pathKinematics.Append("/Kinematics.root");
296 TString pathgalice = objPath->GetString();
297 pathgalice.Append("/galice.root");
299 TString pathTrackRefs = objPath->GetString();
300 pathTrackRefs.Append("/TrackRefs.root");
303 // Add ESD file for this folder
304 if ( fPublishESD || fPublishHLTESD ) {
305 FileDesc* pDesc=new FileDesc( pathAliESDs.Data(), kAliHLTDataTypeESDObject, kAliHLTVoidDataSpec, kFALSE);
307 iResult = InsertFile( pCurrentFolder, pDesc );
312 // Add MC files for this folder
314 FileDesc* pDescKinematics=new FileDesc( pathKinematics.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
315 if ( pDescKinematics )
316 iResult = InsertFile( pCurrentFolder, pDescKinematics );
320 FileDesc* pDescgalice=new FileDesc( pathgalice.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
322 iResult = InsertFile( pCurrentFolder, pDescgalice );
327 FileDesc* pDescTrackRefs=new FileDesc( pathTrackRefs.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
328 if ( pDescTrackRefs )
329 iResult = InsertFile( pCurrentFolder, pDescTrackRefs );
335 // -- Add all files belonging to one eventfolder
336 InsertEvent( pCurrentFolder );
338 } // while ( ( path = (TObjString*)next() ) ) {
343 // #################################################################################
344 Int_t AliHLTESDMCEventPublisherComponent::GetEvent( const AliHLTComponentEventData& /*evtData*/,
345 AliHLTComponentTriggerData& /*trigData*/,
346 AliHLTUInt8_t* /*outputPtr*/,
347 AliHLTUInt32_t& size,
348 vector<AliHLTComponentBlockData>& /*outputBlocks*/ ) {
349 // see header file for class documentation
351 if ( !IsDataEvent() ) return 0;
356 // -- Initialize folder list and
357 // re-initialize after reaching the end in the event before
359 if ( !fpCurrentFolder ) {
360 fpCurrentFolder = GetEventList()->FirstLink();
362 // -- Set file list of this folder and open the files
363 if ( fpCurrentFolder ) {
364 iResult = OpenCurrentFileList();
367 HLTError("Folder link can not be retrieved from folder list.");
372 // -- if file list was properly created
373 if ( iResult >= 0 && fpCurrentFileList ) {
376 if ( fPublishESD && fpTreeESD ) {
377 fpTreeESD->GetEntry(fCurrentEvent);
378 PushBack( fpESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline , fSpecification );
381 // -- Publish HLTESDs
382 if ( fPublishHLTESD && fpTreeHLTESD ) {
383 fpTreeHLTESD->GetEntry(fCurrentEvent);
384 PushBack( fpHLTESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT , fSpecification );
390 TObjLink *flnk = fpCurrentFileList->FirstLink();
392 // -- Loop over files in the current list
393 while (flnk && iResult>=0) {
395 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
396 TFile* pFile = *pFileDesc;
398 TString filename = pFile->GetName();
399 filename.Remove(0,filename.Last('/')+1);
401 TString foldername( Form("Event%d", fCurrentEvent) );
403 // -- Open Kinematics
404 if ( !filename.CompareTo("Kinematics.root") ){
406 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
407 HLTError("Not all files contain the same number of events : %u and %d",
408 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
414 TDirectoryFile *dirK = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
416 fpTreeK = dynamic_cast<TTree*> (dirK->Get("TreeK"));
418 HLTError("Directory %s could not be found.", foldername.Data() );
422 } // if ( !filename.CompareTo("Kinematics.root") ){
424 // -- Open TrackReferences
425 else if ( !fFastMC && !filename.CompareTo("TrackRefs.root") ) {
427 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
428 HLTError("Not all files contain the same number of events : %u and %d",
429 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
435 TDirectoryFile *dirTR = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
437 fpTreeTR = dynamic_cast<TTree*> (dirTR->Get("TreeTR"));
439 HLTError("Directory %s could not be found.", foldername.Data() );
443 } // else if ( !filename.CompareTo("TrackRefs.root") ){
447 } // while (flnk && iResult>=0) {
449 if ( fpMC ) delete fpMC;
450 fpMC = new AliMCEvent();
452 if ( fpTreeE && fpTreeK && fpTreeTR ) {
453 fpMC->ConnectTreeE(fpTreeE);
454 fpTreeE->GetEntry(fCurrentEvent);
456 fpMC->ConnectTreeK(fpTreeK);
457 fpMC->ConnectTreeTR(fpTreeTR);
459 else if( fpTreeE && fpTreeK && fFastMC ) {
460 fpMC->ConnectTreeE(fpTreeE);
461 fpTreeE->GetEntry(fCurrentEvent);
463 fpMC->ConnectTreeK(fpTreeK);
466 HLTError("Not all trees needed for MC Event have been created.");
470 // -- Fill AliHLTMCEvent
471 if ( iResult>=0 && fpMC ) {
472 PushBack( fpMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginOffline , fSpecification );
474 fpHLTMC = new AliHLTMCEvent(fpMC);
477 PushBack( fpHLTMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginHLT , fSpecification );
480 } // if ( fPublishMC ) {
482 // -- Next Event in Folder,
483 // if last event was already reached:
484 // -> Reser and close files
485 if ( ++fCurrentEvent >= fNEventsInFolder )
486 iResult = CloseCurrentFileList();
488 } // if ( iResult >= 0 && fCurrentFileList ) {
491 // (fpCurrentEvent will be NULL*pFileDesc if end of events in file is reached)
492 if ( iResult >= 0 && fpCurrentFolder && !fCurrentEvent ) {
493 fpCurrentFolder=fpCurrentFolder->Next();
495 // -- if next Folder present : Set current file list of next folder
496 // else : end of folder list reached, reset fpCurrentFileList
497 if ( fpCurrentFolder )
498 iResult = OpenCurrentFileList();
505 // #################################################################################
506 Int_t AliHLTESDMCEventPublisherComponent::OpenCurrentFileList() {
507 // see header file for class documentation
512 // -- Get Current Folder
513 EventFiles* pCurrentFolderDesc = dynamic_cast<EventFiles*>( fpCurrentFolder->GetObject() );
515 // -- Get List of files in current folder
516 if ( pCurrentFolderDesc )
517 fpCurrentFileList = *pCurrentFolderDesc; // type conversion operator defined
519 HLTError("Folder descriptor can not be retrieved from folder list link");
523 if ( fpCurrentFileList && iResult >= 0 ) {
525 TObjLink *flnk = fpCurrentFileList->FirstLink();
527 // -- Loop over all files in folder
528 // ----------------------------------
529 while ( flnk && iResult>=0 ) {
530 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
533 // If file is not opened yet, open it ... which it should not
534 if ( pFileDesc && ( pFile = *pFileDesc ) == NULL )
535 pFileDesc->OpenFile();
537 HLTError("File already open, this should not happen.");
541 if ( (pFile = *pFileDesc ) == NULL ) {
542 HLTError("Opening file failed." );
548 TString filename = pFile->GetName();
549 filename.Remove(0,filename.Last('/')+1);
552 if ( !filename.CompareTo("AliESDs.root") && fPublishESD ) {
553 fpTreeESD = dynamic_cast<TTree*>(pFile->Get("esdTree") );
556 if ( nEntries == -1 )
557 nEntries = (Int_t)(fpTreeESD->GetEntriesFast());
558 else if ( nEntries != (Int_t)(fpTreeESD->GetEntriesFast()) ) {
559 HLTError("Not all files contain the same number of events : %d and %ld",
560 nEntries, fpTreeESD->GetEntriesFast());
565 if ( fpESD ) delete fpESD;
566 fpESD = new AliESDEvent();
567 fpESD->ReadFromTree(fpTreeESD);
569 } // if ( fpTreeESD ) {
571 HLTError("Getting ESD tree failed for file %s.", pFile->GetName() );
574 } // if ( !filename.CompareTo("AliESDs.root") && fPublishESD ){
577 // -- Open HLTESD tree
578 if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ) {
579 fpTreeHLTESD = dynamic_cast<TTree*>(pFile->Get("HLTesdTree") );
581 if ( fpTreeHLTESD ) {
582 if ( nEntries == -1 )
583 nEntries = (Int_t)(fpTreeHLTESD->GetEntriesFast());
584 else if ( nEntries != (Int_t)(fpTreeHLTESD->GetEntriesFast()) ) {
585 HLTError("Not all files contain the same number of events : %d and %ld",
586 nEntries, fpTreeHLTESD->GetEntriesFast());
591 if ( fpHLTESD ) delete fpHLTESD;
592 fpHLTESD = new AliESDEvent();
593 fpHLTESD->ReadFromTree(fpTreeHLTESD);
595 } // if ( fpTreeHLTESD ) {
597 HLTError("Getting HLTESD tree failed for file %s.", pFile->GetName() );
601 } // if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ){
603 // -- Open galice tree : TreeE
604 else if ( !filename.CompareTo("galice.root") && fPublishMC ) {
605 fpTreeE = dynamic_cast<TTree*>(pFile->Get("TE") );
608 if ( nEntries == -1 )
609 nEntries = (Int_t)(fpTreeE->GetEntriesFast());
610 else if ( nEntries != (Int_t)(fpTreeE->GetEntriesFast()) ) {
611 HLTError("Not all files contain the same number of events : %d and %ld",
612 nEntries, fpTreeE->GetEntriesFast());
615 } // if ( fpTreeE ) {
617 HLTError("Getting TreeE failed for file %s.", pFile->GetName() );
620 } // if ( !filename.CompareTo("galice.root") && fPublishMC ){
624 } // while ( flnk && iResult>=0 ) {
626 } // if ( fpCurrentFileList && iResult >= 0 ) {
628 HLTError("Current file list could not get created.");
633 fNEventsInFolder = (UInt_t)(nEntries);
638 // #################################################################################
639 Int_t AliHLTESDMCEventPublisherComponent::CloseCurrentFileList() {
640 // see header file for class documentation
646 fNEventsInFolder = 0;
648 // if not set, no files are open ...
649 if ( fpCurrentFileList ) {
651 TObjLink *flnk = fpCurrentFileList->FirstLink();
653 // -- Loop over all files in folder
654 while ( flnk && iResult>=0 ) {
655 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
659 // If is opened, close it
660 if ( pFileDesc && ( pFile = *pFileDesc ) != NULL )
661 pFileDesc->CloseFile();
663 HLTError("File already open, this should not happen.");
667 if ( (pFile = *pFileDesc ) != NULL ) {
668 HLTError("Closing file %s failed.", pFile->GetName() );
674 } // while ( flnk && iResult>=0 ) {
676 fpCurrentFileList = NULL;
678 } // if ( fpCurrentFileList && iResult >= 0 ) {