source: BAORadio/AmasNancay/trunk/analyse.cc

Last change on this file was 626, checked in by campagne, 13 years ago

drift scan runs (jec)

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