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