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. *
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 AliHLTHOMERManager.cxx
20 @author Jochen Thaeder
22 @brief Manger for HOMER in aliroot
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
37 #include "AliHLTHOMERManager.h"
38 // -- -- -- -- -- -- --
39 #include "AliHLTHOMERLibManager.h"
40 #include "AliHLTHOMERSourceDesc.h"
41 #include "AliHLTHOMERBlockDesc.h"
42 // -- -- -- -- -- -- --
43 ClassImp(AliHLTHOMERManager)
46 * ---------------------------------------------------------------------------------
47 * Constructor / Destructor
48 * ---------------------------------------------------------------------------------
51 //##################################################################################
52 AliHLTHOMERManager::AliHLTHOMERManager() :
53 fLibManager(new AliHLTHOMERLibManager),
54 fStateHasChanged(kTRUE),
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
71 //##################################################################################
72 AliHLTHOMERManager::~AliHLTHOMERManager() {
73 // see header file for class documentation
77 fLibManager->DeleteReader(fReader);
83 if ( fProxyHandler != NULL )
87 if ( fSourceList != NULL )
91 if ( fBlockList != NULL )
96 //##################################################################################
97 Int_t AliHLTHOMERManager::Initialize() {
98 // see header file for class documentation
102 if ( !fProxyHandler )
103 fProxyHandler = new AliHLTHOMERProxyHandler();
105 if ( fProxyHandler ) {
106 iResult = fProxyHandler->Initialize();
108 HLTError(Form("Initialize of ProxyHandler failed."));
112 HLTError(Form("Creating of ProxyHandler failed."));
119 * ---------------------------------------------------------------------------------
121 * ---------------------------------------------------------------------------------
124 //##################################################################################
125 Int_t AliHLTHOMERManager::CreateSourcesList() {
126 // see header file for class documentation
130 if ( fSourceList != NULL )
134 fSourceList = new TList();
135 fSourceList->SetOwner( kTRUE );
137 iResult = fProxyHandler->FillSourceList( fSourceList );
139 HLTWarning(Form("There have been errors, while creating the sources list."));
141 else if ( iResult > 0 ) {
142 HLTWarning(Form("No active services found."));
145 HLTInfo(Form("New sources list created."));
147 // -- New SourceList has been created
148 // --> All Sources are new --> State has changed
149 fStateHasChanged = kTRUE;
155 //##################################################################################
156 void AliHLTHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
157 // see header file for class documentation
159 if ( source->IsSelected() != state ) {
160 source->SetState( state );
161 fStateHasChanged = kTRUE;
168 * ---------------------------------------------------------------------------------
169 * Connection Handling -oublic
170 * ---------------------------------------------------------------------------------
173 //##################################################################################
174 Int_t AliHLTHOMERManager::ConnectHOMER( TString detector ){
175 // see header file for class documentation
179 // -- Check if already connected and state has not changed
180 if ( fStateHasChanged == kFALSE && IsConnected() ) {
181 HLTInfo(Form("No need for reconnection."));
185 // -- If already connected, disconnect before connect
189 // -- Create the Readoutlist
190 UShort_t* sourcePorts = new UShort_t [fSourceList->GetEntries()];
191 const char ** sourceHostnames = new const char* [fSourceList->GetEntries()];
192 UInt_t sourceCount = 0;
194 CreateReadoutList( sourceHostnames, sourcePorts, sourceCount, detector );
195 if ( sourceCount == 0 ) {
196 HLTError(Form("No sources selected, aborting."));
200 // *** Connect to data sources
201 if ( !fReader && fLibManager )
202 fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
204 HLTError(Form("No LibManager present."));
208 iResult = fReader->GetConnectionStatus();
210 // -- Connection failed
212 UInt_t ndx = fReader->GetErrorConnectionNdx();
214 if ( ndx < sourceCount ) {
215 HLTError(Form("Error establishing connection to TCP source %s:%hu: %s (%d)",
216 sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult));
219 HLTError(Form("Error establishing connection to unknown source with index %d: %s (%d)",
220 ndx, strerror(iResult), iResult));
224 fLibManager->DeleteReader( fReader );
228 // -- Connection ok - set reader
231 HLTInfo(Form("Connection established."));
234 delete[] sourceHostnames;
235 delete[] sourcePorts;
240 //##################################################################################
241 void AliHLTHOMERManager::DisconnectHOMER(){
242 // see header file for class documentation
244 if ( ! IsConnected() )
248 fLibManager->DeleteReader( fReader );
251 fStateHasChanged = kTRUE;
254 HLTInfo(Form("Connection closed."));
259 //##################################################################################
260 Int_t AliHLTHOMERManager::ReconnectHOMER( TString detector="" ){
261 // see header file for class documentation
268 iResult = ConnectHOMER(detector);
270 HLTError(Form("Error reconnecting."));
277 * ---------------------------------------------------------------------------------
278 * Event Handling - public
279 * ---------------------------------------------------------------------------------
283 //##################################################################################
284 Int_t AliHLTHOMERManager::NextEvent(){
285 // see header file for class documentation
288 Int_t iRetryCount = 0;
290 if ( !fReader || ! IsConnected() ) {
291 HLTWarning(Form( "Not connected yet." ));
295 // fReader->SetEventRequestAdvanceTime( 20000000 /*timeout in us*/ );
297 // -- Read next event data and error handling for HOMER (error codes and empty blocks)
300 iResult = fReader->ReadNextEvent( 40000000 /*timeout in us*/);
302 if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
303 HLTError(Form("No Connection to source %d: %s (%d)",
304 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
307 else if ( iResult == 110 ) {
308 HLTError(Form("Timout occured, reading event from source %d: %s (%d)",
309 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
312 else if ( iResult == 56) {
315 if ( iRetryCount >= 20 ) {
316 HLTError(Form("Retry Failed: Error reading event from source %d: %s (%d)",
317 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
321 HLTError(Form("Retry: Error reading event from source %d: %s (%d)",
322 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
326 else if ( iResult ) {
327 HLTError(Form("General Error reading event from source %d: %s (%d)",
328 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
337 // -- Get blockCnt and eventID
338 fNBlks = static_cast<ULong_t>(fReader->GetBlockCnt());
339 fEventID = static_cast<ULong64_t>(fReader->GetEventID());
342 HLTInfo(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
345 // Loop for Debug only
346 for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
347 Char_t tmp1[9], tmp2[5];
348 memset( tmp1, 0, 9 );
349 memset( tmp2, 0, 5 );
351 ULong64_t* tmp12 = static_cast<ULong64_t*>(tmp11);
352 *tmp12 = fReader->GetBlockDataType(ii);
354 ULong_t* tmp22 = static_cast<ULong_t*>(tmp21);
355 *tmp22 = fReader->GetBlockDataOrigin(ii);
356 HLTInfo(Form( "Block %lu length: %lu - type: %s - origin: %s - spec 0x%08X",
357 ii, fReader->GetBlockDataLength(ii), tmp1, tmp2, fReader->GetBlockDataSpec(ii) ));
358 } // end for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
361 // -- Create BlockList
363 HLTInfo(Form("Create Block List"));
367 HLTWarning(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
374 * ---------------------------------------------------------------------------------
375 * Connection Handling - private
376 * ---------------------------------------------------------------------------------
379 //##################################################################################
380 void AliHLTHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts,
381 UInt_t &sourceCount, TString detector ){
382 // see header file for class documentation
384 AliHLTHOMERSourceDesc * source= NULL;
386 // -- Read all sources and check if they should be read out
387 TIter next( fSourceList );
388 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
390 // -- If detector NO detector name given
391 if ( ! detector.CompareTo("ALL") ) {
392 // -- Continue if source is not selected
393 if ( ! source->IsSelected() )
396 // -- DetectorName given
398 // -- Continue if detector name doesn't match
399 if ( detector.CompareTo(source->GetDetector()) )
405 Bool_t exists = kFALSE;
407 // -- Loop over existing entries and check if entry is already in readout list
408 for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
409 if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() )
410 && sourcePorts[ii] == source->GetPort() ) {
416 // -- Add new entires to readout list
418 sourcePorts[sourceCount] = source->GetPort();
419 sourceHostnames[sourceCount] = source->GetHostname().Data();
423 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
425 fStateHasChanged = kFALSE;
431 * ---------------------------------------------------------------------------------
433 * ---------------------------------------------------------------------------------
437 //##################################################################################
438 void AliHLTHOMERManager::CreateBlockList() {
439 // see header file for class documentation
441 // -- Initialize block list
442 if ( fBlockList != NULL )
446 fBlockList = new TList();
447 fBlockList->SetOwner(kTRUE);
451 // -- Fill block list
454 // -- Create new block
455 AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
456 block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
457 GetBlkType(), GetBlkSpecification() );
459 // -- Check sources list if block is requested
460 if ( CheckIfRequested( block ) ) {
461 fBlockList->Add( block );
469 } while( GetNextBlk() );
475 * ---------------------------------------------------------------------------------
477 * ---------------------------------------------------------------------------------
480 //##################################################################################
481 void* AliHLTHOMERManager::GetBlk( Int_t ndx ) {
482 // see header file for class documentation
483 // Get pointer to current block in current event
485 if ( !fReader || !IsConnected() ) {
486 HLTError(Form("Not connected yet."));
489 if ( ndx < static_cast<Int_t>(fNBlks) )
490 return const_cast<void*> (fReader->GetBlockData(ndx));
495 //##################################################################################
496 ULong_t AliHLTHOMERManager::GetBlkSize( Int_t ndx ) {
497 // see header file for class documentation
499 if ( !fReader || !IsConnected() ) {
500 HLTError(Form("Not connected yet."));
504 if ( ndx < static_cast<Int_t>(fNBlks) )
505 return static_cast<ULong_t> (fReader->GetBlockDataLength(ndx));
510 //##################################################################################
511 TString AliHLTHOMERManager::GetBlkOrigin( Int_t ndx ) {
512 // see header file for class documentation
516 // -- Check for Connection
517 if ( !fReader || ! IsConnected() ) {
518 HLTError(Form("Not connected yet."));
522 // -- Check block index
523 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
524 HLTError(Form("Block index %d out of range.", ndx ));
534 reverseOrigin.data = static_cast<UInt_t>(fReader->GetBlockDataOrigin(ndx));
536 // -- Reverse the order
537 for (Int_t ii = 3; ii >= 0; ii-- )
538 if ( reverseOrigin.array[ii] != ' ')
539 origin.Append( reverseOrigin.array[ii] );
541 origin.Remove( TString::kTrailing, ' ' );
546 //##################################################################################
547 TString AliHLTHOMERManager::GetBlkType( Int_t ndx ) {
548 // see header file for class documentation
552 // -- Check for Connection
553 if ( !fReader || ! IsConnected() ) {
554 HLTError(Form("Not connected yet."));
558 // -- Check block index
559 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
560 HLTError(Form("Block index %d out of range.", ndx ));
570 reverseType.data = static_cast<ULong64_t> (fReader->GetBlockDataType(ndx));
572 // -- Reverse the order
573 for (Int_t ii = 7; ii >= 0; ii-- )
574 if ( reverseType.array[ii] != ' ')
575 type.Append( reverseType.array[ii] );
577 type.Remove( TString::kTrailing, ' ' );
582 //##################################################################################
583 ULong_t AliHLTHOMERManager::GetBlkSpecification( Int_t ndx ) {
584 // see header file for class documentation
586 // -- Check for Connection
587 if ( !fReader || ! IsConnected() ) {
588 HLTError(Form("Not connected yet."));
592 // -- Check block index
593 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
594 HLTError(Form("Block index %d out of range.", ndx ));
598 return static_cast<ULong_t>(fReader->GetBlockDataSpec(ndx));
601 //##################################################################################
602 Bool_t AliHLTHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
603 // see header file for class documentation
605 Bool_t requested = kFALSE;
607 AliHLTHOMERSourceDesc * source= NULL;
609 // -- Read all sources and check if they should be read out
610 TIter next( fSourceList );
611 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
613 // -- Check if source is selected
614 if ( ! source->IsSelected() )
617 // -- Check if detector matches
618 if ( source->GetSourceName().CompareTo( block->GetBlockName() ) )
624 } // while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
628 HLTInfo(Form("Block requested : %s", block->GetBlockName().Data()));
631 HLTInfo(Form("Block NOT requested : %s", block->GetBlockName().Data()));