]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRGda.cxx
Overload the Clone() method of TObject to replace the method CreateCopy().
[u/mrichter/AliRoot.git] / MUON / MUONTRGda.cxx
1 /*
2
3 Version 2 for MUONTRGda MUON trigger
4 Working version for: 
5
6  reading back raw data
7
8  Versionning of the Mtg file
9
10  DA for ELECTRONICS_CALIBRATION_RUN (calib)
11    checking dead channels
12
13  DA for DETECTOR_CALIBRATION_RUN (ped)
14    checking the noisy channels
15
16  Interfaced with online database and file exchange server
17
18 November 2007
19 Ch. Finck
20
21 To be done:
22  Writing into the online database (need update of daqDAlib)
23  Looking at scalers outputs
24
25
26 */
27 extern "C" {
28 #include <daqDA.h>
29 }
30
31 #include "event.h"
32 #include "monitor.h"
33
34 #include <Riostream.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 //AliRoot
39 #include "AliRawReaderDate.h"
40
41 #include "AliMpConstants.h"
42 #include "AliMUONRawStreamTrigger.h"
43 #include "AliMUONDarcHeader.h"
44 #include "AliMUONRegHeader.h"
45 #include "AliMUONLocalStruct.h"
46 #include "AliMUONDDLTrigger.h"
47 #include "AliMUONVCalibParam.h"
48 #include "AliMUONVStore.h"
49 #include "AliMUONCalibParamND.h"
50 #include "AliMUON1DArray.h"
51 #include "AliMUONTriggerIO.h"
52
53 //ROOT
54 #include "TString.h"
55 #include "TSystem.h"
56 #include "TStopwatch.h"
57 #include "TMath.h"
58 #include "TTimeStamp.h"
59 #include "TROOT.h"
60 #include "TPluginManager.h"
61 #include "TFile.h"
62 #include "TH1F.h"
63 #include "TArrayI.h"
64 #include "TArrayS.h"
65
66 // global variables
67 const Int_t gkNLocalBoard = AliMpConstants::NofLocalBoards();
68
69 TString gCommand("ped");
70
71 TString gCurrentFileName("MtgCurrent.dat");
72 TString gLastCurrentFileName("MtgLastCurrent.dat");
73
74 TString gSodName;
75 Int_t   gSodFlag = 0;
76
77 TString gDAName;
78 Int_t   gDAFlag = 0;
79
80 TString gGlobalFileName;
81 TString gRegionalFileName;
82 TString gLocalMaskFileName;
83 TString gLocalLutFileName;
84 TString gSignatureFileName;
85
86 Int_t gGlobalFileVersion;
87 Int_t gRegionalFileVersion;
88 Int_t gLocalMaskFileVersion;
89 Int_t gLocalLutFileVersion;
90 Int_t gSignatureFileVersion;
91
92 Int_t gGlobalFileLastVersion;
93 Int_t gRegionalFileLastVersion;
94 Int_t gLocalMaskFileLastVersion;
95 Int_t gLocalLutFileLastVersion;
96
97 UInt_t gRunNumber = 0;
98 Int_t  gNEvents = 0;
99
100 Int_t gPrintLevel = 0;
101
102 AliMUONVStore* gLocalMasks    = 0x0;
103 AliMUONVStore* gRegionalMasks = 0x0;
104 AliMUONVCalibParam* globalMasks = 0x0;
105
106 AliMUONTriggerIO gTriggerIO;
107
108 AliMUONVStore* gPatternStore =  new AliMUON1DArray(gkNLocalBoard+9);
109
110 Char_t gHistoFileName[256];
111
112 Float_t gkThreshold = 0.8;
113
114 //__________________________________________________________________
115 void UpdateLocalMask(Int_t localBoardId, Int_t connector, Int_t strip)
116 {
117
118     // update local mask
119     AliMUONVCalibParam* localMask = 
120         static_cast<AliMUONVCalibParam*>(gLocalMasks->FindObject(localBoardId));
121
122     UShort_t mask = localMask->ValueAsInt(connector,0); 
123
124     mask ^= (0x1 << strip); // set strip mask to zero
125
126     localMask->SetValueAsInt(connector,0, mask);  
127 }
128
129 //__________________________________________________________________
130 void WriteLastCurrentFile(TString currentFile = gLastCurrentFileName)
131 {
132
133     // write last current file
134     ofstream out;
135     TString file;
136     file = currentFile;
137     out.open(file.Data());
138     out << gSodName << " " << gSodFlag << endl;
139     out << gDAName  << " " << gDAFlag  << endl;
140
141     out << gGlobalFileName    << " " << gGlobalFileVersion    << endl;
142     out << gRegionalFileName  << " " << gRegionalFileVersion  << endl;
143     out << gLocalMaskFileName << " " << gLocalMaskFileVersion << endl;
144     out << gLocalLutFileName  << " " << gLocalLutFileVersion  << endl;
145     out << gSignatureFileName << " " << gSignatureFileVersion << endl;
146
147     out.close();
148 }
149
150 //___________________________________________________________________________________________
151 Bool_t ReadCurrentFile(TString currentFile = gCurrentFileName, Bool_t lastCurrentFlag = false)
152 {
153
154     // read last current file name and version
155     char line[80];
156     char name[80];
157
158     TString file;
159     file = currentFile;
160     std::ifstream in(gSystem->ExpandPathName(file.Data()));
161     if (!in.good()) {
162       printf("Cannot open last current file %s\n",currentFile.Data());
163       return false;
164     }
165     
166
167     // read SOD 
168     in.getline(line,80);  
169     sscanf(line, "%s %d", name, &gSodFlag);
170     gSodName = name;
171     if (gPrintLevel) printf("Sod Flag %d\n", gSodFlag);
172
173     //read DA
174     in.getline(line,80);    
175     sscanf(line, "%s %d", name, &gDAFlag);
176     gDAName = name;
177     if (gPrintLevel) printf("DA Flag: %d\n", gDAFlag);
178
179
180     // read global
181     in.getline(line,80);    
182     TString tmp(line);
183     Int_t pos =  tmp.First(" ");
184     gGlobalFileName = tmp(0, pos);
185     
186     if (!lastCurrentFlag) {
187         gGlobalFileVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
188         if (gPrintLevel) printf("Global File Name: %s version: %d\n", 
189                             gGlobalFileName.Data(), gGlobalFileVersion);
190     } else {
191         gGlobalFileLastVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
192         if (gPrintLevel) printf("Global File Name: %s last version: %d\n", 
193                                 gGlobalFileName.Data(), gGlobalFileLastVersion);
194     }
195
196     // read regional
197     in.getline(line,80);
198     tmp = line;
199     pos = tmp.First(" ");
200     gRegionalFileName = tmp(0, pos);
201
202     if (!lastCurrentFlag) {
203         gRegionalFileVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
204         if (gPrintLevel) printf("Regional File Name: %s version: %d\n", 
205                                 gRegionalFileName.Data(), gRegionalFileVersion);
206
207     } else {
208         gRegionalFileLastVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
209         if (gPrintLevel) printf("Regional File Name: %s last version: %d\n", 
210                                 gRegionalFileName.Data(), gRegionalFileLastVersion);
211     }
212
213  
214
215     // read mask
216     in.getline(line,80);    
217     tmp = line;
218     pos = tmp.First(" ");
219     gLocalMaskFileName = tmp(0, pos);
220
221     if (!lastCurrentFlag) {
222       gLocalMaskFileVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
223       if (gPrintLevel) printf("Mask File Name: %s version: %d\n", 
224                             gLocalMaskFileName.Data(), gLocalMaskFileVersion);
225     } else {
226       gLocalMaskFileLastVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
227       if (gPrintLevel) printf("Mask File Name: %s last version: %d\n", 
228                             gLocalMaskFileName.Data(), gLocalMaskFileLastVersion);
229     }
230     // read Lut
231     in.getline(line,80);    
232     tmp = line;
233     pos = tmp.First(" ");
234     gLocalLutFileName = tmp(0, pos);
235
236     if (!lastCurrentFlag) {
237         gLocalLutFileVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
238         if (gPrintLevel) printf("Lut File Name: %s version: %d\n", 
239                                 gLocalLutFileName.Data(), gLocalLutFileVersion);
240     } else {
241         gLocalLutFileLastVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
242         if (gPrintLevel) printf("Lut File Name: %s last version: %d\n", 
243                                 gLocalLutFileName.Data(), gLocalLutFileLastVersion);
244     }
245
246     in.getline(line,80);    
247     tmp = line;
248     pos = tmp.First(" ");
249     gSignatureFileName = tmp(0, pos);
250     gSignatureFileVersion = atoi(tmp(pos+1, tmp.Length()-pos).Data());
251     if (gPrintLevel) printf("Lut File Name: %s version: %d\n", 
252                             gSignatureFileName.Data(), gSignatureFileVersion);
253
254     return true;
255 }
256
257 //_____________
258 void ReadFileNames()
259 {
260     // if last current file does not exist than read current file
261     if (!ReadCurrentFile(gLastCurrentFileName, true)) 
262     {
263       ReadCurrentFile(gCurrentFileName, true);
264       WriteLastCurrentFile();
265     } 
266
267     // any case read current file
268     ReadCurrentFile();
269
270 }
271
272 //__________________
273 Bool_t ExportFiles()
274 {
275
276     // Export files to FES
277     // Export files to DB not yet done, waiting for a version > 1.2 of daqDAlib
278     // env variables have to be set (suppose by ECS ?)
279     // setenv DATE_FES_PATH
280     // setenv DATE_RUN_NUMBER
281     // setenv DATE_ROLE_NAME
282     // setenv DATE_DETECTOR_CODE
283
284     // to be sure that env variable is set
285     gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/infoLogger");
286
287     // update files
288     Int_t status = 0;
289
290     Bool_t modified = false;
291
292     ofstream out;
293     TString fileExp("ExportedFiles.dat");
294     TString file;
295
296     out.open(fileExp.Data());
297     if (!out.good()) {
298         printf("Failed to create file: %s\n",file.Data());
299         return false;
300     }
301
302     if (gGlobalFileLastVersion != gGlobalFileVersion) {
303       file = gGlobalFileName.Data();
304       status = daqDA_FES_storeFile(file.Data(), file.Data());
305       if (status) {
306         printf("Failed to export file: %s\n",gGlobalFileName.Data());
307         return false;
308       }
309       out << gGlobalFileName.Data() << endl;
310       if(gPrintLevel) printf("Export file: %s\n",gGlobalFileName.Data());
311     }
312
313     if (gLocalMaskFileLastVersion != gLocalMaskFileVersion) {
314       modified = true;
315       file = gLocalMaskFileName;
316       status = daqDA_FES_storeFile(file.Data(), file.Data());
317       if (status) {
318         printf("Failed to export file: %s\n",gLocalMaskFileName.Data());
319         return false;
320       }
321       if(gPrintLevel) printf("Export file: %s\n",gLocalMaskFileName.Data());
322       out << gLocalMaskFileName.Data() << endl;
323     }
324
325     if (gLocalLutFileLastVersion != gLocalLutFileVersion) {
326       file = gLocalLutFileName;
327       modified = true;
328       status = daqDA_FES_storeFile(file.Data(), file.Data());
329       if (status) {
330         printf("Failed to export file: %s\n",gLocalLutFileName.Data());
331         return false;
332       }
333       if(gPrintLevel) printf("Export file: %s\n",gLocalLutFileName.Data());
334       out << gLocalLutFileName.Data() << endl;
335
336     }
337
338     // exported regional file whenever mask or/and Lut are modified
339     if ( (gRegionalFileLastVersion != gRegionalFileVersion) || modified) {
340       file = gRegionalFileName;
341       status = daqDA_FES_storeFile(file.Data(), file.Data());
342       if (status) {
343         printf("Failed to export file: %s\n",gRegionalFileName.Data());
344         return false;
345       }
346       if(gPrintLevel) printf("Export file: %s\n",gRegionalFileName.Data());
347       out << gRegionalFileName.Data() << endl;
348     }
349
350     out.close();
351
352     // export Exported file to FES anyway
353     status = daqDA_FES_storeFile(fileExp.Data(), fileExp.Data());
354     if (status) {
355       printf("Failed to export file: %s\n", fileExp.Data());
356       return false;
357     }
358     if(gPrintLevel) printf("Export file: %s\n",fileExp.Data());
359
360     return true;
361 }
362 //__________________
363 Bool_t ImportFiles()
364 {
365     // copy locally a file from daq detector config db 
366     // The current detector is identified by detector code in variable
367     // DATE_DETECTOR_CODE. It must be defined.
368     // If environment variable DAQDA_TEST_DIR is defined, files are copied from DAQDA_TEST_DIR
369     // instead of the database. The usual environment variables are not needed.
370
371     // to be sure that env variable is set
372     gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/db");
373
374     Int_t status = 0;
375
376     status = daqDA_DB_getFile(gCurrentFileName.Data(), gCurrentFileName.Data());
377     if (status) {
378       printf("Failed to get current config file from DB: %s\n",gCurrentFileName.Data());
379       return false;
380     }
381  
382     ReadFileNames();
383
384     status = daqDA_DB_getFile(gGlobalFileName.Data(), gGlobalFileName.Data());
385     if (status) {
386       printf("Failed to get current config file from DB: %s\n", gGlobalFileName.Data());
387       return false;
388     }
389
390     status = daqDA_DB_getFile(gRegionalFileName.Data(), gRegionalFileName.Data());
391     if (status) {
392       printf("Failed to get current config file from DB: %s\n",gRegionalFileName.Data());
393       return false;
394     }
395
396     status = daqDA_DB_getFile(gLocalMaskFileName.Data(), gLocalMaskFileName.Data());
397     if (status) {
398       printf("Failed to get current config file from DB: %s\n",gLocalMaskFileName.Data());
399       return false;
400     }
401
402     status = daqDA_DB_getFile(gLocalLutFileName.Data(), gLocalLutFileName.Data());
403     if (status) {
404       printf("Failed to get current config file from DB: %s\n",gLocalLutFileName.Data());
405       return false;
406     }
407  
408     return true;
409 }
410
411 //_____________
412 void ReadMaskFiles()
413 {
414     // read mask files
415     gLocalMasks    = new AliMUON1DArray(gkNLocalBoard+9);
416     gRegionalMasks = new AliMUON1DArray(16);
417
418
419     TString localFile    = gLocalMaskFileName;
420     TString regionalFile = gRegionalFileName;
421     TString globalFile   = gGlobalFileName;
422
423     gTriggerIO.ReadMasks(localFile.Data(), regionalFile.Data(), globalFile.Data(),
424                          gLocalMasks, gRegionalMasks, globalMasks, false);                      
425 }
426 //__________
427 void MakePattern(Int_t localBoardId, TArrayS& xPattern,  TArrayS& yPattern)
428 {
429
430     // calculate the hit map for each strip in x and y direction
431     AliMUONVCalibParam* pat = 
432         static_cast<AliMUONVCalibParam*>(gPatternStore->FindObject(localBoardId));
433
434     if (!pat) {
435       pat = new AliMUONCalibParamND(2, 64, localBoardId, 0,0.); // put default wise 0.
436       gPatternStore->Add(pat);  
437     }
438
439     for (Int_t i = 0; i < 4; ++i) {
440       for (Int_t j = 0; j < 16; ++j) {
441         
442         Int_t xMask = xPattern[i];
443         Int_t yMask = yPattern[i];
444
445         Int_t index = 16*i + j;
446         Double_t patOcc = 0.;
447
448         if ( (xMask >> j ) & 0x1 ) {
449             patOcc  = pat->ValueAsDouble(index, 0) + 1.;
450             pat->SetValueAsDouble(index, 0, patOcc);
451         }
452         if ( (yMask >> j ) & 0x1 ) {
453             patOcc  = pat->ValueAsDouble(index, 0) + 1.;
454             pat->SetValueAsDouble(index, 0, patOcc);
455         }
456       }
457     }
458
459 }
460
461 //__________
462 void MakePatternStore(Bool_t pedestal = true)
463 {
464
465     // calculates the occupancy (option: store in a root file)
466     // check noisy strip (pedestal true, software trigger)
467     // check dead channel (pesdetal false, FET trigger)
468
469     Int_t localBoardId = 0;
470     Bool_t updated = false;
471
472     // histo
473
474     Char_t name[255];
475     Char_t title[255];
476
477     TH1F*  xOccHisto[243];
478     TH1F*  yOccHisto[243];
479     TH1F*  xPatOccHisto = 0x0;
480     TH1F*  yPatOccHisto = 0x0;
481
482     TFile*  histoFile = 0x0;
483
484     if (gHistoFileName[0] != 0) {
485       histoFile = new TFile(gHistoFileName,"RECREATE","MUON Tracking pedestals");
486
487       sprintf(name,"pat_x");
488       sprintf(title,"Occupancy for x strip");
489       Int_t nx = 200;
490       Float_t xmin = -0.2;
491       Float_t xmax = 1.2; 
492       xPatOccHisto = new TH1F(name,title,nx,xmin,xmax);
493       xPatOccHisto ->SetDirectory(histoFile);
494
495       sprintf(name,"pat_y");
496       sprintf(title,"Occupancy for y strip");
497       yPatOccHisto = new TH1F(name,title,nx,xmin,xmax);
498       yPatOccHisto->SetDirectory(histoFile);
499     
500     }
501
502     // iterator over pedestal
503     TIter next(gPatternStore->CreateIterator());
504     AliMUONVCalibParam* pat;
505   
506     while ( ( pat = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
507     {
508       localBoardId  = pat->ID0();
509
510       if (gHistoFileName[0] != 0) {
511
512         Int_t nx = 64;
513         Float_t xmin = 0;
514         Float_t xmax = 64;
515
516         sprintf(name,"pat_x_%d",localBoardId);
517         sprintf(title,"Occupancy for x strip, board %d",localBoardId);
518         xOccHisto[localBoardId] = new TH1F(name,title,nx,xmin,xmax);
519
520         sprintf(name,"pat_y_%d",localBoardId);
521         sprintf(title,"Occupancy for y strip, board %d",localBoardId);
522         yOccHisto[localBoardId] = new TH1F(name,title,nx,xmin,xmax);
523
524       }
525
526       for (Int_t index = 0; index < pat->Size() ; ++index) {// 64 bits for X and 64 bits for Y strips
527
528         Double_t patXOcc  = pat->ValueAsDouble(index, 0)/(Double_t)gNEvents;
529         Double_t patYOcc  = pat->ValueAsDouble(index, 1)/(Double_t)gNEvents;
530
531         pat->SetValueAsDouble(index, 0, patXOcc);
532         pat->SetValueAsDouble(index, 1, patYOcc);
533
534
535         // check for x strip
536         if ( (patXOcc > gkThreshold && pedestal) || (patXOcc < 1.- gkThreshold && !pedestal) ) {
537           UShort_t strip  = index % 16;
538           Int_t connector = index/16;
539           UpdateLocalMask(localBoardId, connector, strip); 
540           updated = true;
541         }
542
543         // check for y strip
544         if ( (patYOcc > gkThreshold && pedestal) || (patYOcc < 1.- gkThreshold && !pedestal) ) {
545           UShort_t strip  = index % 16;
546           Int_t connector = index/16 + 4;
547           UpdateLocalMask(localBoardId, connector, strip);
548           updated = true;
549
550         }
551
552         if (gHistoFileName[0] != 0)  {
553           xPatOccHisto->Fill(patXOcc);
554           yPatOccHisto->Fill(patYOcc);
555           xOccHisto[localBoardId]->Fill(index, patXOcc);
556           yOccHisto[localBoardId]->Fill(index, patYOcc);
557
558         }       
559       }
560     }
561
562     if (gHistoFileName[0] != 0) {
563       histoFile->Write();
564       histoFile->Close();
565     }
566
567
568     if (updated) {
569
570       // update version
571       gLocalMaskFileVersion++;
572
573       TString tmp(gLocalMaskFileName);
574       Int_t pos = tmp.First("-");
575       gLocalMaskFileName = tmp(0,pos+1) + Form("%d",gLocalMaskFileVersion) + ".dat"; 
576
577       // write last current file
578       WriteLastCurrentFile();
579
580       gTriggerIO.WriteMasks(gLocalMaskFileName, gRegionalFileName, " ", gLocalMasks, gRegionalMasks , 0);
581
582     }
583 }
584
585 //*************************************************************//
586
587   // main routine
588 int main(Int_t argc, Char_t **argv) 
589 {
590   
591     // needed for streamer application
592     gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo", "*", "TStreamerInfo",
593                                           "RIO", "TStreamerInfo()"); 
594
595     Int_t skipEvents = 0;
596     Int_t maxEvents  = 1000000;
597     Char_t inputFile[256];
598     TString flatOutputFile;
599     TString configFile;
600
601 // option handler
602
603     // decode the input line
604     for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
605     {
606       Char_t* arg;
607       
608       arg = argv[i];
609       if (arg[0] != '-') continue;
610       switch (arg[1])
611       {
612       case 'f' : 
613           i++;
614           sprintf(inputFile,argv[i]);
615           break;
616       case 'c' : 
617           i++;
618           configFile = argv[i];
619           break;
620       case 'e' : 
621           i++;
622           gCommand = argv[i];
623           break;
624       case 'd' :
625           i++; 
626           gPrintLevel=atoi(argv[i]);
627           break;
628       case 's' :
629           i++; 
630           skipEvents=atoi(argv[i]);
631           break;
632       case 'n' :
633           i++; 
634           sscanf(argv[i],"%d",&maxEvents);
635           break;
636      case 'r' :
637           i++; 
638           sscanf(argv[i],"%s",gHistoFileName);
639           break;
640       case 'h' :
641           i++;
642           printf("\n******************* %s usage **********************",argv[0]);
643           printf("\n%s -options, the available options are :",argv[0]);
644           printf("\n-h help                   (this screen)");
645           printf("\n");
646           printf("\n Input");
647           printf("\n-f <raw data file>        (default = %s)",inputFile); 
648           printf("\n");
649           printf("\n output");
650           printf("\n-r <root file>            (default = %s)",gHistoFileName); 
651           printf("\n");
652           printf("\n Options");
653           printf("\n-d <print level>          (default = %d)",gPrintLevel);
654           printf("\n-s <skip events>          (default = %d)",skipEvents);
655           printf("\n-n <max events>           (default = %d)",maxEvents);
656           printf("\n-e <execute ped/calib>    (default = %s)",gCommand.Data());
657
658           printf("\n\n");
659           exit(-1);
660       default :
661           printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
662           argc = 2; exit(-1); // exit if error
663       } // end of switch  
664     } // end of for i  
665
666     // set command to lower case
667     gCommand.ToLower();
668
669     // decoding the events
670   
671     Int_t status;
672     Int_t nDateEvents = 0;
673
674     void* event;
675
676     // containers
677     AliMUONDDLTrigger*       ddlTrigger  = 0x0;
678     AliMUONDarcHeader*       darcHeader  = 0x0;
679     AliMUONRegHeader*        regHeader   = 0x0;
680     AliMUONLocalStruct*      localStruct = 0x0;
681
682     TStopwatch timers;
683
684     timers.Start(kTRUE); 
685
686     // comment out, since we do not retrieve files from database
687     if (!ImportFiles()) {
688       printf("Import from DB failed\n");
689       printf("For local test set DAQDA_TEST_DIR to the local directory where the Mtg files are located \n");
690       return -1;
691     }
692
693     if (!gDAFlag) {
694       if(!ExportFiles()) return -1;
695       return 0;
696     }
697
698     ReadMaskFiles();
699
700     status = monitorSetDataSource(inputFile);
701     if (status) {
702       cerr << "ERROR : monitorSetDataSource status (hex) = " << hex << status
703            << " " << monitorDecodeError(status) << endl;
704       return -1;
705     }
706     status = monitorDeclareMp("MUON Trigger monitoring");
707     if (status) {
708       cerr << "ERROR : monitorDeclareMp status (hex) = " << hex << status
709            << " " << monitorDecodeError(status) << endl;
710       return -1;
711     }
712
713     cout << "MUONTRKda : Reading data from file " << inputFile <<endl;
714
715     while(1) 
716     {
717       if (gNEvents >= maxEvents) break;
718       if (gNEvents && gNEvents % 100 == 0)      
719           cout<<"Cumulated events " << gNEvents << endl;
720
721       // check shutdown condition 
722       if (daqDA_checkShutdown()) 
723           break;
724
725       // Skip Events if needed
726       while (skipEvents) {
727         status = monitorGetEventDynamic(&event);
728         skipEvents--;
729       }
730
731       // starts reading
732       status = monitorGetEventDynamic(&event);
733       if (status < 0)  {
734         cout<<"EOF found"<<endl;
735         break;
736       }
737
738       nDateEvents++;
739
740       // decoding rawdata headers
741       AliRawReader *rawReader = new AliRawReaderDate(event);
742  
743       Int_t eventType = rawReader->GetType();
744       gRunNumber = rawReader->GetRunNumber();
745     
746
747       if (eventType != PHYSICS_EVENT) 
748           continue; // for the moment
749
750       gNEvents++;
751       if (gPrintLevel) printf("\nEvent # %d\n",gNEvents);
752
753       // decoding MUON payload
754       AliMUONRawStreamTrigger* rawStream  = new AliMUONRawStreamTrigger(rawReader);
755       //rawStream->SetMaxReg(1);
756
757       Int_t index = 0;
758       // loops over DDL 
759       while((status = rawStream->NextDDL())) {
760
761         if (gPrintLevel) printf("iDDL %d\n", rawStream->GetDDL());
762
763         ddlTrigger = rawStream->GetDDLTrigger();
764         darcHeader = ddlTrigger->GetDarcHeader();
765
766         if (gPrintLevel) printf("Global output %x\n", (Int_t)darcHeader->GetGlobalOutput());
767
768         // loop over regional structures
769         Int_t nReg = darcHeader->GetRegHeaderEntries();
770         for(Int_t iReg = 0; iReg < nReg; ++iReg){   //REG loop
771
772           if (gPrintLevel) printf("RegionalId %d\n", iReg);
773
774           regHeader =  darcHeader->GetRegHeaderEntry(iReg);
775
776           // loop over local structures
777           Int_t nLocal = regHeader->GetLocalEntries();
778           for(Int_t iLocal = 0; iLocal < nLocal; ++iLocal) {  
779
780             localStruct = regHeader->GetLocalEntry(iLocal);
781
782             Int_t localBoardId = gTriggerIO.LocalBoardId(index++);
783             if (gPrintLevel) printf("local %d\n",  localBoardId );
784
785             TArrayS xPattern(4);
786             TArrayS yPattern(4);
787             localStruct->GetXPattern(xPattern);
788             localStruct->GetYPattern(yPattern);
789             MakePattern(localBoardId, xPattern, yPattern);
790
791             if (gPrintLevel) printf("X pattern %x %x %x %x, Y pattern %x %x %x %x\n", 
792                                    localStruct->GetX1(), localStruct->GetX2(),localStruct->GetX3(),localStruct->GetX4(),
793                                    localStruct->GetY1(), localStruct->GetY2(),localStruct->GetY3(),localStruct->GetY4());
794            
795           } // iLocal
796         } // iReg
797       } // NextDDL
798
799       delete rawReader;
800       delete rawStream;
801
802     } // while (1)
803
804     if (gCommand.Contains("ped")) 
805         MakePatternStore();
806
807     if (gCommand.Contains("cal")) 
808         MakePatternStore(false);
809
810     if (!ExportFiles())
811         return -1;
812
813     timers.Stop();
814
815     cout << "MUONTRKda : Run number                    : " << gRunNumber << endl;
816     cout << "MUONTRKda : Histo file generated          : " << gHistoFileName  << endl;
817     cout << "MUONTRKda : Nb of DATE events     = "         << nDateEvents    << endl;
818     cout << "MUONTRKda : Nb of events used     = "         << gNEvents        << endl;
819
820     printf("Execution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
821
822     delete gLocalMasks;
823     delete gRegionalMasks;
824     delete globalMasks; // in case
825     delete gPatternStore;
826
827     return status;
828 }