]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRGda.cxx
Add missing target/projectile information
[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
7 Run Type:  PHYSICS
8 DA Type: MON
9 Number of events needed: 1000 events
10 Input files: MtgGlobalCrate.dat MtgRegionalCrate.dat MtgLocalMask.dat MtgLocalLut.dat MtgCurrent.dat DAConfig.dat
11 Output Files: ExportedFiles.dat MtgGlobalCrate.dat
12 Trigger types used: PHYSICS_EVENT CALIBRATION_EVENT
13 */
14
15 //////////////////////////////////////////////////////////////////////////////
16 // Detector Algorithm for the MUON trigger configuration.                   //
17 //                                                                          //
18 // Calculates masks for the global trigger input, by looking at dead        //
19 // channels in calibration events and at noisy channels in physics events.  //
20 // Transfers trigger configuration files to the File Exchange Server and    //
21 // writes them (if modified) into the trigger configuration data base.      //
22 //                                                                          //
23 // Authors:                                                                 //
24 // Christian Fink (formerly at Subatech, Nantes)                            //
25 // Franck Manso (LPC Clermont-Ferrand, manso@clermont.in2p3.fr)             //
26 // Bogdan Vulpescu (LPC Clermont-Ferrand, vulpescu@clermont.in2p3.fr)       //
27 //                                                                          //
28 //////////////////////////////////////////////////////////////////////////////
29
30 //#define OFFLINE
31
32 extern "C" {
33 #include <daqDA.h>
34 }
35
36 #include "event.h"
37 #include "monitor.h"
38
39 #include <Riostream.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 //AliRoot
44 #include "AliRawReaderDate.h"
45
46 #include "AliMpConstants.h"
47 #include "AliMUONRawStreamTrigger.h"
48 #include "AliMUONRawStreamTriggerHP.h"
49 #include "AliMUONDarcHeader.h"
50 #include "AliMUONDDLTrigger.h"
51 #include "AliMUONVStore.h"
52 #include "AliMUON1DArray.h"
53 #include "AliMUONTriggerIO.h"
54 #include "AliMUONRegionalTriggerConfig.h"
55 #include "AliMUONGlobalCrateConfig.h"
56 #include "AliMUONTriggerCrateConfig.h"
57
58 //ROOT
59 #include "TString.h"
60 #include "TSystem.h"
61 #include "TStopwatch.h"
62 #include "TROOT.h"
63 #include "TPluginManager.h"
64
65 /// class for DA run parameters and DA working space
66 class AliDAConfig : TObject {
67
68 public:
69
70   AliDAConfig() : 
71     fDAConfigFileName("DAConfig.dat"),
72     fCurrentFileName("MtgCurrent.dat"), 
73     fLastCurrentFileName("MtgLastCurrent.dat"), 
74     fSodName(""),
75     fSodFlag(0),
76     fDAName(""),
77     fDAFlag(0),
78     fGlobalFileName(""),
79     fRegionalFileName(""),
80     fLocalMaskFileName(""),
81     fLocalLutFileName(""),
82     fSignatureFileName(""),
83     fGlobalFileVersion(0),
84     fRegionalFileVersion(0),
85     fLocalMaskFileVersion(0),
86     fLocalLutFileVersion(0),
87     fSignatureFileVersion(0),
88     fGlobalFileLastVersion(0),
89     fRegionalFileLastVersion(0),
90     fLocalMaskFileLastVersion(0),
91     fLocalLutFileLastVersion(0),
92     fEventsN(0),
93     fEventsD(0),
94     fPrintLevel(0),
95     fLocalMasks(0x0),
96     fRegionalMasks(0x0),
97     fGlobalMasks(0x0),
98     fTriggerIO(new AliMUONTriggerIO),
99     fAlgoNoisyInput(false),
100     fAlgoDeadcInput(false),
101     fThrN(0.1),
102     fThrD(0.9),
103     fMinEvents(10),
104     fSkipEvents(0),
105     fMaxEvents(65535),
106     fWithWarnings(false),
107     fUseFastDecoder(false),
108     fNLocalBoard(AliMpConstants::TotalNofLocalBoards()+1)
109   {
110     /// default constructor
111     for (Int_t ii = 0; ii < kGlobalInputs; ii++) {
112       for (Int_t il = 0; il < kGlobalInputLength; il++) {
113         fAccGlobalInputN[ii][il] = 0;
114         fAccGlobalInputD[ii][il] = 0;
115       }
116     }
117     fLocalMasks    = new AliMUON1DArray(fNLocalBoard);
118     fRegionalMasks = new AliMUONRegionalTriggerConfig();
119     fGlobalMasks   = new AliMUONGlobalCrateConfig();
120   }
121
122   AliDAConfig (const AliDAConfig& cfg); // copy constructor
123   AliDAConfig& operator=(const AliDAConfig& cfg);  // assignment operator
124   virtual ~AliDAConfig()
125   {
126     /// destructor
127     delete fLocalMasks;
128     delete fRegionalMasks;
129     delete fGlobalMasks; 
130     delete fTriggerIO;
131   }
132   void PrintConfig()
133   {
134     /// print DA parameters
135     printf("DA config file name: %s \n",GetDAConfigFileName());
136     printf("Current file name: %s \n",GetCurrentFileName());
137     printf("Last current file name: %s \n",GetLastCurrentFileName());
138     printf("Global file name: %s (%d %d)\n",GetGlobalFileName(),GetGlobalFileVersion(),GetGlobalFileLastVersion());
139     printf("Regional file name: %s (%d %d)\n",GetRegionalFileName(),GetRegionalFileVersion(),GetRegionalFileLastVersion());
140     printf("Local mask file name: %s (%d %d)\n",GetLocalMaskFileName(),GetLocalMaskFileVersion(),GetLocalMaskFileLastVersion());
141     printf("Local LUT file name: %s (%d %d)\n",GetLocalLutFileName(),GetLocalLutFileVersion(),GetLocalLutFileLastVersion());
142     printf("Signature file name: %s (%d)\n",GetSignatureFileName(),GetSignatureFileVersion());
143   }
144
145   const Char_t* GetDAConfigFileName()    { return fDAConfigFileName.Data(); }
146   const Char_t* GetCurrentFileName()     { return fCurrentFileName.Data(); }
147   const Char_t* GetLastCurrentFileName() { return fLastCurrentFileName.Data(); }
148
149   const Char_t* GetSodName() { return fSodName.Data(); }
150   Int_t GetSodFlag() const { return fSodFlag; }
151
152   const Char_t* GetDAName() { return fDAName.Data(); }
153   Int_t GetDAFlag() const { return fDAFlag; }
154
155   const Char_t* GetGlobalFileName()    { return fGlobalFileName.Data(); }
156   const Char_t* GetRegionalFileName()  { return fRegionalFileName.Data(); }
157   const Char_t* GetLocalMaskFileName() { return fLocalMaskFileName.Data(); }
158   const Char_t* GetLocalLutFileName()  { return fLocalLutFileName.Data(); }
159   const Char_t* GetSignatureFileName() { return fSignatureFileName.Data(); }
160
161   Int_t GetGlobalFileVersion()    const { return fGlobalFileVersion; }
162   Int_t GetRegionalFileVersion()  const { return fRegionalFileVersion; }
163   Int_t GetLocalMaskFileVersion() const { return fLocalMaskFileVersion; }
164   Int_t GetLocalLutFileVersion()  const { return fLocalLutFileVersion; }
165   Int_t GetSignatureFileVersion() const { return fSignatureFileVersion; }
166
167   Int_t GetGlobalFileLastVersion()    const { return fGlobalFileLastVersion; }
168   Int_t GetRegionalFileLastVersion()  const { return fRegionalFileLastVersion; }
169   Int_t GetLocalMaskFileLastVersion() const { return fLocalMaskFileLastVersion; }
170   Int_t GetLocalLutFileLastVersion()  const { return fLocalLutFileLastVersion; }
171
172   AliMUONVStore*                GetLocalMasks()    const { return fLocalMasks; }
173   AliMUONRegionalTriggerConfig* GetRegionalMasks() const { return fRegionalMasks; }
174   AliMUONGlobalCrateConfig*     GetGlobalMasks()   const { return fGlobalMasks; }
175
176   AliMUONTriggerIO* GetTriggerIO() const { return fTriggerIO; }
177
178   Int_t GetEventsN() const { return fEventsN; }
179   Int_t GetEventsD() const { return fEventsD; }
180
181   Int_t GetPrintLevel() const { return fPrintLevel; }
182
183   Bool_t GetAlgoNoisyInput() const { return fAlgoNoisyInput; }
184   Bool_t GetAlgoDeadcInput() const { return fAlgoDeadcInput; }
185
186   Float_t GetThrN() const { return fThrN; }
187   Float_t GetThrD() const { return fThrD; }
188
189   Int_t GetMinEvents()  const { return fMinEvents; }
190   Int_t GetMaxEvents()  const { return fMaxEvents; }
191   Int_t GetSkipEvents() const { return fSkipEvents; }
192
193   Bool_t WithWarnings() const { return fWithWarnings; }
194   Bool_t UseFastDecoder() const { return fUseFastDecoder; }
195
196   Int_t GetGlobalInputs()      const { return kGlobalInputs; }
197   Int_t GetGlobalInputLength() const { return kGlobalInputLength; }
198
199   Int_t GetAccGlobalInputN(Int_t ii, Int_t ib) const { return fAccGlobalInputN[ii][ib]; }
200   Int_t GetAccGlobalInputD(Int_t ii, Int_t ib) const { return fAccGlobalInputD[ii][ib]; }
201
202   void SetSodName(Char_t *name) { fSodName = TString(name); }
203   void SetSodFlag(Int_t flag)   { fSodFlag = flag; }
204
205   void SetDAName(Char_t *name) { fDAName = TString(name); }
206   void SetDAFlag(Int_t flag)   { fDAFlag = flag; }
207
208   void SetGlobalFileName(const Char_t *name)    { fGlobalFileName = TString(name); }
209   void SetRegionalFileName(const Char_t *name)  { fRegionalFileName = TString(name); }
210   void SetLocalMaskFileName(const Char_t *name) { fLocalMaskFileName = TString(name); }
211   void SetLocalLutFileName(const Char_t *name)  { fLocalLutFileName = TString(name); }
212   void SetSignatureFileName(const Char_t *name) { fSignatureFileName = TString(name); }
213
214   void SetGlobalFileVersion(Int_t ver)    { fGlobalFileVersion = ver; }
215   void SetRegionalFileVersion(Int_t ver)  { fRegionalFileVersion = ver; }
216   void SetLocalMaskFileVersion(Int_t ver) { fLocalMaskFileVersion = ver; }
217   void SetLocalLutFileVersion(Int_t ver)  { fLocalLutFileVersion = ver; }
218   void SetSignatureFileVersion(Int_t ver) { fSignatureFileVersion = ver; }
219
220   void SetGlobalFileLastVersion(Int_t ver)    { fGlobalFileLastVersion = ver; }
221   void SetRegionalFileLastVersion(Int_t ver)  { fRegionalFileLastVersion = ver; }
222   void SetLocalMaskFileLastVersion(Int_t ver) { fLocalMaskFileLastVersion = ver; }
223   void SetLocalLutFileLastVersion(Int_t ver)  { fLocalLutFileLastVersion = ver; }
224
225   void IncNoiseEvent() { fEventsN++; }
226   void IncDeadcEvent() { fEventsD++; }
227
228   void AddAccGlobalInputN(Int_t ii, Int_t ib, Int_t val) { fAccGlobalInputN[ii][ib] += val; }
229   void AddAccGlobalInputD(Int_t ii, Int_t ib, Int_t val) { fAccGlobalInputD[ii][ib] += val; }
230
231   void SetPrintLevel(Int_t level) { fPrintLevel = level; }
232
233   void SetAlgoNoisyInput(Bool_t val) { fAlgoNoisyInput = val; }
234   void SetAlgoDeadcInput(Bool_t val) { fAlgoDeadcInput = val; }
235
236   void SetThrN(Float_t val) { fThrN = val; }
237   void SetThrD(Float_t val) { fThrD = val; }
238
239   void SetMinEvents(Int_t val)  { fMinEvents = val; }
240   void SetMaxEvents(Int_t val)  { fMaxEvents = val; }
241   void SetSkipEvents(Int_t val) { fSkipEvents = val; }
242
243   void SetWithWarnings() { fWithWarnings = true; }
244   void SetUseFastDecoder() { fUseFastDecoder = true; }
245
246   void IncGlobalFileVersion() { fGlobalFileVersion++; }
247   void DecSkipEvents() { fSkipEvents--; }
248
249 private:
250
251   const TString fDAConfigFileName;
252   const TString fCurrentFileName;
253   const TString fLastCurrentFileName;
254
255   TString fSodName;
256   Int_t   fSodFlag;
257
258   TString fDAName;
259   Int_t   fDAFlag;
260
261   TString fGlobalFileName;
262   TString fRegionalFileName;
263   TString fLocalMaskFileName;
264   TString fLocalLutFileName;
265   TString fSignatureFileName;
266
267   Int_t   fGlobalFileVersion;
268   Int_t   fRegionalFileVersion;
269   Int_t   fLocalMaskFileVersion;
270   Int_t   fLocalLutFileVersion;
271   Int_t   fSignatureFileVersion;
272   
273   Int_t   fGlobalFileLastVersion;
274   Int_t   fRegionalFileLastVersion;
275   Int_t   fLocalMaskFileLastVersion;
276   Int_t   fLocalLutFileLastVersion;
277
278   Int_t   fEventsN;
279   Int_t   fEventsD;
280
281   Int_t   fPrintLevel;
282
283   AliMUONVStore*                fLocalMasks;
284   AliMUONRegionalTriggerConfig* fRegionalMasks;
285   AliMUONGlobalCrateConfig*     fGlobalMasks;    
286
287   AliMUONTriggerIO *fTriggerIO;
288
289   Bool_t fAlgoNoisyInput;
290   Bool_t fAlgoDeadcInput;
291
292   Float_t fThrN;
293   Float_t fThrD;
294   Int_t   fMinEvents;
295   Int_t   fSkipEvents;
296   Int_t   fMaxEvents;
297   Bool_t  fWithWarnings;
298   Bool_t  fUseFastDecoder;
299
300   const Int_t fNLocalBoard;
301
302   enum { kGlobalInputs = 4, kGlobalInputLength = 32 };
303
304   Int_t fAccGlobalInputN[kGlobalInputs][kGlobalInputLength];
305   Int_t fAccGlobalInputD[kGlobalInputs][kGlobalInputLength];
306
307 };
308
309 //__________________________________________________________________
310 Bool_t ReadDAConfig(AliDAConfig& cfg)
311 {
312     /// read run parameters for the DA
313
314     char line[80];
315
316     TString file;
317     file = cfg.GetDAConfigFileName();
318     std::ifstream in(gSystem->ExpandPathName(file.Data()));
319     if (!in.good()) {
320       printf("Cannot open DA configuration file %s ; use default values.\n",file.Data());
321       return true;
322     }
323
324     TString tmp;
325     Int_t pos;
326     
327     in.getline(line,80);  
328     tmp = line;
329     pos = tmp.First(" ");
330     tmp = tmp(0,pos);
331     cfg.SetPrintLevel(tmp.Atoi());
332     
333     in.getline(line,80);  
334     tmp = line;
335     pos = tmp.First(" ");
336     tmp = tmp(0,pos);
337     cfg.SetThrN(tmp.Atof());
338
339     in.getline(line,80);  
340     tmp = line;
341     pos = tmp.First(" ");
342     tmp = tmp(0,pos);
343     cfg.SetThrD(tmp.Atof());
344
345     in.getline(line,80);  
346     tmp = line;
347     pos = tmp.First(" ");
348     tmp = tmp(0,pos);
349     cfg.SetMinEvents(tmp.Atoi());
350
351     in.getline(line,80);  
352     tmp = line;
353     pos = tmp.First(" ");
354     tmp = tmp(0,pos);
355     cfg.SetSkipEvents(tmp.Atoi());
356
357     in.getline(line,80);  
358     tmp = line;
359     pos = tmp.First(" ");
360     tmp = tmp(0,pos);
361     cfg.SetMaxEvents(tmp.Atoi());
362
363     in.getline(line,80);  
364     tmp = line;
365     pos = tmp.First(" ");
366     tmp = tmp(0,pos);
367     if (tmp.Atoi() != 0) cfg.SetWithWarnings();
368     
369     in.getline(line,80);  
370     tmp = line;
371     pos = tmp.First(" ");
372     tmp = tmp(0,pos);
373     if (tmp.Atoi() != 0) cfg.SetUseFastDecoder();
374     
375     return true;
376
377 }
378
379 //__________________________________________________________________
380 void WriteLastCurrentFile(AliDAConfig& cfg, TString currentFile)
381 {
382     /// write last current file
383
384     ofstream out;
385     TString file;
386     file = currentFile;
387     out.open(file.Data());
388     out << cfg.GetSodName() << " " << cfg.GetSodFlag() << endl;
389     out << cfg.GetDAName()  << " " << cfg.GetDAFlag()  << endl;
390
391     out << cfg.GetGlobalFileName()    << " " << cfg.GetGlobalFileVersion()    << endl;
392     out << cfg.GetRegionalFileName()  << " " << cfg.GetRegionalFileVersion()  << endl;
393     out << cfg.GetLocalMaskFileName() << " " << cfg.GetLocalMaskFileVersion() << endl;
394     out << cfg.GetLocalLutFileName()  << " " << cfg.GetLocalLutFileVersion()  << endl;
395     out << cfg.GetSignatureFileName() << " " << cfg.GetSignatureFileVersion() << endl;
396
397     out.close();
398 }
399
400 //___________________________________________________________________________________________
401 Bool_t ReadCurrentFile(AliDAConfig& cfg, TString currentFile, Bool_t lastCurrentFlag = false)
402 {
403     /// read last current file name and version
404
405     char line[80];
406     char name[80];
407     Int_t flag;
408
409     TString file;
410     file = currentFile;
411     std::ifstream in(gSystem->ExpandPathName(file.Data()));
412     if (!in.good()) {
413       printf("Cannot open last current file %s\n",currentFile.Data());
414       return false;
415     }
416     
417     // read SOD 
418     in.getline(line,80);  
419     sscanf(line, "%s %d", name, &flag);
420     cfg.SetSodName(name);
421     cfg.SetSodFlag(flag);
422     if (cfg.GetPrintLevel()) printf("Sod Flag %d\n", cfg.GetSodFlag());
423
424     //read DA
425     in.getline(line,80);    
426     sscanf(line, "%s %d", name, &flag);
427     cfg.SetDAName(name);
428     cfg.SetDAFlag(flag);
429     if (cfg.GetPrintLevel()) printf("DA Flag: %d\n", cfg.GetDAFlag());
430
431     // read global
432     in.getline(line,80);    
433     TString tmp(line);
434     Int_t pos =  tmp.First(" ");
435     TString tmp1 = tmp(0, pos);
436     cfg.SetGlobalFileName(tmp1.Data());
437     
438     if (!lastCurrentFlag) {
439       cfg.SetGlobalFileVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
440       if (cfg.GetPrintLevel()) printf("Global File Name: %s version: %d\n", 
441                                       cfg.GetGlobalFileName(), cfg.GetGlobalFileVersion());
442     } else {
443       cfg.SetGlobalFileLastVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
444       if (cfg.GetPrintLevel()) printf("Global File Name: %s last version: %d\n", 
445                                       cfg.GetGlobalFileName(), cfg.GetGlobalFileLastVersion());
446     }
447
448     // read regional
449     in.getline(line,80);
450     tmp = line;
451     pos = tmp.First(" ");
452     tmp1 = tmp(0, pos);
453     cfg.SetRegionalFileName(tmp1.Data());
454
455     if (!lastCurrentFlag) {
456       cfg.SetRegionalFileVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
457       if (cfg.GetPrintLevel()) printf("Regional File Name: %s version: %d\n", 
458                                       cfg.GetRegionalFileName(), cfg.GetRegionalFileVersion());
459
460     } else {
461       cfg.SetRegionalFileLastVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
462       if (cfg.GetPrintLevel()) printf("Regional File Name: %s last version: %d\n", 
463                                       cfg.GetRegionalFileName(), cfg.GetRegionalFileLastVersion());
464     }
465
466     // read mask
467     in.getline(line,80);    
468     tmp = line;
469     pos = tmp.First(" ");
470     tmp1 = tmp(0, pos);
471     cfg.SetLocalMaskFileName(tmp1.Data());
472
473     if (!lastCurrentFlag) {
474       cfg.SetLocalMaskFileVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
475       if (cfg.GetPrintLevel()) printf("Mask File Name: %s version: %d\n", 
476                                       cfg.GetLocalMaskFileName(), cfg.GetLocalMaskFileVersion());
477     } else {
478       cfg.SetLocalMaskFileLastVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
479       if (cfg.GetPrintLevel()) printf("Mask File Name: %s last version: %d\n", 
480                                       cfg.GetLocalMaskFileName(), cfg.GetLocalMaskFileLastVersion());
481     }
482     // read Lut
483     in.getline(line,80);    
484     tmp = line;
485     pos = tmp.First(" ");
486     tmp1 = tmp(0, pos);
487     cfg.SetLocalLutFileName(tmp1.Data());
488
489     if (!lastCurrentFlag) {
490       cfg.SetLocalLutFileVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
491       if (cfg.GetPrintLevel()) printf("Lut File Name: %s version: %d\n", 
492                                       cfg.GetLocalLutFileName(), cfg.GetLocalLutFileVersion());
493     } else {
494       cfg.SetLocalLutFileLastVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
495       if (cfg.GetPrintLevel()) printf("Lut File Name: %s last version: %d\n", 
496                                       cfg.GetLocalLutFileName(), cfg.GetLocalLutFileLastVersion());
497     }
498
499     in.getline(line,80);    
500     tmp = line;
501     pos = tmp.First(" ");
502     tmp1 = tmp(0, pos);
503     cfg.SetSignatureFileName(tmp1.Data());
504     cfg.SetSignatureFileVersion(atoi(tmp(pos+1, tmp.Length()-pos).Data()));
505     if (cfg.GetPrintLevel()) printf("Lut File Name: %s version: %d\n", 
506                                     cfg.GetSignatureFileName(), cfg.GetSignatureFileVersion());
507
508     return true;
509 }
510
511 //_____________
512 void ReadFileNames(AliDAConfig& cfg)
513 {
514   /// if last current file does not exist than read current file
515
516   if (!ReadCurrentFile(cfg,cfg.GetLastCurrentFileName(), true)) 
517     {
518       ReadCurrentFile(cfg,cfg.GetCurrentFileName(), true);
519     } 
520   
521   // any case read current file
522   ReadCurrentFile(cfg,cfg.GetCurrentFileName());
523   
524 }
525
526 //__________________
527 Bool_t ExportFiles(AliDAConfig& cfg)
528 {
529     /// Export files to FES
530
531     // env variables have to be set (suppose by ECS ?)
532     // setenv DATE_FES_PATH
533     // setenv DATE_RUN_NUMBER
534     // setenv DATE_ROLE_NAME
535     // setenv DATE_DETECTOR_CODE
536
537 #ifdef OFFLINE
538     gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/infoLogger");
539     gSystem->Setenv("DAQDA_TEST_DIR", "/alisoft/FES");
540 #endif
541
542     // update files
543     Int_t status = 0;
544
545     Bool_t modified = false;
546
547     ofstream out;
548     TString fileExp("ExportedFiles.dat");
549     TString file;
550
551     out.open(fileExp.Data());
552     if (!out.good()) {
553         printf("Failed to create file: %s\n",file.Data());
554         return false;
555     }      
556
557     // check if MtgLastCurrent.dat exists
558     // if not, do initial export of all files
559     Bool_t initFES = false;
560     if (gSystem->AccessPathName("MtgLastCurrent.dat"))
561       initFES = true;
562     if (initFES) printf("Copy all configuration files to the FES.\n");
563
564     file = cfg.GetGlobalFileName();
565     if ((cfg.GetGlobalFileLastVersion() != cfg.GetGlobalFileVersion()) || initFES) {
566       status = daqDA_FES_storeFile(file.Data(), "GLOBAL");
567       if (status) {
568         printf("Failed to export file: %s\n",cfg.GetGlobalFileName());
569         return false;
570       }
571       if(cfg.GetPrintLevel()) printf("Export file: %s\n",cfg.GetGlobalFileName());
572       out << cfg.GetGlobalFileName() << endl;
573     }
574
575     file = cfg.GetLocalMaskFileName();  
576     if ((cfg.GetLocalMaskFileLastVersion() != cfg.GetLocalMaskFileVersion()) || initFES) {
577       modified = true;
578       status = daqDA_FES_storeFile(file.Data(), "LOCAL");
579       if (status) {
580         printf("Failed to export file: %s\n",cfg.GetLocalMaskFileName());
581         return false;
582       }
583       if(cfg.GetPrintLevel()) printf("Export file: %s\n",cfg.GetLocalMaskFileName());
584       out << cfg.GetLocalMaskFileName() << endl;
585     }
586
587     file = cfg.GetLocalLutFileName();
588     if ((cfg.GetLocalLutFileLastVersion() != cfg.GetLocalLutFileVersion()) || initFES) {
589       modified = true;
590       status = daqDA_FES_storeFile(file.Data(), "LUT");
591       if (status) {
592         printf("Failed to export file: %s\n",cfg.GetLocalLutFileName());
593         return false;
594       }
595       if(cfg.GetPrintLevel()) printf("Export file: %s\n",cfg.GetLocalLutFileName());
596       out << cfg.GetLocalLutFileName() << endl;
597
598     }
599
600     // exported regional file whenever mask or/and Lut are modified
601     file = cfg.GetRegionalFileName();
602     if ( (cfg.GetRegionalFileLastVersion() != cfg.GetRegionalFileVersion()) || modified || initFES) {
603       status = daqDA_FES_storeFile(file.Data(), "REGIONAL");
604       if (status) {
605         printf("Failed to export file: %s\n",cfg.GetRegionalFileName());
606         return false;
607       }
608       if(cfg.GetPrintLevel()) printf("Export file: %s\n",cfg.GetRegionalFileName());
609       out << cfg.GetRegionalFileName() << endl;
610     }
611
612     out.close();
613
614     // export Exported file to FES anyway
615     status = daqDA_FES_storeFile(fileExp.Data(), "EXPORTED");
616     if (status) {
617       printf("Failed to export file: %s\n", fileExp.Data());
618       return false;
619     }
620     if(cfg.GetPrintLevel()) printf("Export file: %s\n",fileExp.Data());
621
622     // write last current file
623     WriteLastCurrentFile(cfg,cfg.GetLastCurrentFileName());
624
625     return true;
626 }
627
628 //__________________
629 Bool_t ImportFiles(AliDAConfig& cfg)
630 {
631     /// copy locally a file from daq detector config db 
632     /// The current detector is identified by detector code in variable
633     /// DATE_DETECTOR_CODE. It must be defined.
634     /// If environment variable DAQDA_TEST_DIR is defined, files are copied from 
635     /// DAQDA_TEST_DIR instead of the database. 
636     /// The usual environment variables are not needed.
637
638     Int_t status = 0;
639
640 #ifdef OFFLINE
641     gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/db");
642 #endif
643
644     status = daqDA_DB_getFile(cfg.GetDAConfigFileName(), cfg.GetDAConfigFileName());
645     if (status) {
646       printf("Failed to get DA config file from DB: %s\n",cfg.GetDAConfigFileName());
647       return false;
648     }
649  
650     ReadDAConfig(cfg);
651     
652     status = daqDA_DB_getFile(cfg.GetCurrentFileName(), cfg.GetCurrentFileName());
653     if (status) {
654       printf("Failed to get current config file from DB: %s\n",cfg.GetCurrentFileName());
655       return false;
656     }
657     
658     ReadFileNames(cfg);
659
660     status = daqDA_DB_getFile(cfg.GetGlobalFileName(), cfg.GetGlobalFileName());
661     if (status) {
662       printf("Failed to get current config file from DB: %s\n", cfg.GetGlobalFileName());
663       return false;
664     }
665
666     status = daqDA_DB_getFile(cfg.GetRegionalFileName(), cfg.GetRegionalFileName());
667     if (status) {
668       printf("Failed to get current config file from DB: %s\n",cfg.GetRegionalFileName());
669       return false;
670     }
671
672     status = daqDA_DB_getFile(cfg.GetLocalMaskFileName(), cfg.GetLocalMaskFileName());
673     if (status) {
674       printf("Failed to get current config file from DB: %s\n",cfg.GetLocalMaskFileName());
675       return false;
676     }
677
678     status = daqDA_DB_getFile(cfg.GetLocalLutFileName(), cfg.GetLocalLutFileName());
679     if (status) {
680       printf("Failed to get current config file from DB: %s\n",cfg.GetLocalLutFileName());
681       return false;
682     }
683  
684     return true;
685 }
686
687 //_____________
688 void ReadMaskFiles(AliDAConfig& cfg)
689 {
690     /// read mask files
691
692     const Char_t* localFile    = cfg.GetLocalMaskFileName();
693     const Char_t* regionalFile = cfg.GetRegionalFileName();
694     const Char_t* globalFile   = cfg.GetGlobalFileName();
695
696     cfg.GetTriggerIO()->ReadConfig(localFile, regionalFile, globalFile, cfg.GetLocalMasks(), cfg.GetRegionalMasks(), cfg.GetGlobalMasks());                     
697 }
698
699 //______________________________________________________________
700 UInt_t GetFetMode(const AliDAConfig & cfg)
701 {
702   /// FET mode = 3 to run algorithm for dead global inputs
703   /// 0x3 prepulse
704   /// 0x0 internal
705
706   return cfg.GetGlobalMasks()->GetFetRegister(3);
707
708 }
709
710 //______________________________________________________________
711 void StoreGlobalInput(AliDAConfig& cfg, const UInt_t * const globalInput) 
712 {
713   /// accumulate and build statistics of global input values
714   
715   for (Int_t ii = 0; ii < cfg.GetGlobalInputs(); ii++) {
716     for (Int_t ib = 0; ib < cfg.GetGlobalInputLength(); ib++) {
717       // lsb -> msb
718       if (cfg.GetAlgoNoisyInput())
719         cfg.AddAccGlobalInputN(ii,ib,((globalInput[ii] >> ib) & 0x1));
720       if (cfg.GetAlgoDeadcInput())
721         cfg.AddAccGlobalInputD(ii,ib,((globalInput[ii] >> ib) & 0x1));
722     }
723   }
724
725 }
726
727 //______________________________________________________________
728 void UpdateGlobalMasks(AliDAConfig& cfg) 
729 {
730   /// update the global masks
731   
732 #ifdef OFFLINE
733   gSystem->Setenv("DAQDALIB_PATH", "$DATE_SITE/db");
734 #endif
735
736   Float_t rateN = 0.0, rateD = 0.0;
737   UInt_t gmask[4], omask;
738   Bool_t noise, deadc, withEvN, withEvD, updated = false;
739
740   for (Int_t ii = 0; ii < cfg.GetGlobalInputs(); ii++) {
741     gmask[ii] = 0;
742
743     for (Int_t ib = 0; ib < cfg.GetGlobalInputLength(); ib++) {
744       // lsb -> msb
745       noise = false;
746       deadc = false;
747       withEvN = false;
748       withEvD = false;
749       if (cfg.GetEventsN() > cfg.GetMinEvents()) {
750         rateN = (Float_t)cfg.GetAccGlobalInputN(ii,ib)/(Float_t)cfg.GetEventsN();
751         noise = (rateN > cfg.GetThrN());        
752         withEvN = true;
753       }
754       if (cfg.GetEventsD() > cfg.GetMinEvents()) {
755         rateD = (Float_t)cfg.GetAccGlobalInputD(ii,ib)/(Float_t)cfg.GetEventsD();
756         deadc = (rateD < cfg.GetThrD());
757         withEvD = true;
758       }
759       if (!withEvN && !withEvD) {
760         // - copy the bit from the old mask
761         gmask[ii] |= ((cfg.GetGlobalMasks()->GetGlobalMask(ii) >> ib) & 0x1) << ib;
762         if (cfg.GetPrintLevel()) 
763           printf("Mask not changed (just copy the old values)\n");
764       }
765       if (!withEvN && withEvD) {
766         if (!deadc) {
767           // - create a new mask, set the bit to 1
768           //   not allowed!
769           //gmask[ii] |= 0x1 << ib;
770           // - copy the bit from the old mask
771           gmask[ii] |= ((cfg.GetGlobalMasks()->GetGlobalMask(ii) >> ib) & 0x1) << ib;
772         } else {
773           // - create a new mask, set the bit to 0
774           gmask[ii] |= 0x0 << ib;
775           if (cfg.GetPrintLevel()) 
776             printf("Found dead  channel %1d:%02d (%4.2f) \n",ii,ib,rateD);
777         }
778       }
779       if (withEvN && !withEvD) {
780         if (!noise) {
781           // - create a new mask, set the bit to 1
782           //   not allowed!
783           //gmask[ii] |= 0x1 << ib;
784           // - copy the bit from the old mask
785           gmask[ii] |= ((cfg.GetGlobalMasks()->GetGlobalMask(ii) >> ib) & 0x1) << ib;
786         } else {
787           // - create a new mask, set the bit to 0
788           gmask[ii] |= 0x0 << ib;
789           if (cfg.GetPrintLevel()) 
790             printf("Found noisy channel %1d:%02d (%4.2f) \n",ii,ib,rateN);
791         }
792       }
793       if (withEvN && withEvD) {
794         if (!noise && !deadc) {
795           // - create a new mask, set the bit to 1
796           //   not allowed!
797           //gmask[ii] |= 0x1 << ib;
798           // - copy the bit from the old mask
799           gmask[ii] |= ((cfg.GetGlobalMasks()->GetGlobalMask(ii) >> ib) & 0x1) << ib;
800         } else {
801           // - create a new mask, set the bit to 0
802           gmask[ii] |= 0x0 << ib;
803           if (cfg.GetPrintLevel()) {
804             if (noise)
805               printf("Found noisy channel %1d:%02d (%4.2f) \n",ii,ib,rateN);
806             if (deadc)
807               printf("Found dead  channel %1d:%02d (%4.2f) \n",ii,ib,rateD);
808           }
809         }
810       }
811     }
812   }
813
814   // check if at least one mask value has been changed from previous version
815   for (Int_t ii = 0; ii < cfg.GetGlobalInputs(); ii++) {
816     printf("Global mask [%1d] %08x \n",ii,gmask[ii]);
817     omask = cfg.GetGlobalMasks()->GetGlobalMask(ii);
818     if (gmask[ii] != omask) {
819       updated = true;
820       cfg.GetGlobalMasks()->SetGlobalMask(ii,gmask[ii]);
821     }
822   }
823
824   Int_t status = 0;
825   if (updated) {
826     
827     // update version
828     cfg.IncGlobalFileVersion();
829     
830     // don't change the file version ("-x.dat")
831     
832     cfg.GetTriggerIO()->WriteGlobalConfig(cfg.GetGlobalFileName(),cfg.GetGlobalMasks());
833     
834     // write last current file
835     WriteLastCurrentFile(cfg,cfg.GetCurrentFileName());
836
837     status = daqDA_DB_storeFile(cfg.GetGlobalFileName(), cfg.GetGlobalFileName());
838     if (status) {
839       printf("Failed to export file to DB: %s\n",cfg.GetGlobalFileName());
840       return;
841     }
842     
843     status = daqDA_DB_storeFile(cfg.GetCurrentFileName(), cfg.GetCurrentFileName());
844     if (status) {
845       printf("Failed to export file to DB: %s\n",cfg.GetCurrentFileName());
846       return;
847     }
848
849   }
850   
851 }
852
853 //*************************************************************//
854 int main(Int_t argc, Char_t **argv) 
855 {
856     /// main routine
857   
858     // needed for streamer application
859     gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo", "*", "TStreamerInfo", "RIO", "TStreamerInfo()"); 
860
861     /* check that we got some arguments = list of files */
862     if (argc<2) {
863       printf("Wrong number of arguments\n");
864       return -1;
865     }
866
867     AliDAConfig cfg;
868
869     Char_t inputFile[256] = "";
870     inputFile[0] = 0;
871     if (argc > 1)
872       if (argv[1] != NULL)
873         strncpy(inputFile, argv[1], 256);
874       else {
875         printf("MUONTRGda : No input File !\n");
876         return -1;
877       }
878
879     // decoding the events
880   
881     Int_t status;
882     Int_t nDateEvents = 0;
883
884     void* event;
885
886     // containers
887     // old decoder
888     AliMUONDDLTrigger*       ddlTrigger  = 0x0;
889     AliMUONDarcHeader*       darcHeader  = 0x0;
890     // new (fast) decoder
891     const AliMUONRawStreamTriggerHP::AliHeader* darcHeaderHP = 0x0;
892
893     TStopwatch timers;
894
895     timers.Start(kTRUE); 
896
897     // comment out, since we do not retrieve files from database
898     if (!ImportFiles(cfg)) {
899       printf("Import from DB failed\n");
900       printf("For local test set DAQDA_TEST_DIR to the local directory where the Mtg files are located \n");
901       return -1;
902     }
903     
904     ReadMaskFiles(cfg);
905
906 #ifdef OFFLINE
907     // the run number extracted from the file name
908     TString tmp(inputFile);
909     Int_t pos = tmp.First("daq");
910     tmp = tmp(pos+3,5);
911     gSystem->Setenv("DATE_RUN_NUMBER",tmp.Data());
912     gSystem->Exec("echo \"DATE_RUN_NUMBER = \" $DATE_RUN_NUMBER");
913 #endif
914
915     if(!ExportFiles(cfg)) {
916       printf("ExportFiles failed\n");
917       return -1;
918     }
919
920     // FET is triggered by CTP
921     Bool_t modeFET3 = kTRUE;
922     if (GetFetMode(cfg) != 3) {
923       printf("FET is not in mode 3. Only PHYSICS events will be analysed (noisy channels)\n");
924       modeFET3 = kFALSE;
925     }
926
927     // All 5 global cards are controlled by the Mts proxy
928     if (cfg.GetGlobalMasks()->GetGlobalCrateEnable() != 0x1F) {
929       printf("The MTS proxy does not control all global cards\n");
930       return -1;
931     }
932
933     // The global cards are ON (active on the global inputs)
934     if (!cfg.GetGlobalMasks()->GetMasksOn()) {
935       printf("Global masks are not ON\n");
936       return -1;
937     }
938   
939     // make sure to catch the "rare" calib events (1 every 50s in physics)
940     const Char_t* tableSOD[]  = {"ALL", "yes", "CAL", "all", NULL, NULL};
941     monitorDeclareTable(const_cast<char**>(tableSOD));
942
943     status = monitorSetDataSource(inputFile);
944     if (status) {
945       cerr << "ERROR : monitorSetDataSource status (hex) = " << hex << status
946            << " " << monitorDecodeError(status) << endl;
947       return -1;
948     }
949     status = monitorDeclareMp("MUON Trigger monitoring");
950     if (status) {
951       cerr << "ERROR : monitorDeclareMp status (hex) = " << hex << status
952            << " " << monitorDecodeError(status) << endl;
953       return -1;
954     }
955
956     /* define wait event timeout - 1s max */
957     monitorSetNowait();
958     monitorSetNoWaitNetworkTimeout(1000);
959
960     cout << "MUONTRGda : Reading data from file " << inputFile <<endl;
961
962     UInt_t *globalInput = new UInt_t[4];
963     Bool_t doUpdate = false;
964     Int_t runNumber = 0;
965     Int_t nEvents = 0;
966
967     while(1) 
968     {
969       if (nEvents >= cfg.GetMaxEvents()) break;
970       if (cfg.GetPrintLevel()) {
971         if (nEvents && nEvents % 1000 == 0)     
972           cout<<"Cumulated events " << nEvents << endl;
973       }
974       // check shutdown condition 
975       if (daqDA_checkShutdown()) 
976           break;
977
978       // Skip Events if needed
979       while (cfg.GetSkipEvents()) {
980         status = monitorGetEventDynamic(&event);
981         cfg.DecSkipEvents();
982       }
983
984       // starts reading
985       status = monitorGetEventDynamic(&event);
986       if (status < 0)  {
987         cout << "MUONTRGda : EOF found" << endl;
988         break;
989       }
990
991       nDateEvents++;
992
993       // decoding rawdata headers
994       AliRawReader *rawReader = new AliRawReaderDate(event);
995  
996       Int_t eventType = rawReader->GetType();
997       runNumber = rawReader->GetRunNumber();
998     
999       // L1Swc1
1000       // CALIBRATION_EVENT 
1001       // SYSTEM_SOFTWARE_TRIGGER_EVENT
1002       // DETECTOR_SOFTWARE_TRIGGER_EVENT
1003       cfg.SetAlgoNoisyInput(false);
1004       cfg.SetAlgoDeadcInput(false);
1005       if (eventType == PHYSICS_EVENT) {
1006         cfg.SetAlgoNoisyInput(true);
1007         doUpdate = true;
1008         cfg.IncNoiseEvent();
1009       } else if (modeFET3 && eventType == CALIBRATION_EVENT) {
1010         cfg.SetAlgoDeadcInput(true);
1011         doUpdate = true;
1012         cfg.IncDeadcEvent();
1013       } else {
1014         continue;
1015       }
1016       
1017       nEvents++;
1018       if (cfg.GetPrintLevel() == 2) printf("\nEvent # %d\n",nEvents);
1019
1020       // decoding MUON payload
1021       AliMUONVRawStreamTrigger   *rawStream   = 0x0;
1022       if (cfg.UseFastDecoder()) {
1023         rawStream = new AliMUONRawStreamTriggerHP(rawReader);
1024       } else {
1025         rawStream = new AliMUONRawStreamTrigger(rawReader);
1026       }
1027
1028       // ... without warnings from the decoder !!!
1029       if (!cfg.WithWarnings())
1030         rawStream->DisableWarnings();
1031
1032       // loops over DDL 
1033       while((status = rawStream->NextDDL())) {
1034
1035         if (cfg.GetPrintLevel() == 2) printf("iDDL %d\n", rawStream->GetDDL());
1036
1037         if (rawStream->GetDDL() == 0) {
1038           if (cfg.UseFastDecoder()) {
1039             darcHeaderHP = static_cast<AliMUONRawStreamTriggerHP*>(rawStream)->GetHeaders();
1040             if (cfg.GetPrintLevel() == 2) printf("Global output (fast decoder) %x\n", (Int_t)darcHeaderHP->GetGlobalOutput());
1041             for (Int_t ig = 0; ig < cfg.GetGlobalInputs(); ig++) 
1042               globalInput[ig] = darcHeaderHP->GetGlobalInput(ig);
1043           } else {
1044             ddlTrigger = rawStream->GetDDLTrigger();
1045             darcHeader = ddlTrigger->GetDarcHeader();
1046             if (cfg.GetPrintLevel() == 2) printf("Global output %x\n", (Int_t)darcHeader->GetGlobalOutput());
1047             globalInput = darcHeader->GetGlobalInput();
1048           }
1049           StoreGlobalInput(cfg,globalInput);
1050         }
1051
1052       } // NextDDL
1053
1054       delete rawReader;
1055       delete rawStream;
1056
1057     } // while (1)
1058
1059     // update configuration files ifrequested event types were found
1060     if (doUpdate && cfg.GetDAFlag()) 
1061       UpdateGlobalMasks(cfg);
1062
1063     timers.Stop();
1064
1065     cout << "MUONTRGda: DA enable: " << cfg.GetDAFlag() << endl;
1066     cout << "MUONTRGda: Run number: " << runNumber << endl;
1067     cout << "MUONTRGda: Nb of DATE events: " << nDateEvents << endl;
1068     cout << "MUONTRGda: Nb of events used: " << nEvents << endl;
1069     cout << "MUONTRGda: Nb of events used (noise): " << cfg.GetEventsN() << endl;
1070     cout << "MUONTRGda: Nb of events used (deadc): " << cfg.GetEventsD() << endl;
1071     cout << "MUONTRGda: Minumum nr of events for rate calculation: " << cfg.GetMinEvents() << endl;
1072     cout << "MUONTRGda: Maximum nr of analyzed events: " << cfg.GetMaxEvents() << endl;
1073     cout << "MUONTRGda: Skip events from start: " << cfg.GetSkipEvents() << endl;
1074     cout << "MUONTRGda: Threshold for noisy inputs: " << 100*cfg.GetThrN() << "%" << endl;
1075     cout << "MUONTRGda: Threshold for dead inputs: " << 100*cfg.GetThrD() << "%" << endl;
1076     cout << "MUONTRGda: Print level: " << cfg.GetPrintLevel() << endl;
1077     cout << "MUONTRGda: Show decoder warnings: " << cfg.WithWarnings() << endl;
1078     cout << "MUONTRGda: Use the fast decoder: " << cfg.UseFastDecoder() << endl;
1079
1080     printf("MUONTRGda: Execution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
1081
1082
1083     return status;
1084
1085 }
1086