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),
66 fApplyParticleCuts(kFALSE) {
67 // see header file for class documentation
69 // refer to README to build package
71 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
73 // Set file to ROOT-File
74 SetIsRawFile( kFALSE );
76 fFolderList.SetOwner();
79 // #################################################################################
80 AliHLTESDMCEventPublisherComponent::~AliHLTESDMCEventPublisherComponent() {
81 // see header file for class documentation
83 // file list and file name list are owner of their objects and
84 // delete all the objects
88 * ---------------------------------------------------------------------------------
89 * Public functions to implement AliHLTComponent's interface.
90 * These functions are required for the registration process
91 * ---------------------------------------------------------------------------------
94 // #################################################################################
95 const char* AliHLTESDMCEventPublisherComponent::GetComponentID() {
96 // see header file for class documentation
97 return "ESDMCEventPublisher";
100 // #################################################################################
101 AliHLTComponent* AliHLTESDMCEventPublisherComponent::Spawn() {
102 // see header file for class documentation
103 return new AliHLTESDMCEventPublisherComponent;
106 void AliHLTESDMCEventPublisherComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) {
107 // see header file for class documentation
108 constBase=fOutputSize;
113 * ---------------------------------------------------------------------------------
114 * Protected functions to implement AliHLTComponent's interface.
115 * These functions provide initialization as well as the actual processing
116 * capabilities of the component.
117 * ---------------------------------------------------------------------------------
120 // #################################################################################
121 Int_t AliHLTESDMCEventPublisherComponent::DoInit(int argc, const char** argv) {
122 // see header file for class documentation
125 Int_t bMissingParam=0;
129 fSpecification = kAliHLTVoidDataSpec;
131 // -- Loop over all arguments
132 for ( Int_t iter = 0; iter<argc && iResult>=0; iter++) {
134 if (argument.IsNull()) continue;
136 // -- Check for which type to publish
137 // Can be one, all or some of : ESD, HLTESD and MC
138 if ( !argument.CompareTo("-entrytype") ) {
139 if ((bMissingParam=(++iter>=argc))) break;
141 TString parameter(argv[iter]);
142 parameter.Remove(TString::kLeading, ' ');
144 if ( !parameter.CompareTo("ESD") )
146 else if ( !parameter.CompareTo("HLTESD") )
147 fPublishHLTESD = kTRUE;
148 else if ( !parameter.CompareTo("MC") )
150 else if ( !parameter.CompareTo("MCFAST") ) {
155 HLTError("Wrong parameter %s for argument %s.", parameter.Data(), argument.Data());
160 // -- Data path to reconstructed data
161 else if ( ! argument.CompareTo("-datapath") ) {
162 if ((bMissingParam=(++iter>=argc))) break;
164 TString parameter(argv[iter]);
165 parameter.Remove(TString::kLeading, ' ');
167 fFolderList.Add( new TObjString(parameter) );
170 // -- Data Specification
171 // -- see header file !!!
172 else if ( ! argument.CompareTo("-dataspec") ) {
173 if ((bMissingParam=(++iter>=argc))) break;
175 TString parameter(argv[iter]);
176 parameter.Remove(TString::kLeading, ' ');
178 if ( parameter.IsDigit() )
179 fSpecification = (AliHLTUInt32_t) parameter.Atoi();
180 else if ( parameter.BeginsWith("0x") && parameter.Replace(0,2,"",0).IsHex() )
181 sscanf(parameter.Data(),"%x", &fSpecification);
183 HLTError("Wrong parameter for argument %s, number expected", argument.Data());
187 // -- applyParticleCuts
188 else if ( !argument.CompareTo("-applyParticleCuts") ) {
189 fApplyParticleCuts = kTRUE;
192 // -- Argument not known
194 HLTError("Unknown argument %s.", argument.Data());
198 } // for ( Int iter = 0; iter<argc && iResult>=0; iter++) {
200 // -- Check if parameter is missing
201 if ( bMissingParam ) {
202 HLTError("Missing parameter for argument %s.", argument.Data());
206 // -- Check if at least on entrytype was specified
207 if ( fPublishESD||fPublishHLTESD||fPublishMC )
208 AddDataTypesToOutputlist();
210 HLTError("At least one -entrytype argument needs to be specified.");
214 // -- Check if at least one datapath was specified
215 if ( !fFolderList.GetEntries() ) {
216 HLTError("At least one -datapath argument needs to be specified.");
220 // -- Add files according to entry type to event
221 if ( iResult >= 0 ) {
222 iResult = InsertFiles();
225 // -- Calculate event size
227 fMaxSize += sizeof(AliESDEvent);
228 if ( fPublishHLTESD )
229 fMaxSize += sizeof(AliESDEvent);
231 fMaxSize += sizeof(AliMCEvent) + 20000;
233 if ( fEvents.GetSize() == 0) {
234 HLTError("No Files have been added.");
245 // #################################################################################
246 Int_t AliHLTESDMCEventPublisherComponent::DoDeinit() {
247 // see header file for class documentation
268 CloseCurrentFileList();
270 AliHLTFilePublisher::DoDeinit();
275 // #################################################################################
276 void AliHLTESDMCEventPublisherComponent::AddDataTypesToOutputlist() {
277 // see header file for class documentation
279 // add all different data types to the list
281 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline);
282 else if ( fPublishHLTESD )
283 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT);
284 else if( fPublishMC )
285 fOutputDataTypes.push_back(kAliHLTDataTypeMCObject);
288 // #################################################################################
289 Int_t AliHLTESDMCEventPublisherComponent::InsertFiles(){
290 // see header file for class documentation
294 EventFiles* pCurrentFolder = NULL;
296 TObjString *objPath = 0;
297 TIter next(&fFolderList);
299 // Loop over all folders
300 while ( ( objPath = (TObjString*) next() ) && iResult>=0 ) {
302 TString pathAliESDs = objPath->GetString();
303 pathAliESDs.Append("/AliESDs.root");
305 TString pathKinematics = objPath->GetString();
306 pathKinematics.Append("/Kinematics.root");
308 TString pathgalice = objPath->GetString();
309 pathgalice.Append("/galice.root");
311 TString pathTrackRefs = objPath->GetString();
312 pathTrackRefs.Append("/TrackRefs.root");
315 // Add ESD file for this folder
316 if ( fPublishESD || fPublishHLTESD ) {
317 FileDesc* pDesc=new FileDesc( pathAliESDs.Data(), kAliHLTDataTypeESDObject, kAliHLTVoidDataSpec, kFALSE);
319 iResult = InsertFile( pCurrentFolder, pDesc );
324 // Add MC files for this folder
326 FileDesc* pDescKinematics=new FileDesc( pathKinematics.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
327 if ( pDescKinematics )
328 iResult = InsertFile( pCurrentFolder, pDescKinematics );
332 FileDesc* pDescgalice=new FileDesc( pathgalice.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
334 iResult = InsertFile( pCurrentFolder, pDescgalice );
339 FileDesc* pDescTrackRefs=new FileDesc( pathTrackRefs.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
340 if ( pDescTrackRefs )
341 iResult = InsertFile( pCurrentFolder, pDescTrackRefs );
347 // -- Add all files belonging to one eventfolder
348 InsertEvent( pCurrentFolder );
350 } // while ( ( path = (TObjString*)next() ) ) {
355 // #################################################################################
356 Int_t AliHLTESDMCEventPublisherComponent::GetEvent( const AliHLTComponentEventData& /*evtData*/,
357 AliHLTComponentTriggerData& /*trigData*/,
358 AliHLTUInt8_t* /*outputPtr*/,
359 AliHLTUInt32_t& size,
360 vector<AliHLTComponentBlockData>& /*outputBlocks*/ ) {
361 // see header file for class documentation
363 if ( !IsDataEvent() ) return 0;
368 // -- Initialize folder list and
369 // re-initialize after reaching the end in the event before
371 if ( !fpCurrentFolder ) {
372 fpCurrentFolder = GetEventList()->FirstLink();
374 // -- Set file list of this folder and open the files
375 if ( fpCurrentFolder ) {
376 iResult = OpenCurrentFileList();
379 HLTError("Folder link can not be retrieved from folder list.");
384 // -- if file list was properly created
385 if ( iResult >= 0 && fpCurrentFileList ) {
388 if ( fPublishESD && fpTreeESD ) {
389 fpTreeESD->GetEntry(fCurrentEvent);
390 if ((iResult=PushBack( fpESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline , fSpecification ))==-ENOSPC) {
391 fOutputSize+=GetLastObjectSize();
395 // -- Publish HLTESDs
396 if ( fPublishHLTESD && fpTreeHLTESD ) {
397 fpTreeHLTESD->GetEntry(fCurrentEvent);
398 if ((iResult=PushBack(fpHLTESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT , fSpecification ))==-ENOSPC) {
399 fOutputSize+=GetLastObjectSize();
406 TObjLink *flnk = fpCurrentFileList->FirstLink();
408 // -- Loop over files in the current list
409 while (flnk && iResult>=0) {
411 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
412 TFile* pFile = *pFileDesc;
414 TString filename = pFile->GetName();
415 filename.Remove(0,filename.Last('/')+1);
417 TString foldername( Form("Event%d", fCurrentEvent) );
419 // -- Open Kinematics
420 if ( !filename.CompareTo("Kinematics.root") ){
422 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
423 HLTError("Not all files contain the same number of events : %u and %d",
424 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
430 TDirectoryFile *dirK = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
432 fpTreeK = dynamic_cast<TTree*> (dirK->Get("TreeK"));
434 HLTError("Directory %s could not be found.", foldername.Data() );
438 } // if ( !filename.CompareTo("Kinematics.root") ){
440 // -- Open TrackReferences
441 else if ( !fFastMC && !filename.CompareTo("TrackRefs.root") ) {
443 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
444 HLTError("Not all files contain the same number of events : %u and %d",
445 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
451 TDirectoryFile *dirTR = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
453 fpTreeTR = dynamic_cast<TTree*> (dirTR->Get("TreeTR"));
455 HLTError("Directory %s could not be found.", foldername.Data() );
459 } // else if ( !filename.CompareTo("TrackRefs.root") ){
463 } // while (flnk && iResult>=0) {
465 if ( fpMC ) delete fpMC;
466 fpMC = new AliMCEvent();
468 if ( fpTreeE && fpTreeK && fpTreeTR ) {
469 fpMC->ConnectTreeE(fpTreeE);
470 fpTreeE->GetEntry(fCurrentEvent);
472 fpMC->ConnectTreeK(fpTreeK);
473 fpMC->ConnectTreeTR(fpTreeTR);
475 else if( fpTreeE && fpTreeK && fFastMC ) {
476 fpMC->ConnectTreeE(fpTreeE);
477 fpTreeE->GetEntry(fCurrentEvent);
479 fpMC->ConnectTreeK(fpTreeK);
482 HLTError("Not all trees needed for MC Event have been created.");
486 // -- Fill AliHLTMCEvent
487 if ( iResult>=0 && fpMC ) {
488 if ((iResult=PushBack( fpMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginOffline , fSpecification ))==-ENOSPC) {
489 fOutputSize+=GetLastObjectSize();
492 fpHLTMC = new AliHLTMCEvent( fApplyParticleCuts );
494 if ( !(iResult=fpHLTMC->FillMCEvent(fpMC)) )
495 if ((iResult=PushBack( fpHLTMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginHLT , fSpecification ))==-ENOSPC) {
496 fOutputSize+=GetLastObjectSize();
500 } // if ( fPublishMC ) {
502 // -- Next Event in Folder,
503 // if last event was already reached:
504 // -> Reser and close files
505 if (iResult>=0 && ++fCurrentEvent >= fNEventsInFolder )
506 iResult = CloseCurrentFileList();
508 } // if ( iResult >= 0 && fCurrentFileList ) {
511 // (fpCurrentEvent will be NULL*pFileDesc if end of events in file is reached)
512 if ( iResult >= 0 && fpCurrentFolder && !fCurrentEvent ) {
513 fpCurrentFolder=fpCurrentFolder->Next();
515 // -- if next Folder present : Set current file list of next folder
516 // else : end of folder list reached, reset fpCurrentFileList
517 if ( fpCurrentFolder )
518 iResult = OpenCurrentFileList();
525 // #################################################################################
526 Int_t AliHLTESDMCEventPublisherComponent::OpenCurrentFileList() {
527 // see header file for class documentation
532 // -- Get Current Folder
533 EventFiles* pCurrentFolderDesc = dynamic_cast<EventFiles*>( fpCurrentFolder->GetObject() );
535 // -- Get List of files in current folder
536 if ( pCurrentFolderDesc )
537 fpCurrentFileList = *pCurrentFolderDesc; // type conversion operator defined
539 HLTError("Folder descriptor can not be retrieved from folder list link");
543 if ( fpCurrentFileList && iResult >= 0 ) {
545 TObjLink *flnk = fpCurrentFileList->FirstLink();
547 // -- Loop over all files in folder
548 // ----------------------------------
549 while ( flnk && iResult>=0 ) {
550 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
553 // If file is not opened yet, open it ... which it should not
554 if ( pFileDesc && ( pFile = *pFileDesc ) == NULL )
555 pFileDesc->OpenFile();
557 HLTError("File already open, this should not happen.");
561 if ( (pFile = *pFileDesc ) == NULL ) {
562 HLTError("Opening file failed." );
568 TString filename = pFile->GetName();
569 filename.Remove(0,filename.Last('/')+1);
572 if ( !filename.CompareTo("AliESDs.root") && fPublishESD ) {
573 fpTreeESD = dynamic_cast<TTree*>(pFile->Get("esdTree") );
576 if ( nEntries == -1 )
577 nEntries = (Int_t)(fpTreeESD->GetEntriesFast());
578 else if ( nEntries != (Int_t)(fpTreeESD->GetEntriesFast()) ) {
579 HLTError("Not all files contain the same number of events : %d and %ld",
580 nEntries, fpTreeESD->GetEntriesFast());
585 if ( fpESD ) delete fpESD;
586 fpESD = new AliESDEvent();
587 fpESD->ReadFromTree(fpTreeESD);
589 } // if ( fpTreeESD ) {
591 HLTError("Getting ESD tree failed for file %s.", pFile->GetName() );
594 } // if ( !filename.CompareTo("AliESDs.root") && fPublishESD ){
597 // -- Open HLTESD tree
598 if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ) {
599 fpTreeHLTESD = dynamic_cast<TTree*>(pFile->Get("HLTesdTree") );
601 if ( fpTreeHLTESD ) {
602 if ( nEntries == -1 )
603 nEntries = (Int_t)(fpTreeHLTESD->GetEntriesFast());
604 else if ( nEntries != (Int_t)(fpTreeHLTESD->GetEntriesFast()) ) {
605 HLTError("Not all files contain the same number of events : %d and %ld",
606 nEntries, fpTreeHLTESD->GetEntriesFast());
611 if ( fpHLTESD ) delete fpHLTESD;
612 fpHLTESD = new AliESDEvent();
613 fpHLTESD->ReadFromTree(fpTreeHLTESD);
615 } // if ( fpTreeHLTESD ) {
617 HLTError("Getting HLTESD tree failed for file %s.", pFile->GetName() );
621 } // if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ){
623 // -- Open galice tree : TreeE
624 else if ( !filename.CompareTo("galice.root") && fPublishMC ) {
625 fpTreeE = dynamic_cast<TTree*>(pFile->Get("TE") );
628 if ( nEntries == -1 )
629 nEntries = (Int_t)(fpTreeE->GetEntriesFast());
630 else if ( nEntries != (Int_t)(fpTreeE->GetEntriesFast()) ) {
631 HLTError("Not all files contain the same number of events : %d and %ld",
632 nEntries, fpTreeE->GetEntriesFast());
635 } // if ( fpTreeE ) {
637 HLTError("Getting TreeE failed for file %s.", pFile->GetName() );
640 } // if ( !filename.CompareTo("galice.root") && fPublishMC ){
644 } // while ( flnk && iResult>=0 ) {
646 } // if ( fpCurrentFileList && iResult >= 0 ) {
648 HLTError("Current file list could not get created.");
653 fNEventsInFolder = (UInt_t)(nEntries);
658 // #################################################################################
659 Int_t AliHLTESDMCEventPublisherComponent::CloseCurrentFileList() {
660 // see header file for class documentation
666 fNEventsInFolder = 0;
668 // if not set, no files are open ...
669 if ( fpCurrentFileList ) {
671 TObjLink *flnk = fpCurrentFileList->FirstLink();
673 // -- Loop over all files in folder
674 while ( flnk && iResult>=0 ) {
675 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
679 // If is opened, close it
680 if ( pFileDesc && ( pFile = *pFileDesc ) != NULL )
681 pFileDesc->CloseFile();
683 HLTError("File already open, this should not happen.");
687 if ( (pFile = *pFileDesc ) != NULL ) {
688 HLTError("Closing file %s failed.", pFile->GetName() );
694 } // while ( flnk && iResult>=0 ) {
696 fpCurrentFileList = NULL;
698 } // if ( fpCurrentFileList && iResult >= 0 ) {