source: BAORadio/AmasNancay/trunk/analyse.cc @ 602

Last change on this file since 602 was 598, checked in by campagne, 13 years ago

cleaning of the set of scripts, and improve th submit2ge options, and improve analysis (jec)

File size: 77.6 KB
Line 
1//
2// Analyse of Amas@Nancay runs by J.E Campagne (LAL)
3// Version 0: 1/6/2011
4//-----------------------------
5
6// Utilisation de SOPHYA pour faciliter les tests ...
7#include "sopnamsp.h"
8#include "machdefs.h"
9
10#include <stdlib.h>
11#include <dirent.h>
12#include <matharr.h>
13
14// include standard c/c++
15#include <iostream>
16#include <fstream>
17#include <string>
18#include <vector>
19#include <map>
20#include <functional>
21#include <algorithm>
22#include <numeric>
23#include <list>
24#include <exception>
25
26// include sophya mesure ressource CPU/memoire ...
27#include "resusage.h"
28#include "ctimer.h"
29#include "timing.h"
30#include "timestamp.h"
31#include "strutilxx.h"
32#include "ntuple.h"
33#include "fioarr.h"
34#include "tarrinit.h"
35#include "histinit.h"
36#include "fitsioserver.h"
37#include "fiosinit.h"
38#include "ppersist.h"
39
40
41//~/private/work/AmasNancay/Objs/analyse -act redMeanONOFF -source Abell85 -date 20110428 -sca sca151675.sum.trans -specdir meancycle -specname mspecmtx -sigmaname sigspecmtx -npaq 25000 -numcycle 1,11 -debug 100 > & redMean.log
42
43
44
45const sa_size_t NUMBER_OF_CHANNELS = 2;
46const sa_size_t  NUMBER_OF_FREQ = 8192;
47const r_4    LOWER_FREQUENCY = 1250.0; //MHz
48const r_4    TOTAL_BANDWIDTH = 250.0; //MHz
49
50//decalration of non class members functions
51extern "C" {
52  int Usage(bool);
53}
54
55
56//----------------------------------------------------------
57//Utility fonctions
58class median_of_empty_list_exception:public std::exception{
59    virtual const char* what() const throw() {
60    return "Attempt to take the median of an empty list of numbers.  "
61      "The median of an empty list is undefined.";
62  }
63};
64template<class RandAccessIter>
65double median(RandAccessIter begin, RandAccessIter end) 
66  throw(median_of_empty_list_exception){
67  if(begin == end){ throw median_of_empty_list_exception(); }
68  std::size_t size = end - begin;
69  std::size_t middleIdx = size/2;
70  RandAccessIter target = begin + middleIdx;
71  std::nth_element(begin, target, end);
72   
73  if(size % 2 != 0){ //Odd number of elements
74    return *target;
75  }else{            //Even number of elements
76    double a = *target;
77    RandAccessIter targetNeighbor= target-1;
78    std::nth_element(begin, targetNeighbor, end);
79    return (a+*targetNeighbor)/2.0;
80  }
81}
82
83//-------------
84
85// Function for deleting pointers in map.
86template<class A, class B>
87struct DeleteMapFntor
88{
89    // Overloaded () operator.
90    // This will be called by for_each() function.
91    bool operator()(pair<A,B> x) const
92    {
93        // Assuming the second item of map is to be
94        // deleted. Change as you wish.
95        delete x.second;
96        return true;
97    }
98};
99//-----
100bool compare(const pair<string,r_4>& i, const pair<string,r_4>& j) {
101  return i.second < j.second;
102}
103//-----
104sa_size_t round_sa(r_4 r) {
105  return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
106}
107//-----
108string StringToLower(string strToConvert){
109  //change each element of the string to lower case
110  for(unsigned int i=0;i<strToConvert.length();i++) {
111    strToConvert[i] = tolower(strToConvert[i]);
112  }
113  return strToConvert;//return the converted string
114}
115//-----
116//JEC 22/9/11 comparison, not case sensitive to sort File liste START
117bool stringCompare( const string &left, const string &right ){
118   if( left.size() < right.size() )
119      return true;
120   ////////////POSSIBLY A BUG return false;
121   for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
122      if( tolower( *lit ) < tolower( *rit ) )
123         return true;
124      else if( tolower( *lit ) > tolower( *rit ) )
125         return false;
126   return false; ///////TO BE FIXED
127}//JEC 22/9/11 comparison, not case sensitive to sort File liste END
128//-----
129list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
130  list<string> theList;
131
132
133  DIR *dip;
134  struct dirent *dit;
135  string msg;  string fileName;
136  string fullFileName;
137  size_t found;
138
139  if ((dip=opendir(dir.c_str())) == NULL ) {
140    msg = "opendir failed on directory "+dir;
141    throw msg;
142  }
143  while ( (dit = readdir(dip)) != NULL ) {
144    fileName = dit->d_name;
145    found=fileName.find(filePettern);
146    if (found != string::npos) {
147      fullFileName = dir + "/";
148      fullFileName += fileName;
149      theList.push_back(fullFileName);
150    }
151  }//eo while
152  if (closedir(dip) == -1) {
153    msg = "closedir failed on directory "+dir;
154    throw msg;
155  }
156 
157  //JEC 22/9/11 START
158  theList.sort(stringCompare);
159  //JEC 22/9/11 END
160
161  return theList;
162}
163
164
165//Declaration of local classes
166//----------------------------------------------
167//Process Interface
168class IProcess {
169public:
170  IProcess() {}
171  virtual ~IProcess() {}
172  virtual int processCmd() throw(string) =0;
173};
174//------------
175//Common Process
176class ProcessBase : public IProcess {
177public:
178  ProcessBase();
179  virtual ~ProcessBase();
180  void SetInputPath(const string& inputPath) {inputPath_ = inputPath;}
181  void SetOutputPath(const string& outputPath) {outputPath_ = outputPath;}
182  void SetSourceName(const string& sourceName) {sourceName_ = sourceName;}
183  void SetDateOfRun(const string& dateOfRun) {dateOfRun_ = dateOfRun;}
184  void SetSpectraDirectory(const string& spectraDirectory) {spectraDirectory_ = spectraDirectory;}
185  void SetTypeOfFile(const string& typeOfFile) { typeOfFile_ = typeOfFile; }
186  void SetNumCycle(const string& numcycle) {numcycle_ = numcycle; }
187  void SetScaFileName(const string& scaFileName) { scaFileName_ =scaFileName; }
188
189  void SetDebugLevel(const string& debuglev) {
190    debuglev_ = atoi(debuglev.c_str());
191  }
192
193  virtual int processCmd() throw(string);
194 
195protected:
196  string inputPath_;
197  string outputPath_;
198  string sourceName_;
199  string dateOfRun_;
200  string spectraDirectory_;
201  string typeOfFile_;
202
203  string numcycle_; //cycle numbers format="first,last"
204  sa_size_t ifirstCycle_;
205  sa_size_t ilastCycle_;
206
207
208  uint_4 debuglev_; 
209  string scaFileName_;
210  NTuple* scaTuple_;
211  map<sa_size_t,sa_size_t> idCycleInTuple_;
212};
213ProcessBase::ProcessBase() {
214  scaTuple_ = 0;
215}
216ProcessBase::~ProcessBase() {
217  if (scaTuple_) delete scaTuple_;
218  scaTuple_ = 0;
219}
220//------------
221//Process ON/OFF data
222//------------
223class ProcessONOFFData : public ProcessBase {
224protected:
225  string  freqBAOCalibration_;//string MHz
226public:
227  ProcessONOFFData(){}
228  virtual ~ProcessONOFFData(){}
229
230  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
231    freqBAOCalibration_ = freqBAOCalibration; 
232  }
233 
234  virtual int processCmd() throw(string);
235};
236
237
238//------------
239//Process ON/OFF Raw data
240//------------
241class ProcessONOFFRawData : public ProcessBase {
242
243public:
244  ProcessONOFFRawData(){}
245  virtual ~ProcessONOFFRawData(){}
246 
247  virtual int processCmd() throw(string);
248};
249
250//------------
251//Process ON/OFF data treated with the mspec (specmfib) => no filtering at all: paquets & freq.
252//------------
253class ProcessONOFFMeanData : public ProcessBase {
254
255public:
256  ProcessONOFFMeanData(){}
257  virtual ~ProcessONOFFMeanData(){}
258 
259  virtual int processCmd() throw(string);
260};
261
262//------------
263//Process ON/OFF data treated with the mspec (specmfib) => no filtering at all: paquets & freq.
264//------------
265class ProcessONOFFReducedMeanData : public ProcessBase {
266protected:
267  string sigma2FileName_; //name of the file which contains the sigma2
268  string nPaqPerWin_; // number of paquets used for mean and sigma2 computations 'cf. proc_specmfib'
269  r_4 valNPaqPerWin_; // value
270
271public:
272  ProcessONOFFReducedMeanData(){}
273  virtual ~ProcessONOFFReducedMeanData(){}
274 
275  void SetSigma2FileName(const string& val) {sigma2FileName_ = val;}
276  void SetNPaqPerWin(const string& val) {
277    nPaqPerWin_    = val;
278    valNPaqPerWin_ = atof(val.c_str());
279  }
280
281  virtual int processCmd() throw(string);
282};
283
284
285//------------
286//Process ON/OFF data treated with the gain (specmfib) => filtering paquets only by default
287//------------
288class ProcessONOFFReducedMedianData : public ProcessBase {
289protected:
290  string nPaqPerWin_; // number of paquets used for mean and sigma2 computations 'cf. proc_specmfib'
291  r_4 valNPaqPerWin_; // value
292
293public:
294  ProcessONOFFReducedMedianData(){}
295  virtual ~ProcessONOFFReducedMedianData(){}
296 
297  void SetNPaqPerWin(const string& val) {
298    nPaqPerWin_    = val;
299    valNPaqPerWin_ = atof(val.c_str());
300  }
301
302  virtual int processCmd() throw(string);
303};
304
305
306//------------
307//Process Gain
308//------------
309class ProcessGain : public ProcessBase {
310protected:
311  string mode_; //mode of data taken for gain computation On || Off
312public:
313  ProcessGain(){}
314  virtual ~ProcessGain(){}
315
316  void SetMode(const string& mode) {mode_ = mode;}
317 
318  virtual int processCmd() throw(string);
319};
320//------------
321//Process Calibration
322//------------
323class ProcessCalibration : public ProcessBase {
324protected:
325  string option_; //option of calibration procedure
326  string  freqBAOCalibration_;//string MHz
327  r_4 valfreqBAOCalibration_; //value MHz
328  string bandWidthBAOCalibration_;//string MHz
329  r_4 valbandWidthBAOCalibration_;//value MHz
330 
331  sa_size_t lowerFreqBin_;
332  sa_size_t upperFreqBin_;
333
334public:
335  ProcessCalibration() {}
336  virtual ~ProcessCalibration(){}
337
338  void SetOption(const string& option) {option_ = option;}
339  void SetFreqBAOCalibration(const string& freqBAOCalibration) { 
340    freqBAOCalibration_ = freqBAOCalibration; 
341    valfreqBAOCalibration_ = atof(freqBAOCalibration_.c_str());
342  }
343  void SetBandWidthBAOCalibration(const string& bandWidthBAOCalibration) { 
344    bandWidthBAOCalibration_ = bandWidthBAOCalibration; 
345    valbandWidthBAOCalibration_ = atof(bandWidthBAOCalibration_.c_str());
346  }
347
348  void ComputeLowerUpperFreqBin();
349     
350  virtual int processCmd() throw(string);
351};
352void ProcessCalibration::ComputeLowerUpperFreqBin() {
353  sa_size_t c0 = round_sa(NUMBER_OF_FREQ*(valfreqBAOCalibration_-LOWER_FREQUENCY)/TOTAL_BANDWIDTH);
354  sa_size_t dc = round_sa(NUMBER_OF_FREQ*valbandWidthBAOCalibration_/TOTAL_BANDWIDTH);
355  lowerFreqBin_ = c0-dc/2;
356  upperFreqBin_ = c0+dc/2;
357}
358//----------------------------------------------------
359int main(int narg, char* arg[])
360{
361
362  //Init process types
363  map<string,IProcess*> process;
364  process["redMedianONOFF"] = new ProcessONOFFReducedMedianData(); //JEC  8/11/11
365  process["redMeanONOFF"] = new ProcessONOFFReducedMeanData(); //JEC  8/11/11
366  process["meanONOFF"] = new ProcessONOFFMeanData(); //JEC 27/10/11
367  process["rawOnOff"]  = new ProcessONOFFRawData(); 
368  process["dataOnOff"] = new ProcessONOFFData();
369  process["gain"]      = new ProcessGain();
370  process["calib"]     = new ProcessCalibration();
371
372  //Init Sophya related modules
373  //  SophyaInit();
374  TArrayInitiator _inia; //nneded for TArray persistancy
375  FitsIOServerInit(); //needed for input file
376
377  //message used in Exceptions
378  string msg;
379
380  //Return code
381  int rc = 0;
382
383  //Arguments managements
384  if ((narg>1)&&(strcmp(arg[1],"-h")==0))  return Usage(false);
385  if (narg<11) return Usage(true);
386
387  string action;
388  string inputPath = "."; 
389  string outputPath = "."; 
390  string sourceName;
391  string scaFile;
392  string dateOfRun;
393  string spectraDirectory;
394  string freqBAOCalib = "";
395  string bandWidthBAOCalib = "";
396  string debuglev = "0";
397  string mode = "";
398  string numcycle;
399  string calibrationOpt = ""; 
400  string nPaqPerWin = "";
401
402  string typeOfFile="medfiltmtx";
403  string secondTypeOfFile=""; //valid for ProcessONOFFReducedMeanData JEC 8/11/11
404
405  //  bool okarg=false;
406  int ka=1;
407  while (ka<(narg-1)) {
408    cout << "Debug arglist: <" << arg[ka] <<">" << endl;
409    if (strcmp(arg[ka],"-debug")==0) {
410      debuglev=arg[ka+1];
411      ka+=2;
412    }
413    else if (strcmp(arg[ka],"-act")==0) {
414      action=arg[ka+1];
415      ka+=2;
416    }
417    else if (strcmp(arg[ka],"-inPath")==0) {
418      inputPath=arg[ka+1];
419      ka+=2;
420    }
421    else if (strcmp(arg[ka],"-outPath")==0) {
422      outputPath=arg[ka+1];
423      ka+=2;
424    }
425    else if (strcmp(arg[ka],"-source")==0) {
426      sourceName=arg[ka+1];
427      ka+=2;
428    }
429    else if (strcmp(arg[ka],"-sca")==0) {
430      scaFile=arg[ka+1];
431      ka+=2;
432    }
433    else if (strcmp(arg[ka],"-date")==0) {
434      dateOfRun=arg[ka+1];
435      ka+=2;
436    }
437    else if (strcmp(arg[ka],"-specdir")==0) {
438      spectraDirectory=arg[ka+1];
439      ka+=2;
440    }
441    else if (strcmp(arg[ka],"-specname")==0) {
442      typeOfFile=arg[ka+1];
443      ka+=2;
444    }   
445    else if (strcmp(arg[ka],"-sigmaname")==0) {
446      secondTypeOfFile=arg[ka+1];
447      ka+=2;
448    }   
449    else if (strcmp(arg[ka],"-npaq")==0) {
450      nPaqPerWin=arg[ka+1];
451      ka+=2;
452    } 
453    else if (strcmp(arg[ka],"-freqBAOCalib")==0) {
454      freqBAOCalib = arg[ka+1];
455      ka+=2;
456    }
457    else if (strcmp(arg[ka],"-bwBAOCalib")==0) {
458      bandWidthBAOCalib = arg[ka+1];
459      ka+=2;
460    } 
461    else if (strcmp(arg[ka],"-mode")==0) {
462      mode =arg[ka+1];
463      ka+=2; 
464    }
465    else if (strcmp(arg[ka],"-numcycle")==0) {
466      numcycle =arg[ka+1];
467      ka+=2; 
468    }
469    else if (strcmp(arg[ka],"-calibopt")==0) {
470      calibrationOpt =arg[ka+1];
471      ka+=2; 
472    }
473    else ka++;
474  }//eo while
475
476
477  //JEC 21/9/11 Give the input parameters START
478  cout << "Dump Iiitial parameters ............" << endl;
479  cout << " action = " << action << "\n"
480       << " inputPath = " << inputPath << "\n" 
481       << " outputPath = " << outputPath << "\n"
482       << " sourceName = " << sourceName << "\n"
483       << " scaFile = " << scaFile << "\n"
484       << " dateOfRun = " << dateOfRun << "\n"
485       << " spectraDirectory = " << spectraDirectory << "\n"
486       << " spectraName = " << typeOfFile << "\n"
487       << " sigmaName = " << secondTypeOfFile << "\n"
488       << " nPaqPerWin = " << nPaqPerWin << "\n"
489       << " freqBAOCalib = " << freqBAOCalib  << "\n"
490       << " bandWidthBAOCalib = " << bandWidthBAOCalib << "\n"
491       << " debug = "  << debuglev  << "\n"
492       << " mode = " << mode << "\n"
493       << " numcycle = " << numcycle << "\n"
494       << " calibrationOpt = " << calibrationOpt << endl;
495  cout << "...................................." << endl;
496  //JEC 21/9/11 Give the input parameters END
497
498
499  try {
500    //verification of action
501    if(process.find(action) == process.end()) {
502      msg = "action ";
503      msg += action + " not valid... FATAL";
504      rc = 999;
505      throw msg;
506    }
507   
508
509    //
510    //Process initialisation...
511    //
512    try {
513      ProcessBase* procbase = dynamic_cast<ProcessBase*>(process[action]);
514      if (procbase == 0) {
515        msg= "action ";
516        msg += action + "Not a <process base> type...FATAL";
517        rc = 999;
518        throw msg;
519      }
520      procbase->SetInputPath(inputPath);
521      procbase->SetOutputPath(outputPath);
522
523      if ("" == sourceName) {
524        msg = "(FATAL) missingsourceName  for action " + action;
525        Usage(true);
526        throw msg;
527      }
528      procbase->SetSourceName(sourceName);
529
530      if ("" == dateOfRun) {
531        msg = "(FATAL) missing dateOfRun for action " + action;
532        Usage(true);
533        throw msg;
534      }
535      procbase->SetDateOfRun(dateOfRun);
536
537     
538      if ("" == spectraDirectory) {
539        msg = "(FATAL) missing spectraDirectory for action " + action;
540        Usage(true);
541        throw msg;
542      }
543      procbase->SetSpectraDirectory(spectraDirectory);
544
545      if ("" == scaFile) {
546        msg = "(FATAL) missing scaFile for action " + action;
547        Usage(true);
548        throw msg;
549      }
550      procbase->SetScaFileName(scaFile);
551
552      if ("" == numcycle) {
553        msg = "(FATAL) missing cycle number for action " + action;
554        Usage(true);
555        throw msg;
556      }
557      procbase->SetNumCycle(numcycle);
558
559
560      procbase->SetTypeOfFile(typeOfFile);
561
562      procbase->SetDebugLevel(debuglev);
563    }
564    catch(exception& e){
565      throw e.what();
566    }
567
568    //JEC 22/9/11 Make ON-OFF analysis WO any calibration START
569//     try {
570//       ProcessONOFFRawData* procRawdata = dynamic_cast<ProcessONOFFRawData*>(process[action]);
571//     }
572//     catch(exception& e){
573//       throw e.what();
574//     }
575    //JEC 22/9/11 Make ON-OFF analysis WO any calibration END
576
577    //JEC 8/11/11 Start
578    try {
579      ProcessONOFFReducedMeanData* procdata=dynamic_cast<ProcessONOFFReducedMeanData*>(process[action]);
580      if (procdata) {
581        if (secondTypeOfFile == ""){
582          msg = "(FATAL) missing file of the sigmas for action " + action;
583          Usage(true);
584          throw msg;     
585        }
586        if (nPaqPerWin == ""){
587          msg = "(FATAL) missing number of paquets per window for action " + action;
588          Usage(true);
589          throw msg;     
590        }
591        procdata->SetSigma2FileName(secondTypeOfFile);
592        procdata->SetNPaqPerWin(nPaqPerWin);
593      }
594    }
595    catch(exception& e){
596      throw e.what();
597    }
598    //JEC 8/11/11 End
599
600    //JEC 8/11/11 Start
601    try {
602      ProcessONOFFReducedMedianData* procdata=dynamic_cast<ProcessONOFFReducedMedianData*>(process[action]);
603      if (procdata) {
604        if (nPaqPerWin == ""){
605          msg = "(FATAL) missing number of paquets per window for action " + action;
606          Usage(true);
607          throw msg;     
608        }
609        procdata->SetNPaqPerWin(nPaqPerWin);
610      }
611    }
612    catch(exception& e){
613      throw e.what();
614    }
615    //JEC 8/11/11 End
616   
617
618
619    try {
620      ProcessONOFFData* procdata = dynamic_cast<ProcessONOFFData*>(process[action]);
621      if (procdata) {
622        if (freqBAOCalib == "") {
623          msg = "(FATAL) missing calibration BAO frequency for action " + action;
624          Usage(true);
625          throw msg;
626        }
627        procdata->SetFreqBAOCalibration(freqBAOCalib);
628      }
629    }
630    catch(exception& e){
631      throw e.what();
632    }
633   
634
635    try {
636      ProcessGain* procgain = dynamic_cast<ProcessGain*>(process[action]);
637      if(procgain) {
638        if (mode == "") {
639          msg = "(FATAL) missing mode-type for action " + action;
640          Usage(true);
641          throw msg;
642        }
643        procgain->SetMode(mode);
644      }
645    }
646    catch(exception& e){
647      throw e.what();
648    }
649
650    try {
651      ProcessCalibration* proccalib = dynamic_cast<ProcessCalibration*>(process[action]);
652      if(proccalib) {
653        if (calibrationOpt == "") {
654          msg = "(FATAL) missing calibration option";
655          Usage(true);
656          throw msg;
657        }
658        if (freqBAOCalib == "") {
659          msg = "(FATAL) missing calibration BAO frequency for action " + action;
660          Usage(true);
661          throw msg;
662        }
663        if (bandWidthBAOCalib == "") {
664          msg = "(FATAL) missing calibration BAO frequency band width for action " + action;
665          Usage(true);
666          throw msg;
667        }
668        proccalib->SetOption(calibrationOpt);
669        proccalib->SetFreqBAOCalibration(freqBAOCalib);
670        proccalib->SetBandWidthBAOCalibration(bandWidthBAOCalib);
671        proccalib->ComputeLowerUpperFreqBin();
672      }
673    }
674    catch(exception& e){
675      throw e.what();
676    }
677
678    //
679    //execute command
680    //
681    rc = process[action]->processCmd();
682
683  }
684  catch (std::exception& sex) {
685    cerr << "\n analyse.cc std::exception :"  << (string)typeid(sex).name() 
686         << "\n msg= " << sex.what() << endl;
687    rc = 78;
688  }
689  catch ( string str ) {
690    cerr << "analyse.cc Exception raised: " << str << endl;
691  }
692  catch (...) {
693    cerr << " analyse.cc catched unknown (...) exception  " << endl; 
694    rc = 79; 
695  } 
696
697 
698
699
700  cout << ">>>> analyse.cc ------- END ----------- RC=" << rc << endl;
701 
702  //Delete processes
703  for_each(process.begin(),process.end(), DeleteMapFntor<string,IProcess*>());
704
705  return rc;
706}
707
708//---------------------------------------------------
709int Usage(bool flag) {
710  cout << "Analyse.cc usage...." << endl;
711  cout << "analyse  -act <action_type>: redMeanONOFF, meanONOFF,dataOnOff, rawOnOff, gain, calib\n"
712       << "         -inPath <path for input files: default='.'>\n"
713       << "         -outPath <path for output files: default='.'>\n"
714       << "         -source <source name> \n" 
715       << "         -date <YYYYMMDD>\n"
716       << "         -sca <file name scaXYZ.sum.trans>\n"
717       << "         -specdir <generic directory name of spectra fits files>\n"
718       << "         -specname <generic name of spectra fits files>\n"
719       << "         -sigmaname <generic name of the sigma2 fits files>\n"
720       << "            valid for redMeanONOFF\n"
721       << "         -npaq <number of paquest per windows for mean&sigma2 computaion (brproc)>\n"
722       << "            valid for redMeanONOFF\n"
723       << "         -freqBAOCalib <freq in MHZ> freq. of calibration BAO\n"
724       << "            valid for act=dataOnOff\n"
725       << "         -bwBAOCalib <band width MHz> band width arround central freq. for calibration BAO\n"
726       << "            valid for act=calib\n"
727       << "         -mode <mode_type>:\n"
728       << "            valid for act=gain, mode_type: On, Off\n"
729       << "         -numcycle <number>,<number>:\n"
730       << "            valid for all actions"
731       << "         -calibopt <option>:\n"
732       << "            valid for act=calib: indiv OR mean (NOT USED)"
733       << "         -debug <number> [0=default]\n"
734       << "           1: normal print\n"
735       << "           2: save intermediate spectra\n"
736       << endl;
737  if (flag) {
738    cout << "use <path>/analyse -h for detailed instructions" << endl;
739    return 5;
740  }
741  return 1;
742}
743
744int ProcessBase::processCmd() throw(string) {
745  int rc =0;
746  string msg;
747  if(debuglev_>0)cout << "Process Base" << endl;
748  //------------------------
749  //Use the sca file informations
750  //------------------------
751  //  string scaFullPathName = "./";
752  //TOBE FIXED  scaFullPathName += sourceName_+"/"+dateOfRun_ + StringToLower(sourceName_)+"/";
753  string scaFullPathName = inputPath_ + "/" 
754    + sourceName_+ "/" +dateOfRun_ + StringToLower(sourceName_)+ "/" + scaFileName_;
755  char* scaTupleColumnName[9] = {"cycle","stcalOn","spcalOn","stOn","spOn","stcalOff","spcalOff","stOff","spOff"};
756  scaTuple_ = new NTuple(9,scaTupleColumnName);
757  int n = scaTuple_->FillFromASCIIFile(scaFullPathName);
758  if(n<0){ //Error
759    msg = "(FATAL) NTuple error loading "+ scaFullPathName;
760    rc = 999;
761    throw msg;
762  }
763 
764  if(debuglev_>1){
765    cout << "ProcessBase::processCmd: dump tuple in " << scaFullPathName << endl;
766    scaTuple_->Show(cout);
767  }
768 
769 
770  //Get the cycles (here consider consecutive cycles)   
771  //The SCA file cannot be used as the DAQ can miss some cycles...
772  //     r_8 firstCycle, lastCycle;
773  //     scaTuple_->GetMinMax("cycle",firstCycle,lastCycle);
774  //     ifirstCycle_ = (sa_size_t)firstCycle;
775  //     ilastCycle_  = (sa_size_t)lastCycle;
776  //Analyse the string given by -numcycle command line
777  int ai1=0,ai2=0;
778  sscanf(numcycle_.c_str(),"%d,%d",&ai1,&ai2);
779  ifirstCycle_ = (sa_size_t)ai1;
780  ilastCycle_  = (sa_size_t)ai2;
781 
782
783  //associate cycle number to index line in tuple
784  sa_size_t nLines = scaTuple_->NbLines();
785  for(sa_size_t iL=0; iL<nLines; ++iL){
786    idCycleInTuple_[(sa_size_t)scaTuple_->GetCell(iL,"cycle")]=iL;
787  }
788
789
790  return rc;
791}
792//----------------------------------------------
793//JEC 27/10/11
794//Process the files that are output of the specmfib -act = mspec (mean+sigma of signal files)
795//----------------------------------------------
796int ProcessONOFFMeanData::processCmd() throw(string) {
797  int rc = 0;
798  try {
799    rc = ProcessBase::processCmd();
800  } 
801  catch (string s) {
802    throw s;
803  }
804
805  if(debuglev_>0)cout << "Process Data" << endl;
806  vector<string> modeList;
807  modeList.push_back("On");
808  modeList.push_back("Off");
809  vector<string>::const_iterator iMode;
810 
811  uint_4 id; 
812  string tag;
813
814  map< pair<string, sa_size_t>, TMatrix<r_4> > meanCollect;
815  map< pair<string, sa_size_t>, TMatrix<r_4> > sigma2Collect;
816  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
817 
818  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
819    string mode = *iMode;
820    if(debuglev_>0)cout << "Process Mean-mspec Mode " << mode << endl;
821
822    //------------------------------------------
823    //Produce mean of the mean spectra per cycle
824    //------------------------------------------
825
826    string directoryName;
827    list<string> listOfSpecFiles;
828    list<string>::const_iterator iFile, iFileEnd;
829   
830       
831    //
832    //loop on cycles
833    //
834    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
835      directoryName = inputPath_ + "/" 
836        + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/"
837        + mode + "/";
838      stringstream sicycle;
839      sicycle << icycle;
840      directoryName += spectraDirectory_ + sicycle.str() + "/";
841
842      //read directory
843      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
844     
845
846      //compute mean of spectra created in a cycle
847      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
848      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
849      TMatrix<r_4> spectreSigma2(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
850      iFileEnd = listOfSpecFiles.end();
851      r_4 nSpectres  = 0;
852      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
853        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
854        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
855        aSpectrum >> spectre;
856        spectreMean   += spectre;
857        spectreSigma2 += spectre && spectre;
858        nSpectres++;
859      }// end of for files
860      if(nSpectres>0) {
861        spectreMean /= nSpectres;
862        spectreSigma2 /= nSpectres;
863        spectreSigma2 -= spectreMean && spectreMean;
864      }
865     
866      //save mean spectrum
867      meanCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
868      sigma2Collect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreSigma2,false) ));
869    }//end of for cycles
870  }//end loop on mode for raw preocess
871
872  if(debuglev_>1) {//save mean spectra on file
873    cout << "Save mean spectra" << endl;
874    string fileName;
875
876    fileName = "./dataMeanSigma2_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
877
878    POutPersist fos(fileName);
879    id=0;
880    iSpectreEnd = meanCollect.end();
881    for (iSpectre = meanCollect.begin();
882         iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
883      tag = "specMean";
884
885      tag += (iSpectre->first).first;
886      stringstream sid;
887      sid << (iSpectre->first).second;
888      tag += sid.str();
889      if(debuglev_>9) {
890        cout << "save tag<" << tag << ">" << endl;
891      }
892      fos << PPFNameTag(tag) << iSpectre->second;
893    }
894
895
896    id=0;
897    iSpectreEnd = sigma2Collect.end();
898    for (iSpectre = sigma2Collect.begin();
899         iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
900      tag = "specSigma2";
901
902      tag += (iSpectre->first).first;
903      stringstream sid;
904      sid << (iSpectre->first).second;
905      tag += sid.str();
906      if(debuglev_>9) {
907        cout << "save tag<" << tag << ">" << endl;
908      }
909      fos << PPFNameTag(tag) << iSpectre->second;
910    }
911
912  }//end of save fits
913 
914
915
916
917
918  cout << "OK ProcessONOFFMeanData finished" <<endl;
919  return rc;
920}
921//----------------------------------------------
922//----------------------------------------------
923//JEC 8/11/11
924//Process the files that are output of the specmfib -act = mspec (mean+sigma of signal files)
925//Compute the reduced variables r_i = (m_i - <m_i>)/(sig_i/sqrt(Npaq.))
926//----------------------------------------------
927int ProcessONOFFReducedMeanData::processCmd() throw(string) {
928  int rc = 0;
929  try {
930    rc = ProcessBase::processCmd();
931  } 
932  catch (string s) {
933    throw s;
934  }
935
936  if(debuglev_>0)cout << "Process Data" << endl;
937  vector<string> modeList;
938  modeList.push_back("On");
939  modeList.push_back("Off");
940  vector<string>::const_iterator iMode;
941 
942  uint_4 id; 
943  string tag;
944
945  map<string,  TMatrix<r_4> > meanSigmaRedRatioCollect;
946  map<string,  TMatrix<r_4> >::iterator iMeanSigmaRed, iMeanSigmaRedEnd;
947
948  map< pair<string, sa_size_t>, TMatrix<r_4> > reducedRatioCollect;
949  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iReduced, iReducedEnd;
950
951  map< sa_size_t, TMatrix<r_4> > meanCollect;
952  map< sa_size_t, TMatrix<r_4> >::iterator iMean, iMeanEnd;
953
954  map< sa_size_t, TMatrix<r_4> > sigmaCollect;
955  map< sa_size_t, TMatrix<r_4> >::iterator iSigma, iSigmaEnd;
956 
957  //
958  //loop on modes
959  //
960  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
961    string mode = *iMode;
962    if(debuglev_>0)cout << "Process Mean-mspec Mode " << mode << endl;
963
964
965    string directoryName;
966    list<string> listOfMeanFiles;
967    list<string> listOfSigma2Files;
968   
969    if (listOfMeanFiles.size() != listOfSigma2Files.size()) {
970      throw "ProcessONOFFReducedMeanData: FATAL: length (1) missmatch";
971    }
972   
973    list<string>::const_iterator iFile, iFileEnd;           
974    //Mean of all Means (one per Mode)
975    TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
976    uint_4 nMeans  = 0;
977    uint_4 nSigma2s  = 0;
978    //
979    //loop on cycles
980    //
981    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
982      directoryName = inputPath_ + "/" 
983        + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/"
984        + mode + "/";
985      stringstream sicycle;
986      sicycle << icycle;
987      directoryName += spectraDirectory_ + sicycle.str() + "/";
988
989      //read directory
990      listOfMeanFiles   = ListOfFileInDir(directoryName,typeOfFile_); //the means
991      listOfSigma2Files = ListOfFileInDir(directoryName,sigma2FileName_); //the sigma2s
992     
993      //get a mean spectra per file
994      iFileEnd = listOfMeanFiles.end();
995      for (iFile = listOfMeanFiles.begin(); iFile != iFileEnd; ++iFile) {
996//      if(debuglev_>10){
997//        cout << "read file <" << *iFile << ">:";
998//      }
999        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1000        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1001        aSpectrum >> spectre;
1002//      if(debuglev_>10){
1003//        cout << " mean of spectre = " << Mean(spectre) << endl;
1004//      }
1005        //update mean
1006        spectreMean   += spectre;
1007        //save spectrum
1008        meanCollect.insert( pair< sa_size_t, TMatrix<r_4> >(nMeans,TMatrix<r_4>(spectre,false) ));
1009
1010        nMeans++;
1011      }// end of for files
1012
1013      //get a sigma2 spectra per file and store the sigma
1014      iFileEnd = listOfSigma2Files.end();
1015      for (iFile = listOfSigma2Files.begin(); iFile != iFileEnd; ++iFile) {
1016//      if(debuglev_>10){
1017//        cout << "read file <" << *iFile << ">:";
1018//      }
1019        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1020        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1021        aSpectrum >> spectre;
1022//      if(debuglev_>10){
1023//        cout << " mean of spectre = " << Mean(Sqrt(spectre)) << endl;
1024//      }
1025        //save sigma = Sqrt(sigma2)
1026        sigmaCollect.insert( pair< sa_size_t, TMatrix<r_4> >(nSigma2s,TMatrix<r_4>(Sqrt(spectre),false) ));
1027
1028        nSigma2s++;
1029      }// end of for files
1030
1031    }//end of for cycles
1032
1033   
1034    //finalize mean of means
1035    if(nMeans>0) {
1036      spectreMean /= (r_4)nMeans;
1037//       if(debuglev_>10){
1038//      cout << "Mean mode [" << mode << "]: " << Mean(spectreMean) <<endl;
1039//       }
1040    } else {
1041      throw "ProcessONOFFReducedMeanData: FATAL: nMeans=0 !!!";
1042    }
1043
1044    if ( nMeans != nSigma2s ) {
1045      cout << "ProcessONOFFReducedMeanData: nMeans, nSigma2s " 
1046           <<  nMeans << " " <<nSigma2s 
1047           << endl;
1048      throw "ProcessONOFFReducedMeanData: FATAL: nMeans <> nSigma2s !!!";
1049    }
1050   
1051    //from here nMeans == nSigma2s
1052    iMeanEnd   = meanCollect.end();
1053    iSigmaEnd  = sigmaCollect.end();
1054    TMatrix<r_4> meanReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);   //implicit init to 0
1055    TMatrix<r_4> sigma2ReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1056    for (id=0,iMean = meanCollect.begin(), iSigma=sigmaCollect.begin();
1057         id<nMeans; 
1058         ++id, ++iMean, ++iSigma) {
1059      TMatrix<r_4> reducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1060//       if(debuglev_>10){
1061//      cout << "reduced Mean [" << mode << "," << id << "]: "
1062//           << Mean(iMean->second) <<  " "
1063//           << Mean(spectreMean) <<  " "
1064//           << Mean(iSigma->second) << " "
1065//           << sqrt(valNPaqPerWin_)
1066//           << endl;
1067//       }
1068     
1069      reducedRatio = iMean->second - spectreMean;
1070      reducedRatio.Div(iSigma->second);
1071      reducedRatio *= sqrt(valNPaqPerWin_);
1072
1073      meanReducedRatio   += reducedRatio;
1074      sigma2ReducedRatio += reducedRatio&&reducedRatio;
1075     
1076//       if(debuglev_>10){
1077//      cout << "reduced Mean [" << mode << "," << id << "]: " << Mean(reducedRatio) <<endl;
1078//       }
1079     
1080      reducedRatioCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,id),TMatrix<r_4>(reducedRatio,false) ));
1081    }
1082   
1083    if(debuglev_>10) cout << "number of Means used: " << nMeans << "( =" << nSigma2s << ")" << endl;
1084    meanReducedRatio   /= (r_4)nMeans;
1085    sigma2ReducedRatio /= (r_4)nMeans;
1086    sigma2ReducedRatio -= meanReducedRatio&&meanReducedRatio;
1087    TMatrix<r_4> sigmaReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1088    sigmaReducedRatio = Sqrt(sigma2ReducedRatio);
1089   
1090   
1091    meanSigmaRedRatioCollect.insert( pair< string,  TMatrix<r_4> >(mode+"Mean",TMatrix<r_4>(meanReducedRatio,false)) );
1092    meanSigmaRedRatioCollect.insert( pair< string,  TMatrix<r_4> >(mode+"Sigma",TMatrix<r_4>(sigmaReducedRatio,false)) );
1093
1094
1095  }//end loop on mode for raw preocess
1096
1097  cout << "Save reduced variable based on Means/Sigmas" << endl;
1098  string fileName;
1099 
1100  fileName = "./reducedMean_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1101 
1102  POutPersist fos(fileName);
1103  id=0;
1104  iReducedEnd = reducedRatioCollect.end();
1105  for (iReduced = reducedRatioCollect.begin();
1106       iReduced != iReducedEnd ; ++iReduced, ++id) {
1107    tag = "redMean";
1108   
1109    tag += (iReduced->first).first;
1110    stringstream sid;
1111    sid << (iReduced->first).second;
1112    tag += sid.str();
1113    if(debuglev_>9) {
1114      cout << "save tag<" << tag << ">" << endl;
1115    }
1116    fos << PPFNameTag(tag) << iReduced->second;
1117  }
1118 
1119  iMeanSigmaRedEnd = meanSigmaRedRatioCollect.end();
1120  for (iMeanSigmaRed = meanSigmaRedRatioCollect.begin();
1121       iMeanSigmaRed != iMeanSigmaRedEnd; 
1122       ++iMeanSigmaRed){
1123    tag = "redMean";
1124    tag += iMeanSigmaRed->first;
1125    if(debuglev_>9) {
1126      cout << "save tag<" << tag << ">" << endl;
1127    }
1128    fos << PPFNameTag(tag) << iMeanSigmaRed->second;
1129  }
1130 
1131  cout << "OK ProcessONOFFReducedMeanData finished" <<endl;
1132  return rc;
1133}
1134//----------------------------------------------
1135
1136//----------------------------------------------
1137//JEC 8/11/11
1138//Process the files that are output of the specmfib -act = gain (median of signal files)
1139//Compute the reduced variables r_i = (med_i - <med_i>)/(med_i/(Ln(2)*sqrt(N))
1140//----------------------------------------------
1141int ProcessONOFFReducedMedianData::processCmd() throw(string) {
1142  int rc = 0;
1143  try {
1144    rc = ProcessBase::processCmd();
1145  } 
1146  catch (string s) {
1147    throw s;
1148  }
1149
1150  if(debuglev_>0)cout << "Process Data" << endl;
1151  vector<string> modeList;
1152  modeList.push_back("On");
1153  modeList.push_back("Off");
1154  vector<string>::const_iterator iMode;
1155 
1156  uint_4 id; 
1157  string tag;
1158
1159  map<string,  TMatrix<r_4> > meanSigmaRedRatioCollect;
1160  map<string,  TMatrix<r_4> >::iterator iMeanSigmaRed, iMeanSigmaRedEnd;
1161
1162  map< pair<string, sa_size_t>, TMatrix<r_4> > reducedRatioCollect;
1163  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iReduced, iReducedEnd;
1164
1165  map< sa_size_t, TMatrix<r_4> > medianCollect;
1166  map< sa_size_t, TMatrix<r_4> >::iterator iMedian, iMedianEnd;
1167
1168  map< sa_size_t, TMatrix<r_4> > sigmaCollect;
1169  map< sa_size_t, TMatrix<r_4> >::iterator iSigma, iSigmaEnd;
1170 
1171  //
1172  //loop on modes
1173  //
1174  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
1175    string mode = *iMode;
1176    if(debuglev_>0)cout << "Process Mean-mspec Mode " << mode << endl;
1177
1178
1179    string directoryName;
1180    list<string> listOfMedianFiles;
1181   
1182    list<string>::const_iterator iFile, iFileEnd;           
1183    //Mean of all Medians (one per Mode)
1184    TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1185    uint_4 nMedians  = 0;
1186
1187    //
1188    //loop on cycles
1189    //
1190    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1191      directoryName = inputPath_ + "/" 
1192        + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/"
1193        + mode + "/";
1194      stringstream sicycle;
1195      sicycle << icycle;
1196      directoryName += spectraDirectory_ + sicycle.str() + "/";
1197
1198      //read directory
1199      listOfMedianFiles   = ListOfFileInDir(directoryName,typeOfFile_); //the means
1200     
1201      //get a mean spectra per file
1202      iFileEnd = listOfMedianFiles.end();
1203      for (iFile = listOfMedianFiles.begin(); iFile != iFileEnd; ++iFile) {
1204//      if(debuglev_>10){
1205//        cout << "read file <" << *iFile << ">:";
1206//      }
1207        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1208        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1209        aSpectrum >> spectre;
1210//      if(debuglev_>10){
1211//        cout << " mean of spectre = " << Mean(spectre) << endl;
1212//      }
1213        //update mean
1214        spectreMean   += spectre;
1215        //save spectrum
1216        medianCollect.insert( pair< sa_size_t, TMatrix<r_4> >(nMedians,TMatrix<r_4>(spectre,false) ));
1217        sigmaCollect.insert( pair< sa_size_t, TMatrix<r_4> >(nMedians,TMatrix<r_4>(spectre,false) ));
1218
1219        nMedians++;
1220      }// end of for files
1221
1222    }//end of for cycles
1223
1224   
1225    //finalize mean of means
1226    if(nMedians>0) {
1227      spectreMean /= (r_4)nMedians;
1228//       if(debuglev_>10){
1229//      cout << "Mean mode [" << mode << "]: " << Mean(spectreMean) <<endl;
1230//       }
1231    } else {
1232      throw "ProcessONOFFReducedMeanData: FATAL: nMedians=0 !!!";
1233    }
1234
1235    iMedianEnd   = medianCollect.end();
1236    iSigmaEnd  = sigmaCollect.end();
1237    TMatrix<r_4> meanReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);   //implicit init to 0
1238    TMatrix<r_4> sigma2ReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1239    for (id=0,iMedian = medianCollect.begin(), iSigma=sigmaCollect.begin();
1240         id<nMedians; 
1241         ++id, ++iMedian, ++iSigma) {
1242      TMatrix<r_4> reducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1243//       if(debuglev_>10){
1244//      cout << "reduced Mean [" << mode << "," << id << "]: "
1245//           << Mean(iMedian->second) <<  " "
1246//           << Mean(spectreMean) <<  " "
1247//           << Mean(iSigma->second) << " "
1248//           << sqrt(valNPaqPerWin_)
1249//           << endl;
1250//       }
1251     
1252      reducedRatio = iMedian->second - spectreMean;
1253      reducedRatio.Div(iSigma->second);
1254      reducedRatio *= sqrt(valNPaqPerWin_)*log(2.0);
1255
1256      meanReducedRatio   += reducedRatio;
1257      sigma2ReducedRatio += reducedRatio&&reducedRatio;
1258     
1259//       if(debuglev_>10){
1260//      cout << "reduced Mean [" << mode << "," << id << "]: " << Mean(reducedRatio) <<endl;
1261//       }
1262     
1263      reducedRatioCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,id),TMatrix<r_4>(reducedRatio,false) ));
1264    }
1265   
1266    if(debuglev_>10) cout << "number of Means used: " << nMedians << endl;
1267    meanReducedRatio   /= (r_4)nMedians;
1268    sigma2ReducedRatio /= (r_4)nMedians;
1269    sigma2ReducedRatio -= meanReducedRatio&&meanReducedRatio;
1270    TMatrix<r_4> sigmaReducedRatio(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1271    sigmaReducedRatio = Sqrt(sigma2ReducedRatio);
1272   
1273   
1274    meanSigmaRedRatioCollect.insert( pair< string,  TMatrix<r_4> >(mode+"Mean",TMatrix<r_4>(meanReducedRatio,false)) );
1275    meanSigmaRedRatioCollect.insert( pair< string,  TMatrix<r_4> >(mode+"Sigma",TMatrix<r_4>(sigmaReducedRatio,false)) );
1276
1277
1278  }//end loop on mode for raw preocess
1279
1280  cout << "Save reduced variable based on Means/Sigmas" << endl;
1281  string fileName;
1282 
1283  fileName = "./reducedMedian_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1284 
1285  POutPersist fos(fileName);
1286  id=0;
1287  iReducedEnd = reducedRatioCollect.end();
1288  for (iReduced = reducedRatioCollect.begin();
1289       iReduced != iReducedEnd ; ++iReduced, ++id) {
1290    tag = "redMedian";
1291   
1292    tag += (iReduced->first).first;
1293    stringstream sid;
1294    sid << (iReduced->first).second;
1295    tag += sid.str();
1296    if(debuglev_>9) {
1297      cout << "save tag<" << tag << ">" << endl;
1298    }
1299    fos << PPFNameTag(tag) << iReduced->second;
1300  }
1301 
1302  iMeanSigmaRedEnd = meanSigmaRedRatioCollect.end();
1303  for (iMeanSigmaRed = meanSigmaRedRatioCollect.begin();
1304       iMeanSigmaRed != iMeanSigmaRedEnd; 
1305       ++iMeanSigmaRed){
1306    tag = "redMedian";
1307    tag += iMeanSigmaRed->first;
1308    if(debuglev_>9) {
1309      cout << "save tag<" << tag << ">" << endl;
1310    }
1311    fos << PPFNameTag(tag) << iMeanSigmaRed->second;
1312  }
1313 
1314  cout << "OK ProcessONOFFReducedMedianData finished" <<endl;
1315  return rc;
1316}
1317//----------------------------------------------
1318
1319//JEC 22/9/11 Make ON-OFF analysis WO any calibration START
1320//----------------------------------------------
1321int ProcessONOFFRawData::processCmd() throw(string) {
1322  int rc = 0;
1323  try {
1324    rc = ProcessBase::processCmd();
1325  } 
1326  catch (string s) {
1327    throw s;
1328  }
1329  if(debuglev_>0)cout << "Process Raw Data ON OFF" << endl;
1330  vector<string> modeList;
1331  modeList.push_back("On");
1332  modeList.push_back("Off");
1333  vector<string>::const_iterator iMode;
1334 
1335  uint_4 id; 
1336  string tag;
1337
1338  //
1339  //Process to get sucessively
1340  //Raw Spectra,
1341  //The processes are separated to allow intermediate save of results
1342
1343  map< pair<string, sa_size_t>, TMatrix<r_4> > spectreCollect;
1344  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
1345 
1346  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
1347    string mode = *iMode;
1348    if(debuglev_>0)cout << "Process RAW Mode " << mode << endl;
1349
1350    //------------------------------------------
1351    //Produce Raw spectra per cycle
1352    //------------------------------------------
1353
1354    string directoryName;
1355    list<string> listOfSpecFiles;
1356    list<string>::const_iterator iFile, iFileEnd;
1357   
1358       
1359    //
1360    //loop on cycles
1361    //
1362    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1363      directoryName = inputPath_ + "/" 
1364        + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/"
1365        + mode + "/";
1366      stringstream sicycle;
1367      sicycle << icycle;
1368      directoryName += spectraDirectory_ + sicycle.str() + "/";
1369
1370      //read directory
1371      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
1372     
1373
1374      //compute mean of spectra created in a cycle
1375      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
1376      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1377      iFileEnd = listOfSpecFiles.end();
1378      r_4 nSpectres  = 0;
1379      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
1380        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1381        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1382        aSpectrum >> spectre;
1383        spectreMean += spectre;
1384        nSpectres++;
1385      }// end of for files
1386      if(nSpectres>0) spectreMean /= nSpectres;
1387     
1388      //save mean spectrum
1389      spectreCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
1390    }//end of for cycles
1391  }//end loop on mode for raw preocess
1392
1393  //JEC 23/9/11 DO IT
1394  //  if(debuglev_>1) {//save mean spectra on file
1395  cout << "Save mean raw spectra" << endl;
1396  string fileName;
1397  fileName = "./dataRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1398
1399  POutPersist fos(fileName);
1400  id=0;
1401  iSpectreEnd = spectreCollect.end();
1402  for (iSpectre = spectreCollect.begin();
1403       iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
1404    tag = "specRaw";
1405    tag += (iSpectre->first).first;
1406    stringstream sid;
1407    sid << (iSpectre->first).second;
1408    tag += sid.str();
1409    if(debuglev_>9) {
1410      cout << "save tag<" << tag << ">" << endl;
1411    }
1412    fos << PPFNameTag(tag) << iSpectre->second;
1413  }
1414    //  }//end of save fits
1415 
1416
1417  //------------------------------------------
1418  // Perform ON-OFF
1419  //------------------------------------------
1420 
1421  map< sa_size_t, TMatrix<r_4> > diffCollect;
1422  map< sa_size_t, TMatrix<r_4> >::iterator iDiff, iDiffEnd;
1423
1424  TMatrix<r_4> diffMeanOnOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //init zero
1425  r_4 nCycles = 0;
1426  for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1427    nCycles++;
1428    TMatrix<r_4> specmtxOn(spectreCollect[make_pair(modeList[0],icycle)],false); //clone the memory
1429    TMatrix<r_4> specmtxOff(spectreCollect[make_pair(modeList[1],icycle)],false); //clone the memory
1430    TMatrix<r_4> diffOnOff = specmtxOn - specmtxOff;
1431    diffCollect.insert(pair< sa_size_t,TMatrix<r_4> >(icycle,TMatrix<r_4>(diffOnOff,false) ));
1432    diffMeanOnOff += diffOnOff;
1433  }//end loop on cycle
1434  if(nCycles>0) diffMeanOnOff/=nCycles;
1435
1436  //extract channels and do the mean
1437  TVector<r_4> meanOfChan(NUMBER_OF_FREQ); //implicitly init to 0
1438  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
1439    meanOfChan += diffMeanOnOff.Row(iCh).Transpose();
1440  }
1441  meanOfChan /= (r_4)NUMBER_OF_CHANNELS;
1442 
1443
1444
1445  {//save diff ON-OFF on Raw data
1446    if(debuglev_>0)cout << "save ON-OFF RAW spectra" << endl;
1447    string fileName;
1448    fileName = "./diffOnOffRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1449    POutPersist fos(fileName);
1450    iDiffEnd = diffCollect.end();
1451    id = 0;
1452
1453    //JEC 22/9/11 Mean & Sigma in 32-bins size START
1454    sa_size_t nSliceFreq = 32; //TODO: put as an input parameter option ?
1455    sa_size_t deltaFreq = NUMBER_OF_FREQ/nSliceFreq;
1456    //JEC 22/9/11 Mean & Sigma in 32-bins size END
1457
1458    for (iDiff = diffCollect.begin();iDiff != iDiffEnd ; ++iDiff, id++) {
1459      tag = "specONOFFRaw";
1460      stringstream sid;
1461      sid << iDiff->first;
1462      tag += sid.str();
1463      fos << PPFNameTag(tag) << iDiff->second;
1464
1465      //JEC 22/9/11 Mean & Sigma in 32-bins size START
1466      if (debuglev_>9) {
1467        cout << "Debug slicing: slice/delta " << nSliceFreq << " " << deltaFreq << endl;
1468      }
1469      TMatrix<r_4> reducedMeanDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
1470      TMatrix<r_4> reducedSigmaDiffOnOff(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
1471      for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
1472        sa_size_t freqLow= iSlice*deltaFreq;
1473        sa_size_t freqHigh= freqLow + deltaFreq -1;
1474        if (debuglev_>9) {
1475          cout << "Debug .......... slicing ["<< iSlice << "]: low/high freq" << freqLow << "/" << freqHigh << endl;
1476        }
1477        for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
1478          if (debuglev_>9) {
1479            cout << "Debug .......... Channel " << iCh;
1480          }
1481          TVector<r_4> reducedRow;
1482          reducedRow = (iDiff->second).SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
1483          double mean; 
1484          double sigma;
1485          MeanSigma(reducedRow,mean,sigma);
1486          if (debuglev_>9) {
1487            cout << "mean/sigma " << mean << "/" << sigma << endl;
1488          }
1489          reducedMeanDiffOnOff(iCh,iSlice) = mean;
1490          reducedSigmaDiffOnOff(iCh,iSlice) = sigma;
1491        }//loop on Channel
1492      }//loop on Freq. slice
1493      tag = "redMeanONOFFRaw";
1494      tag += sid.str();
1495      fos << PPFNameTag(tag) << reducedMeanDiffOnOff;
1496      tag = "redSigmaONOFFRaw";
1497      tag += sid.str();
1498      fos << PPFNameTag(tag) << reducedSigmaDiffOnOff;
1499      //JEC 22/9/11 END
1500
1501    }//loop on ON-OFF spectre
1502    //save the mean also
1503    fos << PPFNameTag("specONOFFRawMean") << diffMeanOnOff;
1504
1505    //JEC 22/9/11 START
1506    TMatrix<r_4> reducedMeanDiffOnOffAll(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
1507    TMatrix<r_4> reducedSigmaDiffOnOffAll(NUMBER_OF_CHANNELS,nSliceFreq); //init 0 by default
1508    for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
1509      sa_size_t freqLow= iSlice*deltaFreq;
1510      sa_size_t freqHigh= freqLow + deltaFreq -1;
1511      if (debuglev_>9) {
1512        cout << "Debug .......... slicing ["<< iSlice << "]: low/high freq" << freqLow << "/" << freqHigh << endl;
1513      }
1514      for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
1515        if (debuglev_>9) {
1516          cout << "Debug .......... Channel " << iCh;
1517        }
1518        TVector<r_4> reducedRow;
1519        reducedRow = diffMeanOnOff.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
1520        double mean; 
1521        double sigma;
1522        MeanSigma(reducedRow,mean,sigma);
1523        if (debuglev_>9) {
1524          cout << "mean/signa " << mean << "/" << sigma << endl;
1525        }
1526        reducedMeanDiffOnOffAll(iCh,iSlice) = mean;
1527        reducedSigmaDiffOnOffAll(iCh,iSlice) = sigma;
1528      }//loop on Channel
1529    }//loop on Freq. slice
1530    tag = "redMeanONOFFRawAll";
1531    fos << PPFNameTag(tag) << reducedMeanDiffOnOffAll;
1532    tag = "redSigmaONOFFRawAll";
1533    fos << PPFNameTag(tag) << reducedSigmaDiffOnOffAll;
1534    //JEC 22/9/11 END
1535
1536
1537
1538    fos << PPFNameTag("specONOFFRaw2ChanMean") << meanOfChan;
1539  }//end of save fits
1540
1541 
1542  cout << "OK rawOnOff finished" <<endl;
1543  return rc;
1544} //ProcessONOFFRawData::processCmd
1545
1546//JEC 22/9/11 Make ON-OFF analysis WO any calibration END
1547//----------------------------------------------
1548int ProcessONOFFData::processCmd() throw(string) {
1549  int rc = 0;
1550  try {
1551    rc = ProcessBase::processCmd();
1552  } 
1553  catch (string s) {
1554    throw s;
1555  }
1556  if(debuglev_>0)cout << "Process Data" << endl;
1557  vector<string> modeList;
1558  modeList.push_back("On");
1559  modeList.push_back("Off");
1560  vector<string>::const_iterator iMode;
1561 
1562  uint_4 id; 
1563  string tag;
1564
1565  //
1566  //Process to get sucessively
1567  //Raw Spectra,
1568  //BAO Calibrated Spectra
1569  //and RT Calibrated Spectra
1570  //The pocesses are separated to allow intermediate save of results
1571
1572  map< pair<string, sa_size_t>, TMatrix<r_4> > spectreCollect;
1573  map< pair<string, sa_size_t>, TMatrix<r_4> >::iterator iSpectre, iSpectreEnd;
1574 
1575  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
1576    string mode = *iMode;
1577    if(debuglev_>0)cout << "Process RAW Mode " << mode << endl;
1578
1579    //------------------------------------------
1580    //Produce Raw spectra per cycle
1581    //------------------------------------------
1582
1583    string directoryName;
1584    list<string> listOfSpecFiles;
1585    list<string>::const_iterator iFile, iFileEnd;
1586   
1587       
1588    //
1589    //loop on cycles
1590    //
1591    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1592      //TOBE FIXED      directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
1593      directoryName = "./" + mode + "/";
1594      stringstream sicycle;
1595      sicycle << icycle;
1596      directoryName += spectraDirectory_ + sicycle.str() + "/";
1597
1598      //read directory
1599      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
1600     
1601
1602      //compute mean of spectra created in a cycle
1603      if(debuglev_>0)cout << "compute mean for cycle " << icycle << endl;
1604      TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1605      iFileEnd = listOfSpecFiles.end();
1606      r_4 nSpectres  = 0;
1607      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
1608        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1609        TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1610        aSpectrum >> spectre;
1611        spectreMean += spectre;
1612        nSpectres++;
1613      }// end of for files
1614      if(nSpectres>0) spectreMean /= nSpectres;
1615     
1616      //save mean spectrum
1617      spectreCollect.insert( pair< pair<string,sa_size_t>, TMatrix<r_4> >(make_pair(mode,icycle),TMatrix<r_4>(spectreMean,false) ));
1618    }//end of for cycles
1619  }//end loop on mode for raw preocess
1620
1621  if(debuglev_>1) {//save mean spectra on file
1622    cout << "Save mean raw spectra" << endl;
1623    string fileName;
1624    //TOBE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataRaw" + ".ppf";
1625    fileName = "./dataRaw_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1626
1627    POutPersist fos(fileName);
1628    id=0;
1629    iSpectreEnd = spectreCollect.end();
1630    for (iSpectre = spectreCollect.begin();
1631         iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
1632      tag = "specRaw";
1633
1634      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
1635//       stringstream sid;
1636//       sid << id;
1637//       tag += sid.str();
1638      tag += (iSpectre->first).first;
1639      stringstream sid;
1640      sid << (iSpectre->first).second;
1641      tag += sid.str();
1642      if(debuglev_>9) {
1643        cout << "save tag<" << tag << ">" << endl;
1644      }
1645      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
1646
1647      fos << PPFNameTag(tag) << iSpectre->second;
1648    }
1649  }//end of save fits
1650 
1651
1652
1653  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
1654    string mode = *iMode;
1655    if(debuglev_>0)cout << "Process CALIB BAO Mode " << mode << endl;
1656    //------------------------------------------
1657    // Correct Raw spectra for BAO calibration
1658    //------------------------------------------
1659    //Read BAO calibration files
1660    sa_size_t nr,nc; //values read
1661   
1662    //JEC 20/9/11 use mean calibration coeff upon all cycles START
1663    string calibFileName = inputPath_+ "/" 
1664      + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) 
1665      + "/calib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
1666      + mode + "_" + freqBAOCalibration_ + "MHz-All.txt";
1667
1668    if(debuglev_>0) cout << "Read file " << calibFileName << endl;
1669    ifstream ifs(calibFileName.c_str());
1670    if ( ! ifs.is_open() ) {
1671      rc = 999;
1672      throw calibFileName + " cannot be opened...";
1673    }   
1674    TVector<r_4> calibBAOfactors;
1675    if(debuglev_>9) cout << "Debug 1" << endl;
1676    calibBAOfactors.ReadASCII(ifs,nr,nc);
1677    if(debuglev_>9){
1678      cout << "Debug 2: (nr,nc): "<< nr << "," << nc << endl;
1679      calibBAOfactors.Print(cout);
1680    }
1681
1682    //JEC 20/9/11 use mean calibration coeff upon all cycles END
1683
1684    //
1685    //spectra corrected by BAO calibration factor
1686    //-----make it different on Channels and Cycles (1/06/2011) OBSOLETE
1687    //use mean calibration coeff upon all cycles (20/6/11)
1688    //warning cycles are numbered from 1,...,N
1689    //
1690    if(debuglev_>0)cout << "do calibration..." << endl;
1691    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1692      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory
1693     
1694      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
1695        //JEC 20/9/11 use mean calibration coeff upon all cycles START
1696
1697        //      specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh,icycle-1);
1698        specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh);
1699        //JEC 20/9/11 use mean calibration coeff upon all cycles END
1700      }
1701    }
1702  } //end loop mode for BAO calib
1703
1704  cout << "save calibrated BAO spectra" << endl;
1705  string fileName;
1706  //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAOCalib" + ".ppf";
1707  fileName = "./dataBAOCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1708 
1709  POutPersist fos(fileName);
1710  iSpectreEnd = spectreCollect.end();
1711  id=0;
1712  for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
1713   
1714    tag = "specBAOCalib";
1715    //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
1716    //       stringstream sid;
1717    //       sid << id;
1718    //       tag += sid.str();
1719    tag += (iSpectre->first).first;
1720    stringstream sid;
1721    sid << (iSpectre->first).second;
1722    tag += sid.str();
1723    if(debuglev_>9) {
1724      cout << "save tag<" << tag << ">" << endl;
1725    }
1726    //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
1727   
1728    fos << PPFNameTag(tag) << iSpectre->second;
1729  }
1730 
1731  for (iMode = modeList.begin(); iMode != modeList.end(); ++iMode) {
1732    string mode = *iMode;
1733    if(debuglev_>0)cout << "Process CALIB RT Mode " << mode << endl;
1734    //------------------------------------------
1735    // Correct BAO calib spectra for RT calibration
1736    //------------------------------------------
1737    //Very Preliminary May-June 11
1738    //coef RT @ 1346MHz Ouest - Est associees a Ch 0 et 1
1739    r_4 calibRT[NUMBER_OF_CHANNELS] = {27.724, 22.543};
1740    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1741      TMatrix<r_4> specmtx(spectreCollect[make_pair(mode,icycle)],true); //share the memory   
1742      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
1743        specmtx( Range(iCh), Range::all() ) *= calibRT[iCh];
1744      }
1745    }
1746  }//end loop on mode RT calib
1747
1748  {//save mean spectra BAO & RT calibrated on file
1749    if(debuglev_>0)cout << "save calibrated BAO & RT spectra" << endl;
1750    string fileName;
1751    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "dataBAORTCalib" + ".ppf";
1752     fileName = "./dataBAORTCalib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";   
1753    POutPersist fos(fileName);
1754    iSpectreEnd = spectreCollect.end();
1755    id = 0;
1756    for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
1757      tag = "specBAORTCalib";
1758      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
1759//       stringstream sid;
1760//       sid << id;
1761//       tag += sid.str();
1762      tag += (iSpectre->first).first;
1763      stringstream sid;
1764      sid << (iSpectre->first).second;
1765      tag += sid.str();
1766      if(debuglev_>9) {
1767        cout << "save tag<" << tag << ">" << endl;
1768      }
1769      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
1770      fos << PPFNameTag(tag) << iSpectre->second;
1771    }
1772  }//end of save fits
1773
1774  //------------------------------------------
1775  // Perform ON-OFF
1776  //------------------------------------------
1777 
1778  map< sa_size_t, TMatrix<r_4> > diffCollect;
1779  map< sa_size_t, TMatrix<r_4> >::iterator iDiff, iDiffEnd;
1780
1781  TMatrix<r_4> diffMeanOnOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //init zero
1782  r_4 nCycles = 0;
1783  for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
1784    nCycles++;
1785    TMatrix<r_4> specmtxOn(spectreCollect[make_pair(modeList[0],icycle)],false); //clone the memory
1786    TMatrix<r_4> specmtxOff(spectreCollect[make_pair(modeList[1],icycle)],false); //clone the memory
1787    TMatrix<r_4> diffOnOff = specmtxOn - specmtxOff;
1788    diffCollect.insert(pair< sa_size_t,TMatrix<r_4> >(icycle,TMatrix<r_4>(diffOnOff,false) ));
1789    diffMeanOnOff += diffOnOff;
1790  }//end loop on cycle
1791  if(nCycles>0) diffMeanOnOff/=nCycles;
1792
1793  //exctract channels and do the mean
1794  TVector<r_4> meanOfChan(NUMBER_OF_FREQ); //implicitly init to 0
1795  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
1796    meanOfChan += diffMeanOnOff.Row(iCh).Transpose();
1797  }
1798  meanOfChan /= (r_4)NUMBER_OF_CHANNELS;
1799 
1800
1801
1802  {//save diff ON-OFF BAO & RT calibrated
1803    if(debuglev_>0)cout << "save ON-OFF spectra" << endl;
1804    string fileName;
1805    //TO BE FIXED    fileName = "./" + sourceName_ + "/" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_" + "diffOnOff" + ".ppf";
1806    fileName = "./diffOnOff_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1807    POutPersist fos(fileName);
1808    iDiffEnd = diffCollect.end();
1809    id = 0;
1810    for (iDiff = diffCollect.begin();iDiff != iDiffEnd ; ++iDiff, id++) {
1811      tag = "specONOFF";
1812      stringstream sid;
1813      //JEC 20/9/11      sid << id;
1814      sid << iDiff->first;
1815      tag += sid.str();
1816      if(debuglev_>9) {
1817        cout << "save tag<" << tag << ">" << endl;
1818      }
1819      fos << PPFNameTag(tag) << iDiff->second;
1820    }
1821    //save the mean also
1822    fos << PPFNameTag("specONOFFMean") << diffMeanOnOff;
1823    fos << PPFNameTag("specONOFF2ChanMean") << meanOfChan;
1824  }//end of save fits
1825
1826  cout << "OK dataOnOff finished";
1827
1828  return rc;
1829} //ProcessONOFFData::processCmd
1830//
1831//----------------------------------------------
1832//
1833int ProcessGain::processCmd() throw(string) {
1834  int rc = 0;
1835  string msg = "";
1836
1837  try {
1838    rc = ProcessBase::processCmd();
1839  } 
1840  catch (string s) {
1841    throw s;
1842  }
1843  if(debuglev_>0)cout << "Process Gain" << endl;
1844 
1845  string directoryName;
1846  //TOBE FIXED  directoryName = "./" + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode_ + "/";
1847  //JEC 6/09/2011 numcycle_ repalced by ifirstCycle_ according to definition of numcycle_ and the fact that for GAIN 1 cycle is involved
1848  stringstream thegaincycle;
1849  thegaincycle << ifirstCycle_;
1850  directoryName = inputPath_ + "/" 
1851    + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) + "/" 
1852    + mode_ + "/" + spectraDirectory_ + thegaincycle.str()  + "/";
1853 
1854  list<string> listOfSpecFiles;
1855  list<string>::const_iterator iFile, iFileEnd;
1856  //read directory
1857
1858  listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
1859 
1860  //Mean power computed over the N channels to select the spectra for gain computation
1861  //The threshold is computed "on-line" as  1%  of the difference (max power -min power) over the
1862  // the min power.
1863  //A possible alternative is to set an absolute value...
1864  if(debuglev_>0)cout << "compute mean poser spectra for files in " << directoryName << endl;
1865  iFileEnd = listOfSpecFiles.end();
1866  map<string, r_4> meanpowerCollect;
1867  //JEC 21/9/11 add meanpower for each Channels START
1868  map<string, r_4> meanPowerPerChanCollect;
1869  //JEC 21/9/11 add meanpower for each Channels END
1870
1871  map<string, r_4>::const_iterator iMeanPow, iMeanPowEnd;
1872
1873  for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
1874    FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
1875    TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1876    aSpectrum >> spectre;
1877    meanpowerCollect[*iFile] = spectre.Sum()/spectre.Size();
1878    //JEC 21/9/11 add meanpower for each Channels START
1879    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
1880      TVector<r_4> specChan(NUMBER_OF_FREQ);
1881      specChan = spectre.Row(iCh).Transpose();
1882      stringstream tmp;
1883      tmp << iCh;
1884      string tag = *iFile + "_" + tmp.str();
1885      meanPowerPerChanCollect[tag] = specChan.Sum()/specChan.Size();
1886    }
1887    //JEC 21/9/11 add meanpower for each Channels END
1888  }//end of for files
1889  pair<string, r_4> minelement = *min_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
1890  pair<string, r_4> maxelement = *max_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
1891  if(debuglev_>1){
1892    cout << "meanpowerCollect has " << meanpowerCollect.size() << " spectra registered" << endl;
1893    cout << "find min mean power "<<minelement.second << " for " << minelement.first << endl;
1894    cout << "find max mean power "<<maxelement.second << " for " << maxelement.first << endl;
1895  }
1896  r_4 threshold = minelement.second + 0.01*(maxelement.second-minelement.second);
1897  if(debuglev_>1){
1898    cout << "threshold found at " << threshold <<endl;
1899  } 
1900
1901  TMatrix<r_4> spectreMean(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
1902  r_4 nSpectres  = 0;
1903  iMeanPowEnd = meanpowerCollect.end();
1904  for (iMeanPow = meanpowerCollect.begin(); iMeanPow != iMeanPowEnd; ++iMeanPow) {
1905    if ( iMeanPow->second <= threshold ) {
1906      //TODO avoid the reloading of the file may speed up
1907      FitsInOutFile aSpectrum(iMeanPow->first,FitsInOutFile::Fits_RO);
1908      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1909      aSpectrum >> spectre;
1910      spectreMean += spectre;
1911      nSpectres++;
1912    }
1913  }
1914  if(debuglev_>1){
1915    cout << "Number of files selected for gain " << nSpectres <<endl;
1916  }   
1917  if(nSpectres>0) {
1918    spectreMean /= nSpectres;
1919  } else {
1920    stringstream tmp;
1921    tmp << threshold;
1922    msg = "Gain: cannot find a spectra above threshold value =" + tmp.str() + " ... FATAL";
1923    throw msg;
1924  }
1925
1926  //Save gain spectra
1927  {
1928    //use ! to override the file: special features of cfitsio library
1929    string fileName;
1930    //TOBE FIXED   fileName = "!./" + sourceName_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
1931    fileName = "!"+ outputPath_ + "/gain_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".fits";
1932    if(debuglev_>1){
1933      cout << "save gains in " << fileName << endl;
1934    }
1935    FitsInOutFile fos(fileName, FitsInOutFile::Fits_Create);
1936    fos << spectreMean;
1937  }
1938  //save mean power values
1939  {
1940    vector<r_4> tmp;
1941    //JEC 21/9/11 add meanpower for each Channels START
1942    vector<r_4> tmpCh0; //for Chan 0
1943    vector<r_4> tmpCh1; //for Chan 1
1944    //JEC 21/9/11 add meanpower for each Channels END
1945    for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
1946      if (debuglev_>9) {
1947        cout << "Gain: save mean power of file: " << *iFile << endl;
1948      }
1949      tmp.push_back(meanpowerCollect[*iFile]);
1950      //JEC 21/9/11 add meanpower for each Channels START 
1951      stringstream tmp0;
1952      tmp0 << (sa_size_t)0;
1953      string tag0 = *iFile + "_" + tmp0.str();
1954      tmpCh0.push_back(meanPowerPerChanCollect[tag0]);
1955      if (NUMBER_OF_CHANNELS>1){
1956        stringstream tmp1;
1957        tmp1 << (sa_size_t)1;
1958        string tag1 = *iFile + "_" + tmp1.str();
1959        tmpCh1.push_back(meanPowerPerChanCollect[tag1]);
1960      }
1961      //JEC 21/9/11 add meanpower for each Channels END
1962    }
1963    string fileName;
1964    //TOBE FIXED    fileName = "./" + sourceName_ + "/gain_monitor_" + dateOfRun_
1965    fileName = outputPath_ + "/gain_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
1966    POutPersist fos(fileName); 
1967    TVector<r_4> tvtmp(tmp);
1968    fos.PutObject(tvtmp,"gainmoni"); //Attention initialement le tag etait "monitor"...
1969    //JEC 21/9/11 add meanpower for each Channels START 
1970    TVector<r_4> tvtmp0(tmpCh0);
1971    fos.PutObject(tvtmp0,"gainmoni0");
1972    if (NUMBER_OF_CHANNELS>1){
1973      TVector<r_4> tvtmp1(tmpCh1);
1974      fos.PutObject(tvtmp1,"gainmoni1");
1975    }
1976    //JEC 21/9/11 add meanpower for each Channels END
1977  }
1978 
1979  cout << "OK gain finished" <<endl;
1980   return rc;
1981}//ProcessGain::processCmd
1982//
1983//----------------------------------------------
1984//
1985int ProcessCalibration::processCmd() throw(string) {
1986  int rc = 0;
1987  string msg = "";
1988
1989  try {
1990    rc = ProcessBase::processCmd();
1991  } 
1992  catch (string s) {
1993    throw s;
1994  }
1995  if(debuglev_>0)cout << "Process Calibration with option "<< option_ << endl;
1996 
1997  vector<string> modeList;
1998  modeList.push_back("On");
1999  modeList.push_back("Off");
2000
2001  r_8 t0absolute = -1;  //TIMEWIN of the first spectra used
2002  r_8 timeTag0   = -1;   //MEANTT, mean TIME TAG of the first paquet window 
2003  bool first = true;     // for initialisation
2004 
2005  // Power Tuple
2006  // mode, cycle, time, {Ch0, ... ,ChN} mean poser in the range [f0-Bw/2, f0+Bw/2]
2007  vector<string> varPowerTupleName; //ntuple variable naming
2008  varPowerTupleName.push_back("mode");
2009  varPowerTupleName.push_back("cycle"); 
2010  varPowerTupleName.push_back("time");
2011  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
2012    stringstream tmp;
2013    tmp << iCh;
2014    varPowerTupleName.push_back("Ch"+tmp.str());
2015  }
2016  r_4 valPowerTuple[varPowerTupleName.size()];
2017  NTuple powerEvolution(varPowerTupleName); 
2018 
2019 
2020  //-----------------
2021  //Start real process
2022  //------------------
2023  for (size_t iMode = 0; iMode < modeList.size(); ++iMode) {
2024    string mode = modeList[iMode];
2025    if(debuglev_>0)cout << "Process Calibration for Mode " << mode << endl;
2026
2027    //initialize the start of each calibration procedure given by the RT SCA file
2028    //see ProcessBase::processCmd for the structure of the sca file
2029    string scaStartCalibName = "stcal"+mode; 
2030    sa_size_t idStartCalib   = scaTuple_->ColumnIndex(scaStartCalibName);
2031
2032    string directoryName;
2033    list<string> listOfSpecFiles;
2034    list<string>::const_iterator iFile, iFileEnd;           
2035    //
2036    //loop on cycles
2037    //
2038    for (sa_size_t icycle = ifirstCycle_; icycle <= ilastCycle_; icycle++) {
2039
2040      directoryName = inputPath_ + "/" 
2041        + sourceName_ + "/"+ dateOfRun_ + StringToLower(sourceName_) + "/" +mode + "/";
2042      stringstream sicycle;
2043      sicycle << icycle;
2044      directoryName += spectraDirectory_ + sicycle.str() + "/";
2045     
2046      //read directory
2047      listOfSpecFiles = ListOfFileInDir(directoryName,typeOfFile_);
2048
2049      iFileEnd = listOfSpecFiles.end();
2050      DVList header;
2051      TMatrix<r_4> spectre(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //implicit init to 0
2052
2053      for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
2054        FitsInOutFile aSpectrum(*iFile,FitsInOutFile::Fits_RO);
2055        aSpectrum.GetHeaderRecords(header);
2056        aSpectrum >> spectre;
2057
2058        if(first){//initialise the timer
2059          first = false;
2060          t0absolute = header.GetD("TIMEWIN")/1000.;
2061          timeTag0 = header.GetD("MEANTT");
2062          if (debuglev_>1){
2063            cout << "debug Header of " << *iFile << endl;
2064            cout << "TIMEWIN = " << setprecision(12) << t0absolute << " "
2065                 << "MEANTT = " << setprecision(12) << timeTag0 << endl;
2066          }
2067        }//end init timer
2068       
2069        //Set time of current file
2070        //Add to the non-precise absolute origin an precise increment
2071        r_4 timeTag = t0absolute + (header.GetD("MEANTT") - timeTag0);
2072        int index=0;
2073        valPowerTuple[index++] = iMode;
2074        valPowerTuple[index++] = icycle;
2075        valPowerTuple[index++] = timeTag-scaTuple_->GetCell(idCycleInTuple_[icycle],idStartCalib); //take the RT time start as refernce
2076
2077        //Compute the mean power of the two channel (separatly) in the range [f-bw/2, f+bw/2]
2078        for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
2079          TMatrix<r_4> tmp(spectre(Range(iCh),Range(lowerFreqBin_,upperFreqBin_)),false);
2080          r_4 mean = tmp.Sum()/(r_4)tmp.Size();
2081          valPowerTuple[index++] = mean;
2082        }//end of channel loop
2083       
2084        //Fill Tuple
2085        powerEvolution.Fill(valPowerTuple);
2086      }//end of files loop
2087    }//end of cycles loop
2088  }//end of mode loop
2089
2090  //store power evolution Tuple
2091  if(debuglev_>0){
2092    cout << "ProcessCalibration::processCmd: dump powerEvolution tuple" << endl;
2093    powerEvolution.Show(cout);
2094  }
2095  //
2096  //Compute Calibration Coefficients as function of mode/cycle/channels
2097  //
2098
2099  //Tsys,Incremant Intensity... Tuple
2100  //mode mode cycle [(tsys0,dint0),...,(tsysN,dintN)]
2101  vector<string> varCalibTupleName;
2102  varCalibTupleName.push_back("mode");
2103  varCalibTupleName.push_back("cycle");
2104  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS;++iCh){
2105    stringstream tmp;
2106    tmp << iCh;
2107    varCalibTupleName.push_back("tsys"+tmp.str());
2108    varCalibTupleName.push_back("dint"+tmp.str());
2109  }
2110  r_4 valCalibTuple[varCalibTupleName.size()];
2111  NTuple calibEvolution(varCalibTupleName);
2112
2113  // select time E [twin0,twin1] U [twin2,twin3] for Tsys
2114  // time unit = second
2115  const r_4 twin0 = -3.;
2116  const r_4 twin1 = -1.;
2117  const r_4 twin2 =  6.;
2118  const r_4 twin3 =  8;
2119  const r_4 thresholdFactor = 0.20; //80% of the diff. Max-Min intensity
2120
2121  sa_size_t inModeIdx = powerEvolution.ColumnIndex("mode");
2122  sa_size_t inCycleIdx= powerEvolution.ColumnIndex("cycle");
2123  sa_size_t inTimeIdx = powerEvolution.ColumnIndex("time");
2124  sa_size_t inOffsetCh0 = powerEvolution.ColumnIndex("Ch0"); //nb Ch0 position in the powerEvolution tuple 
2125  if(debuglev_>1) cout << "DEBUG: in Idx: (" 
2126                       << inModeIdx << ", "
2127                       << inCycleIdx << ", "
2128                       << inTimeIdx << ", "
2129                       << inOffsetCh0 << ")"
2130                       << endl;
2131
2132 
2133  size_t outModeIdx = calibEvolution.ColumnIndex("mode");
2134  size_t outCycleIdx= calibEvolution.ColumnIndex("cycle");
2135  size_t outOffsetCh0 = calibEvolution.ColumnIndex("tsys0"); //nb Ch0 position in the monitor tuple 
2136  size_t outNDataPerCh= 2;
2137  if(debuglev_>1)  cout << "DEBUG: out Idx: (" 
2138                        << outModeIdx << ", "
2139                        << outCycleIdx << ", "
2140                        << outOffsetCh0 << ")"
2141                        << endl;
2142
2143  sa_size_t nPowerEvolEntry = powerEvolution.NEntry();
2144  double minMode;
2145  double maxMode;
2146  powerEvolution.GetMinMax("mode",minMode,maxMode);
2147  double minCycleNum;
2148  double maxCycleNum;
2149  powerEvolution.GetMinMax("cycle",minCycleNum,maxCycleNum);
2150  if(debuglev_>1)  cout << "DEBUG mode ("<<minMode<<","<<maxMode<<")\n"
2151                        << "cycle ("<<minCycleNum<<","<<maxCycleNum<<")"
2152                        << endl;
2153
2154  r_4* aPowerEvolEntry; // a ligne of the powerEvolution tuple
2155//   r_4* aPowerEvolEntry = new r_4[powerEvolution.NbColumns()]; // a ligne of the powerEvolution tuple
2156
2157  r_4 minMean;
2158  r_4 maxMean;
2159
2160  for (size_t iMode = (size_t)minMode; iMode <= (size_t)maxMode; iMode++){
2161    for (size_t iCycle = (size_t)minCycleNum; iCycle <= (size_t)maxCycleNum; iCycle++ ){
2162      if(debuglev_>1) cout<<"DEBUG >>>>>>> mode/cycle: " << iMode << "/" << iCycle << endl;
2163 
2164      valCalibTuple[outModeIdx]=iMode;
2165      valCalibTuple[outCycleIdx]=iCycle;
2166      //
2167      //Compute the Min && Max value of each Ch<i> data
2168      if(debuglev_>1) cout<<"DEBUG compute Min and Max for each channels" << endl;
2169      //
2170      TVector<r_4> chMean[NUMBER_OF_CHANNELS];
2171      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
2172        chMean[iCh].SetSize(nPowerEvolEntry);
2173      }
2174      for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
2175        aPowerEvolEntry = powerEvolution.GetVec(ie);
2176        if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
2177          for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
2178            chMean[iCh](ie) = aPowerEvolEntry[iCh+inOffsetCh0];
2179          }//end cut
2180        }
2181      }//eo loop on tuple entries
2182      for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;iCh++){
2183        //
2184        //select values to compute background Tsys
2185        if(debuglev_>1) {
2186          cout<< "DEBUG >>>> Ch[" << iCh << "]" << endl;
2187          cout<< "DEBUG select values to compute background Tsys " << endl;
2188        }
2189        //
2190       
2191        std::vector<r_4> lowerInt;
2192        for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
2193          aPowerEvolEntry = powerEvolution.GetVec(ie);
2194          if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
2195            r_4 time = aPowerEvolEntry[inTimeIdx];
2196            if ( (time >= twin0 && time <= twin1) ||
2197                 (time >= twin2 && time <= twin3)
2198                 ) lowerInt.push_back(aPowerEvolEntry[iCh+inOffsetCh0]);
2199          }//end cut
2200        } //end loop entries
2201        //
2202        //compute the Tsys
2203        if(debuglev_>1) cout <<"DEBUG compute Tsys" << endl;
2204        //
2205        std::nth_element(lowerInt.begin(),
2206                         lowerInt.begin()+lowerInt.size()/2,
2207                         lowerInt.end());
2208        r_4 tsys = *(lowerInt.begin()+lowerInt.size()/2);
2209        if(debuglev_>1) cout << "DEBUG tsys["<<iCh<<"] : " << tsys <<endl;
2210        //
2211        //set the threshold for DAB detection
2212        //
2213        chMean[iCh].MinMax(minMean,maxMean);
2214        minMean = (tsys > minMean) ? tsys : minMean; //pb of empty vector
2215        if(debuglev_>1) cout << "DEBUG min["<<iCh<<"] : " << minMean
2216                             << " max["<<iCh<<"] : " << maxMean
2217                             <<endl;
2218        r_4 deltathres = thresholdFactor * (maxMean-minMean);
2219        r_4 thres =  tsys + deltathres;
2220        if(debuglev_>1) cout << "DEBUG thres["<<iCh<<"] : " << thres <<endl;
2221        //
2222        //collect upper part of the DAB
2223        if(debuglev_>1) cout <<"DEBUG collect upper part of the DAB" << endl;
2224        //
2225        std::vector<r_4> upperInt;
2226        for (sa_size_t ie=0; ie<nPowerEvolEntry; ie++){
2227          aPowerEvolEntry = powerEvolution.GetVec(ie);
2228          if ( (size_t)aPowerEvolEntry[inModeIdx] == iMode && (size_t)aPowerEvolEntry[inCycleIdx] == iCycle ){
2229            r_4 mean = aPowerEvolEntry[iCh+inOffsetCh0];
2230            if (mean >= thres) upperInt.push_back(mean);
2231          }//end cut
2232        }//eo loop on channels
2233        //
2234        //compute adjacent differences to detect the 2 DAB levels
2235        //
2236        if(debuglev_>1) cout << "(DEBUG )size upper [" << iCh << "]: " << upperInt.size() <<endl;
2237        std::vector<r_4> upperIntAdjDiff(upperInt.size());
2238        std::adjacent_difference(upperInt.begin(),
2239                                 upperInt.end(),
2240                                 upperIntAdjDiff.begin());
2241        //
2242        //Search the 2 DAB states
2243        if(debuglev_>1) cout<<"DEBUG Search the 2 DAB states" << endl;
2244        //
2245        std::vector<r_4> upIntState[2];
2246        int state=-1;
2247        for (size_t i=1;i<upperInt.size();i++) {//skip first value
2248          if (fabs(upperIntAdjDiff[i])<upperInt[i]*0.010) {
2249            if(state == -1) state=0;
2250            if(state == -2) state=1;
2251            upIntState[state].push_back(upperInt[i]);
2252          } else {
2253            if (state == 0) state=-2;
2254          }
2255        }
2256        //
2257        //Take the mean of the median values of each levels
2258        if(debuglev_>1)cout << "DEBUG mean of the median values of each levels" << endl;       
2259        //
2260        r_4 meanUpper = 0;
2261        r_4 nval = 0;
2262        for (int i=0;i<2;i++) {
2263          if (!upIntState[i].empty()) {
2264//          std::nth_element(upIntState[i].begin(),
2265//                           upIntState[i].begin()+upIntState[i].size()/2,
2266//                           upIntState[i].end());
2267//          meanUpper += *(upIntState[i].begin()+upIntState[i].size()/2);
2268            meanUpper += median(upIntState[i].begin(),upIntState[i].end());
2269            nval++;
2270          } 
2271        }
2272        meanUpper /= nval;
2273        //
2274        //Finaly the increase of power due to the DAB is:
2275        //
2276        r_4 deltaInt = meanUpper - tsys;
2277        if(debuglev_>1) cout << "DEBUG deltaInt["<<iCh<<"] : " << deltaInt <<endl;
2278        //
2279        //Save for monitoring and final calibration operations
2280        //
2281        valCalibTuple[outOffsetCh0+outNDataPerCh*iCh]   = tsys;
2282        valCalibTuple[outOffsetCh0+outNDataPerCh*iCh+1] = deltaInt;
2283      }//end loop on channels
2284      if(debuglev_>1) cout<<"DEBUG Fill the calibEvolution tuple" << endl;
2285      calibEvolution.Fill(valCalibTuple);
2286    }//eo loop on Cycles
2287  }//eo loop on Modes
2288  //
2289  //store calibration evolution Tuple
2290  //
2291  if(debuglev_>0){
2292    cout << "ProcessCalibration::processCmd: dump calibEvolution tuple" << endl;
2293    calibEvolution.Show(cout);
2294  }
2295  //
2296  //Compute the means per channel of the calibration coefficients (deltaInt)
2297  //and save cycle based Calibration Ctes in file named as
2298  //    <source>-<date>-<mode>-<fcalib>MHz-Ch<channel>Cycles.txt
2299  //   format <cycle> <coef>
2300  //the means are stored in files     
2301  //    <source>-<date>-<mode>-<fcalib>MHz-All.txt
2302  //   format <channel> <coef>
2303  //
2304  sa_size_t nModes = (sa_size_t)(maxMode - minMode) + 1;
2305  string calibCoefFileName;
2306  ofstream  calibMeanCoefFile[nModes]; //Mean over cycles
2307  ofstream  calibCoefFile[nModes][NUMBER_OF_CHANNELS]; //cycle per cycle
2308  for (sa_size_t iMode=0; iMode<nModes; iMode++){
2309    //The file for the Means Coeff.
2310    calibCoefFileName = outputPath_ + "/calib_" 
2311      + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
2312      + modeList[iMode] + "_"
2313      + freqBAOCalibration_ + "MHz-All.txt";
2314    calibMeanCoefFile[iMode].open(calibCoefFileName.c_str());
2315    //The file for the cycle per cycle Coeff.
2316    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
2317      stringstream chString;
2318      chString << iCh;
2319      calibCoefFileName = outputPath_ + "/calib_" 
2320        + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
2321        + modeList[iMode] + "_"
2322        + freqBAOCalibration_ + "MHz-Ch" + chString.str() + "Cycles.txt";
2323      calibCoefFile[iMode][iCh].open(calibCoefFileName.c_str());
2324    }
2325  }
2326
2327  r_4* aCalibEvolEntry;
2328  sa_size_t nCalibEvolEntry = calibEvolution.NEntry();
2329
2330  TMatrix<r_4> meanCalibCoef(nModes,NUMBER_OF_CHANNELS); //by default init to 0
2331  TMatrix<r_4> nData4Mean(nModes,NUMBER_OF_CHANNELS);
2332
2333  //READ the calibration tuple, fill the array for mean and write to ascii file
2334  for (sa_size_t ie=0; ie<nCalibEvolEntry; ie++){
2335    aCalibEvolEntry = calibEvolution.GetVec(ie);
2336    if(debuglev_>1){
2337      cout << "DEBUG mode/cycle/Coef " 
2338           << aCalibEvolEntry[outModeIdx] << " "
2339           << aCalibEvolEntry[outCycleIdx] << " ";
2340    }
2341    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
2342      if(debuglev_>1) cout << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1] << " "; // for debug
2343      sa_size_t currentMode = (sa_size_t)(aCalibEvolEntry[outModeIdx] - minMode); //ok even if minMode <> 0
2344      nData4Mean(currentMode,iCh)++;
2345      meanCalibCoef(currentMode,iCh) += aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1];
2346
2347      calibCoefFile[currentMode][iCh] << aCalibEvolEntry[outCycleIdx] << " "
2348                                      << setprecision(12)
2349                                      << aCalibEvolEntry[outOffsetCh0+outNDataPerCh*iCh+1]
2350                                      << endl;
2351    }
2352    if(debuglev_>1) cout << endl; //for debug
2353  }
2354 
2355  //Perform means: true means that div per 0 is treated (slower but safer)
2356  meanCalibCoef.Div(nData4Mean,true);
2357 
2358  if(debuglev_>0){
2359    cout << "DEBUG nData4Mean" << endl;
2360    nData4Mean.Print(cout);
2361    cout << "DEBUG meanCalibCoef (averaged)" << endl;
2362    meanCalibCoef.Print(cout);
2363  }
2364
2365  //Save means Coef
2366  for (sa_size_t iMode=0; iMode<nModes; iMode++){
2367    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
2368      calibMeanCoefFile[iMode] << setprecision(12)
2369                               << meanCalibCoef(iMode,iCh)
2370                               << endl;
2371    }
2372  }
2373
2374  //Save Monitor File
2375  {
2376    string fileName = outputPath_ + "/calib_monitor_" + dateOfRun_ + "_" + StringToLower(sourceName_) + ".ppf";
2377    POutPersist calibMonitorFile(fileName);
2378    calibMonitorFile << PPFNameTag("powermoni") <<  powerEvolution;
2379    calibMonitorFile << PPFNameTag("calibmoni") <<  calibEvolution;
2380  }
2381
2382  //Clean & Return
2383  for (sa_size_t iMode=0; iMode<nModes; iMode++){
2384    calibMeanCoefFile[iMode].close();
2385    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
2386      calibCoefFile[iMode][iCh].close();
2387    }
2388  }
2389
2390  cout << "Ok calibration finished" << endl; 
2391  return rc;
2392}//ProcessCalibration::processCmd
2393
Note: See TracBrowser for help on using the repository browser.