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