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