Minor bugfixes and extensions for the onlineDiplay Interface
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTHOMERManager.cxx
CommitLineData
06272c83 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
703482d8 35#define EVE_DEBUG 1
06272c83 36
37#include "AliHLTHOMERManager.h"
38// -- -- -- -- -- -- --
39#include "AliHLTHOMERLibManager.h"
40#include "AliHLTHOMERSourceDesc.h"
41#include "AliHLTHOMERBlockDesc.h"
e728e5ba 42// -- -- -- -- -- -- --
06272c83 43ClassImp(AliHLTHOMERManager)
44
45/*
46 * ---------------------------------------------------------------------------------
47 * Constructor / Destructor
48 * ---------------------------------------------------------------------------------
49 */
50
51//##################################################################################
52 AliHLTHOMERManager::AliHLTHOMERManager() :
53 fLibManager(new AliHLTHOMERLibManager),
a82a31af 54 fStateHasChanged(kTRUE),
06272c83 55 fProxyHandler(NULL),
56 fReader(NULL),
57 fSourceList(NULL),
58 fBlockList(NULL),
59 fNBlks(0),
60 fEventID(0),
61 fCurrentBlk(0),
a82a31af 62 fConnected(kFALSE) {
06272c83 63 // see header file for class documentation
64 // or
65 // refer to README to build package
66 // or
67 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
68
69}
70
71//##################################################################################
72AliHLTHOMERManager::~AliHLTHOMERManager() {
73 // see header file for class documentation
74
75 if ( fLibManager ) {
76 if ( fReader )
77 fLibManager->DeleteReader(fReader);
78 delete fLibManager;
79 fLibManager = NULL;
80 fReader = NULL;
81 }
82
83 if ( fProxyHandler != NULL )
84 delete fProxyHandler;
85 fProxyHandler = NULL;
86
87 if ( fSourceList != NULL )
88 delete fSourceList;
89 fSourceList = NULL;
90
91 if ( fBlockList != NULL )
92 delete fBlockList;
93 fBlockList = NULL;
94}
95
96//##################################################################################
97Int_t AliHLTHOMERManager::Initialize() {
98 // see header file for class documentation
99
100 Int_t iResult = 0;
101
102 if ( !fProxyHandler )
103 fProxyHandler = new AliHLTHOMERProxyHandler();
104
105 if ( fProxyHandler ) {
106 iResult = fProxyHandler->Initialize();
107 if (iResult)
b6bb7ca3 108 HLTError(Form("Initialize of ProxyHandler failed."));
06272c83 109 }
110 else {
111 iResult = -1;
b6bb7ca3 112 HLTError(Form("Creating of ProxyHandler failed."));
06272c83 113 }
114
115 return iResult;
116}
117
118/*
119 * ---------------------------------------------------------------------------------
120 * Source Handling
121 * ---------------------------------------------------------------------------------
122 */
123
124//##################################################################################
125Int_t AliHLTHOMERManager::CreateSourcesList() {
126 // see header file for class documentation
127
128 Int_t iResult = 0;
129
130 if ( fSourceList != NULL )
131 delete fSourceList;
132 fSourceList = NULL;
133
134 fSourceList = new TList();
135 fSourceList->SetOwner( kTRUE );
136
137 iResult = fProxyHandler->FillSourceList( fSourceList );
ae17e63c 138 if ( iResult < 0 ) {
b6bb7ca3 139 HLTWarning(Form("There have been errors, while creating the sources list."));
06272c83 140 }
ae17e63c 141 else if ( iResult > 0 ) {
b6bb7ca3 142 HLTWarning(Form("No active services found."));
ae17e63c 143 }
9e78371d 144 else if ( fSourceList->IsEmpty() ) {
145 HLTWarning(Form("No active services in the list."));
146 iResult = 2;
147 }
06272c83 148 else {
b6bb7ca3 149 HLTInfo(Form("New sources list created."));
06272c83 150
151 // -- New SourceList has been created
152 // --> All Sources are new --> State has changed
153 fStateHasChanged = kTRUE;
154 }
155
156 return iResult;
157}
158
159//##################################################################################
160void AliHLTHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
161 // see header file for class documentation
162
163 if ( source->IsSelected() != state ) {
164 source->SetState( state );
165 fStateHasChanged = kTRUE;
166 }
167
168 return;
169}
170
171/*
172 * ---------------------------------------------------------------------------------
173 * Connection Handling -oublic
174 * ---------------------------------------------------------------------------------
175 */
176
177//##################################################################################
e728e5ba 178Int_t AliHLTHOMERManager::ConnectHOMER( TString detector ){
06272c83 179 // see header file for class documentation
180
181 Int_t iResult = 0;
182
9e78371d 183 // -- Check if LibManager is present
184 if ( ! fLibManager ) {
185 HLTError(Form("No LibManager present."));
186 return -1;
187 }
188
06272c83 189 // -- Check if already connected and state has not changed
190 if ( fStateHasChanged == kFALSE && IsConnected() ) {
b6bb7ca3 191 HLTInfo(Form("No need for reconnection."));
06272c83 192 return iResult;
193 }
9e78371d 194
06272c83 195 // -- If already connected, disconnect before connect
196 if ( IsConnected() )
197 DisconnectHOMER();
9e78371d 198
06272c83 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;
9e78371d 203
4faff50e 204 CreateReadoutList( sourceHostnames, sourcePorts, sourceCount, detector );
06272c83 205 if ( sourceCount == 0 ) {
b6bb7ca3 206 HLTError(Form("No sources selected, aborting."));
9e78371d 207 return -2;
06272c83 208 }
209
210 // *** Connect to data sources
9e78371d 211 if ( !fReader )
06272c83 212 fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
9e78371d 213
06272c83 214 iResult = fReader->GetConnectionStatus();
215 if ( iResult ) {
216 // -- Connection failed
217
218 UInt_t ndx = fReader->GetErrorConnectionNdx();
219
220 if ( ndx < sourceCount ) {
b6bb7ca3 221 HLTError(Form("Error establishing connection to TCP source %s:%hu: %s (%d)",
e728e5ba 222 sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult));
06272c83 223 }
224 else {
b6bb7ca3 225 HLTError(Form("Error establishing connection to unknown source with index %d: %s (%d)",
e728e5ba 226 ndx, strerror(iResult), iResult));
06272c83 227 }
228
229 if ( fReader )
230 fLibManager->DeleteReader( fReader );
231 fReader = NULL;
232 }
233 else {
234 // -- Connection ok - set reader
235 fConnected = kTRUE;
236
b6bb7ca3 237 HLTInfo(Form("Connection established."));
06272c83 238 }
239
240 delete[] sourceHostnames;
241 delete[] sourcePorts;
242
243 return iResult;
244}
245
246//##################################################################################
247void AliHLTHOMERManager::DisconnectHOMER(){
248 // see header file for class documentation
249
250 if ( ! IsConnected() )
251 return;
252
253 if ( fReader )
254 fLibManager->DeleteReader( fReader );
255 fReader = NULL;
256
257 fStateHasChanged = kTRUE;
258 fConnected = kFALSE;
259
b6bb7ca3 260 HLTInfo(Form("Connection closed."));
06272c83 261
262 return;
263}
264
265//##################################################################################
4faff50e 266Int_t AliHLTHOMERManager::ReconnectHOMER( TString detector="" ){
06272c83 267 // see header file for class documentation
268
269 Int_t iResult = 0;
270
271 if ( IsConnected() )
272 DisconnectHOMER();
273
4faff50e 274 iResult = ConnectHOMER(detector);
06272c83 275 if ( iResult ) {
b6bb7ca3 276 HLTError(Form("Error reconnecting."));
06272c83 277 }
278
279 return iResult;
280}
281
282/*
283 * ---------------------------------------------------------------------------------
284 * Event Handling - public
285 * ---------------------------------------------------------------------------------
286 */
287
06272c83 288//##################################################################################
289Int_t AliHLTHOMERManager::NextEvent(){
290 // see header file for class documentation
291
292 Int_t iResult = 0;
293 Int_t iRetryCount = 0;
9e78371d 294
295 if ( !IsConnected() || fStateHasChanged )
296 ConnectHOMER();
297
298 if ( !fReader || !IsConnected() ) {
b6bb7ca3 299 HLTWarning(Form( "Not connected yet." ));
06272c83 300 return -1;
301 }
302
303 // fReader->SetEventRequestAdvanceTime( 20000000 /*timeout in us*/ );
304
305 // -- Read next event data and error handling for HOMER (error codes and empty blocks)
306 while( 1 ) {
307
308 iResult = fReader->ReadNextEvent( 40000000 /*timeout in us*/);
309
310 if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
b6bb7ca3 311 HLTError(Form("No Connection to source %d: %s (%d)",
e728e5ba 312 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
06272c83 313 return -iResult;
314 }
315 else if ( iResult == 110 ) {
703482d8 316 HLTError(Form("Timeout occured, reading event from source %d: %s (%d)",
e728e5ba 317 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
06272c83 318 return -iResult;
319 }
320 else if ( iResult == 56) {
321 ++iRetryCount;
322
323 if ( iRetryCount >= 20 ) {
b6bb7ca3 324 HLTError(Form("Retry Failed: Error reading event from source %d: %s (%d)",
e728e5ba 325 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
06272c83 326 return -iResult;
327 }
328 else {
b6bb7ca3 329 HLTError(Form("Retry: Error reading event from source %d: %s (%d)",
e728e5ba 330 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
06272c83 331 continue;
332 }
333 }
334 else if ( iResult ) {
b6bb7ca3 335 HLTError(Form("General Error reading event from source %d: %s (%d)",
e728e5ba 336 fReader->GetErrorConnectionNdx(), strerror(iResult), iResult));
06272c83 337 fConnected = kFALSE;
338 return -iResult;
339 }
340 else {
341 break;
342 }
343 } // while( 1 ) {
344
345 // -- Get blockCnt and eventID
346 fNBlks = static_cast<ULong_t>(fReader->GetBlockCnt());
347 fEventID = static_cast<ULong64_t>(fReader->GetEventID());
348 fCurrentBlk = 0;
349
b6bb7ca3 350 HLTInfo(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
06272c83 351
352#if EVE_DEBUG
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 );
358 void *tmp11 = tmp1;
359 ULong64_t* tmp12 = static_cast<ULong64_t*>(tmp11);
e728e5ba 360 *tmp12 = fReader->GetBlockDataType(ii);
06272c83 361 void *tmp21 = tmp2;
362 ULong_t* tmp22 = static_cast<ULong_t*>(tmp21);
e728e5ba 363 *tmp22 = fReader->GetBlockDataOrigin(ii);
b6bb7ca3 364 HLTInfo(Form( "Block %lu length: %lu - type: %s - origin: %s - spec 0x%08X",
e728e5ba 365 ii, fReader->GetBlockDataLength(ii), tmp1, tmp2, fReader->GetBlockDataSpec(ii) ));
06272c83 366 } // end for ( ULong_t ii = 0; ii < fNBlks; ii++ ) {
367#endif
368
369 // -- Create BlockList
74682ab7 370 if ( fNBlks > 0 ) {
b6bb7ca3 371 HLTInfo(Form("Create Block List"));
74682ab7 372 CreateBlockList();
373 }
374 else {
b6bb7ca3 375 HLTWarning(Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks));
74682ab7 376 }
377
06272c83 378 return iResult;
379}
380
381/*
382 * ---------------------------------------------------------------------------------
383 * Connection Handling - private
384 * ---------------------------------------------------------------------------------
385 */
386
387//##################################################################################
4faff50e 388void AliHLTHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts,
389 UInt_t &sourceCount, TString detector ){
06272c83 390 // see header file for class documentation
391
392 AliHLTHOMERSourceDesc * source= NULL;
393
394 // -- Read all sources and check if they should be read out
395 TIter next( fSourceList );
396 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
397
e728e5ba 398 // -- If detector NO detector name given
399 if ( ! detector.CompareTo("ALL") ) {
400 // -- Continue if source is not selected
401 if ( ! source->IsSelected() )
4faff50e 402 continue;
403 }
e728e5ba 404 // -- DetectorName given
4faff50e 405 else {
e728e5ba 406 // -- Continue if detector name doesn't match
407 if ( detector.CompareTo(source->GetDetector()) )
4faff50e 408 continue;
e728e5ba 409 else
410 source->Select();
4faff50e 411 }
06272c83 412
413 Bool_t exists = kFALSE;
414
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() ) {
419 exists = kTRUE;
420 break;
421 }
422 }
423
424 // -- Add new entires to readout list
425 if ( ! exists ) {
426 sourcePorts[sourceCount] = source->GetPort();
427 sourceHostnames[sourceCount] = source->GetHostname().Data();
428 sourceCount++;
429 }
430
431 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
432
433 fStateHasChanged = kFALSE;
434
435 return;
436}
437
438/*
439 * ---------------------------------------------------------------------------------
440 * Event Handling
441 * ---------------------------------------------------------------------------------
442 */
443
444
445//##################################################################################
446void AliHLTHOMERManager::CreateBlockList() {
447 // see header file for class documentation
448
449 // -- Initialize block list
450 if ( fBlockList != NULL )
451 delete fBlockList;
452 fBlockList = NULL;
453
454 fBlockList = new TList();
455 fBlockList->SetOwner(kTRUE);
456
457 GetFirstBlk();
458
459 // -- Fill block list
460 do {
461
462 // -- Create new block
463 AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc();
464 block->SetBlock( GetBlk(), GetBlkSize(), GetBlkOrigin(),
465 GetBlkType(), GetBlkSpecification() );
466
467 // -- Check sources list if block is requested
d8691591 468 if ( CheckIfRequested( block ) ) {
06272c83 469 fBlockList->Add( block );
d8691591 470 }
06272c83 471 else {
9e78371d 472 // XXX HACK Jochen
473 fBlockList->Add( block );
474 // delete block;
475 // block = NULL;
06272c83 476 }
477
478 } while( GetNextBlk() );
479
480 return;
481}
482
483/*
484 * ---------------------------------------------------------------------------------
485 * BlockHandling
486 * ---------------------------------------------------------------------------------
487 */
488
489//##################################################################################
490void* AliHLTHOMERManager::GetBlk( Int_t ndx ) {
491 // see header file for class documentation
492 // Get pointer to current block in current event
493
494 if ( !fReader || !IsConnected() ) {
b6bb7ca3 495 HLTError(Form("Not connected yet."));
06272c83 496 return NULL;
497 }
498 if ( ndx < static_cast<Int_t>(fNBlks) )
499 return const_cast<void*> (fReader->GetBlockData(ndx));
500 else
501 return NULL;
502}
503
504//##################################################################################
505ULong_t AliHLTHOMERManager::GetBlkSize( Int_t ndx ) {
506 // see header file for class documentation
507
508 if ( !fReader || !IsConnected() ) {
b6bb7ca3 509 HLTError(Form("Not connected yet."));
06272c83 510 return 0;
511 }
512
513 if ( ndx < static_cast<Int_t>(fNBlks) )
514 return static_cast<ULong_t> (fReader->GetBlockDataLength(ndx));
515 else
516 return 0;
517}
518
519//##################################################################################
520TString AliHLTHOMERManager::GetBlkOrigin( Int_t ndx ) {
521 // see header file for class documentation
522
523 TString origin = "";
524
525 // -- Check for Connection
526 if ( !fReader || ! IsConnected() ) {
b6bb7ca3 527 HLTError(Form("Not connected yet."));
06272c83 528 return origin;
529 }
530
531 // -- Check block index
532 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
b6bb7ca3 533 HLTError(Form("Block index %d out of range.", ndx ));
06272c83 534 return origin;
535 }
536
537 // -- Get origin
538 union{
539 UInt_t data;
540 Char_t array[4];
541 } reverseOrigin;
542
543 reverseOrigin.data = static_cast<UInt_t>(fReader->GetBlockDataOrigin(ndx));
544
545 // -- Reverse the order
546 for (Int_t ii = 3; ii >= 0; ii-- )
547 if ( reverseOrigin.array[ii] != ' ')
548 origin.Append( reverseOrigin.array[ii] );
549
550 origin.Remove( TString::kTrailing, ' ' );
551
552 return origin;
553}
554
555//##################################################################################
556TString AliHLTHOMERManager::GetBlkType( Int_t ndx ) {
557 // see header file for class documentation
558
559 TString type = "";
560
561 // -- Check for Connection
562 if ( !fReader || ! IsConnected() ) {
b6bb7ca3 563 HLTError(Form("Not connected yet."));
06272c83 564 return type;
565 }
566
567 // -- Check block index
568 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
b6bb7ca3 569 HLTError(Form("Block index %d out of range.", ndx ));
06272c83 570 return type;
571 }
572
573 // -- Get type
574 union{
575 ULong64_t data;
576 Char_t array[8];
577 } reverseType;
578
579 reverseType.data = static_cast<ULong64_t> (fReader->GetBlockDataType(ndx));
580
581 // -- Reverse the order
582 for (Int_t ii = 7; ii >= 0; ii-- )
583 if ( reverseType.array[ii] != ' ')
584 type.Append( reverseType.array[ii] );
585
586 type.Remove( TString::kTrailing, ' ' );
587
588 return type;
589}
590
591//##################################################################################
592ULong_t AliHLTHOMERManager::GetBlkSpecification( Int_t ndx ) {
593 // see header file for class documentation
594
595 // -- Check for Connection
596 if ( !fReader || ! IsConnected() ) {
b6bb7ca3 597 HLTError(Form("Not connected yet."));
06272c83 598 return 0;
599 }
600
601 // -- Check block index
602 if ( ndx >= static_cast<Int_t>(fNBlks) ) {
b6bb7ca3 603 HLTError(Form("Block index %d out of range.", ndx ));
06272c83 604 return 0;
605 }
606
607 return static_cast<ULong_t>(fReader->GetBlockDataSpec(ndx));
608}
609
610//##################################################################################
611Bool_t AliHLTHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
612 // see header file for class documentation
613
614 Bool_t requested = kFALSE;
615
616 AliHLTHOMERSourceDesc * source= NULL;
617
618 // -- Read all sources and check if they should be read out
619 TIter next( fSourceList );
620 while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
621
622 // -- Check if source is selected
623 if ( ! source->IsSelected() )
624 continue;
9e78371d 625
06272c83 626 // -- Check if detector matches
627 if ( source->GetSourceName().CompareTo( block->GetBlockName() ) )
628 continue;
629
630 requested = kTRUE;
631 break;
632
633 } // while ( ( source = dynamic_cast<AliHLTHOMERSourceDesc*>(next()) ) ) {
634
9e78371d 635#if EVE_DEBUG
06272c83 636 if ( requested ) {
b6bb7ca3 637 HLTInfo(Form("Block requested : %s", block->GetBlockName().Data()));
06272c83 638 }
639 else {
b6bb7ca3 640 HLTInfo(Form("Block NOT requested : %s", block->GetBlockName().Data()));
06272c83 641 }
642#endif
643
644 return requested;
645}
646