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