2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3 // Author: Jochen Thaeder <thaeder@kip.uni-heidelberg.de> *
4 // for The ALICE HLT Project. *
8 /** @file AliEveHOMERManager.cxx
11 @brief Manger for HOMER in offline
18 #include "AliEveHOMERManager.h"
22 #define ROWHOUGHPARAMS
23 #define use_reconstruction
25 #define ROOTVERSION "unchecked"
26 #define ALIROOTVERSION "unchecked"
32 // -- -- -- -- -- -- --
33 #include "AliHLTHOMERLibManager.h"
34 #include "AliHLTHOMERSourceDesc.h"
35 #include "AliHLTHOMERBlockDesc.h"
36 // -- -- -- -- -- -- --
37 #include "AliEveHOMERSource.h"
38 // -- -- -- -- -- -- --
40 #include <TApplication.h>
41 #include "Riostream.h"
43 #include "TCollection.h"
45 #include "TObjString.h"
46 #include "TObjArray.h"
47 // -- -- -- -- -- -- --
49 #include "AliTPCCalibPedestal.h"
50 #include "AliTPCCalibPulser.h"
51 #include "AliTPCCalibCE.h"
52 #include "AliTPCPreprocessorOnline.h"
53 #include "AliTPCCalROC.h"
55 //______________________________________________________________________________
57 // Manage connections to HLT data-sources.
59 ClassImp(AliEveHOMERManager)
62 * ---------------------------------------------------------------------------------
63 * Constructor / Destructor
64 * ---------------------------------------------------------------------------------
67 //##################################################################################
68 AliEveHOMERManager::AliEveHOMERManager( TString xmlFile ) :
69 TEveElementList("AliEveHOMERManager"),
70 fLibManager(new AliHLTHOMERLibManager),
71 fXMLHandler( new AliEveHOMERXMLHandler( xmlFile ) ),
79 fStateHasChanged(kTRUE),
82 // This Class should handle the communication
83 // from the HLT to AliEVE. The HLT sends data via
84 // the HOMER interface on several TCP ports of nodes
85 // in the CERN GPN and DCS network.
86 // All this communication is hidden from the user.
88 // Right now, a xml file ( SCC1 ) is used to get the
89 // configuration, this will/ has to change to a proxy
90 // running on dedicated nodes.
94 //##################################################################################
95 AliEveHOMERManager::~AliEveHOMERManager() {
100 fLibManager->DeleteReader(fReader);
106 if ( fXMLHandler != NULL )
110 if ( fSourceList != NULL )
114 if ( fBlockList != NULL )
118 if ( fSrcList != NULL )
122 if ( fTPCPre != NULL )
129 * ---------------------------------------------------------------------------------
131 * ---------------------------------------------------------------------------------
134 //##################################################################################
135 Int_t AliEveHOMERManager::CreateHOMERSourcesList() {
136 // Create Sources List from HOMER-Proxy
140 // -- Initialize sources list
142 if ( fSourceList != NULL )
146 fSourceList = new TList();
147 fSourceList->SetOwner( kTRUE );
149 iResult = fXMLHandler->FillSourceList( fSourceList );
152 AliWarning( Form("There have been errors, while creating the sources list.") );
155 AliInfo( Form("New sources list created.") );
157 // -- New SourceList has been created --> All Sources are new --> State has changed
158 fStateHasChanged = kTRUE;
163 // -- Create new AliEVE sources list
164 fSrcList = new AliEveHOMERSourceList("HLT Sources");
165 fSrcList->SetManager(this);
167 AddElement(fSrcList);
168 fSrcList->CreateByType();
174 //##################################################################################
175 void AliEveHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
176 // Set state of a source
177 // * param source Pointer to AliHLTHOMERSourceDesc object.
178 // * param state New (selected/not selected) state.
180 if ( source->IsSelected() != state ) {
181 source->SetState( state );
182 fStateHasChanged = kTRUE;
189 * ---------------------------------------------------------------------------------
190 * Connection Handling
191 * ---------------------------------------------------------------------------------
194 //##################################################################################
195 Int_t AliEveHOMERManager::ConnectHOMER(){
196 // Connect to HOMER sources, out of Readout List, which gets created when state has changed
197 // * return 0 on sucess, "HOMER" errors on error
201 fStateHasChanged = fSrcList->GetSelectedSources();
203 // -- Check if already connected and state has not changed
204 if ( fStateHasChanged == kFALSE && IsConnected() ) {
205 AliInfo( Form("No need for reconnection.") );
209 // -- If already connected, disconnect before connect
213 // *** Create the Readoutlist
215 UShort_t* sourcePorts = new UShort_t [fSourceList->GetEntries()];
216 const char ** sourceHostnames = new const char* [fSourceList->GetEntries()];
217 UInt_t sourceCount = 0;
219 CreateReadoutList( sourceHostnames, sourcePorts, sourceCount );
221 if ( sourceCount == 0 ) {
222 AliError(Form("No sources selected, aborting.") );
226 // *** Connect to data sources
230 fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
233 iResult = fReader->GetConnectionStatus();
236 // -- Connection failed
238 UInt_t ndx = fReader->GetErrorConnectionNdx();
240 if ( ndx < sourceCount ) {
241 AliError( Form("Error : Error establishing connection to TCP source %s:%hu: %s (%d)",
242 sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult) );
245 AliError( Form("Error : Error establishing connection to unknown source with index %d: %s (%d)",
246 ndx, strerror(iResult), iResult) );
250 fLibManager->DeleteReader( fReader );
255 // -- Connection ok - set reader
258 AliInfo( Form("Connection established") );
261 delete[] sourceHostnames;
262 delete[] sourcePorts;
267 //##################################################################################
268 void AliEveHOMERManager::DisconnectHOMER(){
269 // Disconnect from HOMER sources
271 if ( ! IsConnected() )
275 fLibManager->DeleteReader( fReader );
278 fStateHasChanged = kTRUE;
281 AliInfo( Form("Connection closed") );
286 //##################################################################################
287 Int_t AliEveHOMERManager::ReconnectHOMER(){
288 // Reconnect from HOMER sources
289 // * return 0 on sucess, "ConnectHOMER()" errors on error
296 iResult = ConnectHOMER();
298 AliError( Form("Error connecting.") );
304 //##################################################################################
305 void AliEveHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts, UInt_t &sourceCount ){
306 // Create a readout list for Hostname and ports
307 // * param socurceHostnames Array of selected hostnames
308 // * param socurcePorts Array of selected ports
309 // * param socurceCount Number of selected hostname:port
311 AliHLTHOMERSourceDesc * source= NULL;
313 // -- Read all sources and check if they should be read out
314 TIter next( fSourceList );
315 while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
317 if ( ! source->IsSelected() )
320 Bool_t exists = kFALSE;
322 // -- Loop over existing entries and check if entry is already in readout list
323 for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
324 if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() ) && sourcePorts[ii] == source->GetPort() ) {
330 // -- Add new entires to readout list
332 sourcePorts[sourceCount] = source->GetPort();
333 sourceHostnames[sourceCount] = source->GetHostname().Data();
337 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
339 fStateHasChanged = kFALSE;
345 * ---------------------------------------------------------------------------------
347 * ---------------------------------------------------------------------------------
350 //##################################################################################
351 Int_t AliEveHOMERManager::NextEvent(){
352 // Loads the next Event, after being connected
353 // * return 0 on sucess, "HOMER" errors on error
356 Int_t iRetryCount = 0;
358 if ( !fReader || ! IsConnected() ) {
359 AliWarning( Form( "Not connected yet." ) );
363 // fReader->SetEventRequestAdvanceTime( 20000000 /*timeout in us*/ );
365 // -- Read next event data and error handling for HOMER (error codes and empty blocks)
368 iResult = fReader->ReadNextEvent( 40000000 /*timeout in us*/);
370 if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
371 Int_t ndx = fReader->GetErrorConnectionNdx();
372 AliError( Form("Error, No Connection to source %d: %s (%d)",
373 ndx, strerror(iResult), iResult) );
376 else if ( iResult == 110 ) {
377 Int_t ndx = fReader->GetErrorConnectionNdx();
378 AliError( Form("Timout occured, reading event from source %d: %s (%d)",
379 ndx, strerror(iResult), iResult) );
382 else if ( iResult == 56) {
383 Int_t ndx = fReader->GetErrorConnectionNdx();
387 if ( iRetryCount >= 20 ) {
388 AliError( Form("Retry Failed: Error reading event from source %d: %s (%d)",
389 ndx, strerror(iResult), iResult) );
393 AliError( Form("Retry: Error reading event from source %d: %s (%d)",
394 ndx, strerror(iResult), iResult) );
398 else if ( iResult ) {
399 Int_t ndx = fReader->GetErrorConnectionNdx();
400 AliError( Form("General Error reading event from source %d: %s (%d)",
401 ndx, strerror(iResult), iResult) );
413 // -- Get blockCnt and eventID
414 fNBlks = (ULong_t) fReader->GetBlockCnt();
415 fEventID = (ULong64_t) fReader->GetEventID();
418 AliInfo( Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks) );
421 // Loop for Debug only
422 for ( ULong_t i = 0; i < fNBlks; i++ ) {
423 Char_t tmp1[9], tmp2[5];
424 memset( tmp1, 0, 9 );
425 memset( tmp2, 0, 5 );
427 ULong64_t* tmp12 = (ULong64_t*)tmp11;
428 *tmp12 = fReader->GetBlockDataType( i );
430 ULong_t* tmp22 = (ULong_t*)tmp21;
431 *tmp22 = fReader->GetBlockDataOrigin( i );
432 AliInfo( Form("Block %lu length: %lu - type: %s - origin: %s",
433 i, fReader->GetBlockDataLength( i ), tmp1, tmp2) );
434 } // end for ( ULong_t i = 0; i < fNBlks; i++ ) {
437 // -- Create BlockList
438 AliInfo( Form("Create Block List") );
439 iResult = CreateBlockList();
444 //##################################################################################
445 Int_t AliEveHOMERManager::CreateBlockList() {
446 // Create a TList of blocks, which have been readout
450 // -- Initialize block list
451 if ( fBlockList != NULL )
455 fBlockList = new TList();
456 fBlockList->SetOwner( kTRUE );
458 void* iter = GetFirstBlk();
460 // -- Fill block list
461 while ( iter != NULL ){
463 // -- Create new block
464 AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc( GetBlk(), GetBlkSize(), GetBlkOrigin(),
465 GetBlkType(), GetBlkSpecification() );
467 // -- Check sources list if block is requested
468 if ( CheckIfRequested( block ) )
469 fBlockList->Add( block );
471 //The Following 2 line commented out and the previous is added.
474 fBlockList->Add( block );
478 } // while ( iter != NULL ){
484 * ---------------------------------------------------------------------------------
486 * ---------------------------------------------------------------------------------
489 //##################################################################################
490 void* AliEveHOMERManager::GetBlk( Int_t ndx ) {
491 // Get pointer to current block in current event
492 // * param ndx Block index
493 // * return returns pointer to blk, NULL if no block present
497 if ( !fReader || ! IsConnected() ) {
498 AliError( Form("Not connected yet.") );
501 if ( ( ndx ) < (Int_t) fNBlks )
502 data = (void*) fReader->GetBlockData( ndx );
508 //##################################################################################
509 ULong_t AliEveHOMERManager::GetBlkSize( Int_t ndx ) {
510 // Get size of block ndx
511 // * param ndx Block index
512 // * return returns pointer to blk, 0 if no block present
516 if ( !fReader || ! IsConnected() ) {
517 AliError( Form("Not connected yet.") );
520 if ( ( ndx ) < (Int_t) fNBlks )
521 length = (ULong_t) fReader->GetBlockDataLength( ndx );
527 //##################################################################################
528 TString AliEveHOMERManager::GetBlkOrigin( Int_t ndx ) {
529 // Get origin of block ndx
530 // * param ndx Block index
531 // * return origin of block
535 // -- Check for Connection
536 if ( !fReader || ! IsConnected() ) {
537 AliError( Form("Not connected yet.") );
541 // -- Check block index
542 if ( ( ndx ) >= (Int_t) fNBlks ) {
543 AliError( Form("Block index %d out of range.", ndx ) );
553 reverseOrigin.data = (UInt_t) fReader->GetBlockDataOrigin( ndx );
555 // -- Reverse the order
556 for (Int_t ii = 3; ii >= 0; ii-- )
557 if ( reverseOrigin.array[ii] != ' ')
558 origin.Append( reverseOrigin.array[ii] );
563 //##################################################################################
564 TString AliEveHOMERManager::GetBlkType( Int_t ndx ) {
565 // Get type of block ndx
566 // * param ndx Block index
567 // * return type of block
571 // -- Check for Connection
572 if ( !fReader || ! IsConnected() ) {
573 AliError( Form("Not connected yet.") );
577 // -- Check blockk index
578 if ( ( ndx ) >= (Int_t) fNBlks ) {
579 AliError( Form("Block index %d out of range.", ndx ) );
589 reverseType.data = (ULong64_t) fReader->GetBlockDataType( ndx );
591 // -- Reverse the order
592 for (Int_t ii = 7; ii >= 0; ii-- )
593 if ( reverseType.array[ii] != ' ')
594 type.Append( reverseType.array[ii] );
599 //##################################################################################
600 ULong_t AliEveHOMERManager::GetBlkSpecification( Int_t ndx ) {
601 // Get specification of block ndx
602 // * param ndx Block index
603 // * return specification of block
607 // -- Check for Connection
608 if ( !fReader || ! IsConnected() ) {
609 AliError( Form("Not connected yet.") );
613 // -- Check blockk index
614 if ( ( ndx ) >= (Int_t) fNBlks ) {
615 AliError( Form("Block index %d out of range.", ndx ) );
619 spec = (ULong_t) fReader->GetBlockDataSpec( ndx );
624 //##################################################################################
625 Bool_t AliEveHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
626 // Checks if current Block should was requested
627 // * return returns kTRUE, if block should was requested
629 Bool_t requested = kFALSE;
631 AliHLTHOMERSourceDesc * source= NULL;
633 // -- Read all sources and check if they should be read out
634 TIter next( fSourceList );
635 while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
637 if ( ! source->IsSelected() )
640 if ( !( block->GetDetector().CompareTo( "*** " ) && block->GetDetector().CompareTo( "***" ) ) ) {
641 // if not any detector
642 if ( source->GetDetector().CompareTo( block->GetDetector() ) )
646 if ( ! ( block->GetDataType().CompareTo( "******* " ) && block->GetDataType().CompareTo( "******* " ) ) ) {
647 if ( source->GetDataType().CompareTo( block->GetDataType() ) )
651 if ( ! block->HasSubDetectorRange() ) {
652 if ( source->GetSubDetector().Atoi() != block->GetSubDetector().Atoi() )
655 if ( ! block->HasSubSubDetectorRange() ) {
657 if ( source->GetSubSubDetector().Atoi() != block->GetSubSubDetector().Atoi() )
660 } // if ( ! block->HasSubSubDetectorRange ) {
661 } // if ( ! block->HasSubDetectorRange ) {
666 } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
670 if ( block->GetDataType().CompareTo("CLUSTERS") ) {
672 AliError( Form("Block requested : %s - %s : %s/%s -> %s ", block->GetDetector().Data(), block->GetDataType().Data(),
673 block->GetSubDetector().Data(), block->GetSubSubDetector().Data(), block->GetClassName().Data() ) );
676 AliError( Form("Block NOT requested : %s - %s : %s/%s -> %s ", block->GetDetector().Data(), block->GetDataType().Data(),
677 block->GetSubDetector().Data(), block->GetSubSubDetector().Data(), block->GetClassName().Data() ) );
687 * ---------------------------------------------------------------------------------
689 * ---------------------------------------------------------------------------------
692 //##################################################################################
693 void AliEveHOMERManager::DumpTPCCalib(TString objectName, Bool_t dumpToFile) {
694 // Still under testing ...
696 if ( fTPCPre != NULL )
699 fTPCPre = new AliTPCPreprocessorOnline();
701 TList* blockList = GetBlockList();
703 AliHLTHOMERBlockDesc *desc = 0;
705 TIter next(blockList);
707 while ( ( desc = (AliHLTHOMERBlockDesc*)next() ) ) {
708 if ( ! desc->IsTObject() )
713 if ( desc->GetSubSubDetector().Atoi() <= 1 ) {
714 sectorTPC = desc->GetSubDetector().Atoi();
717 sectorTPC = 36 + desc->GetSubDetector().Atoi();
720 if ( ! objectName.CompareTo( desc->GetClassName() ) ){
723 // AliTPCCalibPedestal
726 if ( ! objectName.CompareTo( "AliTPCCalibPedestal" ) ) {
727 AliTPCCalROC* calROC = NULL;
729 AliTPCCalibPedestal * cal = (AliTPCCalibPedestal*) desc->GetTObject();
731 cout << "error 1" << endl;
737 calROC = cal->GetCalRocRMS(sectorTPC);
738 if ( calROC == NULL ) {
739 cout << "error 2" << endl;
743 calROC->SetName(Form("RMS_ROC%d", sectorTPC));
744 fTPCPre->AddComponent((TObject*) calROC );
746 calROC = cal->GetCalRocPedestal(sectorTPC);
747 if ( calROC == NULL ) {
748 cout << "error 3" << endl;
753 calROC->SetName(Form("Pedestal_ROC%d", sectorTPC));
754 cout << "added" << endl;
755 fTPCPre->AddComponent((TObject*) calROC );
762 else if ( ! objectName.CompareTo( "AliTPCCalibPulser" ) ) {
763 AliTPCCalROC* calROC = NULL;
765 AliTPCCalibPulser * cal = (AliTPCCalibPulser*) desc->GetTObject();
769 calROC = cal->GetCalRocT0(sectorTPC);
770 calROC->SetName(Form("T0_ROC%d", sectorTPC));
771 fTPCPre->AddComponent((TObject*) calROC );
773 calROC = cal->GetCalRocQ(sectorTPC);
774 calROC->SetName(Form("Q_ROC%d", sectorTPC));
775 fTPCPre->AddComponent((TObject*) calROC );
777 calROC = cal->GetCalRocRMS(sectorTPC);
778 calROC->SetName(Form("RMS_ROC%d", sectorTPC));
779 fTPCPre->AddComponent((TObject*) calROC );
781 calROC = cal->GetCalRocOutliers(sectorTPC);
782 calROC->SetName(Form("Outliers_ROC%d", sectorTPC));
783 fTPCPre->AddComponent((TObject*) calROC );
791 else if ( ! objectName.CompareTo( "AliTPCCalibCE" ) ) {
792 AliTPCCalROC* calROC = NULL;
794 AliTPCCalibPulser * cal = (AliTPCCalibPulser*) desc->GetTObject();
798 calROC = cal->GetCalRocT0(sectorTPC);
799 calROC->SetName(Form("T0_ROC%d", sectorTPC));
800 fTPCPre->AddComponent((TObject*) calROC );
802 calROC = cal->GetCalRocQ(sectorTPC);
803 calROC->SetName(Form("Q_ROC%d", sectorTPC));
804 fTPCPre->AddComponent((TObject*) calROC );
806 calROC = cal->GetCalRocRMS(sectorTPC);
807 calROC->SetName(Form("RMS_ROC%d", sectorTPC));
808 fTPCPre->AddComponent((TObject*) calROC );
810 calROC = cal->GetCalRocOutliers(sectorTPC);
811 calROC->SetName(Form("Outliers_ROC%d", sectorTPC));
812 fTPCPre->AddComponent((TObject*) calROC );
815 } // if ( ! objectName.CompareTo( desc->GetClassName() ) ) {
817 } // while ( ( desc = (AliHLTHOMERBlockDesc*)next() ) ) {
821 fTPCPre->DumpToFile("pedestals.root");
822 cout << "DUMP" << endl;