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),
63 // see header file for class documentation
65 // refer to README to build package
67 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
69 // Set file to ROOT-File
70 SetIsRawFile( kFALSE );
72 fFolderList.SetOwner();
75 // #################################################################################
76 AliHLTESDMCEventPublisherComponent::~AliHLTESDMCEventPublisherComponent() {
77 // see header file for class documentation
79 // file list and file name list are owner of their objects and
80 // delete all the objects
84 * ---------------------------------------------------------------------------------
85 * Public functions to implement AliHLTComponent's interface.
86 * These functions are required for the registration process
87 * ---------------------------------------------------------------------------------
90 // #################################################################################
91 const char* AliHLTESDMCEventPublisherComponent::GetComponentID() {
92 // see header file for class documentation
93 return "ESDMCEventPublisher";
96 // #################################################################################
97 AliHLTComponent* AliHLTESDMCEventPublisherComponent::Spawn() {
98 // see header file for class documentation
99 return new AliHLTESDMCEventPublisherComponent;
103 * ---------------------------------------------------------------------------------
104 * Protected functions to implement AliHLTComponent's interface.
105 * These functions provide initialization as well as the actual processing
106 * capabilities of the component.
107 * ---------------------------------------------------------------------------------
110 // #################################################################################
111 Int_t AliHLTESDMCEventPublisherComponent::DoInit(Int_t argc, const char** argv) {
112 // see header file for class documentation
115 Int_t bMissingParam=0;
119 fSpecification = kAliHLTVoidDataSpec;
121 // -- Loop over all arguments
122 for ( Int_t iter = 0; iter<argc && iResult>=0; iter++) {
124 if (argument.IsNull()) continue;
126 // -- Check for which type to publish
127 // Can be one, all or some of : ESD, HLTESD and MC
128 if ( !argument.CompareTo("-entrytype") ) {
129 if ((bMissingParam=(++iter>=argc))) break;
131 TString parameter(argv[iter]);
132 parameter.Remove(TString::kLeading, ' ');
134 if ( !parameter.CompareTo("ESD") )
136 else if ( !parameter.CompareTo("HLTESD") )
137 fPublishHLTESD = kTRUE;
138 else if ( !parameter.CompareTo("MC") )
141 HLTError("Wrong parameter %s for argument %s.", parameter.Data(), argument.Data());
146 // -- Data path to reconstructed data
147 else if ( ! argument.CompareTo("-datapath") ) {
148 if ((bMissingParam=(++iter>=argc))) break;
150 TString parameter(argv[iter]);
151 parameter.Remove(TString::kLeading, ' ');
153 fFolderList.Add( new TObjString(parameter) );
156 // -- Data Specification
157 // -- see header file !!!
158 else if ( ! argument.CompareTo("-dataspec") ) {
159 if ((bMissingParam=(++iter>=argc))) break;
161 TString parameter(argv[iter]);
162 parameter.Remove(TString::kLeading, ' ');
164 if ( parameter.IsDigit() )
165 fSpecification = (AliHLTUInt32_t) parameter.Atoi();
166 else if ( parameter.BeginsWith("0x") && parameter.Replace(0,2,"",0).IsHex() )
167 sscanf(parameter.Data(),"%x", &fSpecification);
169 HLTError("Wrong parameter for argument %s, number expected", argument.Data());
174 // -- Argument not known
176 HLTError("Unknown argument %s.", argument.Data());
180 } // for ( Int iter = 0; iter<argc && iResult>=0; iter++) {
182 // -- Check if parameter is missing
183 if ( bMissingParam ) {
184 HLTError("Missing parameter for argument %s.", argument.Data());
188 // -- Check if at least on entrytype was specified
189 if ( fPublishESD||fPublishHLTESD||fPublishMC )
190 AddDataTypesToOutputlist();
192 HLTError("At least one -entrytype argument needs to be specified.");
196 // -- Check if at least one datapath was specified
197 if ( !fFolderList.GetEntries() ) {
198 HLTError("At least one -datapath argument needs to be specified.");
202 // -- Add files according to entry type to event
203 if ( iResult >= 0 ) {
204 iResult = InsertFiles();
207 // -- Calculate event size
209 fMaxSize += sizeof(AliESDEvent);
210 if ( fPublishHLTESD )
211 fMaxSize += sizeof(AliESDEvent);
213 fMaxSize += sizeof(AliMCEvent);
215 if ( fEvents.GetSize() == 0) {
216 HLTError("No Files have been added.");
227 // #################################################################################
228 Int_t AliHLTESDMCEventPublisherComponent::DoDeinit() {
229 // see header file for class documentation
245 CloseCurrentFileList();
247 AliHLTFilePublisher::DoDeinit();
252 // #################################################################################
253 void AliHLTESDMCEventPublisherComponent::AddDataTypesToOutputlist() {
254 // see header file for class documentation
256 // add all different data types to the list
258 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline);
259 else if ( fPublishHLTESD )
260 fOutputDataTypes.push_back(kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT);
261 else if( fPublishMC )
262 fOutputDataTypes.push_back(kAliHLTDataTypeMCObject);
265 // #################################################################################
266 Int_t AliHLTESDMCEventPublisherComponent::InsertFiles(){
267 // see header file for class documentation
271 EventFiles* pCurrentFolder = NULL;
273 TObjString *objPath = 0;
274 TIter next(&fFolderList);
276 // Loop over all folders
277 while ( ( objPath = (TObjString*) next() ) && iResult>=0 ) {
279 TString pathAliESDs = objPath->GetString();
280 pathAliESDs.Append("/AliESDs.root");
282 TString pathKinematics = objPath->GetString();
283 pathKinematics.Append("/Kinematics.root");
285 TString pathgalice = objPath->GetString();
286 pathgalice.Append("/galice.root");
288 TString pathTrackRefs = objPath->GetString();
289 pathTrackRefs.Append("/TrackRefs.root");
292 // Add ESD file for this folder
293 if ( fPublishESD || fPublishHLTESD ) {
294 FileDesc* pDesc=new FileDesc( pathAliESDs.Data(), kAliHLTDataTypeESDObject, kAliHLTVoidDataSpec, kFALSE);
296 iResult = InsertFile( pCurrentFolder, pDesc );
301 // Add MC files for this folder
303 FileDesc* pDescKinematics=new FileDesc( pathKinematics.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
304 if ( pDescKinematics )
305 iResult = InsertFile( pCurrentFolder, pDescKinematics );
309 FileDesc* pDescgalice=new FileDesc( pathgalice.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
311 iResult = InsertFile( pCurrentFolder, pDescgalice );
315 FileDesc* pDescTrackRefs=new FileDesc( pathTrackRefs.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
316 if ( pDescTrackRefs )
317 iResult = InsertFile( pCurrentFolder, pDescTrackRefs );
322 // -- Add all files belonging to one eventfolder
323 InsertEvent( pCurrentFolder );
325 } // while ( ( path = (TObjString*)next() ) ) {
330 // #################################################################################
331 Int_t AliHLTESDMCEventPublisherComponent::GetEvent( const AliHLTComponentEventData& /*evtData*/,
332 AliHLTComponentTriggerData& /*trigData*/,
333 AliHLTUInt8_t* /*outputPtr*/,
334 AliHLTUInt32_t& size,
335 vector<AliHLTComponentBlockData>& /*outputBlocks*/ ) {
336 // see header file for class documentation
338 if ( !IsDataEvent() ) return 0;
343 // -- Initialize folder list and
344 // re-initialize after reaching the end in the event before
346 if ( !fpCurrentFolder ) {
347 fpCurrentFolder = GetEventList()->FirstLink();
349 // -- Set file list of this folder and open the files
350 if ( fpCurrentFolder ) {
351 iResult = OpenCurrentFileList();
354 HLTError("Folder link can not be retrieved from folder list.");
359 // -- if file list was properly created
360 if ( iResult >= 0 && fpCurrentFileList ) {
363 if ( fPublishESD && fpTreeESD ) {
364 fpTreeESD->GetEntry(fCurrentEvent);
365 PushBack( fpESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline , fSpecification );
368 // -- Publish HLTESDs
369 if ( fPublishHLTESD && fpTreeHLTESD ) {
370 fpTreeHLTESD->GetEntry(fCurrentEvent);
371 PushBack( fpHLTESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT , fSpecification );
377 TObjLink *flnk = fpCurrentFileList->FirstLink();
379 // -- Loop over files in the current list
380 while (flnk && iResult>=0) {
382 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
383 TFile* pFile = *pFileDesc;
385 TString filename = pFile->GetName();
386 filename.Remove(0,filename.Last('/')+1);
388 TString foldername( Form("Event%d", fCurrentEvent) );
390 // -- Open Kinematics
391 if ( !filename.CompareTo("Kinematics.root") ){
393 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
394 HLTError("Not all files contain the same number of events : %u and %d",
395 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
401 TDirectoryFile *dirK = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
403 fpTreeK = dynamic_cast<TTree*> (dirK->Get("TreeK"));
405 HLTError("Directory %s could not be found.", foldername.Data() );
409 } // if ( !filename.CompareTo("Kinematics.root") ){
411 // -- Open TrackReferences
412 else if ( !filename.CompareTo("TrackRefs.root") ){
414 if ( fNEventsInFolder != (UInt_t)(pFile->GetListOfKeys()->GetEntries()) ) {
415 HLTError("Not all files contain the same number of events : %u and %d",
416 fNEventsInFolder, pFile->GetListOfKeys()->GetEntries() );
422 TDirectoryFile *dirTR = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
424 fpTreeTR = dynamic_cast<TTree*> (dirTR->Get("TreeTR"));
426 HLTError("Directory %s could not be found.", foldername.Data() );
430 } // else if ( !filename.CompareTo("TrackRefs.root") ){
434 } // while (flnk && iResult>=0) {
436 if ( fpMC ) delete fpMC;
437 fpMC = new AliMCEvent();
439 if ( fpTreeE && fpTreeK && fpTreeTR ) {
441 fpMC->ConnectTreeE(fpTreeE);
442 fpTreeE->GetEntry(fCurrentEvent);
444 fpMC->ConnectTreeK(fpTreeK);
445 fpMC->ConnectTreeTR(fpTreeTR);
448 HLTError("Not all trees needed for MC Event have been created.");
452 if ( iResult>=0 && fpMC ) {
453 PushBack( fpMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginOffline , fSpecification );
456 } // if ( fPublishMC ) {
458 // -- Next Event in Folder,
459 // if last event was already reached:
460 // -> Reser and close files
461 if ( ++fCurrentEvent >= fNEventsInFolder )
462 iResult = CloseCurrentFileList();
464 } // if ( iResult >= 0 && fCurrentFileList ) {
467 // (fpCurrentEvent will be NULL*pFileDesc if end of events in file is reached)
468 if ( iResult >= 0 && fpCurrentFolder && !fCurrentEvent ) {
469 fpCurrentFolder=fpCurrentFolder->Next();
471 // -- if next Folder present : Set current file list of next folder
472 // else : end of folder list reached, reset fpCurrentFileList
473 if ( fpCurrentFolder )
474 iResult = OpenCurrentFileList();
481 // #################################################################################
482 Int_t AliHLTESDMCEventPublisherComponent::OpenCurrentFileList() {
483 // see header file for class documentation
488 // -- Get Current Folder
489 EventFiles* pCurrentFolderDesc = dynamic_cast<EventFiles*>( fpCurrentFolder->GetObject() );
491 // -- Get List of files in current folder
492 if ( pCurrentFolderDesc )
493 fpCurrentFileList = *pCurrentFolderDesc; // type conversion operator defined
495 HLTError("Folder descriptor can not be retrieved from folder list link");
499 if ( fpCurrentFileList && iResult >= 0 ) {
501 TObjLink *flnk = fpCurrentFileList->FirstLink();
503 // -- Loop over all files in folder
504 // ----------------------------------
505 while ( flnk && iResult>=0 ) {
506 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
509 // If file is not opened yet, open it ... which it should not
510 if ( pFileDesc && ( pFile = *pFileDesc ) == NULL )
511 pFileDesc->OpenFile();
513 HLTError("File already open, this should not happen.");
517 if ( (pFile = *pFileDesc ) == NULL ) {
518 HLTError("Opening file failed." );
524 TString filename = pFile->GetName();
525 filename.Remove(0,filename.Last('/')+1);
528 if ( !filename.CompareTo("AliESDs.root") && fPublishESD ) {
529 fpTreeESD = dynamic_cast<TTree*>(pFile->Get("esdTree") );
532 if ( nEntries == -1 )
533 nEntries = (Int_t)(fpTreeESD->GetEntriesFast());
534 else if ( nEntries != (Int_t)(fpTreeESD->GetEntriesFast()) ) {
535 HLTError("Not all files contain the same number of events : %d and %ld",
536 nEntries, fpTreeESD->GetEntriesFast());
541 if ( fpESD ) delete fpESD;
542 fpESD = new AliESDEvent();
543 fpESD->ReadFromTree(fpTreeESD);
545 } // if ( fpTreeESD ) {
547 HLTError("Getting ESD tree failed for file %s.", pFile->GetName() );
550 } // if ( !filename.CompareTo("AliESDs.root") && fPublishESD ){
553 // -- Open HLTESD tree
554 if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ) {
555 fpTreeHLTESD = dynamic_cast<TTree*>(pFile->Get("HLTesdTree") );
557 if ( fpTreeHLTESD ) {
558 if ( nEntries == -1 )
559 nEntries = (Int_t)(fpTreeHLTESD->GetEntriesFast());
560 else if ( nEntries != (Int_t)(fpTreeHLTESD->GetEntriesFast()) ) {
561 HLTError("Not all files contain the same number of events : %d and %ld",
562 nEntries, fpTreeHLTESD->GetEntriesFast());
567 if ( fpHLTESD ) delete fpHLTESD;
568 fpHLTESD = new AliESDEvent();
569 fpHLTESD->ReadFromTree(fpTreeHLTESD);
571 } // if ( fpTreeHLTESD ) {
573 HLTError("Getting HLTESD tree failed for file %s.", pFile->GetName() );
577 } // if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ){
579 // -- Open galice tree : TreeE
580 else if ( !filename.CompareTo("galice.root") && fPublishMC ) {
581 fpTreeE = dynamic_cast<TTree*>(pFile->Get("TE") );
584 if ( nEntries == -1 )
585 nEntries = (Int_t)(fpTreeE->GetEntriesFast());
586 else if ( nEntries != (Int_t)(fpTreeE->GetEntriesFast()) ) {
587 HLTError("Not all files contain the same number of events : %d and %ld",
588 nEntries, fpTreeE->GetEntriesFast());
591 } // if ( fpTreeE ) {
593 HLTError("Getting TreeE failed for file %s.", pFile->GetName() );
596 } // if ( !filename.CompareTo("galice.root") && fPublishMC ){
600 } // while ( flnk && iResult>=0 ) {
602 } // if ( fpCurrentFileList && iResult >= 0 ) {
604 HLTError("Current file list could not get created.");
609 fNEventsInFolder = (UInt_t)(nEntries);
614 // #################################################################################
615 Int_t AliHLTESDMCEventPublisherComponent::CloseCurrentFileList() {
616 // see header file for class documentation
622 fNEventsInFolder = 0;
624 // if not set, no files are open ...
625 if ( fpCurrentFileList ) {
627 TObjLink *flnk = fpCurrentFileList->FirstLink();
629 // -- Loop over all files in folder
630 while ( flnk && iResult>=0 ) {
631 FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
635 // If is opened, close it
636 if ( pFileDesc && ( pFile = *pFileDesc ) != NULL )
637 pFileDesc->CloseFile();
639 HLTError("File already open, this should not happen.");
643 if ( (pFile = *pFileDesc ) != NULL ) {
644 HLTError("Closing file %s failed.", pFile->GetName() );
650 } // while ( flnk && iResult>=0 ) {
652 fpCurrentFileList = NULL;
654 } // if ( fpCurrentFileList && iResult >= 0 ) {