1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
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. //
26 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliMonitorProcess.h"
30 #include "AliMonitorTPC.h"
31 #include "AliMonitorITS.h"
32 #include "AliMonitorV0s.h"
33 #include "AliMonitorHLT.h"
34 #include "AliRawReaderRoot.h"
35 #include "AliLoader.h"
38 #include "AliTPCclustererMI.h"
39 #include "AliTPCtrackerMI.h"
41 #include "AliITSclustererV2.h"
42 #include "AliITStrackerV2.h"
43 #include "AliITSLoader.h"
44 #include "AliV0vertexer.h"
48 #include <TGridResult.h>
51 #include <AliL3Transform.h>
54 ClassImp(AliMonitorProcess)
57 const Int_t AliMonitorProcess::kgPort = 9327;
60 //_____________________________________________________________________________
61 AliMonitorProcess::AliMonitorProcess(const char* alienDir,
62 const char* fileNameGalice)
64 // initialize the monitoring process and the monitor histograms
66 fGrid = TGrid::Connect("alien", gSystem->Getenv("USER"));
67 if (!fGrid || fGrid->IsZombie() || !fGrid->IsConnected()) {
69 Fatal("AliMonitorProcess", "could not connect to alien");
72 fLogicalFileName = "";
75 fRunLoader = AliRunLoader::Open(fileNameGalice);
76 if (!fRunLoader) Fatal("AliMonitorProcess",
77 "could not get run loader from file %s",
80 fRunLoader->CdGAFile();
81 fTPCParam = AliTPC::LoadTPCParam(gFile);
82 if (!fTPCParam) Fatal("AliMonitorProcess", "could not load TPC parameters");
84 fRunLoader->LoadgAlice();
85 gAlice = fRunLoader->GetAliRun();
86 if (!gAlice) Fatal("AliMonitorProcess", "no gAlice object found");
87 AliITS* ITS = (AliITS*) gAlice->GetModule("ITS");
88 if (!ITS) Fatal("AliMonitorProcess", "no ITS detector found");
89 fITSgeom = ITS->GetITSgeom();
90 if (!fITSgeom) Fatal("AliMonitorProcess", "could not load ITS geometry");
93 // Init TPC parameters for HLT
94 Bool_t isinit=AliL3Transform::Init(const_cast<char*>(fileNameGalice),kTRUE);
96 cerr << "Could not create transform settings, please check log for error messages!" << endl;
105 fWriteHistoList = kFALSE;
107 fTopFolder = new TFolder("Monitor", "monitor histograms");
108 fTopFolder->SetOwner(kTRUE);
110 fMonitors.Add(new AliMonitorTPC(fTPCParam));
111 fMonitors.Add(new AliMonitorITS(fITSgeom));
112 fMonitors.Add(new AliMonitorV0s);
114 fMonitors.Add(new AliMonitorHLT(fTPCParam));
117 for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
118 ((AliMonitor*) fMonitors[iMonitor])->CreateHistos(fTopFolder);
121 TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
122 while (TFolder* folder = (TFolder*) iFolder->Next()) folder->SetOwner(kTRUE);
125 fFile = TFile::Open("monitor_tree.root", "RECREATE");
126 if (!fFile || !fFile->IsOpen()) {
127 Fatal("AliMonitorProcess", "could not open file for tree");
129 fTree = new TTree("MonitorTree", "tree for monitoring");
130 for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
131 ((AliMonitor*) fMonitors[iMonitor])->CreateBranches(fTree);
135 fServerSocket = new TServerSocket(kgPort, kTRUE);
136 fServerSocket->SetOption(kNoBlock, 1);
137 fDisplaySocket = NULL;
138 CheckForConnections();
147 //_____________________________________________________________________________
148 AliMonitorProcess::~AliMonitorProcess()
156 delete fServerSocket;
158 delete fDisplaySocket;
165 gSystem->Unlink("monitor_tree.root");
173 //_____________________________________________________________________________
174 const char* AliMonitorProcess::GetRevision()
180 //_____________________________________________________________________________
181 void AliMonitorProcess::SetStatus(EStatus status)
184 gSystem->ProcessEvents();
188 //_____________________________________________________________________________
189 void AliMonitorProcess::Run()
191 // run the monitor process:
192 // check for a raw data file, process the raw data file and delete it
198 while (!CheckForNewFile()) {
199 CheckForConnections();
201 if (fStopping) break;
204 if (fStopping) break;
216 //_____________________________________________________________________________
217 void AliMonitorProcess::Stop()
219 // set the fStopping flag to terminate the monitor process after the current
220 // event was processed
222 if (GetStatus() != kStopped) fStopping = kTRUE;
226 //_____________________________________________________________________________
227 void AliMonitorProcess::ProcessFile(const char* fileName)
229 // create a file with monitor histograms for a single file
231 if (GetStatus() != kStopped) {
232 Error("ProcessFile", "ProcessFile can not be called"
233 " while the monitor process is running");
237 fFileName = fileName;
238 Int_t nEventMin = fNEventsMin;
242 fNEventsMin = nEventMin;
247 //_____________________________________________________________________________
248 Bool_t AliMonitorProcess::CheckForNewFile()
250 // check whether a new file was registered in alien
252 TGridResult* result = fGrid->Ls();
257 while (const char* entry = result->Next()) {
258 // entry = host_date_time.root
259 TString entryCopy(entry);
260 char* p = const_cast<char*>(entryCopy.Data());
261 if (!strtok(p, "_") || !p) continue; // host name
262 char* dateStr = strtok(NULL, "_");
263 if (!dateStr || !p) continue;
264 char* timeStr = strtok(NULL, ".");
265 if (!timeStr || !p) continue;
266 Long_t date = atoi(dateStr);
267 Long_t time = atoi(timeStr);
269 if ((date > maxDate) || ((date == maxDate) && (time > maxTime))) {
276 if (maxDate < 0) return kFALSE; // no files found
277 if (fLogicalFileName.CompareTo(fileName) == 0) return kFALSE; // no new file
279 fLogicalFileName = fileName;
280 TGridResult* result2 = fGrid->GetPhysicalFileNames(fLogicalFileName.Data());
281 fFileName = result2->Next();
286 //_____________________________________________________________________________
287 Bool_t AliMonitorProcess::ProcessFile()
289 // loop over all events in the raw data file, run the reconstruction
290 // and fill the monitor histograms
292 Int_t nEvents = GetNumberOfEvents(fFileName);
293 if (nEvents <= 0) return kFALSE;
294 Info("ProcessFile", "found %d event(s) in file %s",
295 nEvents, fFileName.Data());
297 CreateHLT(fFileName);
300 // loop over the events
301 for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
303 fRunLoader->SetEventNumber(0);
304 AliRawReaderRoot rawReader(fFileName, iEvent);
305 if (fStopping) break;
306 if (rawReader.GetRunNumber() != fRunNumber) {
309 fRunNumber = rawReader.GetRunNumber();
310 fEventNumber[0] = rawReader.GetEventId()[0];
311 fEventNumber[1] = rawReader.GetEventId()[1];
313 if (fStopping) break;
316 if (!ReconstructTPC(&rawReader)) return kFALSE;
317 if (fStopping) break;
318 if (!ReconstructITS(&rawReader)) return kFALSE;
319 if (fStopping) break;
320 if (!ReconstructV0s()) return kFALSE;
321 if (fStopping) break;
322 if (!ReconstructHLT(iEvent)) return kFALSE;
323 if (fStopping) break;
325 if (fDisplaySocket) fDisplaySocket->Send("new event");
327 Info("ProcessFile", "filling histograms...");
329 for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
330 ((AliMonitor*) fMonitors[iMonitor])->FillHistos(fRunLoader, &rawReader);
331 if (fStopping) break;
333 if (fStopping) break;
335 Info("ProcessFile", "updating histograms...");
336 SetStatus(kUpdating);
337 TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
338 while (TFolder* folder = (TFolder*) iFolder->Next()) {
339 TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
340 while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
346 if (fStopping) break;
348 Info("ProcessFile", "filling the tree...");
351 Info("ProcessFile", "broadcasting histograms...");
352 CheckForConnections();
356 if (fStopping) break;
367 //_____________________________________________________________________________
368 void AliMonitorProcess::Reset()
370 // write the current histograms to a file and reset them
372 if (fSubRunNumber == 0) fSubRunNumber++;
379 //_____________________________________________________________________________
380 UInt_t AliMonitorProcess::GetEventPeriodNumber()
382 // get the period number from the event id
384 return (fEventNumber[1] >> 4);
387 //_____________________________________________________________________________
388 UInt_t AliMonitorProcess::GetEventOrbitNumber()
390 // get the orbit number from the event id
392 return ((fEventNumber[1] & 0x000F) << 20) + (fEventNumber[0] >> 12);
395 //_____________________________________________________________________________
396 UInt_t AliMonitorProcess::GetEventBunchNumber()
398 // get the bunch number from the event id
400 return (fEventNumber[0] % 0x0FFF);
403 //_____________________________________________________________________________
404 Int_t AliMonitorProcess::GetNumberOfEvents(const char* fileName)
406 // determine the number of events in the given raw data file
410 TFile* file = TFile::Open(fileName);
411 if (!file || !file->IsOpen()) {
412 Error("GetNumberOfEvents", "could not open file %s", fileName);
413 if (file) delete file;
417 TTree* tree = (TTree*) file->Get("RAW");
419 Error("GetNumberOfEvents", "could not find tree with raw data");
421 nEvents = (Int_t) tree->GetEntries();
429 //_____________________________________________________________________________
430 Bool_t AliMonitorProcess::ReconstructTPC(AliRawReader* rawReader)
432 // find TPC clusters and tracks
436 AliLoader* tpcLoader = fRunLoader->GetLoader("TPCLoader");
438 Error("ReconstructTPC", "no TPC loader found");
441 gSystem->Unlink("TPC.RecPoints.root");
442 gSystem->Unlink("TPC.Tracks.root");
445 Info("ReconstructTPC", "reconstructing clusters...");
446 tpcLoader->LoadRecPoints("recreate");
447 AliTPCclustererMI clusterer(fTPCParam);
448 tpcLoader->MakeRecPointsContainer();
449 clusterer.SetOutput(tpcLoader->TreeR());
450 clusterer.Digits2Clusters(rawReader);
451 tpcLoader->WriteRecPoints("OVERWRITE");
454 Info("ReconstructTPC", "reconstructing tracks...");
455 tpcLoader->LoadTracks("recreate");
457 AliTPCtrackerMI tracker(fTPCParam);
458 tracker.Clusters2Tracks();
461 tpcLoader->UnloadRecPoints();
462 tpcLoader->UnloadTracks();
466 //_____________________________________________________________________________
467 Bool_t AliMonitorProcess::ReconstructITS(AliRawReader* rawReader)
469 // find ITS clusters and tracks
473 AliLoader* itsLoader = fRunLoader->GetLoader("ITSLoader");
475 Error("ReconstructITS", "no ITS loader found");
478 AliLoader* tpcLoader = fRunLoader->GetLoader("TPCLoader");
480 Error("ReconstructITS", "no TPC loader found");
483 gSystem->Unlink("ITS.RecPoints.root");
484 gSystem->Unlink("ITS.Tracks.root");
487 Info("ReconstructITS", "reconstructing clusters...");
488 itsLoader->LoadRecPoints("recreate");
489 AliITSclustererV2 clusterer(fITSgeom);
490 itsLoader->MakeRecPointsContainer();
491 clusterer.Digits2Clusters(rawReader);
494 Info("ReconstructITS", "reconstructing tracks...");
495 itsLoader->LoadTracks("recreate");
496 itsLoader->MakeTracksContainer();
497 tpcLoader->LoadTracks();
498 AliITStrackerV2 tracker(fITSgeom);
499 tracker.LoadClusters(itsLoader->TreeR());
500 tracker.Clusters2Tracks(tpcLoader->TreeT(), itsLoader->TreeT());
501 tracker.UnloadClusters();
502 itsLoader->WriteTracks("OVERWRITE");
504 itsLoader->UnloadRecPoints();
505 itsLoader->UnloadTracks();
506 tpcLoader->UnloadTracks();
510 //_____________________________________________________________________________
511 Bool_t AliMonitorProcess::ReconstructV0s()
517 AliITSLoader* itsLoader = (AliITSLoader*) fRunLoader->GetLoader("ITSLoader");
519 Error("ReconstructV0", "no ITS loader found");
522 gSystem->Unlink("ITS.V0s.root");
525 Info("ReconstructV0s", "reconstructing V0s...");
526 itsLoader->LoadTracks("read");
527 itsLoader->LoadV0s("recreate");
528 AliV0vertexer vertexer;
529 TTree* tracks = itsLoader->TreeT();
531 Error("ReconstructV0s", "no ITS tracks tree found");
534 if (!itsLoader->TreeV0()) itsLoader->MakeTree("V0");
535 TTree* v0s = itsLoader->TreeV0();
536 vertexer.Tracks2V0vertices(tracks, v0s);
537 itsLoader->WriteV0s("OVERWRITE");
539 itsLoader->UnloadTracks();
540 itsLoader->UnloadV0s();
544 //_____________________________________________________________________________
546 void AliMonitorProcess::CreateHLT(const char* fileName)
549 // create the HLT (Level3) object
551 if (fHLT) delete fHLT;
554 strcpy(name, fileName);
555 fHLT = new AliLevel3(name);
556 fHLT->Init("./", AliLevel3::kRaw, 1);
558 fHLT->SetClusterFinderParam(0, 0, kTRUE);
560 Int_t phi_segments = 50;
561 Int_t eta_segments = 100;
562 Int_t trackletlength = 3;
563 Int_t tracklength = 40;//40 or 5
564 Int_t rowscopetracklet = 2;
565 Int_t rowscopetrack = 2;
566 Double_t min_pt_fit = 0;
567 Double_t maxangle = 1.31;
568 Double_t goodDist = 5;
569 Double_t maxphi = 100;
570 Double_t maxeta = 100;
571 Double_t hitChi2Cut = 15;//100 or 15
572 Double_t goodHitChi2 = 5;//20 or 5
573 Double_t trackChi2Cut = 10;//50 or 10
574 fHLT->SetTrackerParam(phi_segments, eta_segments,
575 trackletlength, tracklength,
576 rowscopetracklet, rowscopetrack,
577 min_pt_fit, maxangle, goodDist, hitChi2Cut,
578 goodHitChi2, trackChi2Cut, 50, maxphi, maxeta, kTRUE);
580 fHLT->WriteFiles("./hlt/");
584 //_____________________________________________________________________________
585 Bool_t AliMonitorProcess::ReconstructHLT(Int_t iEvent)
587 // run the HLT cluster and track finder
592 Warning("ReconstructHLT", "the code was compiled without HLT support");
596 gSystem->Exec("rm -rf hlt");
597 gSystem->MakeDirectory("hlt");
598 if (!fHLT) return kFALSE;
600 fHLT->ProcessEvent(0, 35, iEvent);
602 // remove the event number from the file names
604 sprintf(command, "rename points_%d points hlt/*.raw", iEvent);
605 gSystem->Exec(command);
606 sprintf(command, "rename tracks_tr_%d tracks_tr hlt/*.raw", iEvent);
607 gSystem->Exec(command);
608 sprintf(command, "rename tracks_gl_%d tracks_gl hlt/*.raw", iEvent);
609 gSystem->Exec(command);
610 sprintf(command, "rename tracks_%d tracks hlt/*.raw", iEvent);
611 gSystem->Exec(command);
617 //_____________________________________________________________________________
618 Bool_t AliMonitorProcess::WriteHistos()
620 // write the monitor tree and the monitor histograms to the file
621 // "monitor_<run number>[_<sub_run_number>].root"
622 // if at least fNEventsMin events were monitored
626 // rename tree file and create a new one
633 sprintf(fileName, "monitor_tree_%d.root", fRunNumber);
634 if (fSubRunNumber > 0) {
635 sprintf(fileName, "monitor_tree_%d_%d.root", fRunNumber, fSubRunNumber);
637 if (fNEvents < fNEventsMin) {
638 gSystem->Unlink("monitor_tree.root");
640 gSystem->Rename("monitor_tree.root", fileName);
643 fFile = TFile::Open("monitor_tree.root", "RECREATE");
644 if (!fFile || !fFile->IsOpen()) {
645 Fatal("WriteHistos", "could not open file for tree");
647 fTree = new TTree("MonitorTree", "tree for monitoring");
648 for (Int_t iMonitor = 0; iMonitor < fMonitors.GetEntriesFast(); iMonitor++) {
649 ((AliMonitor*) fMonitors[iMonitor])->CreateBranches(fTree);
653 // write the histograms
654 if (fNEvents < fNEventsMin) return kTRUE;
656 if (!fWriteHistoList) {
657 TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
658 while (TFolder* folder = (TFolder*) iFolder->Next()) {
659 TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
660 while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
668 Bool_t result = kTRUE;
669 sprintf(fileName, "monitor_%d.root", fRunNumber);
670 if (fSubRunNumber > 0) {
671 sprintf(fileName, "monitor_%d_%d.root", fRunNumber, fSubRunNumber);
673 TFile* file = TFile::Open(fileName, "recreate");
674 if (!file || !file->IsOpen()) {
675 Error("WriteHistos", "could not open file %s", fileName);
681 if (file) delete file;
686 //_____________________________________________________________________________
687 void AliMonitorProcess::StartNewRun()
689 // reset the histograms for a new run
691 SetStatus(kResetting);
692 TIterator* iFolder = fTopFolder->GetListOfFolders()->MakeIterator();
693 while (TFolder* folder = (TFolder*) iFolder->Next()) {
694 TIterator* iHisto = folder->GetListOfFolders()->MakeIterator();
695 while (AliMonitorPlot* histo = (AliMonitorPlot*) iHisto->Next()) {
706 //_____________________________________________________________________________
707 void AliMonitorProcess::CheckForConnections()
709 // check if new clients want to connect and add them to the list of sockets
711 TMessage message(kMESS_OBJECT);
712 message.WriteObject(fTopFolder);
713 SetStatus(kConnecting);
716 while ((socket = fServerSocket->Accept()) != (TSocket*)-1) {
717 socket->SetOption(kNoBlock,1);
718 char socketType[256];
719 if (!socket->Recv(socketType, 255)) continue;
720 if (strcmp(socketType, "client") == 0) {
721 if ((fNEvents == 0) || (socket->Send(message) >= 0)) {
722 fSockets.Add(socket);
723 TInetAddress adr = socket->GetInetAddress();
724 Info("CheckForConnections", "new client:\n %s (%s), port %d\n",
725 adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
727 } else if (strcmp(socketType, "display") == 0) {
728 if (fDisplaySocket) {
729 fDisplaySocket->Close();
730 delete fDisplaySocket;
732 fDisplaySocket = socket;
733 fDisplaySocket->SetOption(kNoBlock, 1);
734 TInetAddress adr = socket->GetInetAddress();
735 Info("CheckForConnections", "new display:\n %s (%s), port %d\n",
736 adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
740 for (Int_t iSocket = 0; iSocket < fSockets.GetEntriesFast(); iSocket++) {
741 socket = (TSocket*) fSockets[iSocket];
742 if (!socket) continue;
743 // remove finished client
745 if (socket->Recv(str, 255)) {
746 TString socketMessage(str);
747 if(socketMessage.CompareTo("Finished") == 0) {
748 TInetAddress adr = socket->GetInetAddress();
749 Info("CheckForConnections",
750 "disconnect finished client:\n %s (%s), port %d\n",
751 adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
752 delete fSockets.RemoveAt(iSocket);
756 if (!socket->IsValid()) {
757 // remove invalid sockets from the list
758 TInetAddress adr = socket->GetInetAddress();
759 Info("BroadcastHistos", "disconnect client:\n %s (%s), port %d\n",
760 adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
761 delete fSockets.RemoveAt(iSocket);
767 //_____________________________________________________________________________
768 void AliMonitorProcess::BroadcastHistos()
770 // send the monitor histograms to the clients
772 SetStatus(kBroadcasting);
773 TMessage message(kMESS_OBJECT);
774 message.WriteObject(fTopFolder);
776 for (Int_t iSocket = 0; iSocket < fSockets.GetEntriesFast(); iSocket++) {
777 TSocket* socket = (TSocket*) fSockets[iSocket];
778 if (!socket) continue;
779 if (!socket->IsValid() || (socket->Send(message) < 0)) {
780 // remove the socket from the list if there was an error
781 TInetAddress adr = socket->GetInetAddress();
782 Info("BroadcastHistos", "disconnect client:\n %s (%s), port %d\n",
783 adr.GetHostName(), adr.GetHostAddress(), adr.GetPort());
784 delete fSockets.RemoveAt(iSocket);