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