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."));
144 else if ( fSourceList->IsEmpty() ) {
145 HLTWarning(Form("No active services in the list."));
149 HLTInfo(Form("New sources list created."));
151 // -- New SourceList has been created
152 // --> All Sources are new --> State has changed
153 fStateHasChanged = kTRUE;
159 //##################################################################################
160 void AliHLTHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
161 // see header file for class documentation
163 if ( source->IsSelected() != state ) {
164 source->SetState( state );
165 fStateHasChanged = kTRUE;
172 * ---------------------------------------------------------------------------------
173 * Connection Handling -oublic
174 * ---------------------------------------------------------------------------------
177 //##################################################################################
178 Int_t AliHLTHOMERManager::ConnectHOMER( TString detector ){
179 // see header file for class documentation
183 // -- Check if LibManager is present
184 if ( ! fLibManager ) {
185 HLTError(Form("No LibManager present."));
189 // -- Check if already connected and state has not changed
190 if ( fStateHasChanged == kFALSE && IsConnected() ) {
191 HLTInfo(Form("No need for reconnection."));
195 // -- If already connected, disconnect before connect
199 // -- Create the Readoutlist
200 UShort_t* sourcePorts = new UShort_t [fSourceList->GetEntries()];
201 const char ** sourceHostnames = new const char* [fSourceList->GetEntries()];
202 UInt_t sourceCount = 0;
204 CreateReadoutList( sourceHostnames, sourcePorts, sourceCount, detector );
205 if ( sourceCount == 0 ) {
206 HLTError(Form("No sources selected, aborting."));
210 // *** Connect to data sources
212 fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
214 iResult = fReader->GetConnectionStatus();
216 // -- Connection failed
218 UInt_t ndx = fReader->GetErrorConnectionNdx();
220 if ( ndx < sourceCount ) {
221 HLTError(Form("Error establishing connection to TCP source %s:%hu: %s (%d)",
222 sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult));
225 HLTError(Form("Error establishing connection to unknown source with index %d: %s (%d)",
226 ndx, strerror(iResult), iResult));
230 fLibManager->DeleteReader( fReader );
234 // -- Connection ok - set reader
237 HLTInfo(Form("Connection established."));
240 delete[] sourceHostnames;
241 delete[] sourcePorts;
246 //##################################################################################
247 void AliHLTHOMERManager::DisconnectHOMER(){
248 // see header file for class documentation
250 if ( ! IsConnected() )
254 fLibManager->DeleteReader( fReader );
257 fStateHasChanged = kTRUE;
260 HLTInfo(Form("Connection closed."));
265 //##################################################################################
266 Int_t AliHLTHOMERManager::ReconnectHOMER( TString detector="" ){
267 // see header file for class documentation
274 iResult = ConnectHOMER(detector);
276 HLTError(Form("Error reconnecting."));
283 * ---------------------------------------------------------------------------------
284 * Event Handling - public
285 * ---------------------------------------------------------------------------------
288 //##################################################################################
289 Int_t AliHLTHOMERManager::NextEvent(){
290 // see header file for class documentation
293 Int_t iRetryCount = 0;
295 if ( !IsConnected() || fStateHasChanged )
298 if ( !fReader || !IsConnected() ) {
299 HLTWarning(Form( "Not connected yet." ));
303 // fReader->SetEventRequestAdvanceTime( 20000000 /*timeout in us*/ );
305 // -- Read next event data and error handling for HOMER (error codes and empty blocks)
308 iResult = fReader->ReadNextEvent( 40000000 /*timeout in us*/);
310 if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
311 HLTError(Form("No Connection to source %d: %s (%d)",
312 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
315 else if ( iResult == 110 ) {
316 HLTError(Form("Timout occured, reading event from source %d: %s (%d)",
317 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
320 else if ( iResult == 56) {
323 if ( iRetryCount >= 20 ) {
324 HLTError(Form("Retry Failed: Error reading event from source %d: %s (%d)",
325 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
329 HLTError(Form("Retry: Error reading event from source %d: %s (%d)",
330 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
334 else if ( iResult ) {
335 HLTError(Form("General Error reading event from source %d: %s (%d)",
336 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
345 // -- Get blockCnt and eventID
346 fNBlks = static_cast<ULong_t>(fReader->GetBlockCnt());
347 fEventID = static_cast<ULong64_t>(fReader->GetEventID());
350 HLTInfo(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
353 // Loop for Debug only
354 for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
355 Char_t tmp1[9], tmp2[5];
356 memset( tmp1, 0, 9 );
357 memset( tmp2, 0, 5 );
359 ULong64_t* tmp12 = static_cast<ULong64_t*>(tmp11);
360 *tmp12 = fReader->GetBlockDataType(ii);
362 ULong_t* tmp22 = static_cast<ULong_t*>(tmp21);
363 *tmp22 = fReader->GetBlockDataOrigin(ii);
364 HLTInfo(Form( "Block %lu length: %lu - type: %s - origin: %s - spec 0x%08X",
365 ii, fReader->GetBlockDataLength(ii), tmp1, tmp2, fReader->GetBlockDataSpec(ii) ));
366 } // end for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
369 // -- Create BlockList
371 HLTInfo(Form("Create Block List"));
375 HLTWarning(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
382 * ---------------------------------------------------------------------------------
383 * Connection Handling - private
384 * ---------------------------------------------------------------------------------
387 //##################################################################################
388 void AliHLTHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts,
389 UInt_t &sourceCount, TString detector ){
390 // see header file for class documentation
392 AliHLTHOMERSourceDesc * source= NULL;
394 // -- Read all sources and check if they should be read out
395 TIter next( fSourceList );
396 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
398 // -- If detector NO detector name given
399 if ( ! detector.CompareTo("ALL") ) {
400 // -- Continue if source is not selected
401 if ( ! source->IsSelected() )
404 // -- DetectorName given
406 // -- Continue if detector name doesn't match
407 if ( detector.CompareTo(source->GetDetector()) )
413 Bool_t exists = kFALSE;
415 // -- Loop over existing entries and check if entry is already in readout list
416 for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
417 if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() )
418 && sourcePorts[ii] == source->GetPort() ) {
424 // -- Add new entires to readout list
426 sourcePorts[sourceCount] = source->GetPort();
427 sourceHostnames[sourceCount] = source->GetHostname().Data();
431 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
433 fStateHasChanged = kFALSE;
439 * ---------------------------------------------------------------------------------
441 * ---------------------------------------------------------------------------------
445 //##################################################################################
446 void AliHLTHOMERManager::CreateBlockList() {
447 // see header file for class documentation
449 // -- Initialize block list
450 if ( fBlockList != NULL )
454 fBlockList = new TList();
455 fBlockList->SetOwner(kTRUE);
459 // -- Fill block list
462 // -- Create new block
463 AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
464 block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
465 GetBlkType(), GetBlkSpecification() );
467 // -- Check sources list if block is requested
468 if ( CheckIfRequested( block ) ) {
469 fBlockList->Add( block );
473 fBlockList->Add( block );
478 } while( GetNextBlk() );
484 * ---------------------------------------------------------------------------------
486 * ---------------------------------------------------------------------------------
489 //##################################################################################
490 void* AliHLTHOMERManager::GetBlk( Int_t ndx ) {
491 // see header file for class documentation
492 // Get pointer to current block in current event
494 if ( !fReader || !IsConnected() ) {
495 HLTError(Form("Not connected yet."));
498 if ( ndx < static_cast<Int_t>(fNBlks) )
499 return const_cast<void*> (fReader->GetBlockData(ndx));
504 //##################################################################################
505 ULong_t AliHLTHOMERManager::GetBlkSize( Int_t ndx ) {
506 // see header file for class documentation
508 if ( !fReader || !IsConnected() ) {
509 HLTError(Form("Not connected yet."));
513 if ( ndx < static_cast<Int_t>(fNBlks) )
514 return static_cast<ULong_t> (fReader->GetBlockDataLength(ndx));
519 //##################################################################################
520 TString AliHLTHOMERManager::GetBlkOrigin( Int_t ndx ) {
521 // see header file for class documentation
525 // -- Check for Connection
526 if ( !fReader || ! IsConnected() ) {
527 HLTError(Form("Not connected yet."));
531 // -- Check block index
532 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
533 HLTError(Form("Block index %d out of range.", ndx ));
543 reverseOrigin.data = static_cast<UInt_t>(fReader->GetBlockDataOrigin(ndx));
545 // -- Reverse the order
546 for (Int_t ii = 3; ii >= 0; ii-- )
547 if ( reverseOrigin.array[ii] != ' ')
548 origin.Append( reverseOrigin.array[ii] );
550 origin.Remove( TString::kTrailing, ' ' );
555 //##################################################################################
556 TString AliHLTHOMERManager::GetBlkType( Int_t ndx ) {
557 // see header file for class documentation
561 // -- Check for Connection
562 if ( !fReader || ! IsConnected() ) {
563 HLTError(Form("Not connected yet."));
567 // -- Check block index
568 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
569 HLTError(Form("Block index %d out of range.", ndx ));
579 reverseType.data = static_cast<ULong64_t> (fReader->GetBlockDataType(ndx));
581 // -- Reverse the order
582 for (Int_t ii = 7; ii >= 0; ii-- )
583 if ( reverseType.array[ii] != ' ')
584 type.Append( reverseType.array[ii] );
586 type.Remove( TString::kTrailing, ' ' );
591 //##################################################################################
592 ULong_t AliHLTHOMERManager::GetBlkSpecification( Int_t ndx ) {
593 // see header file for class documentation
595 // -- Check for Connection
596 if ( !fReader || ! IsConnected() ) {
597 HLTError(Form("Not connected yet."));
601 // -- Check block index
602 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
603 HLTError(Form("Block index %d out of range.", ndx ));
607 return static_cast<ULong_t>(fReader->GetBlockDataSpec(ndx));
610 //##################################################################################
611 Bool_t AliHLTHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
612 // see header file for class documentation
614 Bool_t requested = kFALSE;
616 AliHLTHOMERSourceDesc * source= NULL;
618 // -- Read all sources and check if they should be read out
619 TIter next( fSourceList );
620 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
622 // -- Check if source is selected
623 if ( ! source->IsSelected() )
626 // -- Check if detector matches
627 if ( source->GetSourceName().CompareTo( block->GetBlockName() ) )
633 } // while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
637 HLTInfo(Form("Block requested : %s", block->GetBlockName().Data()));
640 HLTInfo(Form("Block NOT requested : %s", block->GetBlockName().Data()));