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