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