]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTHOMERManager.cxx
Merge branch 'displayDevel'
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHOMERManager.cxx
1 //-*- Mode: C++ -*-
2 // $Id$
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   AliHLTHOMERManager.cxx
20     @author Jochen Thaeder
21     @date
22     @brief  Manger for HOMER in aliroot
23 */
24
25 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #if __GNUC__>= 3
32    using namespace std;
33 #endif
34
35 #define EVE_DEBUG 0
36
37 #include "AliHLTHOMERManager.h"
38 // -- -- -- -- -- -- -- 
39 #include "AliHLTHOMERLibManager.h"
40 #include "AliHLTHOMERSourceDesc.h"
41 #include "AliHLTHOMERBlockDesc.h"
42 // -- -- -- -- -- -- -- 
43 #include "AliHLTGlobalTriggerDecision.h"
44 #include "AliHLTTriggerDecision.h"
45 //---------------------------
46
47 ClassImp(AliHLTHOMERManager)
48
49 /*
50  * ---------------------------------------------------------------------------------
51  *                            Constructor / Destructor
52  * ---------------------------------------------------------------------------------
53  */
54
55 //##################################################################################
56   AliHLTHOMERManager::AliHLTHOMERManager() :
57   fLibManager(new AliHLTHOMERLibManager),
58   fStateHasChanged(kTRUE),
59   fProxyHandler(NULL),
60   fCurrentReader(NULL),
61   fReaderList(NULL),
62   fSourceList(NULL),
63   fNBlks(0),
64   fEventID(),
65   fCurrentBlk(0),
66   fAsyncBlockList(NULL),
67   fBlockList(NULL),
68    fEventBuffer(NULL),
69   fBufferTopIdx(-1),
70   fBufferLowIdx(-1),
71   fCurrentBufferIdx(-1),
72   fNavigateBufferIdx(-1),
73   fNEventsAvailable(0),
74   fConnected(kFALSE), 
75   fTriggerString("ALL"), 
76   fNEventsNotTriggered(0),
77   fRetryNextEvent(kFALSE),
78   fIsBlockOwner(kTRUE)
79 {
80   // see header file for class documentation
81   // or
82   // refer to README to build package
83   // or
84   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85
86 }
87
88 //##################################################################################
89 AliHLTHOMERManager::~AliHLTHOMERManager() {
90   // see header file for class documentation
91
92   if ( fLibManager ) {
93
94     if ( fReaderList ) {
95       TIter next(fReaderList);
96       TObject * object = NULL;
97       while ( ( object = next()) )
98         fLibManager->DeleteReader(static_cast<AliHLTHOMERReader*>(object) );
99       
100       fReaderList->Clear();
101       delete fReaderList;
102     }
103     fReaderList = NULL;   
104     
105     delete fLibManager;
106   } 
107   fLibManager = NULL;
108
109   if ( fProxyHandler != NULL )
110     delete fProxyHandler;
111   fProxyHandler = NULL;
112
113   if ( fSourceList != NULL )
114     delete fSourceList;
115   fSourceList = NULL;
116
117   if ( fEventBuffer ) {
118     fEventBuffer->Clear();
119     delete fEventBuffer;
120   }
121   fEventBuffer = NULL;
122
123   if(fBlockList) {
124     fBlockList->Clear();
125     delete fBlockList;
126   }
127   fBlockList = NULL;
128
129   if ( fAsyncBlockList ) {
130     fAsyncBlockList->Clear();
131     delete fAsyncBlockList;
132   }
133   fAsyncBlockList = NULL;
134 }
135
136 //##################################################################################
137 Int_t AliHLTHOMERManager::Initialize() {
138   // see header file for class documentation
139
140   HLTInfo("Initializing");
141
142   Int_t iResult = 0;
143
144   // -- Initialize ProxyHandler
145   if ( !fProxyHandler )
146     fProxyHandler = new AliHLTHOMERProxyHandler();
147   
148   if ( fProxyHandler ) {
149     iResult = fProxyHandler->Initialize();
150     if (iResult)
151       HLTError(Form("Initialize of ProxyHandler failed."));
152   
153   } else {
154     iResult = -1;
155     HLTError(Form("Creating of ProxyHandler failed."));
156   }
157  
158   // -- Initialize ReaderList
159   //    List ist not owner, as reader have to be created/deleted by the LibManager
160   if( !fReaderList )
161     fReaderList = new TList();
162   
163   // -- Initialize asynchronous BlockList
164   if( !fAsyncBlockList ) {
165     fAsyncBlockList = new TList();
166     fAsyncBlockList->SetOwner(kTRUE);
167   }
168
169   //initialize normal block list
170   if( !fBlockList ) {
171     fBlockList = new TList();
172     fBlockList->SetOwner(kFALSE);
173   }
174
175   // -- Initialize Event Buffer and EventID array
176   if ( !fEventBuffer ) {
177     fEventBuffer = new TClonesArray( "TList", BUFFERSIZE );
178   }
179
180   for ( Int_t idx = 0; idx < BUFFERSIZE; ++idx ) {
181     new ((*fEventBuffer)[idx]) TList( );
182     (reinterpret_cast<TList*>((*fEventBuffer)[idx]))->SetOwner(kTRUE);
183     
184     fEventID[idx] = 0;
185   }
186
187   return iResult;
188 }
189
190 /*
191  * ---------------------------------------------------------------------------------
192  *                                 Source Handling
193  * ---------------------------------------------------------------------------------
194  */
195
196 //##################################################################################
197 Int_t AliHLTHOMERManager::CreateSourcesList() {
198   // see header file for class documentation
199
200   Int_t iResult = 0;
201
202   if ( fSourceList != NULL )
203     delete fSourceList;
204   fSourceList = NULL;
205
206   fSourceList = new TList();
207   fSourceList->SetOwner( kTRUE );
208
209   iResult = fProxyHandler->FillSourceList( fSourceList );
210   if ( iResult < 0 ) {
211     HLTWarning(Form("There have been errors, while creating the sources list."));
212   }
213   else if ( iResult > 0 ) {
214     HLTWarning(Form("No active services found."));
215   }
216   else if ( fSourceList->IsEmpty() ) {
217     HLTWarning(Form("No active services in the list."));
218     iResult = 2;
219   }
220   else {
221      HLTInfo(Form("New sources list created."));
222
223     // -- New SourceList has been created 
224     // --> All Sources are new --> State has changed
225     fStateHasChanged = kTRUE;
226   }
227
228   return iResult;
229 }
230
231 //##################################################################################
232 void AliHLTHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
233   // see header file for class documentation
234
235   if ( source->IsSelected() != state ) {
236     source->SetState( state );
237     fStateHasChanged = kTRUE;
238   }
239
240   return;
241 }
242
243 /*
244  * ---------------------------------------------------------------------------------
245  *                         Connection Handling - public
246  * ---------------------------------------------------------------------------------
247  */
248
249 //##################################################################################
250 Int_t AliHLTHOMERManager::ConnectHOMER( TString detector ){
251   // see header file for class documentation
252
253   Int_t iResult = 0;
254
255   // HAck Jochen
256   //----
257   detector="ALL";
258
259   // -- Check if LibManager is present
260   if ( ! fLibManager ) {
261     HLTError(Form("No LibManager present."));
262     return -1;
263   }
264   
265   // -- Check if already connected and state has not changed
266   if ( fStateHasChanged == kFALSE && IsConnected() ) {
267     HLTInfo(Form("No need for reconnection."));
268     return 0;
269   }
270   
271   // -- If already connected, disconnect before connect
272   //    or if ReaderList already filled
273   if ( IsConnected() || fReaderList->GetSize() != 0 ) {
274     HLTInfo(Form("IsConnected: %d      fReaderList.Size:   %d", IsConnected(), fReaderList->GetSize()));
275     DisconnectHOMER();
276   }
277   // -- Create the Readoutlist
278   UShort_t* sourcePorts = new UShort_t [fSourceList->GetEntries()];
279   const Char_t ** sourceHostnames = new const Char_t* [fSourceList->GetEntries()];
280   UInt_t sourceCount = 0;
281
282   CreateReadoutList( sourceHostnames, sourcePorts, sourceCount, detector );
283   if ( sourceCount == 0 ) {
284     HLTError(Form("No sources selected, aborting."));
285     return -2;
286   }
287
288   // ***
289   // *** Connect to data sources
290   // ***
291   
292   for (UInt_t idx = 0; idx < sourceCount; idx++) {
293     
294     if (sourcePorts[idx] > 60000)
295       continue;
296
297     HLTInfo(Form("Adding source %d as %s : %d", idx, sourceHostnames[idx], sourcePorts[idx]));
298     
299     fReaderList->Add(dynamic_cast<TObject*>(fLibManager->OpenReader(sourceHostnames[idx], sourcePorts[idx])));
300     AliHLTHOMERReader *reader = static_cast<AliHLTHOMERReader*>(fReaderList->Last());
301     if ( !reader ) {
302       HLTError(Form("Adding reader failed, aborting"));
303       return -3;
304     }
305
306     if ( (iResult = reader->GetConnectionStatus()) )  {
307
308       // -- Connection to source failed
309       
310       HLTError(Form("Error establishing connection to TCP source %s:%hu: %s (%d)",
311                     sourceHostnames[idx], sourcePorts[idx], strerror(iResult), iResult));
312
313       if( !(TString(sourceHostnames[idx]).CompareTo("localhost")) ) {
314         HLTInfo("The failed connection is on localhost. is SSH tunnel up????? ");
315         HLTInfo(Form("Do: 'ssh -L %d:alihlt-vobox0.cern.ch:%d cernUser@lxplus.cern.ch -fN'",
316                      sourcePorts[idx], sourcePorts[idx]));
317       }
318       
319       // -- Remove reader
320       fReaderList->RemoveLast();
321
322       if ( reader )
323         fLibManager->DeleteReader( reader );
324       reader = NULL;
325       
326       HLTInfo(Form("Removed source %d,  %s : %d from sourceList", idx, sourceHostnames[idx], sourcePorts[idx]));
327       
328     } 
329     else {
330       // -- Connection succeded
331       fConnected = kTRUE;
332
333       HLTInfo(Form("Connection established to source %s on port %d", sourceHostnames[idx], sourcePorts[idx]));
334     }
335     
336   } // for (Int_t idx = 0; idx < sourceCount; idx++) {
337   
338   delete[] sourceHostnames;
339   delete[] sourcePorts;
340
341   return iResult;
342
343 }
344
345 //##################################################################################
346 void AliHLTHOMERManager::DisconnectHOMER(){
347   // see header file for class documentation
348
349   HLTInfo("Disconnecting");
350
351   if ( fReaderList && fLibManager ) {
352     HLTInfo("Deleting readerlist and libmanager");
353     TIter next(fReaderList);
354     TObject * object = NULL;
355     while ( ( object = next()) ) 
356       fLibManager->DeleteReader(static_cast<AliHLTHOMERReader*>(object) );
357       
358
359     HLTInfo(Form("fReaderList size %d", fReaderList->GetSize()));
360     fReaderList->Clear();
361     HLTInfo(Form("fReaderList size %d", fReaderList->GetSize()));
362     delete fReaderList;
363     fReaderList = new TList ();
364     HLTInfo(Form("fReaderList size %d", fReaderList->GetSize()));
365   }
366   
367   fStateHasChanged = kTRUE;
368   fConnected = kFALSE;
369   
370   HLTInfo(Form("Connection closed."));
371
372   return;
373 }
374
375 //##################################################################################
376 Int_t AliHLTHOMERManager::ReconnectHOMER( TString detector="" ){
377   // see header file for class documentation
378   
379   Int_t iResult = 0;
380
381   if ( IsConnected() )
382     DisconnectHOMER();
383
384   iResult = ConnectHOMER(detector);
385   if ( iResult ) {
386     HLTError(Form("Error reconnecting."));
387   }
388
389   return iResult;
390 }
391
392 /*
393  * ---------------------------------------------------------------------------------
394  *                            Event Handling - public
395  * ---------------------------------------------------------------------------------
396  */
397
398 //##################################################################################
399 Int_t AliHLTHOMERManager::NextEvent(){
400  
401   // see header file for class documentation
402   
403   Int_t iResult = 0;
404   Int_t iRetryCount = 0;
405   
406   if ( !IsConnected() || fStateHasChanged ) {
407     HLTInfo("Not connected or state has changed, returning to AliEveHOMERManager, which will deal with this situation");
408     //    cout << "connectecd  " << IsConnected()  << "haschanged  "<<fStateHasChanged << endl;
409     return 55;//ConnectHOMER();
410   }
411   if ( !IsConnected() ) {
412     HLTWarning(Form( "Not connected yet." ));
413     return -1;
414   }
415
416   // -- Reset asyncronous BlockList
417   fAsyncBlockList->Clear();
418
419   // ***
420   // *** Loop over all readers and get new event data
421   // ***
422   
423   TIter next(fReaderList);
424   TObject * object = NULL;
425   
426   while( (object = next()) ) {
427     
428     fCurrentReader = static_cast<AliHLTHOMERReader*>(object);
429     
430     // -- Read next event data and error handling for HOMER (error codes and empty blocks)
431     while ( 1 ) {
432       
433       iResult = fCurrentReader->ReadNextEvent( 40000000 /*timeout in us*/);
434       
435       if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
436         HLTError(Form("No connection to source %d: %s (%d)", 
437                       fCurrentReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
438         break;
439       } 
440       else if ( iResult == 110 ) {
441         HLTError(Form("Timeout occured, reading event from source %d: %s (%d)", 
442                       fCurrentReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
443         break;
444       } 
445       else if ( iResult == 56 ) {
446         ++iRetryCount;
447       
448         if ( iRetryCount >= 20 ) {
449           HLTError(Form("Retry Failed: Error reading event from source %d: %s (%d), returning", 
450                         fCurrentReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
451           break;
452         } 
453         else {
454           HLTError(Form("Retry: Error reading event from source %d: %s (%d), making another attempt (no %d out of 20)", 
455                         fCurrentReader->GetErrorConnectionNdx(), strerror(iResult), iResult, iRetryCount));
456           //break;
457           continue;
458         }
459       }
460       else if ( iResult ) {
461         HLTError(Form("General Error reading event from source %d: %s (%d), giving up", 
462                       fCurrentReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
463         fConnected = kFALSE;
464         break;
465       } 
466       else {
467         HLTDebug("Successfully read out event from source");
468         break;
469       }
470
471     } // while( 1 ) {
472     
473     // -- Check if event could be read
474     if ( iResult ) {
475       HLTInfo("Reading event from source failed");
476       continue;
477     }
478
479     // -- Handle Blocks from current reader
480     iResult = HandleBlocks();
481     if ( iResult ) {
482       HLTError(Form("Handling of blocks failed."));
483     }
484
485   } // while( (object = next()) ) {
486
487   return 0;  
488 }
489
490 /* ---------------------------------------------------------------------------------
491  *                           Buffer Handling - public
492  * ---------------------------------------------------------------------------------
493  */
494
495 //##################################################################################
496 Int_t AliHLTHOMERManager::NavigateEventBufferBack() { 
497   // see header file for class documentation
498
499   // -- reached the end of the buffer
500   if ( fNavigateBufferIdx == fBufferLowIdx )
501     return -1;
502
503   Int_t newIdx = fNavigateBufferIdx - 1;
504   if ( newIdx == -1 )
505     newIdx = BUFFERSIZE-1;
506
507   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
508
509   return newIdx;
510 }
511
512 //##################################################################################
513 Int_t AliHLTHOMERManager::NavigateEventBufferFwd() {
514   // see header file for class documentation
515
516   HLTInfo(Form("fNavigateBufferIdx: %d, fCurrentBufferIdx %d, fBufferTopIdx %d", fNavigateBufferIdx, fCurrentBufferIdx, fBufferTopIdx));
517
518   // -- reached the top of the buffer
519   if ( fNavigateBufferIdx == fBufferTopIdx )
520     return -1;
521
522   Int_t newIdx = fNavigateBufferIdx + 1;
523   if ( newIdx == BUFFERSIZE )
524     newIdx = 0;
525   
526   fCurrentBufferIdx = fNavigateBufferIdx = newIdx;
527   fNEventsAvailable -= 1;
528
529   HLTInfo(Form("fNavigateBufferIdx: %d, fCurrentBufferIdx %d, fBufferTopIdx %d", fNavigateBufferIdx, fCurrentBufferIdx, fBufferTopIdx));
530
531
532   return 0;
533 }
534
535  ///////////////////////////////////////////////////////////////////////////////////
536
537 /*
538  * ---------------------------------------------------------------------------------
539  *                            Connection Handling - private
540  * ---------------------------------------------------------------------------------
541  */
542
543 //##################################################################################
544 void AliHLTHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts, 
545                                             UInt_t &sourceCount, TString detector ){
546   // see header file for class documentation
547
548   AliHLTHOMERSourceDesc * source= NULL;
549
550   // -- Read all sources and check if they should be read out
551   TIter next( fSourceList );
552   while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
553
554     ///Don't use sources from dev cluster
555     if(source->GetPort() > 60000) continue;
556
557     // -- If detector NO detector name given
558     if ( ! detector.CompareTo("ALL") ) {
559       // -- Continue if source is not selected
560       // HACK Jochen
561       //if ( ! source->IsSelected() )
562       //        continue;
563     }
564     // -- DetectorName given
565     else {
566       // -- Continue if detector name doesn't match
567       if ( detector.CompareTo(source->GetDetector()) )
568         continue;
569       else
570         source->Select();
571     }
572     
573     Bool_t exists = kFALSE;
574     
575     // -- Loop over existing entries and check if entry is already in readout list
576     for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
577       if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() ) 
578            && sourcePorts[ii] == source->GetPort() ) {
579         exists = kTRUE;
580         break;
581       }
582     }
583
584     // -- Add new entires to readout list
585     if ( ! exists ) {
586       sourcePorts[sourceCount] = source->GetPort();
587       sourceHostnames[sourceCount] = source->GetHostname().Data();
588       sourceCount++;
589     }
590
591   } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
592
593   fStateHasChanged = kFALSE;
594
595   return;
596 }
597
598 /*
599  * ---------------------------------------------------------------------------------
600  *                          Buffer Handling - private
601  * ---------------------------------------------------------------------------------
602  */
603
604 //##################################################################################
605 void AliHLTHOMERManager::AddBlockListToBuffer() {
606   // see header file for class documentation
607   // -- Check if event is already in buffer
608   ULong_t eventID = static_cast<ULong64_t>(fCurrentReader->GetEventID());  
609   
610   if ( fEventID[fBufferTopIdx] == eventID ) {
611     HLTInfo(Form("Event 0x%016lX (%lu) already in buffer.", eventID, eventID));
612     return;
613   }
614
615   // -- Check if event should be selected on basis of trigger string
616   if( fTriggerString.CompareTo("ALL") ){
617     if ( !CheckTriggerDecision() ) {
618       HLTInfo("Event not triggered");
619       return;
620     } else {
621       HLTInfo("Event triggered");
622     }
623   }
624   else {
625     HLTDebug("No trigger selection.");
626   }
627
628   // -- Set Top mark 
629   ++fBufferTopIdx;
630   if ( fBufferTopIdx == BUFFERSIZE )
631     fBufferTopIdx = 0;
632
633   // -- Change the low mark if necessary
634   if ( fBufferLowIdx == -1 )
635     fBufferLowIdx = 0;
636   else if ( fBufferTopIdx == fBufferLowIdx ) {
637     ++fBufferLowIdx;
638     if ( fBufferLowIdx == BUFFERSIZE )
639       fBufferLowIdx = 0;
640   }
641
642
643   // -- Fill EventID
644   fEventID[fBufferTopIdx] = eventID;
645
646   // -- Clear Buffer slot
647   (reinterpret_cast<TList*>((*fEventBuffer)[fBufferTopIdx]))->Clear();
648   if(fBlockList->IsOwner()) HLTWarning("block list is owner!!");
649   HLTInfo(Form("fBlockList size %d", fBlockList->GetSize()));
650   //fBlockList->Clear();
651   fBlockList = new TList();
652   HLTInfo(Form("fBlockList size %d", fBlockList->GetSize()));
653
654   GetFirstBlk();
655
656   // -- Fill block list
657   do {
658
659     // -- Create new block
660     AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
661     block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
662                      GetBlkType(), GetBlkSpecification() );
663     
664     // -- Check sources list if block is requested
665     if ( CheckIfRequested( block ) ) {
666       (reinterpret_cast<TList*>((*fEventBuffer)[fBufferTopIdx]))->Add( block );
667       fBlockList->Add(block);
668     }
669     else {
670       // XXX HACK Jochen
671       (reinterpret_cast<TList*>((*fEventBuffer)[fBufferTopIdx]))->Add( block );
672       fBlockList->Add(block);
673       // delete block;
674       // block = NULL;
675     }
676  
677   } while( GetNextBlk() );
678
679   //We have one more event available
680   fNEventsAvailable++;
681   HLTInfo(Form("fNEventsAvailable %d", fNEventsAvailable));
682   return;
683 }
684
685 //##################################################################################
686 void AliHLTHOMERManager::AddToAsyncBlockList() {
687   // see header file for class documentation
688
689   HLTInfo("Adding blocks to the asynchroneous block list");
690
691   GetFirstBlk();
692
693   do {
694     
695     AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
696     block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
697                      GetBlkType(), GetBlkSpecification() );
698     
699
700     fAsyncBlockList->Add( block );
701
702     // -- Check sources list if block is requested
703     // if ( CheckIfRequested( block ) ) 
704     //   fAsyncBlockList->Add( block );
705     // else {
706     //   // XXX HACK Jochen
707     // }
708  
709   } while( GetNextBlk() );
710
711   return;
712 }
713 //__________________________________________________________________________________
714 void AliHLTHOMERManager::AddToBlockList() {
715   // see header file for class documentation
716   HLTInfo("Adding blocks to the synchroneous block list");
717
718   
719   GetFirstBlk();
720   do {
721
722     AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
723     block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
724                      GetBlkType(), GetBlkSpecification() );
725     fBlockList->Add( block );
726   
727   } while( GetNextBlk() );  
728 }
729
730 //__________________________________________________________________________________
731 TList* AliHLTHOMERManager::GetBlockListEventBuffer( Int_t idx ) {
732   // see header file for class documentation
733
734   if(fBlockList)
735     return fBlockList;
736   else 
737     return NULL;
738
739   if ( idx == -1 )
740     return NULL;
741  
742   return reinterpret_cast<TList*>((*fEventBuffer)[idx]);
743
744 }
745
746
747 //__________________________________________________________________________________
748 Int_t AliHLTHOMERManager::HandleBlocks() {
749   // see header file for class documentation
750   
751   Int_t iResult = 0;
752
753   // -- Get blockCnt and eventID
754   fNBlks = static_cast<ULong_t>(fCurrentReader->GetBlockCnt());
755   ULong_t eventID = static_cast<ULong64_t>(fCurrentReader->GetEventID());  
756   fCurrentBlk = 0;
757
758   // -- Check if blocks present
759   if ( fNBlks ==  0 ) {
760     HLTWarning(Form("Event 0x%016lX (%lu) with no blocks", eventID, eventID));
761     return -1;
762   }
763
764   HLTInfo(Form("Event 0x%016lX (%lu) with %lu blocks", eventID, eventID, fNBlks));
765     
766   if ( IsSyncBlocks() ) {
767     //AddBlockListToBuffer();
768     AddToBlockList();
769   } else {
770     AddToAsyncBlockList();
771   }
772
773   return iResult;
774 }
775
776 //__________________________________________________________________________________
777 Bool_t AliHLTHOMERManager::IsSyncBlocks() {
778   // see header file for class documentation
779   
780   Bool_t bResult = kFALSE;
781
782   GetFirstBlk();
783   
784   do {          
785     
786     if ( !GetBlkType().CompareTo("ALIESDV0")) {
787       bResult = kTRUE;
788       break;
789     }
790     
791     if ( !GetBlkType().CompareTo("ROOTTOBJ") ) {
792       AliHLTHOMERBlockDesc blockDesc;
793       
794       blockDesc.SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
795                           GetBlkType(), GetBlkSpecification() );
796       if ( !blockDesc.GetClassName().CompareTo("AliHLTGlobalTriggerDecision") ) {
797
798         bResult = kTRUE;
799         break;
800       }
801     }
802
803   } while( GetNextBlk() );
804
805
806   return bResult;
807 }
808
809 //##################################################################################
810 void* AliHLTHOMERManager::GetBlk( Int_t ndx ) {
811   // see header file for class documentation
812   // Get pointer to current block in current event
813    
814   if ( !fCurrentReader || !IsConnected() ) {
815     HLTError(Form("Not connected yet."));
816     return NULL;
817   }
818   if ( ndx < static_cast<Int_t>(fNBlks) )
819     return  const_cast<void*> (fCurrentReader->GetBlockData(ndx));
820   else
821     return NULL;
822 }
823
824 //##################################################################################
825 ULong_t AliHLTHOMERManager::GetBlkSize( Int_t ndx ) {
826   // see header file for class documentation
827    
828   if ( !fCurrentReader || !IsConnected() ) {
829     HLTError(Form("Not connected yet."));
830     return 0;
831   }
832   
833   if ( ndx < static_cast<Int_t>(fNBlks) )
834     return static_cast<ULong_t> (fCurrentReader->GetBlockDataLength(ndx));
835   else
836     return 0;
837 }
838
839 //##################################################################################
840 TString AliHLTHOMERManager::GetBlkOrigin( Int_t ndx ) {
841   // see header file for class documentation
842
843   TString origin = "";
844
845   // -- Check for Connection
846   if ( !fCurrentReader || ! IsConnected() ) {
847     HLTError(Form("Not connected yet."));
848     return origin;
849   }
850
851   // -- Check block index
852   if ( ndx >= static_cast<Int_t>(fNBlks) ) {
853     HLTError(Form("Block index %d out of range.", ndx ));
854     return origin;
855   }
856
857   // -- Get origin
858   union{
859     UInt_t data;
860     Char_t array[4];
861   } reverseOrigin;
862
863   reverseOrigin.data = static_cast<UInt_t>(fCurrentReader->GetBlockDataOrigin(ndx));
864
865   // -- Reverse the order
866   for (Int_t ii = 3; ii >= 0; ii-- )
867     if ( reverseOrigin.array[ii] != ' ')
868       origin.Append( reverseOrigin.array[ii] );
869
870   origin.Remove( TString::kTrailing, ' ' );
871
872   return origin;
873 }
874
875 //##################################################################################
876 TString AliHLTHOMERManager::GetBlkType( Int_t ndx ) {
877   // see header file for class documentation
878
879   TString type = "";
880
881   // -- Check for Connection
882   if ( !fCurrentReader || ! IsConnected() ) {
883     HLTError(Form("Not connected yet."));
884     return type;
885   }
886
887   // -- Check block index
888   if ( ndx >= static_cast<Int_t>(fNBlks) ) {
889     HLTError(Form("Block index %d out of range.", ndx ));
890     return type;
891   }
892
893   // -- Get type
894   union{
895     ULong64_t data;
896     Char_t array[8];
897   } reverseType;
898
899   reverseType.data = static_cast<ULong64_t> (fCurrentReader->GetBlockDataType(ndx));
900
901   // -- Reverse the order
902   for (Int_t ii = 7; ii >= 0; ii-- )
903     if ( reverseType.array[ii] != ' ')
904       type.Append( reverseType.array[ii] );
905   
906   type.Remove( TString::kTrailing, ' ' );
907
908   return type;
909 }
910
911 //##################################################################################
912 ULong_t AliHLTHOMERManager::GetBlkSpecification( Int_t ndx ) {
913   // see header file for class documentation
914
915   // -- Check for Connection
916   if ( !fCurrentReader || ! IsConnected() ) {
917     HLTError(Form("Not connected yet."));
918     return 0;
919   }
920
921   // -- Check block index
922   if ( ndx >= static_cast<Int_t>(fNBlks) ) {
923     HLTError(Form("Block index %d out of range.", ndx ));
924     return 0;
925   }
926
927   return static_cast<ULong_t>(fCurrentReader->GetBlockDataSpec(ndx));
928 }
929
930 //##################################################################################
931 Bool_t AliHLTHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
932   // see header file for class documentation
933
934   Bool_t requested = kFALSE;
935
936   AliHLTHOMERSourceDesc * source= NULL;
937
938   // -- Read all sources and check if they should be read out
939   TIter next( fSourceList );
940   while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
941     
942     // -- Check if source is selected
943     if ( ! source->IsSelected() )
944       continue;
945     
946     // -- Check if detector matches
947     if ( source->GetSourceName().CompareTo( block->GetBlockName() ) )
948       continue;
949
950     requested = kTRUE;
951     break;
952
953   } // while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
954   
955 #if EVE_DEBUG
956   if ( requested ) {
957     HLTInfo(Form("Block requested : %s", block->GetBlockName().Data())); 
958   }
959   else {
960     HLTInfo(Form("Block NOT requested : %s", block->GetBlockName().Data())); 
961   }
962 #endif
963
964   return requested;
965 }
966
967 /* ---------------------------------------------------------------------------------
968  *                          Trigger Handling - private
969  * ---------------------------------------------------------------------------------
970  */
971
972 //##################################################################################
973 Bool_t AliHLTHOMERManager::CheckTriggerDecision() {
974   // see header file for class documentation
975
976   Bool_t triggered = kFALSE;
977
978   if ( !fCurrentReader || !IsConnected() ) {
979     HLTError(Form("Not connected yet."));
980     return kFALSE;
981   }
982
983   AliHLTHOMERBlockDesc blockDesc;
984
985   GetFirstBlk();
986   
987   // -- Fill block list
988   Bool_t foundTriggerBlock = kFALSE;
989   
990   do {
991     if ( (GetBlkType().CompareTo("ROOTTOBJ") == 0) ) {
992       blockDesc.SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
993                           GetBlkType(), GetBlkSpecification() );
994
995       if ( ! blockDesc.GetClassName().CompareTo("AliHLTGlobalTriggerDecision") ) {
996
997         foundTriggerBlock = kTRUE;
998         break;
999       }
1000       
1001     }
1002   } while( GetNextBlk() );
1003   
1004   if ( !foundTriggerBlock ) {
1005     HLTError(Form("No trigger decision object found"));
1006     return kFALSE;
1007   }
1008
1009   // -- Get the global decision object
1010   AliHLTGlobalTriggerDecision* globalDecision = 
1011     static_cast<AliHLTGlobalTriggerDecision*>(blockDesc.GetTObject());
1012
1013   if ( fTriggerString.CompareTo("HLTGlobalTrigger") == 0 ) {
1014     triggered = globalDecision->EventTriggered();
1015   } 
1016   else {
1017     
1018     for (Int_t idx = 0; idx < globalDecision->NumberOfInputObjects(); idx++) {
1019        
1020       const AliHLTTriggerDecision* triggerDecision = 
1021         reinterpret_cast<const AliHLTTriggerDecision*>(globalDecision->InputObject(idx));
1022     
1023       if ( !(fTriggerString.CompareTo(triggerDecision->Description())) ) {
1024         triggered = triggerDecision->EventTriggered();
1025         break;
1026       }
1027     } // for (Int_t idx = 0; idx < globalDecision->NumberOfInputObjects(); idx++) {
1028   }
1029
1030
1031
1032   if ( triggered ) {
1033     fRetryNextEvent = kFALSE;
1034     fNEventsNotTriggered = 0;
1035   }
1036   else {
1037     fRetryNextEvent = kTRUE;
1038     ++fNEventsNotTriggered;
1039   }
1040
1041   return triggered;
1042 }