]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MONITOR/AliMonitorProcess.cxx
Changing name of libRAW into libRAWData
[u/mrichter/AliRoot.git] / MONITOR / AliMonitorProcess.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  This is the class for perfoming the monitoring process.                  //
21 //  It checks if a raw data file exists, loops over the events in the raw    //
22 //  data file, reconstructs TPC and ITS clusters and tracks, fills the       //
23 //  monitor histograms and sends the updated histograms to the clients.      //
24 //  Then the raw data file is deleted and it waits for a new file.           //
25 //                                                                           //
26 ///////////////////////////////////////////////////////////////////////////////
27
28 #include <TFile.h>
29 #include <TGrid.h>
30 #include <TGridResult.h>
31 #include <TMessage.h>
32 #include <TROOT.h>
33 #include <TServerSocket.h>
34 #include <TSocket.h>
35
36 #include "AliLog.h"
37 #include "AliESD.h"
38 #include "AliITS.h"
39 #include "AliITSclustererV2.h"
40 #include "AliITStrackerV2.h"
41 #include "AliLoader.h"
42 #include "AliMonitorHLT.h"
43 #include "AliMonitorHLTHough.h"
44 #include "AliMonitorITS.h"
45 #include "AliMonitorProcess.h"
46 #include "AliMonitorTPC.h"
47 #include "AliMonitorV0s.h"
48 #include "AliRawReaderRoot.h"
49 #include "AliRun.h"
50 #include "AliTPC.h"
51 #include "AliTPCclustererMI.h"
52 #include "AliTPCtrackerMI.h"
53 #include "AliV0vertexer.h"
54
55 #include <AliL3StandardIncludes.h>
56 #include <AliL3MemHandler.h>
57 #include <AliL3ClusterFitter.h>
58 #include <AliL3Fitter.h>
59 #include <AliL3Hough.h>
60 #include <AliL3HoughBaseTransformer.h>
61 #include <AliL3StandardIncludes.h>
62 #include <AliL3Track.h>
63 #include <AliL3TrackArray.h>
64 #include <AliL3Transform.h>
65 #include <AliL3Vertex.h>
66 #include <AliLevel3.h>
67
68 ClassImp(AliMonitorProcess) 
69
70
71 const Int_t AliMonitorProcess::fgkPort = 9327;
72
73
74 //_____________________________________________________________________________
75 AliMonitorProcess::AliMonitorProcess(
76 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
77                                      const char* /*alienHost*/,
78 #else
79                                      const char* alienHost,
80 #endif
81                                      const char* alienDir,
82                                      const char* selection,
83                                      const char* fileNameGalice):
84   fSelection(selection),
85   fGrid(NULL),
86   fAlienDir(alienDir),
87   fRunLoader(NULL),
88   fTPCParam(NULL),
89   fITSgeom(NULL),
90   fLogicalFileName(""),
91   fFileName(""),
92   fHLT(NULL),
93   fHLTHough(NULL),
94
95   fRunNumber(0),
96   fSubRunNumber(0),
97   fNEvents(0),
98   fNEventsMin(1),
99   fWriteHistoList(kFALSE),
100
101   fTopFolder(NULL),
102   fMonitors(),
103   fFile(NULL),
104   fTree(NULL),
105
106   fServerSocket(NULL),
107   fSockets(),
108   fDisplaySocket(NULL),
109
110   fStatus(kStopped),
111   fStopping(kFALSE),
112
113   fInterruptHandler(NULL)
114 {
115 // initialize the monitoring process and the monitor histograms
116
117   fSelection = selection;
118
119 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
120   fGrid = TGrid::Connect("alien", gSystem->Getenv("USER"));
121 #else
122   fGrid = TGrid::Connect(alienHost, gSystem->Getenv("USER"));
123 #endif
124   if (!fGrid || fGrid->IsZombie() || !fGrid->IsConnected()) {
125     delete fGrid;
126     AliFatal("could not connect to alien");
127   }
128 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
129   fGrid->cd(alienDir);
130 #endif
131
132   fRunLoader = AliRunLoader::Open(fileNameGalice);
133   if (!fRunLoader) AliFatal(Form("could not get run loader from file %s", 
134                                  fileNameGalice));
135
136   fRunLoader->CdGAFile();
137   fTPCParam = AliTPC::LoadTPCParam(gFile);
138   if (!fTPCParam) AliFatal("could not load TPC parameters");
139
140   fRunLoader->LoadgAlice();
141   gAlice = fRunLoader->GetAliRun();
142   if (!gAlice) AliFatal("no gAlice object found");
143   AliITS* its = (AliITS*) gAlice->GetModule("ITS");
144   if (!its) AliFatal("no ITS detector found");
145   fITSgeom = its->GetITSgeom();
146   if (!fITSgeom) AliFatal("could not load ITS geometry");
147
148   // Init TPC parameters for HLT
149   Bool_t isinit=AliL3Transform::Init(const_cast<char*>(fileNameGalice),kTRUE);
150   if(!isinit){
151     AliFatal("Could not create transform settings, please check log for error messages!");
152   }
153
154   fTopFolder = new TFolder("Monitor", "monitor histograms");
155   fTopFolder->SetOwner(kTRUE);
156
157   if (IsSelected("TPC")) fMonitors.Add(new AliMonitorTPC(fTPCParam));
158   if (IsSelected("ITS")) fMonitors.Add(new AliMonitorITS(fITSgeom));
159   if (IsSelected("V0s")) fMonitors.Add(new AliMonitorV0s);
160   if (IsSelected("HLTConfMap")) fMonitors.Add(new AliMonitorHLT(fTPCParam));
161   if (IsSelected("HLTHough")) fMonitors.Add(new AliMonitorHLTHough(fTPCParam));
162
163   for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
164     ((AliMonitor*) fMonitors[iMonitor])->CreateHistos(fTopFolder);
165   }
166
167   TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
168   while (TFolder* folder = (TFolder*) iFolder->Next()) folder->SetOwner(kTRUE);
169   delete iFolder;
170
171   fFile = TFile::Open("monitor_tree.root", "RECREATE");
172   if (!fFile || !fFile->IsOpen()) {
173     AliFatal("could not open file for tree");
174   }
175   fTree = new TTree("MonitorTree", "tree for monitoring");
176   for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
177     ((AliMonitor*) fMonitors[iMonitor])->CreateBranches(fTree);
178   }
179   gROOT->cd();
180
181   fServerSocket = new TServerSocket(fgkPort, kTRUE);
182   fServerSocket->SetOption(kNoBlock, 1);
183   CheckForConnections();
184
185   fInterruptHandler = new AliMonitorInterruptHandler(this);
186   gSystem->AddSignalHandler(fInterruptHandler);
187 }
188
189 //_____________________________________________________________________________
190 AliMonitorProcess::AliMonitorProcess(const AliMonitorProcess& process) :
191   TObject(process),
192
193   fSelection(""),
194   fGrid(NULL),
195   fAlienDir(""),
196   fRunLoader(NULL),
197   fTPCParam(NULL),
198   fITSgeom(NULL),
199   fLogicalFileName(""),
200   fFileName(""),
201   fHLT(NULL),
202   fHLTHough(NULL),
203
204   fRunNumber(0),
205   fSubRunNumber(0),
206   fNEvents(0),
207   fNEventsMin(1),
208   fWriteHistoList(kFALSE),
209
210   fTopFolder(NULL),
211   fMonitors(),
212   fFile(NULL),
213   fTree(NULL),
214
215   fServerSocket(NULL),
216   fSockets(),
217   fDisplaySocket(NULL),
218
219   fStatus(kStopped),
220   fStopping(kFALSE),
221
222   fInterruptHandler(NULL)
223
224 {
225   AliFatal("copy constructor not implemented");
226 }
227
228 //_____________________________________________________________________________
229 AliMonitorProcess& AliMonitorProcess::operator = (const AliMonitorProcess& 
230                                                   /*process*/)
231 {
232   AliFatal("assignment operator not implemented");
233   return *this;
234 }
235
236 //_____________________________________________________________________________
237 AliMonitorProcess::~AliMonitorProcess()
238 {
239 // clean up
240
241   fMonitors.Delete();
242   delete fTopFolder;
243   delete fRunLoader;
244
245   delete fServerSocket;
246   fSockets.Delete();
247   delete fDisplaySocket;
248
249 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
250   fGrid->Close();
251 #endif
252   delete fGrid;
253
254   fFile->Close();
255   delete fFile;
256   gSystem->Unlink("monitor_tree.root");
257
258   delete fHLT;
259   delete fHLTHough;
260
261   gSystem->RemoveSignalHandler(fInterruptHandler);
262   delete fInterruptHandler;
263 }
264
265
266 //_____________________________________________________________________________
267 const char* AliMonitorProcess::GetRevision()
268 {
269   return "$Revision$";
270 }
271
272
273 //_____________________________________________________________________________
274 void AliMonitorProcess::SetStatus(EStatus status)
275 {
276 // set the current status and process system events
277
278   fStatus = status;
279   gSystem->ProcessEvents();
280 }
281
282
283 //_____________________________________________________________________________
284 void AliMonitorProcess::Run()
285 {
286 // run the monitor process: 
287 //  check for a raw data file, process the raw data file and delete it
288
289   fStopping = kFALSE;
290
291   while (!fStopping) {
292     SetStatus(kWaiting);
293     while (!CheckForNewFile()) {
294       CheckForConnections();
295       SetStatus(kWaiting);
296       if (fStopping) break;
297       gSystem->Sleep(10);
298     }
299     if (fStopping) break;
300
301     ProcessFile();
302   }
303
304   WriteHistos();
305
306   fStopping = kFALSE;
307   SetStatus(kStopped);
308 }
309
310
311 //_____________________________________________________________________________
312 void AliMonitorProcess::Stop()
313 {
314 // set the fStopping flag to terminate the monitor process after the current
315 // event was processed
316
317   if (GetStatus() != kStopped) fStopping = kTRUE;
318 }
319
320
321 //_____________________________________________________________________________
322 void AliMonitorProcess::ProcessFile(const char* fileName)
323 {
324 // create a file with monitor histograms for a single file
325
326   if (GetStatus() != kStopped) {
327     AliError("ProcessFile can not be called"
328              " while the monitor process is running");
329     return;
330   }
331
332   fFileName = fileName;
333   Int_t nEventMin = fNEventsMin;
334   fNEventsMin = 1;
335   ProcessFile();
336   WriteHistos();
337   fNEventsMin = nEventMin;
338   SetStatus(kStopped);
339 }
340
341
342 //_____________________________________________________________________________
343 Bool_t AliMonitorProcess::CheckForNewFile()
344 {
345 // check whether a new file was registered in alien
346
347 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
348   TGridResult* result = fGrid->Ls();
349 #else
350   TDatime datime;
351   char dirName[256];
352   sprintf(dirName, "%s/adc-%d", fAlienDir.Data(), datime.GetDate());
353   char findName[256];
354   sprintf(findName, "*.root");
355   Grid_ResultHandle_t handle = fGrid->Find(dirName, findName);
356   if (!handle) {
357     AliError(Form("could not open alien directory %s", 
358                   dirName));
359     return kFALSE;
360   }
361   TGridResult* result = fGrid->CreateGridResult(handle);
362 #endif
363   Long_t maxDate = -1;
364   Long_t maxTime = -1;
365   TString fileName;
366
367 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
368   while (const char* entry = result->Next()) {
369 #else
370   while (Grid_Result_t* resultEntry = result->Next()) {
371     const char* entry = resultEntry->name.c_str();
372 #endif
373     if (strrchr(entry, '/')) entry = strrchr(entry, '/')+1;
374     // entry = host_date_time.root
375     TString entryCopy(entry);
376     char* p = const_cast<char*>(entryCopy.Data());
377     if (!strtok(p, "_") || !p) continue;  // host name
378     char* dateStr = strtok(NULL, "_");
379     if (!dateStr || !p) continue;
380     char* timeStr = strtok(NULL, ".");
381     if (!timeStr || !p) continue;
382     Long_t date = atoi(dateStr);
383     Long_t time = atoi(timeStr);
384
385     if ((date > maxDate) || ((date == maxDate) && (time > maxTime))) {
386       maxDate = date;
387       maxTime = time;
388       fileName = entry;
389     }
390   }
391
392   delete result;
393   if (maxDate < 0) return kFALSE;  // no files found
394   if (fLogicalFileName.CompareTo(fileName) == 0) return kFALSE;  // no new file
395
396   fLogicalFileName = fileName;
397 #if ROOT_VERSION_CODE <= 199169   // 3.10/01
398   result = fGrid->GetPhysicalFileNames(fLogicalFileName.Data());
399   fFileName = result->Next();
400 #else
401   fileName = dirName + ("/" + fLogicalFileName);
402   handle = fGrid->GetPhysicalFileNames(fileName.Data());
403   if (!handle) {
404     AliError(Form("could not get physical file names for %s", 
405                   fileName.Data()));
406     return kFALSE;
407   }
408   result = fGrid->CreateGridResult(handle);
409   result->Reset();
410   Grid_Result_t* resultEntry = result->Next();
411   if (!resultEntry) {
412     AliError(Form("could not get physical file names for %s", 
413                   fileName.Data()));
414     return kFALSE;
415   }
416   fFileName = resultEntry->name2.c_str();
417   fFileName.ReplaceAll("castor:/", "rfio:/");
418 #endif
419   delete result;
420
421   return kTRUE;
422 }
423
424 //_____________________________________________________________________________
425 Bool_t AliMonitorProcess::ProcessFile()
426 {
427 // loop over all events in the raw data file, run the reconstruction
428 // and fill the monitor histograms
429
430   Int_t nEvents = GetNumberOfEvents(fFileName);
431   if (nEvents <= 0) return kFALSE;
432   AliDebug(1, Form("found %d event(s) in file %s", 
433                    nEvents, fFileName.Data()));
434   if (IsSelected("HLTConfMap")) CreateHLT(fFileName);
435   if (IsSelected("HLTHough")) CreateHLTHough(fFileName);
436
437   // loop over the events
438   for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
439     CheckForConnections();
440     SetStatus(kReading);
441     fRunLoader->SetEventNumber(0);
442     AliRawReaderRoot rawReader(fFileName, iEvent);
443     if (fStopping) break;
444     if (rawReader.GetRunNumber() != fRunNumber) {
445       WriteHistos();
446       StartNewRun();
447       fRunNumber = rawReader.GetRunNumber();
448       fEventNumber[0] = rawReader.GetEventId()[0];
449       fEventNumber[1] = rawReader.GetEventId()[1];
450       fSubRunNumber = 0;
451       if (fStopping) break;
452     }
453
454     // monitor only central physics events
455     if (rawReader.GetType() != 7) continue;
456     if ((rawReader.GetAttributes()[0] & 0x02) == 0) continue;
457     AliInfo(Form("run: %d  event: %d %d\n", rawReader.GetRunNumber(), 
458                  rawReader.GetEventId()[0], rawReader.GetEventId()[1]));
459
460     AliESD esd;
461     if (IsSelected("TPC")) {
462       CheckForConnections();
463       if (!ReconstructTPC(&rawReader, &esd)) return kFALSE;
464       if (fStopping) break;
465     }
466     if (IsSelected("ITS")) {
467       CheckForConnections();
468       if (!ReconstructITS(&rawReader, &esd)) return kFALSE;
469       if (fStopping) break;
470     }
471     if (IsSelected("V0s")) {
472       CheckForConnections();
473       if (!ReconstructV0s(&esd)) return kFALSE;
474       if (fStopping) break;
475     }
476     if (IsSelected("HLTConfMap")) {
477       CheckForConnections();
478       if (!ReconstructHLT(iEvent)) return kFALSE;
479       if (fStopping) break;
480     }
481     if (IsSelected("HLTHough")) {
482       CheckForConnections();
483       if (!ReconstructHLTHough(iEvent)) return kFALSE;
484       if (fStopping) break;
485     }
486
487     if (fDisplaySocket) fDisplaySocket->Send("new event");
488
489     AliDebug(1, "filling histograms...");
490     for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
491       CheckForConnections();
492       SetStatus(kFilling);
493       ((AliMonitor*) fMonitors[iMonitor])->FillHistos(fRunLoader, &rawReader, 
494                                                       &esd);
495       if (fStopping) break;
496     }
497     if (fStopping) break;
498
499     AliDebug(1, "updating histograms...");
500     CheckForConnections();
501     SetStatus(kUpdating);
502     TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
503     while (TFolder* folder = (TFolder*) iFolder->Next()) {
504       TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
505       while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
506         histo->Update();
507       }
508       delete iHisto;
509     }
510     delete iFolder;
511     if (fStopping) break;
512
513     AliDebug(1, "filling the tree...");
514     fTree->Fill();
515
516     AliDebug(1, "broadcasting histograms...");
517     CheckForConnections();
518     BroadcastHistos();
519
520     fNEvents++;
521     if (fStopping) break;
522   }
523
524   if (fHLT) {
525     delete fHLT;
526     fHLT = NULL;
527   }
528   if (fHLTHough) {
529     delete fHLTHough;
530     fHLTHough = NULL;
531   }
532
533   return kTRUE;
534 }
535
536 //_____________________________________________________________________________
537 void AliMonitorProcess::Reset()
538 {
539 // write the current histograms to a file and reset them
540
541   if (fSubRunNumber == 0) fSubRunNumber++;
542   WriteHistos();
543   StartNewRun();
544   fSubRunNumber++;
545 }
546
547
548 //_____________________________________________________________________________
549 UInt_t AliMonitorProcess::GetEventPeriodNumber() const
550 {
551 // get the period number from the event id
552
553   return (fEventNumber[1] >> 4);
554 }
555
556 //_____________________________________________________________________________
557 UInt_t AliMonitorProcess::GetEventOrbitNumber() const
558 {
559 // get the orbit number from the event id
560
561   return ((fEventNumber[1] & 0x000F) << 20) + (fEventNumber[0] >> 12);
562 }
563
564 //_____________________________________________________________________________
565 UInt_t AliMonitorProcess::GetEventBunchNumber() const
566 {
567 // get the bunch number from the event id
568
569   return (fEventNumber[0] % 0x0FFF);
570 }
571
572 //_____________________________________________________________________________
573 Int_t AliMonitorProcess::GetNumberOfEvents(const char* fileName) const
574 {
575 // determine the number of events in the given raw data file
576
577   Int_t nEvents = -1;
578
579   TFile* file = TFile::Open(fileName);
580   if (!file || !file->IsOpen()) {
581     AliError(Form("could not open file %s", fileName));
582     if (file) delete file;
583     return -1;
584   }
585
586   TTree* tree = (TTree*) file->Get("RAW");
587   if (!tree) {
588     AliError("could not find tree with raw data");
589   } else {
590     nEvents = (Int_t) tree->GetEntries();
591   }
592   file->Close();
593   delete file;
594
595   return nEvents;
596 }
597
598 //_____________________________________________________________________________
599 Bool_t AliMonitorProcess::ReconstructTPC(AliRawReader* rawReader, AliESD* esd)
600 {
601 // find TPC clusters and tracks
602
603   SetStatus(kRecTPC);
604
605   AliLoader* tpcLoader = fRunLoader->GetLoader("TPCLoader");
606   if (!tpcLoader) {
607     AliError("no TPC loader found");
608     return kFALSE;
609   }
610   gSystem->Unlink("TPC.RecPoints.root");
611
612   // cluster finder
613   AliDebug(1, "reconstructing clusters...");
614   tpcLoader->LoadRecPoints("recreate");
615   AliTPCclustererMI clusterer(fTPCParam);
616   tpcLoader->MakeRecPointsContainer();
617   clusterer.SetOutput(tpcLoader->TreeR());
618   clusterer.Digits2Clusters(rawReader);
619   tpcLoader->WriteRecPoints("OVERWRITE");
620
621   // track finder
622   AliDebug(1, "reconstructing tracks...");
623   AliTPCtrackerMI tracker(fTPCParam);
624   tracker.LoadClusters(tpcLoader->TreeR());
625   tracker.Clusters2Tracks(esd);
626   tracker.UnloadClusters();
627   tpcLoader->UnloadRecPoints();
628
629   return kTRUE;
630 }
631
632 //_____________________________________________________________________________
633 Bool_t AliMonitorProcess::ReconstructITS(AliRawReader* rawReader, AliESD* esd)
634 {
635 // find ITS clusters and tracks
636
637   SetStatus(kRecITS);
638
639   AliLoader* itsLoader = fRunLoader->GetLoader("ITSLoader");
640   if (!itsLoader) {
641     AliError("no ITS loader found");
642     return kFALSE;
643   }
644   gSystem->Unlink("ITS.RecPoints.root");
645
646   // cluster finder
647   AliDebug(1, "reconstructing clusters...");
648   itsLoader->LoadRecPoints("recreate");
649   AliITSclustererV2 clusterer(fITSgeom);
650   itsLoader->MakeRecPointsContainer();
651   clusterer.Digits2Clusters(rawReader);
652
653   // track finder
654   AliDebug(1, "reconstructing tracks...");
655   AliITStrackerV2 tracker(fITSgeom);
656   tracker.LoadClusters(itsLoader->TreeR());
657   tracker.Clusters2Tracks(esd);
658   tracker.UnloadClusters();
659
660   itsLoader->UnloadRecPoints();
661   return kTRUE;
662 }
663
664 //_____________________________________________________________________________
665 Bool_t AliMonitorProcess::ReconstructV0s(AliESD* esd)
666 {
667 // find V0s
668
669   SetStatus(kRecV0s);
670
671   // V0 finder
672   AliDebug(1, "reconstructing V0s...");
673   AliV0vertexer vertexer;
674   Double_t vtx[3];
675   esd->GetVertex()->GetXYZ(vtx);
676   vertexer.SetVertex(vtx);
677   vertexer.Tracks2V0vertices(esd);
678
679   return kTRUE;
680 }
681
682 //_____________________________________________________________________________
683 void AliMonitorProcess::CreateHLT(const char* fileName)
684 {
685
686 // create the HLT (Level3) object
687
688   if (fHLT) delete fHLT;
689
690   char name[256];
691   strcpy(name, fileName);
692   fHLT = new AliLevel3(name);
693   fHLT->Init("./", AliLevel3::kRaw, 1);
694
695   fHLT->SetClusterFinderParam(-1, -1, kTRUE);
696   
697   Int_t phiSegments = 50;
698   Int_t etaSegments = 100;
699   Int_t trackletlength = 3;
700   Int_t tracklength = 20;//40 or 5
701   Int_t rowscopetracklet = 2;
702   Int_t rowscopetrack = 10;
703   Double_t minPtFit = 0;
704   Double_t maxangle = 0.1745;
705   Double_t goodDist = 5;
706   Double_t maxphi = 0.1;
707   Double_t maxeta = 0.1;
708   Double_t hitChi2Cut = 15;//100 or 15
709   Double_t goodHitChi2 = 5;//20 or 5
710   Double_t trackChi2Cut = 10;//50 or 10
711   fHLT->SetTrackerParam(phiSegments, etaSegments, 
712                         trackletlength, tracklength,
713                         rowscopetracklet, rowscopetrack,
714                         minPtFit, maxangle, goodDist, hitChi2Cut,
715                         goodHitChi2, trackChi2Cut, 50, maxphi, maxeta, kTRUE);
716   
717   fHLT->WriteFiles("./hlt/");  
718 }
719
720 //_____________________________________________________________________________
721 void AliMonitorProcess::CreateHLTHough(const char* fileName)
722 {
723
724 // create the HLT Hough transform (L3Hough) object
725
726   if (fHLTHough) delete fHLTHough;
727
728   char name[256];
729   strcpy(name, fileName);
730
731   fHLTHough = new AliL3Hough();
732   fHLTHough->SetThreshold(4);
733   fHLTHough->SetTransformerParams(140,150,0.5,-1);
734   fHLTHough->SetPeakThreshold(9000,-1);// or 6000
735   fHLTHough->Init("./", kFALSE, 50, kFALSE,0,name);
736   fHLTHough->SetAddHistograms();
737   //  fHLTHough->GetMaxFinder()->SetThreshold(14000);
738
739 }
740
741 //_____________________________________________________________________________
742 Bool_t AliMonitorProcess::ReconstructHLT(Int_t iEvent)
743 {
744 // run the HLT cluster and track finder
745
746   SetStatus(kRecHLT);
747
748   gSystem->Exec("rm -rf hlt");
749   gSystem->MakeDirectory("hlt");
750   if (!fHLT) return kFALSE;
751
752   fHLT->ProcessEvent(0, 35, iEvent);
753
754   // remove the event number from the file names
755   char command[256];
756   sprintf(command, "rename points_%d points hlt/*.raw", iEvent);
757   gSystem->Exec(command);
758   sprintf(command, "rename tracks_tr_%d tracks_tr hlt/*.raw", iEvent);
759   gSystem->Exec(command);
760   sprintf(command, "rename tracks_gl_%d tracks_gl hlt/*.raw", iEvent);
761   gSystem->Exec(command);
762   sprintf(command, "rename tracks_%d tracks hlt/*.raw", iEvent);
763   gSystem->Exec(command);
764   return kTRUE;
765 }
766
767 //_____________________________________________________________________________
768 Bool_t AliMonitorProcess::ReconstructHLTHough(Int_t iEvent)
769 {
770 // run the HLT Hough transformer
771
772   SetStatus(kRecHLT);
773
774   gSystem->Exec("rm -rf hlt/hough");
775   gSystem->MakeDirectory("hlt/hough");
776   gSystem->Exec("rm -rf hlt/fitter");
777   gSystem->MakeDirectory("hlt/fitter");
778   if (!fHLTHough) return kFALSE;
779
780   //  fHLTHough->Process(0, 35);
781   // Loop over TPC sectors and process the data
782   for(Int_t i=0; i<=35; i++)
783     {
784       fHLTHough->ReadData(i,iEvent);
785       fHLTHough->Transform();
786       //      if(fHLTHough->fAddHistograms)
787       fHLTHough->AddAllHistograms();
788       fHLTHough->FindTrackCandidates();
789       fHLTHough->AddTracks();
790     }
791   fHLTHough->WriteTracks("./hlt/hough");
792
793   // Run cluster fitter
794   AliL3ClusterFitter *fitter = new AliL3ClusterFitter("./hlt");
795
796   // Set debug flag for the cluster fitter
797   //  fitter->Debug();
798
799   // Setting fitter parameters
800   fitter->SetInnerWidthFactor(1,1.5);
801   fitter->SetOuterWidthFactor(1,1.5);
802   fitter->SetNmaxOverlaps(5);
803   
804   //fitter->SetChiSqMax(5,kFALSE); //isolated clusters
805   fitter->SetChiSqMax(5,kTRUE);  //overlapping clusters
806
807   Int_t rowrange[2] = {0,AliL3Transform::GetNRows()-1};
808
809   // Takes input from global hough tracks produced by HT
810   fitter->LoadSeeds(rowrange,kFALSE,iEvent);
811
812   UInt_t ndigits;
813
814   for(Int_t islice = 0; islice <= 35; islice++)
815     {
816       for(Int_t ipatch = 0; ipatch < AliL3Transform::GetNPatches(); ipatch++)
817         {
818           // Read digits
819           fHLTHough->GetMemHandler(ipatch)->Free();
820           fHLTHough->GetMemHandler(ipatch)->Init(islice,ipatch);
821           AliL3DigitRowData *digits = (AliL3DigitRowData *)fHLTHough->GetMemHandler(ipatch)->AliAltroDigits2Memory(ndigits,iEvent);
822
823           fitter->Init(islice,ipatch);
824           fitter->SetInputData(digits);
825           fitter->FindClusters();
826           fitter->WriteClusters();
827         }
828     }
829
830   // Refit of the clusters
831   AliL3Vertex vertex;
832   //The seeds are the input tracks from circle HT
833   AliL3TrackArray *tracks = fitter->GetSeeds();
834   AliL3Fitter *ft = new AliL3Fitter(&vertex,1);
835
836   ft->LoadClusters("./hlt/fitter/",iEvent,kFALSE);
837   for(Int_t i=0; i<tracks->GetNTracks(); i++)
838     {
839       AliL3Track *track = tracks->GetCheckedTrack(i);
840       if(!track) continue;
841       if(track->GetNHits() < 20) continue;
842       ft->SortTrackClusters(track);
843       ft->FitHelix(track);
844       track->UpdateToFirstPoint();
845     }
846   delete ft;
847         
848   //Write the final tracks
849   fitter->WriteTracks(20);
850
851   delete fitter;
852
853   // remove the event number from the file names
854   char command[256];
855   sprintf(command, "rename tracks_%d tracks hlt/hough/*.raw", iEvent);
856   gSystem->Exec(command);
857   sprintf(command, "rename tracks_%d tracks hlt/fitter/*.raw", iEvent);
858   gSystem->Exec(command);
859   sprintf(command, "rename points_%d points hlt/fitter/*.raw", iEvent);
860   gSystem->Exec(command);
861   return kTRUE;
862 }
863
864 //_____________________________________________________________________________
865 Bool_t AliMonitorProcess::WriteHistos()
866 {
867 // write the monitor tree and the monitor histograms to the file 
868 // "monitor_<run number>[_<sub_run_number>].root"
869 // if at least fNEventsMin events were monitored
870
871   SetStatus(kWriting);
872
873   // rename tree file and create a new one
874   fFile->cd();
875   fTree->Write();
876   fFile->Close();
877   delete fFile;
878
879   char fileName[256];
880   sprintf(fileName, "monitor_tree_%d.root", fRunNumber);
881   if (fSubRunNumber > 0) {
882     sprintf(fileName, "monitor_tree_%d_%d.root", fRunNumber, fSubRunNumber);
883   }
884   if (fNEvents < fNEventsMin) {
885     gSystem->Unlink("monitor_tree.root");
886   } else {
887     gSystem->Rename("monitor_tree.root", fileName);
888   }
889
890   fFile = TFile::Open("monitor_tree.root", "RECREATE");
891   if (!fFile || !fFile->IsOpen()) {
892     AliFatal("could not open file for tree");
893   }
894   fTree = new TTree("MonitorTree", "tree for monitoring");
895   for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
896     ((AliMonitor*) fMonitors[iMonitor])->CreateBranches(fTree);
897   }
898   gROOT->cd();
899
900   // write the histograms
901   if (fNEvents < fNEventsMin) return kTRUE;
902
903   if (!fWriteHistoList) {
904     TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
905     while (TFolder* folder = (TFolder*) iFolder->Next()) {
906       TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
907       while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
908         histo->ResetList();
909       }
910       delete iHisto;
911     }
912     delete iFolder;
913   }
914
915   Bool_t result = kTRUE;
916   sprintf(fileName, "monitor_%d.root", fRunNumber);
917   if (fSubRunNumber > 0) {
918     sprintf(fileName, "monitor_%d_%d.root", fRunNumber, fSubRunNumber);
919   }
920   TFile* file = TFile::Open(fileName, "recreate");
921   if (!file || !file->IsOpen()) {
922     AliError(Form("could not open file %s", fileName));
923     result = kFALSE;
924   } else {
925     fTopFolder->Write();
926     file->Close();
927   }
928   if (file) delete file;
929
930   return result;
931 }
932
933 //_____________________________________________________________________________
934 void AliMonitorProcess::StartNewRun()
935 {
936 // reset the histograms for a new run
937
938   SetStatus(kResetting);
939   TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
940   while (TFolder* folder = (TFolder*) iFolder->Next()) {
941     TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
942     while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
943       histo->Reset();
944     }
945     delete iHisto;
946   }
947   delete iFolder;
948
949   fNEvents = 0;
950 }
951
952
953 //_____________________________________________________________________________
954 void AliMonitorProcess::CheckForConnections()
955 {
956 // check if new clients want to connect and add them to the list of sockets
957
958   TSocket* socket;
959   while ((socket = fServerSocket->Accept()) != (TSocket*)-1) {
960     socket->SetOption(kNoBlock, 1);
961     char socketType[256];
962     if (socket->Recv(socketType, 255) <= 0) {
963       gSystem->Sleep(1000);
964       if (socket->Recv(socketType, 255) <= 0) {
965         TInetAddress adr = socket->GetInetAddress();
966         AliError(Form("no socket type received - "
967                       "disconnect client: %s (%s), port %d",
968                       adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
969         delete socket;
970         continue;
971       }
972     }
973     if (strcmp(socketType, "client") == 0) {
974       fSockets.Add(socket);
975       TInetAddress adr = socket->GetInetAddress();
976       AliInfo(Form("new client: %s (%s), port %d",
977                    adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
978       if (fNEvents > 0) BroadcastHistos(socket);
979     } else if (strcmp(socketType, "display") == 0) {
980       if (fDisplaySocket) {
981         fDisplaySocket->Close();
982         delete fDisplaySocket;
983       }
984       fDisplaySocket = socket;
985       TInetAddress adr = socket->GetInetAddress();
986       AliInfo(Form("new display: %s (%s), port %d",
987                    adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
988     } else {
989       TInetAddress adr = socket->GetInetAddress();
990       AliError(Form("unknown socket type %s - "
991                     "disconnect client: %s (%s), port %d", socketType,
992                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
993       delete socket;
994       continue;
995     }
996   }
997
998   // remove finished or invalid clients
999   for (Int_t iSocket = 0; iSocket < fSockets.GetEntriesFast(); iSocket++) {
1000     socket = (TSocket*) fSockets[iSocket];
1001     if (!socket) continue;
1002     char controlMessage[256];
1003     if (socket->Recv(controlMessage, 255)) {
1004       if (strcmp(controlMessage, "disconnect") == 0) {
1005         TInetAddress adr = socket->GetInetAddress();
1006         AliInfo(Form("disconnect client: %s (%s), port %d",
1007                      adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1008         delete fSockets.RemoveAt(iSocket);
1009         continue;
1010       }
1011     }
1012     if (!socket->IsValid()) {
1013       // remove invalid sockets from the list
1014       TInetAddress adr = socket->GetInetAddress();
1015       AliError(Form("disconnect invalid client: %s (%s), port %d",
1016                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1017       delete fSockets.RemoveAt(iSocket);
1018     }
1019   }
1020   fSockets.Compress();
1021 }
1022
1023 //_____________________________________________________________________________
1024 void AliMonitorProcess::BroadcastHistos(TSocket* toSocket)
1025 {
1026 // send the monitor histograms to the clients
1027
1028   SetStatus(kBroadcasting);
1029   TMessage message(kMESS_OBJECT);
1030   message.WriteObject(fTopFolder); 
1031
1032   for (Int_t iSocket = 0; iSocket < fSockets.GetEntriesFast(); iSocket++) {
1033     TSocket* socket = (TSocket*) fSockets[iSocket];
1034     if (!socket) continue;
1035     if (toSocket && (socket != toSocket)) continue;
1036
1037     // send control message
1038     if (!socket->IsValid() || (socket->Send("histograms") <= 0)) {
1039       TInetAddress adr = socket->GetInetAddress();
1040       AliError(Form("connection to client failed - "
1041                     "disconnect client: %s (%s), port %d",
1042                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1043       delete fSockets.RemoveAt(iSocket);
1044     }
1045
1046     // receive control message
1047     char controlMessage[256];
1048     Int_t result = socket->Recv(controlMessage, 255);
1049     if (result <= 0) {
1050       gSystem->Sleep(1000);  // wait one second and try again
1051       result = socket->Recv(controlMessage, 255);
1052     }
1053     if (result <= 0) {
1054       TInetAddress adr = socket->GetInetAddress();
1055       AliError(Form("no response from client - "
1056                     "disconnect client: %s (%s), port %d",
1057                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1058       delete fSockets.RemoveAt(iSocket);
1059       continue;
1060     }
1061     if (strcmp(controlMessage, "ok") != 0) {
1062       TInetAddress adr = socket->GetInetAddress();
1063       AliError(Form("no \"ok\" message from client - "
1064                     "disconnect client: %s (%s), port %d",
1065                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1066       delete fSockets.RemoveAt(iSocket);
1067       continue;
1068     }
1069
1070     socket->SetOption(kNoBlock, 0);
1071     if (socket->Send(message) < 0) {
1072       // remove the socket from the list if there was an error
1073       TInetAddress adr = socket->GetInetAddress();
1074       AliError(Form("sending histograms failed - "
1075                     "disconnect client: %s (%s), port %d",
1076                     adr.GetHostName(), adr.GetHostAddress(), adr.GetPort()));
1077       delete fSockets.RemoveAt(iSocket);
1078     } else {
1079       gSystem->Sleep(100);
1080       socket->SetOption(kNoBlock, 1);
1081     }
1082   }
1083   fSockets.Compress();
1084 }
1085
1086
1087 //_____________________________________________________________________________
1088 AliMonitorProcess::AliMonitorInterruptHandler::AliMonitorInterruptHandler
1089   (AliMonitorProcess* process):
1090   TSignalHandler(kSigUser1, kFALSE), 
1091   fProcess(process) 
1092 {
1093 // constructor: set process
1094 }
1095
1096 //_____________________________________________________________________________
1097 AliMonitorProcess::AliMonitorInterruptHandler::AliMonitorInterruptHandler
1098   (const AliMonitorInterruptHandler& handler):
1099   TSignalHandler(handler)
1100 {
1101 // copy constructor
1102
1103   AliFatal("copy constructor not implemented");
1104 }
1105
1106 //_____________________________________________________________________________
1107 AliMonitorProcess::AliMonitorInterruptHandler& 
1108   AliMonitorProcess::AliMonitorInterruptHandler::operator = 
1109   (const AliMonitorInterruptHandler& /*handler*/) 
1110 {
1111 // assignment operator
1112
1113   AliFatal("assignment operator not implemented"); 
1114   return *this;
1115 }
1116
1117 //_____________________________________________________________________________
1118 Bool_t AliMonitorProcess::AliMonitorInterruptHandler::Notify() 
1119 {
1120 // interrupt signal -> stop process
1121
1122   AliInfo("the monitoring process will be stopped.");
1123   fProcess->Stop(); 
1124   return kTRUE;
1125 }