]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/util/AliHLTESDMCEventPublisherComponent.cxx
New Component:
[u/mrichter/AliRoot.git] / HLT / BASE / util / AliHLTESDMCEventPublisherComponent.cxx
1 // $Id: AliHLTESDMCEventPublisherComponent.cxx 25452 2008-04-25 21:46:04Z richterm $
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: Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
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   AliHLTESDMCEventPublisherComponent.cxx
20     @author Jochen Thaeder
21     @date   
22     @brief  Component for publishing ESD and MC events.
23     @note   The class is used in Offline (AliRoot) context
24 */
25
26 #include "AliHLTESDMCEventPublisherComponent.h"
27
28 #include "TList.h"
29 #include "TTree.h"
30 #include "TKey.h"
31 #include "TFile.h"
32
33 /** ROOT macro for the implementation of ROOT specific class methods */
34 ClassImp(AliHLTESDMCEventPublisherComponent)
35
36 /*
37  * ---------------------------------------------------------------------------------
38  *                            Constructor / Destructor
39  * ---------------------------------------------------------------------------------
40  */
41
42 // #################################################################################
43 AliHLTESDMCEventPublisherComponent::AliHLTESDMCEventPublisherComponent()
44   :
45   AliHLTFilePublisher(),
46   fpCurrentFolder(NULL),
47   fpCurrentFileList(NULL),
48   fCurrentEvent(0),
49   fNEventsInFolder(0),
50   fFolderList(),
51   fSpecification(0),
52   fPublishESD(kFALSE),
53   fPublishHLTESD(kFALSE),
54   fPublishMC(kFALSE),
55   fpTreeESD(NULL),
56   fpTreeHLTESD(NULL),
57   fpTreeE(NULL),
58   fpTreeK(NULL),
59   fpTreeTR(NULL),
60   fpESD(NULL),
61   fpHLTESD(NULL),
62   fpMC(NULL) {
63   // see header file for class documentation
64   // or
65   // refer to README to build package
66   // or
67   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
68
69   // Set file to ROOT-File
70   SetIsRawFile( kFALSE );
71
72   fFolderList.SetOwner();
73 }
74
75 // #################################################################################
76 AliHLTESDMCEventPublisherComponent::~AliHLTESDMCEventPublisherComponent() {
77   // see header file for class documentation
78
79   // file list and file name list are owner of their objects and
80   // delete all the objects
81 }
82
83 /*
84  * ---------------------------------------------------------------------------------
85  * Public functions to implement AliHLTComponent's interface.
86  * These functions are required for the registration process
87  * ---------------------------------------------------------------------------------
88  */
89
90 // #################################################################################
91 const char* AliHLTESDMCEventPublisherComponent::GetComponentID() {
92   // see header file for class documentation
93   return "ESDMCEventPublisher";
94 }
95
96 // #################################################################################
97 AliHLTComponent* AliHLTESDMCEventPublisherComponent::Spawn() {
98   // see header file for class documentation
99   return new AliHLTESDMCEventPublisherComponent;
100 }
101
102 /*
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  * ---------------------------------------------------------------------------------
108  */
109
110 // #################################################################################
111 Int_t AliHLTESDMCEventPublisherComponent::DoInit(Int_t argc, const char** argv) {
112   // see header file for class documentation
113
114   Int_t iResult = 0;
115   Int_t bMissingParam=0;
116   
117   TString argument="";
118
119   fSpecification = kAliHLTVoidDataSpec;
120
121   // -- Loop over all arguments
122   for ( Int_t iter = 0; iter<argc && iResult>=0; iter++) {
123     argument=argv[iter];
124     if (argument.IsNull()) continue;
125     
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;
130       
131       TString parameter(argv[iter]);
132       parameter.Remove(TString::kLeading, ' ');
133       
134       if ( !parameter.CompareTo("ESD") )
135         fPublishESD = kTRUE;
136       else if ( !parameter.CompareTo("HLTESD") )
137         fPublishHLTESD = kTRUE;
138       else if ( !parameter.CompareTo("MC") )
139         fPublishMC = kTRUE;
140       else {
141         HLTError("Wrong parameter %s for argument %s.", parameter.Data(), argument.Data());
142         iResult=-EINVAL;
143       }
144     } 
145     
146     // -- Data path to reconstructed data
147     else if ( ! argument.CompareTo("-datapath") ) {
148       if ((bMissingParam=(++iter>=argc))) break;
149       
150       TString parameter(argv[iter]);
151       parameter.Remove(TString::kLeading, ' ');
152       
153       fFolderList.Add( new TObjString(parameter) );
154     }
155
156     // -- Data Specification 
157     //    -- see header file !!!
158     else if ( ! argument.CompareTo("-dataspec") ) {
159       if ((bMissingParam=(++iter>=argc))) break;
160       
161       TString parameter(argv[iter]);
162       parameter.Remove(TString::kLeading, ' ');
163             
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);
168       else {
169         HLTError("Wrong parameter for argument %s, number expected", argument.Data());
170         iResult=-EINVAL;
171       }
172     }
173
174     // -- Argument not known
175     else {
176       HLTError("Unknown argument %s.", argument.Data());
177       iResult = -EINVAL;
178     }
179     
180   } // for ( Int iter = 0; iter<argc && iResult>=0; iter++) {
181   
182   // -- Check if parameter is missing
183   if ( bMissingParam ) {
184     HLTError("Missing parameter for argument %s.", argument.Data());
185      iResult=-EINVAL;
186   }
187   
188   //  -- Check if at least on entrytype was specified
189   if ( fPublishESD||fPublishHLTESD||fPublishMC ) 
190     AddDataTypesToOutputlist();
191   else {
192     HLTError("At least one -entrytype argument needs to be specified.");
193     iResult=-EINVAL;
194   }
195   
196   // -- Check if at least one datapath was specified
197   if ( !fFolderList.GetEntries() ) {
198     HLTError("At least one -datapath argument needs to be specified.");
199     iResult=-EINVAL;
200   }
201   
202   // -- Add files according to entry type to event
203   if ( iResult >= 0 ) {
204     iResult = InsertFiles();
205   }
206
207   // -- Calculate event size
208   if ( fPublishESD )
209     fMaxSize += sizeof(AliESDEvent);
210   if ( fPublishHLTESD )
211     fMaxSize += sizeof(AliESDEvent);
212   if ( fPublishMC )
213     fMaxSize += sizeof(AliMCEvent);
214
215   if ( fEvents.GetSize() == 0) {
216     HLTError("No Files have been added.");
217     iResult=-EINVAL;
218   }
219
220   if ( iResult < 0 ) {
221     fEvents.Clear();
222   }
223   
224   return iResult;
225 }
226
227 // #################################################################################
228 Int_t AliHLTESDMCEventPublisherComponent::DoDeinit() {
229   // see header file for class documentation
230
231   Int_t iResult = 0;
232
233   if ( fpESD ) 
234     delete fpESD;
235   fpESD = NULL;
236
237   if ( fpHLTESD ) 
238     delete fpHLTESD;
239   fpHLTESD = NULL;
240
241   if ( fpMC ) 
242     delete fpMC;
243   fpMC = NULL;
244
245   CloseCurrentFileList();
246   
247   AliHLTFilePublisher::DoDeinit();
248
249   return iResult;
250 }
251
252 // #################################################################################
253 void AliHLTESDMCEventPublisherComponent::AddDataTypesToOutputlist() {
254   // see header file for class documentation
255
256   // add all different data types to the list
257   if ( fPublishESD  )
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);
263 }
264
265 // #################################################################################
266 Int_t AliHLTESDMCEventPublisherComponent::InsertFiles(){
267   // see header file for class documentation
268
269   Int_t iResult=0;
270
271   EventFiles* pCurrentFolder = NULL;
272
273   TObjString *objPath = 0;
274   TIter next(&fFolderList);
275
276   // Loop over all folders
277   while ( ( objPath = (TObjString*) next() ) && iResult>=0 ) {
278
279     TString pathAliESDs = objPath->GetString();      
280     pathAliESDs.Append("/AliESDs.root");
281
282     TString pathKinematics = objPath->GetString();      
283     pathKinematics.Append("/Kinematics.root");
284
285     TString pathgalice = objPath->GetString();      
286     pathgalice.Append("/galice.root");
287
288     TString pathTrackRefs = objPath->GetString();      
289     pathTrackRefs.Append("/TrackRefs.root");
290
291
292     // Add ESD file for this folder
293     if ( fPublishESD || fPublishHLTESD ) {
294       FileDesc* pDesc=new FileDesc( pathAliESDs.Data(), kAliHLTDataTypeESDObject, kAliHLTVoidDataSpec, kFALSE);
295       if ( pDesc ) 
296         iResult = InsertFile( pCurrentFolder, pDesc );
297       else 
298         iResult = -ENOMEM;
299     }
300
301     // Add MC files for this folder
302     if ( fPublishMC ) {
303       FileDesc* pDescKinematics=new FileDesc( pathKinematics.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
304       if ( pDescKinematics )  
305         iResult = InsertFile( pCurrentFolder, pDescKinematics );
306       else 
307         iResult = -ENOMEM;
308
309       FileDesc* pDescgalice=new FileDesc( pathgalice.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
310       if ( pDescgalice )  
311         iResult = InsertFile( pCurrentFolder, pDescgalice );
312       else 
313         iResult = -ENOMEM;
314
315       FileDesc* pDescTrackRefs=new FileDesc( pathTrackRefs.Data(), kAliHLTDataTypeMCObject, kAliHLTVoidDataSpec, kFALSE);
316       if ( pDescTrackRefs )  
317         iResult = InsertFile( pCurrentFolder, pDescTrackRefs );
318       else 
319         iResult = -ENOMEM;
320     }
321     
322     // -- Add all files belonging to one eventfolder
323     InsertEvent( pCurrentFolder );
324
325   } // while ( ( path = (TObjString*)next() ) ) {
326   
327   return iResult;
328 }
329
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
337
338   if ( !IsDataEvent() ) return 0;
339
340   Int_t iResult=0;
341   size=0;
342
343   // -- Initialize folder list and 
344   //    re-initialize after reaching the end in the event before
345   //    
346   if ( !fpCurrentFolder ) {
347     fpCurrentFolder = GetEventList()->FirstLink();
348
349     // -- Set file list of this folder and open the files
350     if ( fpCurrentFolder ) {
351       iResult = OpenCurrentFileList();
352     }
353     else {
354       HLTError("Folder link can not be retrieved from folder list.");
355       iResult=-ENOENT;
356     }
357   }
358   
359   // -- if file list was properly created
360   if ( iResult >= 0 && fpCurrentFileList ) {
361     
362     // -- Publish ESDs
363     if ( fPublishESD  && fpTreeESD ) {
364       fpTreeESD->GetEntry(fCurrentEvent);
365       PushBack( fpESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginOffline , fSpecification  ); 
366     }
367
368     // -- Publish HLTESDs
369     if ( fPublishHLTESD && fpTreeHLTESD ) {
370       fpTreeHLTESD->GetEntry(fCurrentEvent);
371       PushBack( fpHLTESD, kAliHLTDataTypeESDObject|kAliHLTDataOriginHLT , fSpecification ); 
372     }
373
374     // -- Publish MC
375     if ( fPublishMC ) {
376
377       TObjLink *flnk = fpCurrentFileList->FirstLink();
378       
379       // -- Loop over files in the current list
380       while (flnk && iResult>=0) {
381         
382         FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
383         TFile* pFile = *pFileDesc;
384
385         TString filename = pFile->GetName();
386         filename.Remove(0,filename.Last('/')+1);
387         
388         TString foldername( Form("Event%d", fCurrentEvent) );
389
390         // -- Open Kinematics
391         if ( !filename.CompareTo("Kinematics.root") ){
392
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() );
396             iResult=-EFAULT;
397           }
398           
399           // -- Get Tree K
400           else {
401             TDirectoryFile *dirK = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
402             if ( dirK )
403               fpTreeK = dynamic_cast<TTree*> (dirK->Get("TreeK"));
404             else {
405               HLTError("Directory %s could not be found.",  foldername.Data() );
406               iResult=-EFAULT;
407             }
408           }
409         } // if ( !filename.CompareTo("Kinematics.root") ){
410
411         // -- Open TrackReferences
412         else if ( !filename.CompareTo("TrackRefs.root") ){
413
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() );
417             iResult=-EFAULT;
418           }
419           
420           // -- Get Tree TR
421           else {
422             TDirectoryFile *dirTR = dynamic_cast<TDirectoryFile*> (pFile->Get(foldername));
423             if ( dirTR )
424               fpTreeTR = dynamic_cast<TTree*> (dirTR->Get("TreeTR"));
425             else {
426               HLTError("Directory %s could not be found.",  foldername.Data() );
427               iResult=-EFAULT;
428             }
429           }
430         } // else if ( !filename.CompareTo("TrackRefs.root") ){
431
432         flnk = flnk->Next();
433         
434       } // while (flnk && iResult>=0) {
435       
436       if ( fpMC ) delete fpMC;
437       fpMC = new AliMCEvent();
438
439       if ( fpTreeE && fpTreeK && fpTreeTR ) {
440
441         fpMC->ConnectTreeE(fpTreeE);
442         fpTreeE->GetEntry(fCurrentEvent);
443
444         fpMC->ConnectTreeK(fpTreeK);
445         fpMC->ConnectTreeTR(fpTreeTR);  
446       }
447       else {
448         HLTError("Not all trees needed for MC Event have been created.");
449         iResult=-EFAULT;
450       }
451
452       if ( iResult>=0 && fpMC ) {
453         PushBack( fpMC, kAliHLTDataTypeMCObject|kAliHLTDataOriginOffline , fSpecification ); 
454       }
455
456     } // if ( fPublishMC ) {
457     
458     // -- Next Event in Folder, 
459     //    if last event was already reached:
460     //    -> Reser and close files
461     if ( ++fCurrentEvent >= fNEventsInFolder )
462       iResult = CloseCurrentFileList();
463     
464   } // if ( iResult >= 0 && fCurrentFileList ) {
465
466   // -- Next Folder 
467   //   (fpCurrentEvent will be NULL*pFileDesc if end of events in file is reached)
468   if ( iResult >= 0 && fpCurrentFolder && !fCurrentEvent ) {
469     fpCurrentFolder=fpCurrentFolder->Next();
470
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();
475
476   }
477
478   return iResult;
479 }
480
481 // #################################################################################
482 Int_t AliHLTESDMCEventPublisherComponent::OpenCurrentFileList() {
483   // see header file for class documentation
484
485   Int_t iResult = 0;
486   Int_t nEntries = -1;
487
488   // -- Get Current Folder
489   EventFiles* pCurrentFolderDesc = dynamic_cast<EventFiles*>( fpCurrentFolder->GetObject() );
490
491   // -- Get List of files in current folder  
492   if ( pCurrentFolderDesc )
493     fpCurrentFileList = *pCurrentFolderDesc; // type conversion operator defined
494   else {
495     HLTError("Folder descriptor can not be retrieved from folder list link");
496     iResult=-EFAULT;
497   }
498   
499   if ( fpCurrentFileList && iResult >= 0 ) {
500   
501     TObjLink *flnk = fpCurrentFileList->FirstLink();
502   
503     // -- Loop over all files in folder
504     // ----------------------------------
505     while ( flnk && iResult>=0 ) {
506       FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
507       TFile* pFile = NULL;
508     
509       // If file is not opened yet, open it ... which it should not
510       if ( pFileDesc && ( pFile = *pFileDesc ) == NULL )
511         pFileDesc->OpenFile();
512       else {
513         HLTError("File already open, this should not happen.");
514         iResult=-EFAULT;
515       }
516       
517       if ( (pFile = *pFileDesc ) == NULL ) {
518         HLTError("Opening file failed." );
519         iResult=-EFAULT;
520       }
521
522       // -- Open trees
523       // ---------------
524       TString filename = pFile->GetName();
525       filename.Remove(0,filename.Last('/')+1);
526       
527       // -- Open ESD tree
528       if ( !filename.CompareTo("AliESDs.root") && fPublishESD ) {
529         fpTreeESD = dynamic_cast<TTree*>(pFile->Get("esdTree") );
530
531         if ( fpTreeESD ) {
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());
537             iResult=-EFAULT;
538           }
539         
540           // Setup AliESDEvent
541           if ( fpESD ) delete fpESD;
542           fpESD = new AliESDEvent();        
543           fpESD->ReadFromTree(fpTreeESD); 
544
545         } // if ( fpTreeESD ) {
546         else {
547           HLTError("Getting ESD tree failed for file %s.", pFile->GetName() );
548           iResult=-EFAULT;
549         }
550       } // if ( !filename.CompareTo("AliESDs.root") && fPublishESD ){
551
552
553       // -- Open HLTESD tree
554       if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ) {
555         fpTreeHLTESD = dynamic_cast<TTree*>(pFile->Get("HLTesdTree") );
556
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());
563             iResult=-EFAULT;
564           }
565
566           // Setup AliESDEvent
567           if ( fpHLTESD ) delete fpHLTESD;
568           fpHLTESD = new AliESDEvent();     
569           fpHLTESD->ReadFromTree(fpTreeHLTESD); 
570
571         } // if ( fpTreeHLTESD ) {
572         else {
573           HLTError("Getting HLTESD tree failed for file %s.", pFile->GetName() );
574           iResult=-EFAULT;
575         }
576
577       } // if ( !filename.CompareTo("AliESDs.root") && fPublishHLTESD ){
578
579       // -- Open galice tree : TreeE
580       else if ( !filename.CompareTo("galice.root") && fPublishMC ) {
581         fpTreeE = dynamic_cast<TTree*>(pFile->Get("TE") );
582
583         if ( fpTreeE ) {
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());
589             iResult=-EFAULT;
590           }
591         } // if ( fpTreeE ) {
592         else {
593           HLTError("Getting TreeE failed for file %s.", pFile->GetName() );
594           iResult=-EFAULT;
595         }
596       } // if ( !filename.CompareTo("galice.root") && fPublishMC ){
597
598       flnk = flnk->Next();
599       
600     } // while ( flnk && iResult>=0 ) {
601
602   } // if ( fpCurrentFileList && iResult >= 0 ) {
603   else {
604     HLTError("Current file list could not get created.");
605     iResult=-EFAULT;
606   }
607   
608   if ( iResult >= 0 )
609     fNEventsInFolder = (UInt_t)(nEntries);
610
611   return iResult;  
612 }
613
614 // #################################################################################
615 Int_t AliHLTESDMCEventPublisherComponent::CloseCurrentFileList() {
616   // see header file for class documentation
617
618   Int_t iResult = 0;
619
620   // -- Reset 
621   fCurrentEvent = 0;
622   fNEventsInFolder = 0;
623
624   // if not set, no files are open ...
625   if ( fpCurrentFileList ) {
626     
627     TObjLink *flnk = fpCurrentFileList->FirstLink();
628   
629     // -- Loop over all files in folder
630     while ( flnk && iResult>=0 ) {
631       FileDesc* pFileDesc = dynamic_cast<FileDesc*>( flnk->GetObject() );
632       
633       TFile* pFile = NULL;
634     
635       // If is opened, close it
636       if ( pFileDesc && ( pFile = *pFileDesc ) != NULL )
637         pFileDesc->CloseFile();
638       else {
639         HLTError("File already open, this should not happen.");
640         iResult=-EFAULT;
641       }
642       
643       if ( (pFile = *pFileDesc ) != NULL ) {
644         HLTError("Closing file %s failed.", pFile->GetName() );
645         iResult=-EFAULT;
646       }
647
648       flnk = flnk->Next();
649       
650     } // while ( flnk && iResult>=0 ) {
651
652     fpCurrentFileList = NULL;
653
654   } // if ( fpCurrentFileList && iResult >= 0 ) {
655   
656   return iResult;  
657 }