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"
43 ClassImp(AliHLTHOMERManager)
46 * ---------------------------------------------------------------------------------
47 * Constructor / Destructor
48 * ---------------------------------------------------------------------------------
51 //##################################################################################
52 AliHLTHOMERManager::AliHLTHOMERManager() :
53 fLibManager(new AliHLTHOMERLibManager),
62 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("Initialize of ProxyHandler failed.");
112 HLTError("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("There have been errors, while creating the sources list.");
141 else if ( iResult > 0 ) {
142 HLTWarning("No active services found.");
145 HLTInfo("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("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("No sources selected, aborting.");
200 // *** Connect to data sources
201 if ( !fReader && fLibManager )
202 fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
204 HLTError("No LibManager present.");
208 iResult = fReader->GetConnectionStatus();
210 // -- Connection failed
212 UInt_t ndx = fReader->GetErrorConnectionNdx();
214 if ( ndx < sourceCount ) {
215 HLTError("Error establishing connection to TCP source %s:%hu: %s (%d)",
216 sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult);
219 HLTError("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("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("Connection closed.");
259 //##################################################################################
260 Int_t AliHLTHOMERManager::ReconnectHOMER( TString detector="" ){
261 // see header file for class documentation
268 iResult = ConnectHOMER(detector);
270 HLTError("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( "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("No Connection to source %d: %s (%d)",
304 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult);
307 else if ( iResult == 110 ) {
308 HLTError("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("Retry Failed: Error reading event from source %d: %s (%d)",
317 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult);
321 HLTError("Retry: Error reading event from source %d: %s (%d)",
322 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult);
326 else if ( iResult ) {
327 HLTError("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("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("Block %lu length: %lu - type: %s - origin: %s",
357 ii, fReader->GetBlockDataLength( ii ), tmp1, tmp2);
358 } // end for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
361 // -- Create BlockList
362 HLTInfo("Create Block List");
369 * ---------------------------------------------------------------------------------
370 * Connection Handling - private
371 * ---------------------------------------------------------------------------------
374 //##################################################################################
375 void AliHLTHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts,
376 UInt_t &sourceCount, TString detector ){
377 // see header file for class documentation
379 AliHLTHOMERSourceDesc * source= NULL;
381 // -- Read all sources and check if they should be read out
382 TIter next( fSourceList );
383 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
387 // -- If detector name given
388 if ( ! detector.IsNull() ) {
389 // -- Continue if detector name doesn't match
390 if ( detector.CompareTo(source->GetDetector()) )
394 // -- Continue if source is not selected
395 if ( ! source->IsSelected() )
399 Bool_t exists = kFALSE;
401 // -- Loop over existing entries and check if entry is already in readout list
402 for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
403 if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() )
404 && sourcePorts[ii] == source->GetPort() ) {
410 // -- Add new entires to readout list
412 sourcePorts[sourceCount] = source->GetPort();
413 sourceHostnames[sourceCount] = source->GetHostname().Data();
417 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
419 fStateHasChanged = kFALSE;
425 * ---------------------------------------------------------------------------------
427 * ---------------------------------------------------------------------------------
431 //##################################################################################
432 void AliHLTHOMERManager::CreateBlockList() {
433 // see header file for class documentation
435 // -- Initialize block list
436 if ( fBlockList != NULL )
440 fBlockList = new TList();
441 fBlockList->SetOwner(kTRUE);
445 // -- Fill block list
448 // -- Create new block
449 AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
450 block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
451 GetBlkType(), GetBlkSpecification() );
453 // -- Check sources list if block is requested
454 if ( CheckIfRequested( block ) )
455 fBlockList->Add( block );
461 } while( GetNextBlk() );
467 * ---------------------------------------------------------------------------------
469 * ---------------------------------------------------------------------------------
472 //##################################################################################
473 void* AliHLTHOMERManager::GetBlk( Int_t ndx ) {
474 // see header file for class documentation
475 // Get pointer to current block in current event
477 if ( !fReader || !IsConnected() ) {
478 HLTError("Not connected yet.");
481 if ( ndx < static_cast<Int_t>(fNBlks) )
482 return const_cast<void*> (fReader->GetBlockData(ndx));
487 //##################################################################################
488 ULong_t AliHLTHOMERManager::GetBlkSize( Int_t ndx ) {
489 // see header file for class documentation
491 if ( !fReader || !IsConnected() ) {
492 HLTError("Not connected yet.");
496 if ( ndx < static_cast<Int_t>(fNBlks) )
497 return static_cast<ULong_t> (fReader->GetBlockDataLength(ndx));
502 //##################################################################################
503 TString AliHLTHOMERManager::GetBlkOrigin( Int_t ndx ) {
504 // see header file for class documentation
508 // -- Check for Connection
509 if ( !fReader || ! IsConnected() ) {
510 HLTError("Not connected yet.");
514 // -- Check block index
515 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
516 HLTError("Block index %d out of range.", ndx );
526 reverseOrigin.data = static_cast<UInt_t>(fReader->GetBlockDataOrigin(ndx));
528 // -- Reverse the order
529 for (Int_t ii = 3; ii >= 0; ii-- )
530 if ( reverseOrigin.array[ii] != ' ')
531 origin.Append( reverseOrigin.array[ii] );
533 origin.Remove( TString::kTrailing, ' ' );
538 //##################################################################################
539 TString AliHLTHOMERManager::GetBlkType( Int_t ndx ) {
540 // see header file for class documentation
544 // -- Check for Connection
545 if ( !fReader || ! IsConnected() ) {
546 HLTError("Not connected yet.");
550 // -- Check block index
551 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
552 HLTError("Block index %d out of range.", ndx );
562 reverseType.data = static_cast<ULong64_t> (fReader->GetBlockDataType(ndx));
564 // -- Reverse the order
565 for (Int_t ii = 7; ii >= 0; ii-- )
566 if ( reverseType.array[ii] != ' ')
567 type.Append( reverseType.array[ii] );
569 type.Remove( TString::kTrailing, ' ' );
574 //##################################################################################
575 ULong_t AliHLTHOMERManager::GetBlkSpecification( Int_t ndx ) {
576 // see header file for class documentation
578 // -- Check for Connection
579 if ( !fReader || ! IsConnected() ) {
580 HLTError("Not connected yet.");
584 // -- Check block index
585 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
586 HLTError("Block index %d out of range.", ndx );
590 return static_cast<ULong_t>(fReader->GetBlockDataSpec(ndx));
593 //##################################################################################
594 Bool_t AliHLTHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
595 // see header file for class documentation
597 Bool_t requested = kFALSE;
599 AliHLTHOMERSourceDesc * source= NULL;
601 // -- Read all sources and check if they should be read out
602 TIter next( fSourceList );
603 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
605 // -- Check if source is selected
606 if ( ! source->IsSelected() )
609 // -- Check if detector matches
610 if ( source->GetSourceName().CompareTo( block->GetBlockName() ) )
616 } // while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
620 HLTInfo ("Block requested : %s", block->GetBlockName().Data());
623 HLTInfo("Block NOT requested : %s", block->GetBlockName().Data());