]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveHLT/AliEveHOMERManager.cxx
In AliEveEventManager::AssertGeometry() remove a hack to bypass
[u/mrichter/AliRoot.git] / EVE / EveHLT / AliEveHOMERManager.cxx
1 // $Id$
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.                                    *
5
6 /** @file   AliEveHOMERManager.cxx
7     @author Jochen Thaeder
8     @date
9     @brief  Manger for HOMER in offline
10 */
11
12 #if __GNUC__>= 3
13    using namespace std;
14 #endif
15
16 #include "AliEveHOMERManager.h"
17
18 #define use_aliroot
19 #define use_root
20 #define ROWHOUGHPARAMS
21 #define use_reconstruction
22 #define use_newio
23 #define ROOTVERSION    "unchecked"
24 #define ALIROOTVERSION "unchecked"
25 #define __ROOT__
26 #define USE_ALILOG
27 #define LINUX
28 // -- -- -- -- -- -- -- 
29 #include "AliHLTHOMERLibManager.h"
30 #include "AliHLTHOMERSourceDesc.h"
31 #include "AliHLTHOMERBlockDesc.h"
32 // -- -- -- -- -- -- -- 
33 #include "AliEveHOMERSource.h"
34 // -- -- -- -- -- -- -- 
35 #include "TString.h"
36 #include <TApplication.h>
37 #include "Riostream.h"
38 #include "TXMLAttr.h"
39 #include "TCollection.h"
40 #include "TList.h"
41 #include "TObjString.h"
42 #include "TObjArray.h"
43 // -- -- -- -- -- -- -- 
44 #include "AliLog.h"
45 #include "AliTPCCalibPedestal.h"
46 #include "AliTPCCalibPulser.h"
47 #include "AliTPCCalibCE.h"
48 #include "AliTPCPreprocessorOnline.h"
49 #include "AliTPCCalROC.h"
50
51 //______________________________________________________________________________
52 //
53 // Manage connections to HLT data-sources.
54
55 ClassImp(AliEveHOMERManager)
56
57 /*
58  * ---------------------------------------------------------------------------------
59  *                            Constructor / Destructor
60  * ---------------------------------------------------------------------------------
61  */
62
63 //##################################################################################
64 AliEveHOMERManager::AliEveHOMERManager( TString xmlFile ) :
65   TEveElementList("AliEveHOMERManager"),
66   fLibManager(new AliHLTHOMERLibManager),
67   fXMLFile(xmlFile),
68   fXMLParser(NULL),
69   fRootNode(NULL),
70   fSourceList(NULL),
71   fReader(NULL),
72   fRealm("GPN"),
73   fBlockList(NULL),
74   fNBlks(0),
75   fEventID(0),
76   fCurrentBlk(0),
77   fConnected(kFALSE),
78   fStateHasChanged(kTRUE),
79   fSrcList(NULL),
80   fTPCPre(NULL) {
81   // This Class should handle the communication
82   // from the HLT to AliEVE. The HLT sends data via 
83   // the HOMER interface on several TCP ports of nodes 
84   // in the CERN GPN and DCS network.
85   // All this communication is hidden from the user.
86   // 
87   // Right now, a xml file ( SCC1 ) is used to get the
88   // configuration, this will/ has to change to a proxy
89   // running on dedicated nodes.
90 }
91
92 //##################################################################################
93 AliEveHOMERManager::~AliEveHOMERManager() {
94   // The destructor
95
96   if ( fLibManager ) {
97     if ( fReader )
98       fLibManager->DeleteReader(fReader);
99     delete fLibManager;
100     fLibManager = NULL;
101     fReader = NULL;
102   }
103
104   if ( fXMLParser )
105     delete fXMLParser;
106   fXMLParser = NULL;
107
108   if ( fSourceList != NULL )
109     delete fSourceList;
110   fSourceList = NULL;
111
112   if ( fBlockList != NULL )
113     delete fBlockList;
114   fBlockList = NULL;
115
116  if ( fSrcList != NULL )
117     delete fSrcList;
118   fSrcList = NULL;
119   
120   if ( fTPCPre != NULL )
121     delete fTPCPre;
122   fTPCPre = NULL;
123 }
124
125 /*
126  * ---------------------------------------------------------------------------------
127  *                            Source Handling
128  * ---------------------------------------------------------------------------------
129  */
130
131 //##################################################################################
132 Int_t AliEveHOMERManager::CreateHOMERSourcesList() {
133   // Create Sources List from HOMER-Proxy
134
135   // -- Initialize XML parser
136   if ( fXMLParser != NULL )
137     delete fXMLParser;
138   fXMLParser = NULL;
139
140   fXMLParser = new TDOMParser();
141   fXMLParser->SetValidate( kFALSE );
142
143   Int_t iResult = fXMLParser->ParseFile( fXMLFile );
144   if ( iResult < 0 ) {
145     iResult = 1;
146     AliError( Form("Parsing file with error: %s", fXMLParser->GetParseCodeMessage( fXMLParser->GetParseCode() )) );
147     return iResult;
148   }
149
150   // -- Initialize sources list
151   DestroyElements();
152   if ( fSourceList != NULL )
153     delete fSourceList;
154   fSourceList = NULL;
155
156   fSourceList = new TList();
157   fSourceList->SetOwner( kTRUE );
158
159   // -- Set ROOT node
160   fRootNode = fXMLParser->GetXMLDocument()->GetRootNode();
161
162   TXMLNode * node = NULL;
163   TXMLNode * prevNode = fRootNode->GetChildren();
164
165   // -- Loop over all nodes
166   while ( ( node = prevNode->GetNextNode() ) ) {
167     prevNode = node;
168
169     // -- Find only "Process" nodes, otherwise continue to next node
170     if ( strcmp( node->GetNodeName(), "Proc" ) != 0 )
171       continue;
172
173     // -- Get Attributes of current node
174     TList *attrList = node->GetAttributes();
175     TXMLAttr *attr = 0;
176     TIter next(attrList);
177
178     while ( ( attr = (TXMLAttr*)next() ) ) {
179
180       // -- Find "ID" attribute, otherwise continue to next attribute
181       if ( strcmp( attr->GetName(), "ID" ) != 0 )
182         continue;
183
184       TString nodeId( attr->GetValue() );
185
186       // -- Find only TDS processes
187       TObjArray * nodeIdTok = nodeId.Tokenize("_");
188
189       for ( Int_t ii=0 ; ii < nodeIdTok->GetEntries() ; ii++ ) {
190         if ( ! ( (TObjString*) nodeIdTok->At(ii) )->GetString().CompareTo("TDS") ) {
191           iResult = GetTDSAttributes( node->GetChildren() );
192           if ( iResult ) {
193             AliError( Form("Error processing TDS process : %s", nodeId.Data()) );
194           }
195         }
196       }
197     } // while ( ( attr = (TXMLAttr*)next() ) ) {
198
199   } // while ( ( node = prevNode->GetNextNode() ) ) {
200
201   // -- New SourceList has been created --> All Sources are new --> State has changed
202   fStateHasChanged = kTRUE;
203
204   if ( iResult ) {
205     AliWarning( Form("There have been errors, while creating the sources list.") );
206   }
207   else {
208     AliInfo( Form("New sources list created.") );
209  
210     if ( fSrcList ) 
211       delete fSrcList;
212
213     // -- Create new AliEVE sources list 
214     fSrcList = new AliEveHOMERSourceList("HOMER Sources");
215     fSrcList->SetManager(this);
216     
217     AddElement(fSrcList);
218     fSrcList->CreateByType();
219   }
220
221   return iResult;
222 }
223
224 //##################################################################################
225 void AliEveHOMERManager::SetSourceState( AliHLTHOMERSourceDesc * source, Bool_t state ) {
226   // Set state of a source
227   // * param source      Pointer to AliHLTHOMERSourceDesc object.
228   // * param state       New (selected/not selected) state.
229   
230   if ( source->IsSelected() != state ) {
231     source->SetState( state );
232     fStateHasChanged = kTRUE;
233   }
234
235   return;
236 }
237
238 //##################################################################################
239 Int_t AliEveHOMERManager::GetTDSAttributes( TXMLNode * xmlNode ) {
240   // Get Information out of a TDS process in XML file
241   // * param xmlNode   Pointer to childs of TDS node
242   // * return          0 on sucess, > 0 on errorsee header file for class documentation
243
244   Int_t iResult = 0;
245
246   TXMLNode * attrNode = NULL;
247   TXMLNode * prevNode = xmlNode;
248
249   TString xmlHostname = 0;
250   TString xmlPort = 0;
251
252   TString hostname = 0;
253   Int_t port = 0;
254
255   // -- Get hostname and port from TDS node out of XML
256   while ( ( attrNode = prevNode->GetNextNode() ) ) {
257     prevNode = attrNode;
258
259     // -- Get port out of the commandline
260     if ( strcmp( attrNode->GetNodeName(), "Cmd" ) == 0 ) {
261       TString cmd( attrNode->GetText() );
262
263       TObjArray * cmdTok = cmd.Tokenize(" ");
264       xmlPort = ((TObjString*) cmdTok->At(2))->GetString();
265     }
266     // -- Get hostname
267     else if ( strcmp( attrNode->GetNodeName(), "Node" ) == 0 )
268       xmlHostname = attrNode->GetText();
269
270   } // while ( ( attrNode = prevNode->GetNextNode() ) ) {
271
272   // -- Resolve hostname and port information
273   iResult = ResolveHostPortInformation ( xmlHostname, xmlPort, hostname, port );
274   if ( iResult == 1 ) {
275     AliError( Form("Error resolving hostname : %s", xmlHostname.Data()) );
276     return iResult;
277   }
278   else if ( iResult == 2 ) {AliInfo( Form("Connection established") );
279     AliError( Form("Error resolving port : %s", xmlPort.Data()) );
280     return iResult;
281   }
282
283   // -- Reset loop to TDS node
284   prevNode = xmlNode;
285
286   // -- Get Sources out of XML, resolve sources, add to sources List
287   while ( ( attrNode = prevNode->GetNextNode() ) ) {
288     prevNode = attrNode;
289
290     // Find only "Parent" tags, otherwise continue to next tag
291     if ( strcmp( attrNode->GetNodeName(), "Parent" ) != 0 )
292       continue;
293
294     TString xmlParent = attrNode->GetText();
295
296     AliHLTHOMERSourceDesc * source = new AliHLTHOMERSourceDesc( hostname, port );
297
298     if ( ResolveSourceInformation( xmlParent, source ) ) {
299       iResult = 3;
300       AliError( Form("Error resolving source : %s", xmlParent.Data()) );
301
302       delete source;
303     }
304     else {
305       fSourceList->Add( source );
306       AliInfo( Form("New Source added : %s", xmlParent.Data()) );
307     }
308
309   } // while ( ( attrNode = prevNode->GetNextNode() ) ) {
310
311   return iResult;
312 }
313
314 //##################################################################################
315 Int_t AliEveHOMERManager::ResolveHostPortInformation ( TString xmlHostname, TString xmlPort, TString &hostname, Int_t &port ) {
316   // Resolve Information of hostname and port for source which has to be used by HOMER
317   // ( due to port mapping inside the HLT )
318   // * param xmlHostname  Hostname out of the XML
319   // * param xmlPort      Port out of the XML
320   // * param hostname     Return of the hostname
321   // * param port         Return of the port
322   // * return             0 on sucess, 1 if hostname couldn't be resolved, 2 if port couldn't be resolved,
323
324   Int_t iResult = 1;
325   TString nodeName = 0;
326   TXMLNode * node = NULL;
327   TXMLNode * prevNode = fRootNode->GetChildren();
328
329   // *** Resolve hostname
330
331   while ( ( node = prevNode->GetNextNode() ) && iResult == 1 ) {
332     prevNode = node;
333
334     // -- Find only "Node" nodes, otherwise continue
335     if ( strcmp( node->GetNodeName(), "Node" ) != 0 )
336       continue;
337
338     // -- Get Attributes of current node
339     TList *attrList = node->GetAttributes();
340     TXMLAttr *attr = 0;
341     TIter next(attrList);
342
343     TString nodeId = 0;
344
345     // Get "nodeID" and "nodeName" of this "Node" node
346     while ( ( attr = (TXMLAttr*)next() ) ) {
347       if ( strcmp( attr->GetName(), "ID" ) == 0 )
348         nodeId = attr->GetValue();
349       else if ( strcmp( attr->GetName(), "hostname" ) == 0 )
350         nodeName = attr->GetValue();
351     }
352
353     // -- if this is not the correct nodeID continue
354     if ( nodeId != xmlHostname )
355       continue;
356
357     // -- Set hostname
358     if ( ! fRealm.CompareTo( "ACR" ) ) 
359       hostname = "alihlt-dcs0.cern.ch";
360     else if ( ! fRealm.CompareTo( "GPN" ) ) 
361       hostname = "alihlt-vobox0.cern.ch";
362     else 
363       hostname = nodeName;
364
365     iResult = 0;
366
367     break;
368   } // while ( ( node = prevNode->GetNextNode() ) ) {
369
370   if ( iResult ) {
371     AliError( Form("Error resolving hostname : %s", xmlHostname.Data()) );
372     return iResult;
373   }
374
375   // *** Resolve port
376
377   if ( xmlPort.IsDigit() ) {
378
379     if ( nodeName.CompareTo("feptriggerdet") ==0 ){
380       if ( xmlPort.CompareTo("49152") == 0 ){
381         port = 58140;
382       } else if ( xmlPort.CompareTo("49153") == 0 ){
383         port = 58141;
384       }
385     } else if ( nodeName.CompareTo("fepfmdaccorde") == 0 ){
386       if ( xmlPort.CompareTo("49152") == 0 ){
387         port = 58144;
388       } else if ( xmlPort.CompareTo("49153") == 0 ){
389         port = 58145;
390       }
391     } else if ( nodeName.CompareTo("feptpcao15") == 0 ){
392       if ( xmlPort.CompareTo("49152") == 0 ){
393         port = 50340;
394       } else if ( xmlPort.CompareTo("49153") == 0 ){
395         port = 50341;
396       }
397     } else if ( nodeName.CompareTo("fepphos2") == 0 ){
398       if ( xmlPort.CompareTo("49152") == 0 ){
399         port = 58656;
400       } else if ( xmlPort.CompareTo("58656") == 0 ){
401         port = 58656;
402       }
403    } else if ( nodeName.CompareTo("fepphos3") == 0 ){
404       if ( xmlPort.CompareTo("49152") == 0 ){
405         port = 58660;
406       } else if ( xmlPort.CompareTo("58660") == 0 ){
407         port = 58660;
408       }
409    } else if ( nodeName.CompareTo("fepphos4") == 0 ){
410       if ( xmlPort.CompareTo("49152") == 0 ){
411         port = 58664;
412       } else if ( xmlPort.CompareTo("58664") == 0 ){
413         port = 58664;
414       }
415     } else if ( nodeName.CompareTo("alihlt-vobox0") == 0 ){
416       port = xmlPort.Atoi();
417     }
418   }
419   else {
420     AliError( Form("Error resolving port : %s", xmlPort.Data()) );
421     iResult = 2;
422   }
423
424   // *** Summary
425
426   if ( !iResult ) {
427     AliInfo( Form("%s:%i resolved out of %s:%s", hostname.Data(), port, xmlHostname.Data(), xmlPort.Data()) );
428   }
429
430   return iResult;
431 }
432
433 //##################################################################################
434 Int_t AliEveHOMERManager::ResolveSourceInformation( TString xmlParent, AliHLTHOMERSourceDesc *source ) {
435   // Resolve information of source
436   // * param xmlParent   ParentString out of the XML
437   // * param source      Return the filled AliHLTHOMERSourceDesc object
438   // * return            0 on sucess, 1 on errorsee header file for class documentation
439
440   Int_t iResult = 0;
441
442   if ( ! xmlParent.Contains( "_" ) ) {
443     AliError( Form("Source %s could not be resolved", xmlParent.Data() ) );
444     iResult = 1;
445
446     return iResult;
447   }
448
449   TObjArray * parentTokens = xmlParent.Tokenize("_");
450
451   Int_t nEntries = parentTokens->GetEntries();
452
453   TString detector = ((TObjString*) parentTokens->At(0) )->GetString();
454   TString subDetector = "";
455   TString subSubDetector = "";
456   TString dataType = "";
457   ULong_t specification = 0;
458
459   TString name = ((TObjString*) parentTokens->At(1) )->GetString();
460   TString objName = "";
461
462   if ( nEntries == 3 )
463     subDetector = ((TObjString*) parentTokens->At(2) )->GetString();
464   else if ( nEntries == 4 ) {
465     subDetector = ((TObjString*) parentTokens->At(2) )->GetString();
466     subSubDetector = ((TObjString*) parentTokens->At(3) )->GetString();
467   }
468
469   // -- Corecct TPC subdetector, because in we have somtimes "A","C"
470   if ( ! detector.CompareTo("TPC") ) {
471     if ( subDetector.BeginsWith('A') ) {
472       subDetector.Remove( TString::kLeading, 'A' );
473     }
474     else if ( subDetector.BeginsWith('C') ) {
475       subDetector.Remove( TString::kLeading, 'C' );
476       Int_t tmp = subDetector.Atoi() + 18;
477       subDetector = "";
478       subDetector += tmp;
479     }
480   }
481   
482   // -- Correct for MUON
483   if ( ! detector.CompareTo("DIMU") ) {
484     detector = "MUON";
485     
486     if ( ! subDetector.CompareTo("TRG") )
487       subDetector = "1";
488     else if ( ! subDetector.CompareTo("TRK") )
489       subDetector = "2";
490   }
491   
492   // -- Remove Leading '0' in sub detector and subsubdetector
493   subDetector.Remove( TString::kLeading, '0' );
494   subSubDetector.Remove( TString::kLeading, '0' );
495   
496   // -- Set Object Names
497   
498   // **** General ****
499   if ( name == "RP" || name == "FP" || name == "Relay" ) {
500     objName = "";
501     dataType = "DDL_RAW";
502     specification = 0;
503   }
504   
505   // **** TPC ****
506   else if ( detector == "TPC" ) {
507     
508     if ( name == "CalibPedestal" ) {
509       objName = "AliTPCCalibPedestal";
510       dataType = "HIS_CAL";
511       specification = 0;
512     }
513     else if ( name == "CalibPulser" ) {
514       objName = "AliTPCCalibPulser";
515       dataType = "HIS_CAL";
516       specification = 0;
517     }
518     else if ( name == "CF" || name == "RelayCF" ) {
519       objName = "AliHLTTPCClusterDataFormat";
520       dataType = "CLUSTERS";
521       specification = 0;
522     }
523     else if ( name == "ESDConv" ) {
524       objName = "TTree";
525       dataType = "ESD_TREE";
526       specification = 0;
527     }
528     else if ( name == "KryptonCF" ) {
529       objName = "TObjArray";
530       dataType = "KRPTHIST";
531       specification = 0;
532     }
533     else {
534       // not defined yet ...
535       AliError( Form("Parent Process not defined yet : %s .", name.Data()) );
536       iResult = 1;
537     }
538
539   } // if ( detector == "TPC" ) {
540
541   // **** TRD ****
542   else if ( detector == "TRD" ) {
543
544     if ( name == "foo" ) {
545       objName = "bar";
546       dataType = "FOO_BAR";
547       specification = 0;
548     }
549   } // else if ( detector == "TRD" ) {
550
551   // **** PHOS ****
552   else if ( detector == "PHOS" ) {
553
554   } // else if ( detector == "PHOS" ) {
555
556   // **** DIMU ****
557   else if ( detector == "MUON" ) {
558  Int_t UpdateSourcesFromSourcesList( );
559   } // else if ( detector == "MUON" ) {
560
561   // -- Fill object
562   source->SetSourceName( name, objName );
563   source->SetSourceType( specification, dataType );
564   source->SetDetectors( detector, subDetector, subSubDetector );
565
566   //  AliInfo( Form("Set Source %s , Type %s, ClassName %s .", name.Data(), dataType.Data(), objName.Data()) );
567   //  AliInfo( Form("    Detector %s , SubDetector : %s, SubSubDetector %s .",
568   //            detector.Data(), subDetector.Data(), subSubDetector.Data()) );
569
570   return iResult;
571 }
572
573 /*
574  * ---------------------------------------------------------------------------------
575  *                            Connection Handling
576  * ---------------------------------------------------------------------------------
577  */
578
579 //##################################################################################
580 Int_t AliEveHOMERManager::ConnectHOMER(){
581   // Connect to HOMER sources, out of Readout List, which gets created when state has changed
582   // * return            0 on sucess, "HOMER" errors on error
583
584
585   Int_t iResult = 0;
586
587   fStateHasChanged = fSrcList->GetSelectedSources();
588
589   // -- Check if already connected and state has not changed
590   if ( fStateHasChanged == kFALSE && IsConnected() ) {
591     AliInfo( Form("No need for reconnection.") );
592     return iResult;
593   }
594
595   // -- If already connected, disconnect before connect
596   if ( IsConnected() )
597     DisconnectHOMER();
598
599   // *** Create the Readoutlist
600
601   UShort_t* sourcePorts = new UShort_t [fSourceList->GetEntries()];
602   const char ** sourceHostnames = new const char* [fSourceList->GetEntries()];
603   UInt_t sourceCount = 0;
604
605   CreateReadoutList( sourceHostnames, sourcePorts, sourceCount );
606
607   if ( sourceCount == 0 ) {
608     AliError(Form("No sources selected, aborting.") );
609     return iResult;
610   }
611
612   // *** Connect to data sources
613
614   if ( !fReader ) {
615     if ( fLibManager )
616       fReader = fLibManager->OpenReader( sourceCount, sourceHostnames, sourcePorts );
617   }
618
619   iResult = fReader->GetConnectionStatus();
620
621   if ( iResult ) {
622     // -- Connection failed
623
624     UInt_t ndx = fReader->GetErrorConnectionNdx();
625
626     if ( ndx < sourceCount ) {
627       AliError( Form("Error : Error establishing connection to TCP source %s:%hu: %s (%d)",
628                      sourceHostnames[ndx], sourcePorts[ndx], strerror(iResult), iResult) );
629     }
630     else {
631       AliError( Form("Error : Error establishing connection to unknown source with index %d: %s (%d)",
632                      ndx, strerror(iResult), iResult) );
633     }
634
635     if ( fReader )
636       fLibManager->DeleteReader( fReader );
637     fReader = NULL;
638
639   }
640   else {
641     // -- Connection ok - set reader
642     fConnected = kTRUE;
643
644     AliInfo( Form("Connection established") );
645   }
646
647   delete[] sourceHostnames;
648   delete[] sourcePorts;
649
650
651   // -- Get next event
652   if ( ! iResult )
653     NextEvent();
654
655   return iResult;
656 }
657
658 //##################################################################################
659 void AliEveHOMERManager::DisconnectHOMER(){
660   // Disconnect from HOMER sources
661
662   if ( ! IsConnected() )
663     return;
664
665   if ( fReader )
666     fLibManager->DeleteReader( fReader );
667   fReader = NULL;
668
669   fStateHasChanged = kTRUE;
670   fConnected = kFALSE;
671
672   AliInfo( Form("Connection closed") );
673
674   return;
675 }
676
677 //##################################################################################
678 Int_t AliEveHOMERManager::ReconnectHOMER(){
679   // Reconnect from HOMER sources
680   // * return            0 on sucess, "ConnectHOMER()" errors on error
681
682   Int_t iResult = 0;
683
684   if ( IsConnected() )
685     DisconnectHOMER();
686
687   iResult = ConnectHOMER();
688   if ( iResult ) {
689     AliError( Form("Error connecting.") );
690   }
691
692   return iResult;
693 }
694
695
696 //##################################################################################
697 void AliEveHOMERManager::CreateReadoutList( const char** sourceHostnames, UShort_t *sourcePorts, UInt_t &sourceCount ){
698   //  Create a readout list for Hostname and ports
699   // * param socurceHostnames   Array of selected hostnames
700   // * param socurcePorts       Array of selected ports
701   // * param socurceCount       Number of selected hostname:port
702
703   AliHLTHOMERSourceDesc * source= NULL;
704
705   // -- Read all sources and check if they should be read out
706   TIter next( fSourceList );
707   while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
708
709     if ( ! source->IsSelected() )
710       continue;
711
712     Bool_t exists = kFALSE;
713
714     // -- Loop over existing entries and check if entry is already in readout list
715     for ( UInt_t ii = 0; ii < sourceCount; ii++ ){
716       if ( !strcmp( sourceHostnames[ii], source->GetHostname().Data() ) &&  sourcePorts[ii] == source->GetPort() ) {
717         exists = kTRUE;
718         break;
719       }
720     }
721
722     // -- Add new entires to readout list
723     if ( ! exists ) {
724       sourcePorts[sourceCount] = source->GetPort();
725       sourceHostnames[sourceCount] = source->GetHostname().Data();
726       sourceCount++;
727     }
728
729   } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
730
731   fStateHasChanged = kFALSE;
732
733   return;
734 }
735
736 /*
737  * ---------------------------------------------------------------------------------
738  *                            Event Handling
739  * ---------------------------------------------------------------------------------
740  */
741
742 //##################################################################################
743 Int_t AliEveHOMERManager::NextEvent(){
744   // Loads the next Event, after being connected
745   // * return            0 on sucess, "HOMER" errors on error
746
747   Int_t iResult = 0;
748
749   if ( !fReader || ! IsConnected() ) {
750     AliWarning( Form( "Not connected yet." ) );
751     return 1;
752   }
753
754   // -- Read next event data and error handling for HOMER (error codes and empty blocks)
755   while( 1 ) {
756     iResult = fReader->ReadNextEvent( 20000000 /*timeout in us*/);
757
758     if ( iResult == 111 || iResult == 32 || iResult == 6 ) {
759       Int_t ndx = fReader->GetErrorConnectionNdx();
760       AliError( Form("Error, No Connection to source %d: %s (%d)", 
761                      ndx, strerror(iResult), iResult) );
762       fConnected = kFALSE;
763       return 2;
764     }
765     else if ( iResult == 110 ) {
766       Int_t ndx = fReader->GetErrorConnectionNdx();
767       AliError( Form("Timout occured, reading event from source %d: %s (%d)", 
768                      ndx, strerror(iResult), iResult) );
769       fConnected = kFALSE;
770       return 3;
771     }
772     else if ( iResult == 56) {
773       Int_t ndx = fReader->GetErrorConnectionNdx();
774       AliError( Form("Retry: Error reading event from source %d: %s (%d)", 
775                      ndx, strerror(iResult), iResult) );
776       fConnected = kFALSE;
777       continue;
778     }
779     else if ( iResult ) {
780       Int_t ndx = fReader->GetErrorConnectionNdx();
781       AliError( Form("General Error reading event from source %d: %s (%d)", 
782                      ndx, strerror(iResult), iResult) );
783       fConnected = kFALSE;
784       return 2;
785     }
786     else {
787       break;
788     }
789   } // while( 1 ) {
790
791   if ( iResult )
792     return iResult;
793   
794   // -- Get blockCnt and eventID
795   fNBlks = (ULong_t) fReader->GetBlockCnt();
796   fEventID = (ULong64_t) fReader->GetEventID();
797   fCurrentBlk = 0;
798
799   AliInfo( Form("Event 0x%016LX (%Lu) with %lu blocks", fEventID, fEventID, fNBlks) );
800
801 #if 0
802
803   // Loop for Debug only
804   for ( ULong_t i = 0; i < fNBlks; i++ ) {
805     Char_t tmp1[9], tmp2[5];
806     memset( tmp1, 0, 9 );
807     memset( tmp2, 0, 5 );
808     void *tmp11 = tmp1;
809     ULong64_t* tmp12 = (ULong64_t*)tmp11;
810     *tmp12 = fReader->GetBlockDataType( i );
811     void *tmp21 = tmp2;
812     ULong_t* tmp22 = (ULong_t*)tmp21;
813     *tmp22 = fReader->GetBlockDataOrigin( i );
814     AliInfo( Form("Block %lu length: %lu - type: %s - origin: %s",
815                   i, fReader->GetBlockDataLength( i ), tmp1, tmp2) );
816   } // end for ( ULong_t i = 0; i < fNBlks; i++ ) {
817
818 #endif
819
820   // -- Create BlockList
821   AliInfo( Form("Create Block List") );
822   iResult = CreateBlockList();
823
824   return iResult;
825 }
826
827 //##################################################################################
828 Int_t AliEveHOMERManager::CreateBlockList() {
829   // Create a TList of blocks, which have been readout
830
831   Int_t iResult = 0;
832
833   // -- Initialize block list
834   if ( fBlockList != NULL )
835     delete fBlockList;
836   fBlockList = NULL;
837
838   fBlockList = new TList();
839   fBlockList->SetOwner( kTRUE );
840
841   void* iter = GetFirstBlk();
842
843   // -- Fill block list
844   while ( iter != NULL ){
845
846     // -- Create new block
847     AliHLTHOMERBlockDesc * block = new AliHLTHOMERBlockDesc( GetBlk(), GetBlkSize(), GetBlkOrigin(),
848                                                              GetBlkType(), GetBlkSpecification() );
849
850     // -- Check sources list if block is requested
851     if ( CheckIfRequested( block ) )
852       fBlockList->Add( block );
853     else {
854       delete block;
855       block = NULL;
856     }
857     iter = GetNextBlk();
858
859   } // while ( iter != NULL ){
860
861   return iResult;
862 }
863
864 /*
865  * ---------------------------------------------------------------------------------
866  *                            BlockHandling
867  * ---------------------------------------------------------------------------------
868  */
869
870 //##################################################################################
871 void* AliEveHOMERManager::GetBlk( Int_t ndx ) {
872   // Get pointer to current block in current event
873   // * param ndx        Block index
874   // * return           returns pointer to blk, NULL if no block present
875    
876   void* data = NULL;
877
878   if ( !fReader || ! IsConnected() ) {
879     AliError( Form("Not connected yet.") );
880   }
881   else {
882     if ( ( ndx ) < (Int_t) fNBlks )
883       data = (void*) fReader->GetBlockData( ndx );
884   }
885
886   return data;
887 }
888
889 //##################################################################################
890 ULong_t AliEveHOMERManager::GetBlkSize( Int_t ndx ) {
891   // Get size of block ndx
892   // * param ndx        Block index
893   // * return           returns pointer to blk, 0 if no block present
894    
895   ULong_t length = 0;
896
897   if ( !fReader || ! IsConnected() ) {
898     AliError( Form("Not connected yet.") );
899   }
900   else {
901     if ( ( ndx ) < (Int_t) fNBlks )
902       length = (ULong_t) fReader->GetBlockDataLength( ndx );
903   }
904
905   return length;
906 }
907
908 //##################################################################################
909 TString AliEveHOMERManager::GetBlkOrigin( Int_t ndx ) {
910   // Get origin of block ndx
911   // * param ndx        Block index
912   // * return           origin of block
913
914   TString origin = "";
915
916   // -- Check for Connection
917   if ( !fReader || ! IsConnected() ) {
918     AliError( Form("Not connected yet.") );
919     return origin;
920   }
921
922   // -- Check block index
923   if ( ( ndx ) >= (Int_t) fNBlks ) {
924     AliError( Form("Block index %d out of range.", ndx ) );
925     return origin;
926   }
927
928   // -- Get origin
929   union{
930     UInt_t data;
931     Char_t array[4];
932   } reverseOrigin;
933
934   reverseOrigin.data = (UInt_t) fReader->GetBlockDataOrigin( ndx );
935
936   // -- Reverse the order
937   for (Int_t ii = 3; ii >= 0; ii-- )
938     if ( reverseOrigin.array[ii] != ' ')
939       origin.Append( reverseOrigin.array[ii] );
940
941   return origin;
942 }
943
944 //##################################################################################
945 TString AliEveHOMERManager:: GetBlkType( Int_t ndx ) {
946   // Get type of block ndx
947   // * param ndx        Block index
948   // * return           type of block
949
950   TString type = "";
951
952   // -- Check for Connection
953   if ( !fReader || ! IsConnected() ) {
954     AliError( Form("Not connected yet.") );
955     return type;
956   }
957
958   // -- Check blockk index
959   if ( ( ndx ) >= (Int_t) fNBlks ) {
960     AliError( Form("Block index %d out of range.", ndx ) );
961     return type;
962   }
963
964   // -- Get type
965   union{
966     ULong64_t data;
967     Char_t array[8];
968   } reverseType;
969
970   reverseType.data = (ULong64_t) fReader->GetBlockDataType( ndx );
971
972   // -- Reverse the order
973   for (Int_t ii = 7; ii >= 0; ii-- )
974     if ( reverseType.array[ii] != ' ')
975       type.Append( reverseType.array[ii] );
976
977   return type;
978 }
979
980 //##################################################################################
981 ULong_t AliEveHOMERManager:: GetBlkSpecification( Int_t ndx ) {
982   // Get specification of block ndx
983   // * param ndx        Block index
984   // * return           specification of block
985
986   ULong_t spec = 0;
987
988   // -- Check for Connection
989   if ( !fReader || ! IsConnected() ) {
990     AliError( Form("Not connected yet.") );
991     return spec;
992   }
993
994   // -- Check blockk index
995   if ( ( ndx ) >= (Int_t) fNBlks ) {
996     AliError( Form("Block index %d out of range.", ndx ) );
997     return spec;
998   }
999
1000   spec = (ULong_t) fReader->GetBlockDataSpec( ndx );
1001
1002   return spec;
1003 }
1004
1005 //##################################################################################
1006 Bool_t AliEveHOMERManager::CheckIfRequested( AliHLTHOMERBlockDesc * block ) {
1007   // Checks if current Block should was requested
1008   // * return           returns kTRUE, if block should was requested
1009
1010   Bool_t requested = kFALSE;
1011
1012   AliHLTHOMERSourceDesc * source= NULL;
1013
1014   // -- Read all sources and check if they should be read out
1015   TIter next( fSourceList );
1016   while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
1017
1018     if ( ! source->IsSelected() )
1019       continue;
1020
1021     if ( source->GetDetector().CompareTo( block->GetDetector() ) )
1022       continue;
1023
1024     if ( source->GetDataType().CompareTo( block->GetDataType() ) )
1025       continue;
1026
1027     if ( ! block->HasSubDetectorRange() ) {
1028
1029       if ( source->GetSubDetector().Atoi() != block->GetSubDetector().Atoi() )
1030         continue;
1031
1032       if ( ! block->HasSubSubDetectorRange() ) {
1033
1034         if ( source->GetSubSubDetector().Atoi() != block->GetSubSubDetector().Atoi() )
1035           continue;
1036
1037       } // if ( ! block->HasSubSubDetectorRange ) {
1038     } //  if ( ! block->HasSubDetectorRange ) {
1039
1040     requested = kTRUE;
1041
1042   } // while ( ( source = (AliHLTHOMERSourceDesc*)next() ) ) {
1043
1044   if ( requested) {
1045     AliInfo( Form("Block requested : %s - %s : %s/%s -> %s ", block->GetDetector().Data(), block->GetDataType().Data(),
1046                   block->GetSubDetector().Data(), block->GetSubSubDetector().Data(), block->GetClassName().Data() ) );
1047   }
1048   else
1049     AliInfo( Form("Block NOT requested : %s - %s : %s/%s -> %s ", block->GetDetector().Data(), block->GetDataType().Data(),               block->GetSubDetector().Data(), block->GetSubSubDetector().Data(), block->GetClassName().Data() ) );
1050
1051   return requested;
1052 }
1053
1054 /*
1055  * ---------------------------------------------------------------------------------
1056  *                            Test Realm ....
1057  * ---------------------------------------------------------------------------------
1058  */
1059
1060 //##################################################################################
1061 void AliEveHOMERManager::DumpTPCCalib(TString objectName, Bool_t dumpToFile) {
1062   // Still under testing ...
1063
1064   if ( fTPCPre != NULL )
1065     delete fTPCPre;
1066
1067   fTPCPre = new AliTPCPreprocessorOnline();
1068
1069   TList* blockList = GetBlockList();
1070
1071   AliHLTHOMERBlockDesc *desc = 0;
1072
1073   TIter next(blockList);
1074
1075   while ( ( desc = (AliHLTHOMERBlockDesc*)next() ) ) {
1076     if ( ! desc->IsTObject() )
1077       continue;
1078
1079     Int_t sectorTPC = 0;
1080
1081     if ( desc->GetSubSubDetector().Atoi() <= 1 ) {
1082       sectorTPC = desc->GetSubDetector().Atoi();
1083     }
1084     else {
1085       sectorTPC = 36 + desc->GetSubDetector().Atoi();
1086     }
1087
1088     if ( ! objectName.CompareTo( desc->GetClassName() ) ){
1089
1090       //
1091       // AliTPCCalibPedestal
1092       //
1093
1094       if ( ! objectName.CompareTo( "AliTPCCalibPedestal" ) ) {
1095         AliTPCCalROC* calROC = NULL;
1096
1097         AliTPCCalibPedestal * cal = (AliTPCCalibPedestal*) desc->GetTObject();
1098         if ( cal == NULL ) {
1099           cout << "error 1" << endl;
1100           continue;
1101         }
1102
1103         cal->Analyse();
1104
1105         calROC = cal->GetCalRocRMS(sectorTPC);
1106         if ( calROC == NULL ) {
1107           cout << "error 2" << endl;
1108           continue;
1109         }
1110
1111         calROC->SetName(Form("RMS_ROC%d", sectorTPC));
1112         fTPCPre->AddComponent((TObject*) calROC );
1113
1114         calROC = cal->GetCalRocPedestal(sectorTPC);
1115         if ( calROC == NULL ) {
1116           cout << "error 3" << endl;
1117           continue;
1118         }
1119
1120
1121         calROC->SetName(Form("Pedestal_ROC%d", sectorTPC));
1122         cout << "added" << endl;
1123         fTPCPre->AddComponent((TObject*) calROC );
1124       }
1125
1126       //
1127       // AliTPCCalibPulser
1128       //
1129       /*
1130       else if ( ! objectName.CompareTo( "AliTPCCalibPulser" ) ) {
1131         AliTPCCalROC* calROC = NULL;
1132
1133         AliTPCCalibPulser * cal = (AliTPCCalibPulser*) desc->GetTObject();
1134
1135         cal->Analyse();
1136
1137         calROC = cal->GetCalRocT0(sectorTPC);
1138         calROC->SetName(Form("T0_ROC%d", sectorTPC));
1139         fTPCPre->AddComponent((TObject*) calROC );
1140
1141         calROC = cal->GetCalRocQ(sectorTPC);
1142         calROC->SetName(Form("Q_ROC%d", sectorTPC));
1143         fTPCPre->AddComponent((TObject*) calROC );
1144
1145         calROC = cal->GetCalRocRMS(sectorTPC);
1146         calROC->SetName(Form("RMS_ROC%d", sectorTPC));
1147         fTPCPre->AddComponent((TObject*) calROC );
1148
1149         calROC = cal->GetCalRocOutliers(sectorTPC);
1150         calROC->SetName(Form("Outliers_ROC%d", sectorTPC));
1151         fTPCPre->AddComponent((TObject*) calROC );
1152       }
1153
1154 */
1155       //
1156       // AliTPCCalibCE
1157       //
1158       /*
1159       else if ( ! objectName.CompareTo( "AliTPCCalibCE" ) ) {
1160         AliTPCCalROC* calROC = NULL;
1161
1162         AliTPCCalibPulser * cal = (AliTPCCalibPulser*) desc->GetTObject();
1163
1164         cal->Analyse();
1165
1166         calROC = cal->GetCalRocT0(sectorTPC);
1167         calROC->SetName(Form("T0_ROC%d", sectorTPC));
1168         fTPCPre->AddComponent((TObject*) calROC );
1169
1170         calROC = cal->GetCalRocQ(sectorTPC);
1171         calROC->SetName(Form("Q_ROC%d", sectorTPC));
1172         fTPCPre->AddComponent((TObject*) calROC );
1173
1174         calROC = cal->GetCalRocRMS(sectorTPC);
1175         calROC->SetName(Form("RMS_ROC%d", sectorTPC));
1176         fTPCPre->AddComponent((TObject*) calROC );
1177
1178         calROC = cal->GetCalRocOutliers(sectorTPC);
1179         calROC->SetName(Form("Outliers_ROC%d", sectorTPC));
1180         fTPCPre->AddComponent((TObject*) calROC );
1181       }
1182       */
1183     } // if ( ! objectName.CompareTo( desc->GetClassName() ) ) {
1184
1185   } // while ( ( desc = (AliHLTHOMERBlockDesc*)next() ) ) {
1186
1187   if ( dumpToFile ) {
1188
1189     fTPCPre->DumpToFile("pedestals.root");
1190     cout << "DUMP" << endl;
1191   }
1192
1193
1194 }