source: BAORadio/AmasNancay/trunk/mergeAnaFiles.cc @ 604

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

add the ON/Filtered_OFF and OFF/Filetred_OFF (jec)

File size: 47.6 KB
RevLine 
[540]1// Utilisation de SOPHYA pour faciliter les tests ...
2#include "sopnamsp.h"
3#include "machdefs.h"
4#include <dirent.h>
5#include <matharr.h>
6
7// include standard c/c++
[562]8#include <regex.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <limits>
[540]12#include <iostream>
13#include <fstream>
14#include <string>
15#include <vector>
16#include <map>
17#include <functional>
18#include <algorithm>
19#include <numeric>
20#include <list>
21#include <exception>
22
23// include sophya mesure ressource CPU/memoire ...
24#include "resusage.h"
25#include "ctimer.h"
26#include "timing.h"
27#include "timestamp.h"
28#include "strutilxx.h"
29#include "ntuple.h"
30#include "fioarr.h"
31#include "tarrinit.h"
32#include "histinit.h"
33#include "fitsioserver.h"
34#include "fiosinit.h"
35#include "ppersist.h"
36
37//-----------------------------------------------
[563]38//Usage
39//
[602]40//./Objs/mergeAnaFiles -act meanRawDiffOnOff -inPath /sps/baoradio/AmasNancay/JEC/ -src NGC4383 -ppfFile dataRaw -debug 1000 -mxcycles 10
[577]41//./Objs/mergeAnaFiles -act meanCalibBAODiffOnOff -inPath /sps/baoradio/AmasNancay/JEC/ -src Abell85 -ppfFile dataRaw -debug 1 -mxcycles 500
42//./Objs/mergeAnaFiles -src Abell85 -act meanCalibBAODiffOnOff -mxcycles 500 -calibfreq 1410 -inPath /sps/baoradio/AmasNancay/JEC/ -ppfFile dataRaw -debug 1 >& mergeAna-500.log
[563]43//
44//
45//-----------------------------------------------
[542]46const sa_size_t NUMBER_OF_CHANNELS = 2;
[544]47const sa_size_t NUMBER_OF_FREQ     = 8192;
[560]48const r_4    LOWER_FREQUENCY       = 1250.0; //MHz
49const r_4    TOTAL_BANDWIDTH       = 250.0;  //MHz
[544]50//Input parameters
51struct Param {
52  int debuglev_;      //debug
53  string inPath_;     //root directory of the input files
54  string outPath_;    //output files are located here
55  string sourceName_; //source name & subdirectory of the input files
56  string ppfFile_;    //generic name of the input files
57  int nSliceInFreq_;  //used by reduceSpectra() fnc
[561]58  string calibFreq_;  //freq. value used for calibration
[562]59  r_4 rcalibFreq_;    //float version
60  string calibBandFreq_;  //band of freq. used for calibration
61  r_4 rcalibBandFreq_;    //float version
62  int maxNumberCycles_;//maximum number of cycles to be treated
[544]63} para;
64//--------------------------------------------------------------
65//Utility functions
[552]66
[562]67sa_size_t freqToChan(r_4 f){
[553]68  return (sa_size_t)((f-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*NUMBER_OF_FREQ);
69}
[562]70//--------
71//COMPUTE the mean value on a freq. range for all channels
72//--------
73void meanInRange(const TMatrix<r_4> mtx, 
74                 sa_size_t chLow,
75                 sa_size_t chHigh, 
76                 TVector<r_4>& vec){
[563]77 
[562]78  sa_size_t nr = mtx.NRows();
[563]79 
[562]80  for (sa_size_t ir=0; ir<nr; ir++){
81    TVector<r_4> tmp(mtx(Range(ir),Range(chLow,chHigh)),false);
82    double mean, sigma;
83    MeanSigma(tmp,mean,sigma);
84    vec(ir) = mean;
85  }
86}
[553]87//---------
[561]88class median_of_empty_list_exception:public std::exception{
[552]89    virtual const char* what() const throw() {
90    return "Attempt to take the median of an empty list of numbers.  "
91      "The median of an empty list is undefined.";
92  }
[595]93};
94template<class RandAccessIter>
95double median(RandAccessIter begin, RandAccessIter end) 
96  throw(median_of_empty_list_exception){
97  if(begin == end){ throw median_of_empty_list_exception(); }
98  std::size_t size = end - begin;
99  std::size_t middleIdx = size/2;
100  RandAccessIter target = begin + middleIdx;
101  std::nth_element(begin, target, end);
[552]102   
[595]103  if(size % 2 != 0){ //Odd number of elements
104    return *target;
105  }else{            //Even number of elements
106    double a = *target;
107    RandAccessIter targetNeighbor= target-1;
108    std::nth_element(begin, targetNeighbor, end);
109    return (a+*targetNeighbor)/2.0;
[552]110  }
[595]111}
[552]112
[561]113//-------------
[591]114//JEC 25/10/11 Perform a median filtering with a sliding window of "halfwidth" half width
115//             It takes care of the edges and is based on the median function (above)
116void medianFiltering(const TMatrix<r_4> mtx, 
117                     sa_size_t halfwidth,
118                     TMatrix<r_4>& vec) {
119 
120  sa_size_t nr = mtx.NRows();
121  sa_size_t nc = mtx.NCols();
122  sa_size_t chMin = 0;
123  sa_size_t chMax = nc-1;
124 
125  for (sa_size_t ir=0; ir<nr; ir++){
126    for (sa_size_t ic=0; ic<nc; ic++) {
127      sa_size_t chLow = ic-halfwidth;
128      chLow = (chLow >= chMin) ? chLow : chMin;
129      sa_size_t chHigh = ic+halfwidth;
130      chHigh = ( chHigh <= chMax ) ? chHigh : chMax;
131      TVector<r_4> tmp(mtx(Range(ir),Range(chLow,chHigh)),false);
132      vector<r_4> val;
133      tmp.FillTo(val);
134      vec(ir,ic) = median(val.begin(),val.end());
135    }
136  }
137}
138//-------------
[561]139void split(const string& str, const string& delimiters , vector<string>& tokens) {
140    // Skip delimiters at beginning.
141    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
142    // Find first "non-delimiter".
143    string::size_type pos     = str.find_first_of(delimiters, lastPos);
144
145    while (string::npos != pos || string::npos != lastPos)
146    {
147        // Found a token, add it to the vector.
148        tokens.push_back(str.substr(lastPos, pos - lastPos));
149        // Skip delimiters.  Note the "not_of"
150        lastPos = str.find_first_not_of(delimiters, pos);
151        // Find next "non-delimiter"
152        pos = str.find_first_of(delimiters, lastPos);
153    }
154}
[544]155//--------------------------------------------------------------
[540]156char *regexp (const char *string, const char *patrn, int *begin, int *end) {   
157        int i, w=0, len;                 
158        char *word = NULL;
159        regex_t rgT;
160        regmatch_t match;
161        regcomp(&rgT,patrn,REG_EXTENDED);
162        if ((regexec(&rgT,string,1,&match,0)) == 0) {
163                *begin = (int)match.rm_so;
164                *end = (int)match.rm_eo;
165                len = *end-*begin;
166                word=(char*)malloc(len+1);
167                for (i=*begin; i<*end; i++) {
168                        word[w] = string[i];
169                        w++; }
170                word[w]=0;
171        }
172        regfree(&rgT);
173        return word;
174}
[544]175//-------
[540]176sa_size_t round_sa(r_4 r) {
177  return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
178}
179//-----
180string StringToLower(string strToConvert){
181  //change each element of the string to lower case
182  for(unsigned int i=0;i<strToConvert.length();i++) {
183    strToConvert[i] = tolower(strToConvert[i]);
184  }
185  return strToConvert;//return the converted string
186}
187//-----
188bool stringCompare( const string &left, const string &right ){
189   if( left.size() < right.size() )
190      return true;
191   for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
192      if( tolower( *lit ) < tolower( *rit ) )
193         return true;
194      else if( tolower( *lit ) > tolower( *rit ) )
195         return false;
196   return false;
197}
198//-----
199list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
200  list<string> theList;
201
202
203  DIR *dip;
204  struct dirent *dit;
205  string msg;  string fileName;
206  string fullFileName;
207  size_t found;
208
209  if ((dip=opendir(dir.c_str())) == NULL ) {
210    msg = "opendir failed on directory "+dir;
211    throw msg;
212  }
213  while ( (dit = readdir(dip)) != NULL ) {
214    fileName = dit->d_name;
215    found=fileName.find(filePettern);
216    if (found != string::npos) {
217      fullFileName = dir + "/";
218      fullFileName += fileName;
219      theList.push_back(fullFileName);
220    }
221  }//eo while
222  if (closedir(dip) == -1) {
223    msg = "closedir failed on directory "+dir;
224    throw msg;
225  }
226 
227  theList.sort(stringCompare);
228
229  return theList;
230
231}
232//
233class  StringMatch : public unary_function<string,bool> {
234public:
235  StringMatch(const string& pattern): pattern_(pattern){}
236  bool operator()(const string& aStr) const {
237
238
239    int b,e;
240    regexp(aStr.c_str(),pattern_.c_str(),&b,&e);
241
[541]242//     cout << "investigate " << aStr << " to find " << pattern_
243//       << "[" <<b<<","<<e<<"]"
244//       << endl;
[540]245
246   
247    if (b != 0) return false;
[544]248    if (e != (int)aStr.size()) return false;
[540]249    return true;
250
251  }
252private:
253  string pattern_;
254};
255//-------------------------------------------------------
[544]256//Rebin in frequence + compute mean and sigma
257void reduceSpectra(const TMatrix<r_4>& specMtxInPut, 
258                    TMatrix<r_4>& meanMtx, 
259                    TMatrix<r_4>& sigmaMtx) {
260  sa_size_t nSliceFreq = para.nSliceInFreq_;
261  sa_size_t deltaFreq =  NUMBER_OF_FREQ/nSliceFreq;
262  for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
263    sa_size_t freqLow= iSlice*deltaFreq;
264    sa_size_t freqHigh= freqLow + deltaFreq -1;
265    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
266      TVector<r_4> reducedRow;
267      reducedRow = specMtxInPut.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
268      double mean; 
269      double sigma;
270      MeanSigma(reducedRow,mean,sigma);
271      meanMtx(iCh,iSlice) = mean;
272      sigmaMtx(iCh,iSlice) = sigma;
273    }//eo loop on channels
274  }//eo loop on slices
275}
[541]276//-------------------------------------------------------
[560]277//Compute the mean of Diff ON-OFF BAO-calibrated spectra and also the mean/sigma of rebinned spectra
278//
[587]279void meanCalibBAODiffOnOffCycles() throw(string) {
[560]280
281  list<string> listOfFiles;
282  string directoryName;
283  directoryName = para.inPath_ + "/" + para.sourceName_;
284
285  //Make the listing of the directory
286  listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
287 
[561]288  list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
[560]289  iFileEnd = listOfFiles.end();
[561]290
[562]291  //mean ON-OFF over the list of cycles
[591]292  TMatrix<r_4> meanDiffONOFFovOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);          //set to 0
[562]293  TMatrix<r_4> meanDiffONOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);          //set to 0
294  TMatrix<r_4> meanDiffONOFF_perRunCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);      //set to 0
295  TMatrix<r_4> meanDiffONOFF_perCycleCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);    //set to 0
[591]296  static const int NINFO=21;
[577]297  char* onffTupleName[NINFO]={"cycle"
[591]298                              ,"onoffRaw0","onoffRaw1"
299                              ,"onoffRun0","onoffRun1"
300                              ,"onoffCycle0","onoffCycle1"
301                              ,"onoffRaw01420","onoffRaw11420"
302                              ,"onoffRun01420","onoffRun11420"
303                              ,"onoffCycle01420","onoffCycle11420"
304                              ,"onoffRaw01420side","onoffRaw11420side"
305                              ,"onoffRun01420side","onoffRun11420side"
306                              ,"onoffCycle01420side","onoffCycle11420side"
307                              ,"onoffRaw0f14001420","onoffRaw1f14001420"
[577]308  };
309  NTuple onoffevolution(NINFO,onffTupleName);
310  r_4 xnt[NINFO];
[562]311
[577]312  //Lower and Higher freq. arround the Calibration Freq. bin to perform mean follow up
313  sa_size_t chCalibLow  = freqToChan(para.rcalibFreq_ - (para.rcalibBandFreq_*0.5));
314  sa_size_t chCalibHigh = freqToChan(para.rcalibFreq_ + (para.rcalibBandFreq_*0.5));
315  //Lower and Higher freq.  just arround 1420.4MHz Freq. bin to perform mean follow up
[582]316  sa_size_t ch1420Low  = freqToChan(1420.4-0.2);
317  sa_size_t ch1420High = freqToChan(1420.4+0.2);
[577]318
319  //Lower and Higher freq. on the sides of 1420.4Mhz Freq. bin to perform mean follow up
320  sa_size_t ch1420aLow  = freqToChan(1418);
321  sa_size_t ch1420aHigh = freqToChan(1419);
322  sa_size_t ch1420bLow  = freqToChan(1422);
323  sa_size_t ch1420bHigh = freqToChan(1423);
[591]324 
325  //25/10/11 follow 1400-1420Mhz
326  sa_size_t ch1420 = freqToChan(1420);
327  sa_size_t ch1400 = freqToChan(1400);
[577]328
[582]329  if (para.debuglev_>0){
[577]330    cout << "freq. band for follow up [" <<  chCalibLow << ", "<< chCalibHigh << "]" << endl;
331    cout << "freq. band for follow up [" <<  ch1420Low << ", "<< ch1420High << "]" << endl;
[582]332    cout << "freq. band for follow up [" <<  ch1420aLow << ", "<< ch1420aHigh << "]" << endl;
333    cout << "freq. band for follow up [" <<  ch1420bLow << ", "<< ch1420bHigh << "]" << endl;
[563]334  }
[562]335 
[561]336  //Loop on files/run
[562]337
338  int totalNumberCycles=0; //total number of cycles for normalisation
[560]339  for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
340    if (para.debuglev_>90){
341      cout << "load file <" << *iFile << ">" << endl;
342    }
[561]343
344    vector<string> tokens;
345    split(*iFile,"_",tokens);
346    string dateOfRun = tokens[1];
347    if (para.debuglev_>90){
348      cout << "date <" << dateOfRun << ">" << endl;
349    }
350    vector<string> tokens2;
351    split(tokens[2],".",tokens2);
352    string srcLower = tokens2[0];
353
354
355   
[560]356    PInPersist fin(*iFile);
357    vector<string> vec = fin.GetNameTags();
358
[561]359    vector<string> modeList;
360    modeList.push_back("On");
361    modeList.push_back("Off");
362    vector<string>::const_iterator iMode;
363   
364    map<string, list<int> > cycleModeCollect;
365   
366    for (iMode = modeList.begin(); iMode!=modeList.end(); ++iMode) {
367      list<string> listOfSpectra;
[560]368      //Keep only required PPF objects
[561]369      string matchstr = "specRaw"+(*iMode)+"[0-9]+"; 
[560]370      std::remove_copy_if(
371                          vec.begin(), vec.end(), back_inserter(listOfSpectra),
[561]372                          not1(StringMatch(matchstr))
[560]373                          );
374     
[561]375      listOfSpectra.sort(stringCompare);
376      iSpecEnd = listOfSpectra.end();
[560]377     
[561]378      matchstr = "[0-9]+";
[560]379      //Loop of spectra matrix
[561]380      list<int> listOfCycles;
381      for (iSpec = listOfSpectra.begin(); iSpec!=iSpecEnd;  ++iSpec){
382        int b,e;
383        regexp(iSpec->c_str(),matchstr.c_str(),&b,&e);
[560]384        if (para.debuglev_>90){
[561]385          cout << " spactra <" << *iSpec << ">" << endl;
386          cout << " cycle " << iSpec->substr(b) << endl;
[560]387        }
[561]388        listOfCycles.push_back(atoi((iSpec->substr(b)).c_str()));
389      }//end loop spectra
390      cycleModeCollect[*iMode] = listOfCycles;
391    }//end of mode   
392
393    //Take the Intersection of the list Of cycles in mode Off and On   
394    list<int> commonCycles;
395    set_intersection(cycleModeCollect["On"].begin(),
396                     cycleModeCollect["On"].end(),
397                     cycleModeCollect["Off"].begin(),
398                     cycleModeCollect["Off"].end(),
399                     back_inserter(commonCycles)
400                     );
401   
402    if (para.debuglev_>90){
403      cout << "Liste of cycles common to On & Off: <";
404      for (list<int>::iterator i=commonCycles.begin(); i!=commonCycles.end(); ++i){
405        cout << *i << " ";
406      }
407      cout << ">" << endl;
408    }
409   
410    //
411    //Load BAO Calibration factors "per Cycle and Channels"
412    //Compute the mean per Cycle to
413    //  fill factors "per Run and Channels" with the same cycle list
414    //
415    //
416    //TODO improve the code....
417
418    TMatrix<r_4> calibBAOfactors_Off_Cycle_Ch0;
419    TMatrix<r_4> calibBAOfactors_Off_Cycle_Ch1;
420    TMatrix<r_4> calibBAOfactors_On_Cycle_Ch0;
421    TMatrix<r_4> calibBAOfactors_On_Cycle_Ch1;
422   
423    string calibFileName;
424    ifstream ifs;
425    sa_size_t nr,nc; //values read
426
427    //OFF Cycle per Channel
428    calibFileName = directoryName + "/" 
429      + "calib_" + dateOfRun + "_" + srcLower + "_Off_" 
430      + para.calibFreq_ +"MHz-Ch0Cycles.txt";
431    if(para.debuglev_>0) cout << "Read Calib file " << calibFileName << endl;
432    ifs.open(calibFileName.c_str());
433    if ( ! ifs.is_open() ) {
434
435      throw calibFileName + " cannot be opened...";
436    }   
437    calibBAOfactors_Off_Cycle_Ch0.ReadASCII(ifs,nr,nc);
438    if(para.debuglev_>9){
439      cout << "(nr,nc): "<< nr << "," << nc << endl;
440      calibBAOfactors_Off_Cycle_Ch0.Print(cout);
[562]441      cout << endl;
[561]442    }
443
444    TMatrix<r_4> calibBAOfactors_Off_Run_Ch0(nr,nc);
445    calibBAOfactors_Off_Run_Ch0.Column(0) = calibBAOfactors_Off_Cycle_Ch0.Column(0);
446    {//Compute the mean
447      TVector<r_4> coef(calibBAOfactors_Off_Cycle_Ch0(Range::all(),Range::last()),false);
448      double mean,sigma;
449      MeanSigma(coef,mean,sigma);
450      calibBAOfactors_Off_Run_Ch0.Column(1).SetCst(mean);
451    }
452    if(para.debuglev_>9){
453      cout << "Fill calib. with mean value " << endl; 
454      calibBAOfactors_Off_Run_Ch0.Print(cout);
[562]455      cout << endl;
[561]456    }
457    ifs.close();
458
459    //
460    calibFileName = directoryName + "/" 
461      + "calib_" + dateOfRun + "_" + srcLower + "_Off_" 
462      + para.calibFreq_ +"MHz-Ch1Cycles.txt";
463    if(para.debuglev_>0) cout << "Read Calib file " << calibFileName << endl;
464    ifs.open(calibFileName.c_str());
465    if ( ! ifs.is_open() ) {
466
467      throw calibFileName + " cannot be opened...";
468    }   
469    calibBAOfactors_Off_Cycle_Ch1.ReadASCII(ifs,nr,nc);
470    if(para.debuglev_>9){
471      cout << "(nr,nc): "<< nr << "," << nc << endl;
472      calibBAOfactors_Off_Cycle_Ch1.Print(cout);
[562]473      cout << endl;
[561]474    }
475    TMatrix<r_4> calibBAOfactors_Off_Run_Ch1(nr,nc);
476    calibBAOfactors_Off_Run_Ch1.Column(0) = calibBAOfactors_Off_Cycle_Ch1.Column(0);
477    {//Compute the mean
478      TVector<r_4> coef(calibBAOfactors_Off_Cycle_Ch1(Range::all(),Range::last()),false);
479      double mean,sigma;
480      MeanSigma(coef,mean,sigma);
[563]481      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
[561]482      calibBAOfactors_Off_Run_Ch1.Column(1).SetCst(mean);
483    }
484    if(para.debuglev_>9){
485      cout << "Fill calib. with mean value " << endl; 
486      calibBAOfactors_Off_Run_Ch1.Print(cout);
[562]487      cout << endl;
[561]488    }
489    ifs.close();
490
491    //ON Cycle per Channel
492    calibFileName = directoryName + "/" 
493      + "calib_" + dateOfRun + "_" + srcLower + "_On_" 
494      + para.calibFreq_ +"MHz-Ch0Cycles.txt";
495    if(para.debuglev_>0) cout << "Read Calib file " << calibFileName << endl;
496    ifs.open(calibFileName.c_str());
497    if ( ! ifs.is_open() ) {
498
499      throw calibFileName + " cannot be opened...";
500    }   
501    calibBAOfactors_On_Cycle_Ch0.ReadASCII(ifs,nr,nc);
502    if(para.debuglev_>9){
503      cout << "(nr,nc): "<< nr << "," << nc << endl;
504      calibBAOfactors_On_Cycle_Ch0.Print(cout);
[562]505      cout << endl;     
[561]506    }
507
508    TMatrix<r_4> calibBAOfactors_On_Run_Ch0(nr,nc);
509    calibBAOfactors_On_Run_Ch0.Column(0) = calibBAOfactors_On_Cycle_Ch0.Column(0);
510    {//Compute the mean
511      TVector<r_4> coef(calibBAOfactors_On_Cycle_Ch0(Range::all(),Range::last()),false);
512      double mean,sigma;
513      MeanSigma(coef,mean,sigma);
[563]514      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
[561]515      calibBAOfactors_On_Run_Ch0.Column(1).SetCst(mean);
516    }
517    if(para.debuglev_>9){
518      cout << "Fill calib. with mean value " << endl; 
519      calibBAOfactors_On_Run_Ch0.Print(cout);
[562]520      cout << endl;
[561]521    }
522    ifs.close();
523
524   
525    calibFileName = directoryName + "/" 
526      + "calib_" + dateOfRun + "_" + srcLower + "_On_" 
527      + para.calibFreq_ +"MHz-Ch1Cycles.txt";
528    if(para.debuglev_>0) cout << "Read Calib file " << calibFileName << endl;
529    ifs.open(calibFileName.c_str());
530    if ( ! ifs.is_open() ) {
531      throw calibFileName + " cannot be opened...";
532    }   
533    calibBAOfactors_On_Cycle_Ch1.ReadASCII(ifs,nr,nc);
534    if(para.debuglev_>9){
535      cout << "(nr,nc): "<< nr << "," << nc << endl;
536      calibBAOfactors_On_Cycle_Ch1.Print(cout);
[562]537      cout << endl;
[561]538    }
539    TMatrix<r_4> calibBAOfactors_On_Run_Ch1(nr,nc);
540    calibBAOfactors_On_Run_Ch1.Column(0) = calibBAOfactors_On_Cycle_Ch1.Column(0);
541    {//Compute the mean
542      TVector<r_4> coef(calibBAOfactors_On_Cycle_Ch1(Range::all(),Range::last()),false);
543      double mean,sigma;
544      MeanSigma(coef,mean,sigma);
[563]545      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
[561]546      calibBAOfactors_On_Run_Ch1.Column(1).SetCst(mean);
547    }
548    if(para.debuglev_>9){
549      cout << "Fill calib. with mean value " << endl; 
550      calibBAOfactors_On_Run_Ch1.Print(cout);
[562]551      cout << endl;
[561]552    }
553
554    ifs.close();
555   
556    //link <cycle> - <calibration coefficient>
[562]557    //We cannot rely on identical cycle list of the OFF and ON calibration
558    map<int,r_4> calibBAO_Off_Run_Ch0;
559    map<int,r_4> calibBAO_Off_Run_Ch1;
560    map<int,r_4> calibBAO_On_Run_Ch0;
561    map<int,r_4> calibBAO_On_Run_Ch1;
[561]562
[562]563    map<int,r_4> calibBAO_Off_Cycle_Ch0;
564    map<int,r_4> calibBAO_Off_Cycle_Ch1;
565    map<int,r_4> calibBAO_On_Cycle_Ch0;
566    map<int,r_4> calibBAO_On_Cycle_Ch1;
[560]567
[562]568    //per Run based BAO coefficients
569    nr = calibBAOfactors_Off_Run_Ch0.NRows();
570    for (sa_size_t ir=0; ir<nr; ++ir){
[563]571//       cout << "Calib. Off Run Ch0 cycle ["<< calibBAOfactors_Off_Run_Ch0(ir,0)<<"], val "
572//         << calibBAOfactors_Off_Run_Ch0(ir,1) << endl;
573
[562]574      calibBAO_Off_Run_Ch0[(int)calibBAOfactors_Off_Run_Ch0(ir,0)]
575        = calibBAOfactors_Off_Run_Ch0(ir,1);
576      calibBAO_Off_Cycle_Ch0[(int)calibBAOfactors_Off_Cycle_Ch0(ir,0)]
577        = calibBAOfactors_Off_Cycle_Ch0(ir,1);
578      calibBAO_Off_Run_Ch1[(int)calibBAOfactors_Off_Run_Ch1(ir,0)]
579        = calibBAOfactors_Off_Run_Ch1(ir,1);
580      calibBAO_Off_Cycle_Ch1[(int)calibBAOfactors_Off_Cycle_Ch1(ir,0)]
581        = calibBAOfactors_Off_Cycle_Ch1(ir,1);
[563]582    }//eo loop on coef Off
[562]583   
584    nr = calibBAOfactors_On_Run_Ch0.NRows();
585    for (sa_size_t ir=0; ir<nr; ++ir){
586      calibBAO_On_Run_Ch0[(int)calibBAOfactors_On_Run_Ch0(ir,0)]
587        = calibBAOfactors_On_Run_Ch0(ir,1);
588      calibBAO_On_Cycle_Ch0[(int)calibBAOfactors_On_Cycle_Ch0(ir,0)]
589        = calibBAOfactors_On_Cycle_Ch0(ir,1);
590      calibBAO_On_Run_Ch1[(int)calibBAOfactors_On_Run_Ch1(ir,0)]
591        = calibBAOfactors_On_Run_Ch1(ir,1);
592      calibBAO_On_Cycle_Ch1[(int)calibBAOfactors_On_Cycle_Ch1(ir,0)]
593        = calibBAOfactors_On_Cycle_Ch1(ir,1);
[563]594    }//eo loop on coef On
[562]595     
[561]596    //Loop on cyles
597    for (list<int>::iterator ic=commonCycles.begin(); ic!=commonCycles.end(); ++ic){
[562]598
[563]599      //Look if the cycle has been calibrated...
600      bool isCycleCalibrated = 
601        ( calibBAO_On_Run_Ch0.count(*ic)    *
602          calibBAO_On_Run_Ch1.count(*ic)    *
603          calibBAO_Off_Run_Ch0.count(*ic)   *
604          calibBAO_Off_Run_Ch1.count(*ic)   *
605          calibBAO_On_Cycle_Ch0.count(*ic)  *
606          calibBAO_On_Cycle_Ch1.count(*ic)  *
607          calibBAO_Off_Cycle_Ch0.count(*ic) *
608          calibBAO_Off_Cycle_Ch1.count(*ic) ) != 0 ? true : false;
609
610      if(para.debuglev_>9) {
[562]611        cout << "Calibration coefficients for cycle "<<*ic << endl; 
[563]612        if (isCycleCalibrated) {
613          cout << "Cycle calibrated " << endl;
614          cout << "Off Run Ch0 " << calibBAO_Off_Run_Ch0[*ic] << " "
615               << "Ch1 " << calibBAO_Off_Run_Ch1[*ic] << "\n"
616               << "On Run Ch0 " << calibBAO_On_Run_Ch0[*ic] << " "
617               << "Ch1 " << calibBAO_On_Run_Ch1[*ic] << "\n"
618               << "Off Cycle Ch0 " << calibBAO_Off_Cycle_Ch0[*ic] << " "
619               << "Ch1 " << calibBAO_Off_Cycle_Ch1[*ic] << "\n"
620               << "On Cycle Ch0 " << calibBAO_On_Cycle_Ch0[*ic] << " "
621               << "Ch1 " << calibBAO_On_Cycle_Ch1[*ic] << endl;
622        } else {
623          cout << "Cycle NOT calibrated " << endl;
624        }
625      }//debug
626
627
628      if ( ! isCycleCalibrated ) continue;
[562]629     
[561]630      string ppftag;
631      //load ON phase
632      stringstream cycle;
633      cycle << *ic;
634     
635      ppftag = "specRawOn"+cycle.str();
[562]636      TMatrix<r_4> aSpecOn(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
637      fin.GetObject(aSpecOn,ppftag);
[561]638
[562]639      TMatrix<r_4> aSpecOn_BAOCalibRun(aSpecOn,false);
640      aSpecOn_BAOCalibRun(Range(0),Range::all()) /= calibBAO_On_Run_Ch0[*ic];
641      aSpecOn_BAOCalibRun(Range(1),Range::all()) /= calibBAO_On_Run_Ch1[*ic];
642
643      TMatrix<r_4> aSpecOn_BAOCalibCycle(aSpecOn,false);
644      aSpecOn_BAOCalibCycle(Range(0),Range::all()) /= calibBAO_On_Cycle_Ch0[*ic];
645      aSpecOn_BAOCalibCycle(Range(1),Range::all()) /= calibBAO_On_Cycle_Ch1[*ic];
[561]646     
[602]647      //Load OFF phase
[562]648      ppftag = "specRawOff"+cycle.str();
649      TMatrix<r_4> aSpecOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
650      fin.GetObject(aSpecOff,ppftag);
[561]651
[562]652      TMatrix<r_4> aSpecOff_BAOCalibRun(aSpecOff,false);
653      aSpecOff_BAOCalibRun(Range(0),Range::all()) /= calibBAO_Off_Run_Ch0[*ic];
654      aSpecOff_BAOCalibRun(Range(1),Range::all()) /= calibBAO_Off_Run_Ch1[*ic];
[561]655
[562]656      TMatrix<r_4> aSpecOff_BAOCalibCycle(aSpecOff,false);
657      aSpecOff_BAOCalibCycle(Range(0),Range::all()) /= calibBAO_Off_Cycle_Ch0[*ic];
658      aSpecOff_BAOCalibCycle(Range(1),Range::all()) /= calibBAO_Off_Cycle_Ch1[*ic];
[561]659
[562]660
661      //Perform the difference ON-OFF with the different calibration options
662      TMatrix<r_4> diffOnOff_noCalib = aSpecOn - aSpecOff;
663      meanDiffONOFF_noCalib += diffOnOff_noCalib;
[561]664     
[591]665      //JEC 29/10/11 add ON-OFF/OFF
666      TMatrix<r_4> diffOnOffOvOff_noCalib(diffOnOff_noCalib,false); //do not share data
667      TMatrix<r_4> aSpecOffFitltered(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
668      sa_size_t halfWidth = 35; //number of freq. bin for the 1/2 width of the filtering window
669      medianFiltering(aSpecOff,halfWidth,aSpecOffFitltered);
670     
671      diffOnOffOvOff_noCalib.Div(aSpecOffFitltered); //division in place
672      meanDiffONOFFovOFF_noCalib += diffOnOffOvOff_noCalib;
673
[562]674      TMatrix<r_4> diffOnOff_perRunCalib = aSpecOn_BAOCalibRun - aSpecOff_BAOCalibRun;
675      meanDiffONOFF_perRunCalib += diffOnOff_perRunCalib;
676
677      TMatrix<r_4> diffOnOff_perCycleCalib = aSpecOn_BAOCalibCycle - aSpecOff_BAOCalibCycle;
678      meanDiffONOFF_perCycleCalib += diffOnOff_perCycleCalib;
679
[563]680      //
681      totalNumberCycles++;
[562]682      //Fill NTuple
[563]683      xnt[0] = totalNumberCycles;
[577]684 
685      //Follow up arround the Calibration Freq.
686      TVector<r_4> meanInRange_CalibFreq_noCalib(NUMBER_OF_CHANNELS);
687      meanInRange(diffOnOff_noCalib,chCalibLow,chCalibHigh,meanInRange_CalibFreq_noCalib);
688      xnt[1] = meanInRange_CalibFreq_noCalib(0);
689      xnt[2] = meanInRange_CalibFreq_noCalib(1);
[562]690
[577]691      TVector<r_4> meanInRange_CalibFreq_perRunCalib(NUMBER_OF_CHANNELS);
692      meanInRange(diffOnOff_perRunCalib,chCalibLow,chCalibHigh,meanInRange_CalibFreq_perRunCalib);
693      xnt[3] = meanInRange_CalibFreq_perRunCalib(0);
694      xnt[4] = meanInRange_CalibFreq_perRunCalib(1);
[562]695
[577]696      TVector<r_4> meanInRange_CalibFreq_perCycleCalib(NUMBER_OF_CHANNELS);
697      meanInRange(diffOnOff_perCycleCalib,chCalibLow,chCalibHigh,meanInRange_CalibFreq_perCycleCalib);
698      xnt[5] = meanInRange_CalibFreq_perCycleCalib(0);
699      xnt[6] = meanInRange_CalibFreq_perCycleCalib(1);
700
701
702      //Follow up arround the 1420.4MHz Freq.
703      TVector<r_4> meanInRange_1420Freq_noCalib(NUMBER_OF_CHANNELS);
704      meanInRange(diffOnOff_noCalib,ch1420Low,ch1420High,meanInRange_1420Freq_noCalib);
705      xnt[7] = meanInRange_1420Freq_noCalib(0);
706      xnt[8] = meanInRange_1420Freq_noCalib(1);
707
708      TVector<r_4> meanInRange_1420Freq_perRunCalib(NUMBER_OF_CHANNELS);
709      meanInRange(diffOnOff_perRunCalib,ch1420Low,ch1420High,meanInRange_1420Freq_perRunCalib);
710      xnt[9] = meanInRange_1420Freq_perRunCalib(0);
711      xnt[10] = meanInRange_1420Freq_perRunCalib(1);
712
713      TVector<r_4> meanInRange_1420Freq_perCycleCalib(NUMBER_OF_CHANNELS);
714      meanInRange(diffOnOff_perCycleCalib,ch1420Low,ch1420High,meanInRange_1420Freq_perCycleCalib);
715      xnt[11] = meanInRange_1420Freq_perCycleCalib(0);
716      xnt[12] = meanInRange_1420Freq_perCycleCalib(1);
717
718
719      //Follow up below the 1420.4MHz Freq.
720      TVector<r_4> meanInRange_1420aFreq_noCalib(NUMBER_OF_CHANNELS);
721      meanInRange(diffOnOff_noCalib,ch1420aLow,ch1420aHigh,meanInRange_1420aFreq_noCalib);
722      TVector<r_4> meanInRange_1420bFreq_noCalib(NUMBER_OF_CHANNELS);
723      meanInRange(diffOnOff_noCalib,ch1420bLow,ch1420bHigh,meanInRange_1420bFreq_noCalib);
724
725      xnt[13] = (meanInRange_1420aFreq_noCalib(0) + meanInRange_1420bFreq_noCalib(0))/2.;
726      xnt[14] = (meanInRange_1420aFreq_noCalib(1) + meanInRange_1420bFreq_noCalib(1))/2.;
727
728
729      TVector<r_4> meanInRange_1420aFreq_perRun(NUMBER_OF_CHANNELS);
730      meanInRange(diffOnOff_perRunCalib,ch1420aLow,ch1420aHigh,meanInRange_1420aFreq_perRun);
731      TVector<r_4> meanInRange_1420bFreq_perRun(NUMBER_OF_CHANNELS);
732      meanInRange(diffOnOff_perRunCalib,ch1420bLow,ch1420bHigh,meanInRange_1420bFreq_perRun);
733
734      xnt[15] = (meanInRange_1420aFreq_perRun(0) + meanInRange_1420bFreq_perRun(0))/2.;
735      xnt[16] = (meanInRange_1420aFreq_perRun(1) + meanInRange_1420bFreq_perRun(1))/2.;
736
737
738      TVector<r_4> meanInRange_1420aFreq_perCycle(NUMBER_OF_CHANNELS);
739      meanInRange(diffOnOff_perCycleCalib,ch1420aLow,ch1420aHigh,meanInRange_1420aFreq_perCycle);
740      TVector<r_4> meanInRange_1420bFreq_perCycle(NUMBER_OF_CHANNELS);
741      meanInRange(diffOnOff_perCycleCalib,ch1420bLow,ch1420bHigh,meanInRange_1420bFreq_perCycle);
742
743      xnt[17] = (meanInRange_1420aFreq_perCycle(0) + meanInRange_1420bFreq_perCycle(0))/2.;
744      xnt[18] = (meanInRange_1420aFreq_perCycle(1) + meanInRange_1420bFreq_perCycle(1))/2.;
745
[591]746
747      //JEC 25/10/11 follow 1400-1420 MHz bande protege et n'inclue pas le 1420.4Mhz de la Galaxie
748      TVector<r_4> meanInRange_1400a1420Freq_noCalib(NUMBER_OF_CHANNELS);
749      meanInRange(diffOnOff_noCalib,ch1400,ch1420,meanInRange_1400a1420Freq_noCalib);
750      xnt[19] = meanInRange_1400a1420Freq_noCalib(0);
751      xnt[20] = meanInRange_1400a1420Freq_noCalib(1);
[562]752     
[591]753
754     
[577]755      //store infos to Ntuple
[562]756      onoffevolution.Fill(xnt);
757
[563]758      //Quit if enough
[562]759      if (totalNumberCycles >= para.maxNumberCycles_) break;   
760
[561]761    }//eo loop on cycles
[562]762    if (totalNumberCycles >= para.maxNumberCycles_) break;         
763 
764  }//eo loop on spectra in a file
765  cout << "End of jobs: we have treated " << totalNumberCycles << " cycles" << endl;
766  //Normalisation
[563]767  if(totalNumberCycles > 0){
[591]768    //JEC 29/10 add ON-OFF/OFF
769    meanDiffONOFFovOFF_noCalib  /= (r_4)totalNumberCycles;
[562]770    meanDiffONOFF_noCalib       /= (r_4)totalNumberCycles;
771    meanDiffONOFF_perRunCalib   /= (r_4)totalNumberCycles;
772    meanDiffONOFF_perCycleCalib /= (r_4)totalNumberCycles;
773  } 
774 
775  //Compute the reduced version of the mean and sigma
776  TMatrix<r_4> meanRedMtx_noCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
777  TMatrix<r_4> sigmaRedMtx_noCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
778  reduceSpectra(meanDiffONOFF_noCalib,meanRedMtx_noCalib,sigmaRedMtx_noCalib);
[561]779
[562]780  TMatrix<r_4> meanRedMtx_perRunCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
781  TMatrix<r_4> sigmaRedMtx_perRunCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
782  reduceSpectra(meanDiffONOFF_perRunCalib,meanRedMtx_perRunCalib,sigmaRedMtx_perRunCalib);
783
784  TMatrix<r_4> meanRedMtx_perCycleCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
785  TMatrix<r_4> sigmaRedMtx_perCycleCalib(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
786  reduceSpectra(meanDiffONOFF_perCycleCalib,meanRedMtx_perCycleCalib,sigmaRedMtx_perCycleCalib);
787
788  {//save the results
789    stringstream tmp;
790    tmp << totalNumberCycles;
791    string fileName = para.outPath_+"/onoffsurvey_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
[563]792
[562]793    POutPersist fos(fileName);
[563]794    cout << "Save output in " << fileName << endl;
[562]795
796    string tag = "meanNoCalib";
797    fos << PPFNameTag(tag) << meanDiffONOFF_noCalib;
[591]798   
799    //JEC 29/10/11
800    tag = "meanOvOffNoCalib";
801    fos << PPFNameTag(tag) << meanDiffONOFFovOFF_noCalib;
802
[562]803    tag = "meanPerRunCalib";
804    fos << PPFNameTag(tag) << meanDiffONOFF_perRunCalib;
805    tag = "meanPerCycleCalib";
806    fos << PPFNameTag(tag) << meanDiffONOFF_perCycleCalib;
807
808    tag = "redmeanNoCalib";
809    fos << PPFNameTag(tag) << meanRedMtx_noCalib;
810    tag = "redsigmaNoCalib";
811    fos << PPFNameTag(tag) << sigmaRedMtx_noCalib;
812
813    tag = "redmeanPerRunCalib";
814    fos << PPFNameTag(tag) << meanRedMtx_perRunCalib;
815    tag = "redsigmaPerRunCalib";
816    fos << PPFNameTag(tag) << sigmaRedMtx_perRunCalib;
817
818    tag = "redmeanPerCycleCalib";
819    fos << PPFNameTag(tag) << meanRedMtx_perCycleCalib;
820    tag = "redsigmaPerCycleCalib";
821    fos << PPFNameTag(tag) << sigmaRedMtx_perCycleCalib;
822   
823    tag = "onoffevol";
824    fos << PPFNameTag(tag) << onoffevolution;   
825  }//end of save
[560]826}
[602]827//JEC 14/11/11 New meanRawDiffOnOffCycles START
[560]828//-------------------------------------------------------
[602]829//Compute the mean of Diff ON-OFF/OFF from Raw spectra
[544]830//Used like:
831//
[560]832void meanRawDiffOnOffCycles() throw(string) {
[540]833  list<string> listOfFiles;
834  string directoryName;
835  directoryName = para.inPath_ + "/" + para.sourceName_;
836
[542]837  //Make the listing of the directory
[541]838  listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
[540]839 
[541]840  list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
[540]841  iFileEnd = listOfFiles.end();
[602]842
843  //mean ON-OFF over the list of cycles
844  TMatrix<r_4> meanDiffONOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);      //(ON-OFF)/GAIN
845  TMatrix<r_4> meanDiffONOFFovOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //(ON-OFF)/Filtered_OFF
[604]846  TMatrix<r_4> meanONovOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); //  ON/Filtered_OFF
847  TMatrix<r_4> meanOFFovOFF_noCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); // OFF/Filtered_OFF
[602]848
849  int totalNumberCycles=0; //total number of cycles
850
[542]851  //Loop on files
[540]852  for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
[542]853    if (para.debuglev_>90){
854    }
[540]855    PInPersist fin(*iFile);
856    vector<string> vec = fin.GetNameTags();
[602]857
858    vector<string> modeList;
859    modeList.push_back("On");
860    modeList.push_back("Off");
861    vector<string>::const_iterator iMode;
862   
863    map<string, list<int> > cycleModeCollect;
864   
865    for (iMode = modeList.begin(); iMode!=modeList.end(); ++iMode) {
866      list<string> listOfSpectra;
867      //Keep only required PPF objects
868      string matchstr = "specRaw"+(*iMode)+"[0-9]+"; 
869      std::remove_copy_if(
870                          vec.begin(), vec.end(), back_inserter(listOfSpectra),
871                          not1(StringMatch(matchstr))
872                          );
873     
874      listOfSpectra.sort(stringCompare);
875      iSpecEnd = listOfSpectra.end();
876     
877      matchstr = "[0-9]+";
878      //Loop of spectra matrix
879      list<int> listOfCycles;
880      for (iSpec = listOfSpectra.begin(); iSpec!=iSpecEnd;  ++iSpec){
881        int b,e;
882        regexp(iSpec->c_str(),matchstr.c_str(),&b,&e);
883        if (para.debuglev_>90){
884          cout << " spactra <" << *iSpec << ">" << endl;
885          cout << " cycle " << iSpec->substr(b) << endl;
886        }
887        listOfCycles.push_back(atoi((iSpec->substr(b)).c_str()));
888      }//end loop spectra
889      cycleModeCollect[*iMode] = listOfCycles;
890    }//end of mode   
891
892    //Take the Intersection of the list Of cycles in mode Off and On   
893    list<int> commonCycles;
894    set_intersection(cycleModeCollect["On"].begin(),
895                     cycleModeCollect["On"].end(),
896                     cycleModeCollect["Off"].begin(),
897                     cycleModeCollect["Off"].end(),
898                     back_inserter(commonCycles)
899                     );
900   
901    if (para.debuglev_>90){
902      cout << "Liste of cycles common to On & Off: <";
903      for (list<int>::iterator i=commonCycles.begin(); i!=commonCycles.end(); ++i){
904        cout << *i << " ";
[542]905      }
[602]906      cout << ">" << endl;
907    }
908
909    //Loop on cyles
910    for (list<int>::iterator ic=commonCycles.begin(); ic!=commonCycles.end(); ++ic){
911     
912      string ppftag;
913      //load ON phase
914      stringstream cycle;
915      cycle << *ic;
916      ppftag = "specRawOn"+cycle.str();
917      TMatrix<r_4> aSpecOn(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
918      fin.GetObject(aSpecOn,ppftag);
919
920      //Load OFF phase
921      ppftag = "specRawOff"+cycle.str();
922      TMatrix<r_4> aSpecOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
923      fin.GetObject(aSpecOff,ppftag);
924     
925      //Perform the difference ON-OFF
926      TMatrix<r_4> diffOnOff_noCalib = aSpecOn - aSpecOff;
927      meanDiffONOFF_noCalib += diffOnOff_noCalib;
928     
929      //JEC 29/10/11 add ON-OFF/OFF
930      TMatrix<r_4> diffOnOffOvOff_noCalib(diffOnOff_noCalib,false); //do not share data
931      TMatrix<r_4> aSpecOffFitltered(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
932      sa_size_t halfWidth = 35; //number of freq. bin for the 1/2 width of the filtering window
933      medianFiltering(aSpecOff,halfWidth,aSpecOffFitltered);
934     
935      diffOnOffOvOff_noCalib.Div(aSpecOffFitltered); //division in place
936      meanDiffONOFFovOFF_noCalib += diffOnOffOvOff_noCalib;
937
[604]938
939      //JEC 15/11/11 add ON/OFF and OFF/OFF
940      TMatrix<r_4> onOvOff(aSpecOn,false);
941      onOvOff.Div(aSpecOffFitltered);
942      meanONovOFF_noCalib += onOvOff;
943     
944      TMatrix<r_4> offOvOff(aSpecOff,false);
945      offOvOff.Div(aSpecOffFitltered);
946      meanOFFovOFF_noCalib += offOvOff;
947
[602]948      totalNumberCycles++;
949
950      //Quit if enough
951      if (totalNumberCycles >= para.maxNumberCycles_) break;   
952    }//end of cycles
953
954    if (totalNumberCycles >= para.maxNumberCycles_) break;         
955
956  }//end files
957  cout << "End of jobs: we have treated " << totalNumberCycles << " cycles" << endl;
[542]958  //Normalisation
[602]959  if(totalNumberCycles > 0){
960    meanDiffONOFFovOFF_noCalib  /= (r_4)totalNumberCycles;
961    meanDiffONOFF_noCalib       /= (r_4)totalNumberCycles;
[604]962    meanONovOFF_noCalib         /= (r_4)totalNumberCycles;
963    meanOFFovOFF_noCalib        /= (r_4)totalNumberCycles;
[602]964  } 
[541]965
[602]966  {//save results
967    stringstream tmp;
968    tmp << totalNumberCycles;
969    string fileName = para.outPath_+"/rawOnOffDiff_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
[544]970
971    POutPersist fos(fileName);
[602]972    cout << "Save output in " << fileName << endl;
[544]973
[602]974    string tag = "meanNoCalib";
975    fos << PPFNameTag(tag) << meanDiffONOFF_noCalib;
976   
977    tag = "meanOvOffNoCalib";
978    fos << PPFNameTag(tag) << meanDiffONOFFovOFF_noCalib;
[604]979   
980    tag = "meanOnovOffNoCalib";
981    fos << PPFNameTag(tag) << meanONovOFF_noCalib;
982
983    tag = "meanOffovOffNoCalib";
984    fos << PPFNameTag(tag) << meanOFFovOFF_noCalib;
985
[602]986  }//end save
[544]987}
[602]988//JEC 14/11/11 New meanRawDiffOnOffCycles END
[544]989//-------------------------------------------------------
[602]990//JEC 14/11/11 Obsolete START
991//-------------------------------------------------------
992//Compute the mean of Diff ON-OFF Raw spectra and also the mean/sigma of rebinned spectra
993//Used like:
994//
995// void meanRawDiffOnOffCycles() throw(string) {
996//   list<string> listOfFiles;
997//   string directoryName;
998//   directoryName = para.inPath_ + "/" + para.sourceName_;
999
1000//   //Make the listing of the directory
1001//   listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
1002 
1003//   list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
1004//   iFileEnd = listOfFiles.end();
1005 
1006//   StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects
1007//   TMatrix<r_4> meanOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1008//   uint_4 nSpectra=0;
1009//   //Loop on files
1010//   for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
1011//     if (para.debuglev_>90){
1012//       cout << "load file <" << *iFile << ">" << endl;
1013//     }
1014//     PInPersist fin(*iFile);
1015//     vector<string> vec = fin.GetNameTags();
1016//     list<string> listOfSpectra;
1017//     //Keep only required PPF objects
1018//     std::remove_copy_if(
1019//                      vec.begin(), vec.end(), back_inserter(listOfSpectra),
1020//                      not1(match)
1021//                      );
1022   
1023//     listOfSpectra.sort(stringCompare);
1024//     iSpecEnd = listOfSpectra.end();
1025//     //Loop of spectra matrix
1026//     for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd;  ++iSpec){
1027//       if (para.debuglev_>90){
1028//      cout << " spactra <" << *iSpec << ">" << endl;
1029//       }
1030//       TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1031//       fin.GetObject(aSpec,*iSpec);
1032//       //How to see if the GetObject is ok?? Ask Reza
1033//       nSpectra++;
1034//       meanOfSpectra+=aSpec;
1035//     }//eo loop on spectra in a file
1036//   }//eo loop on files
1037 
1038//   //Normalisation
1039//   if(nSpectra>0)meanOfSpectra/=(r_4)(nSpectra);
1040
1041//   //Compute the reduced version of the mean and sigma
1042//   TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
1043//   TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
1044//   reduceSpectra(meanOfSpectra,meanRedMtx,sigmaRedMtx);
1045
1046//   {//Save the result
1047//     stringstream tmp;
1048//     tmp << nSpectra;
1049//     string fileName = para.outPath_+"/meanDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
1050//     cout << "Save mean based on " <<  nSpectra << " cycles " << endl;
1051//     POutPersist fos(fileName);
1052
1053//     string tag = "mean";
1054//     fos << PPFNameTag(tag) << meanOfSpectra;
1055//     tag = "meanred";
1056//     fos << PPFNameTag(tag) << meanRedMtx;
1057//     tag = "sigmared";
1058//     fos << PPFNameTag(tag) << sigmaRedMtx;
1059//   }
1060// }
1061//JEC 14/11/11 Obsolete END
1062//-------------------------------------------------------
[560]1063//Compute the median of Diff ON-OFF Raw spectra and also the mean/sigma of rebinned spectra
[544]1064//Used like:
1065//
[560]1066void medianRawDiffOnOffCycles() throw(string) {
[544]1067  list<string> listOfFiles;
1068  string directoryName;
1069  directoryName = para.inPath_ + "/" + para.sourceName_;
1070
1071  //Make the listing of the directory
1072  listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
1073 
1074  list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
1075  iFileEnd = listOfFiles.end();
1076 
1077
[562]1078  TArray<r_4> tableOfSpectra(NUMBER_OF_FREQ,NUMBER_OF_CHANNELS,para.maxNumberCycles_); //para.maxNumberCycles_ should be large enough...
[544]1079
1080  StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects
1081  uint_4 nSpectra=0;
1082  //Loop on files
1083  for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
1084    if (para.debuglev_>90){
1085      cout << "load file <" << *iFile << ">" << endl;
1086    }
1087    PInPersist fin(*iFile);
1088    vector<string> vec = fin.GetNameTags();
1089    list<string> listOfSpectra;
1090    //Keep only required PPF objects
1091    std::remove_copy_if(
1092                        vec.begin(), vec.end(), back_inserter(listOfSpectra),
1093                        not1(match)
1094                        );
1095   
[560]1096    listOfSpectra.sort(stringCompare);
[544]1097    iSpecEnd = listOfSpectra.end();
1098    //Loop of spectra matrix
[562]1099    for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd && (sa_size_t)nSpectra < para.maxNumberCycles_ ;  ++iSpec){
[544]1100      if (para.debuglev_>90){
1101        cout << " spactra <" << *iSpec << ">" << endl;
1102      }
1103      TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1104      fin.GetObject(aSpec,*iSpec);
1105
[553]1106      tableOfSpectra(Range::all(),Range::all(),Range(nSpectra)) = aSpec;
1107
[544]1108      nSpectra++;
1109    }//eo loop on spectra in a file
1110  }//eo loop on files
1111 
1112
1113 
1114  TMatrix<r_4> medianOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
1115  //Compute the median for each freq. and Channel
1116  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
1117    for (sa_size_t freq =0; freq<NUMBER_OF_FREQ; freq++){
[552]1118      TVector<r_4> tmp0(tableOfSpectra(Range(freq),Range(iCh),Range(0,nSpectra-1)).CompactAllDimensions());
[544]1119      vector<r_4> tmp;
1120      tmp0.FillTo(tmp);
[552]1121      medianOfSpectra(iCh,freq) = median(tmp.begin(),tmp.end());
[544]1122    }
1123  }
1124
1125
1126  //Compute the reduced version of the mean and sigma
1127  TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
1128  TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
1129  reduceSpectra(medianOfSpectra,meanRedMtx,sigmaRedMtx);
1130
[553]1131
[562]1132  sa_size_t f1320=freqToChan(1320.);
1133  sa_size_t f1345=freqToChan(1345.);
1134  sa_size_t f1355=freqToChan(1355.);
1135  sa_size_t f1380=freqToChan(1380.);
[553]1136  //Compute baseline arround 1350Mhz on [1320-1345] U [1355-1380]
1137  if (para.debuglev_>9){
1138    cout << "Compute baseline arround 1350Mhz on [1320-1345] U [1355-1380]" << endl;
1139  }
1140  TVector<r_4>meanMed(NUMBER_OF_CHANNELS);
1141  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
1142    double meanMed1;
1143    double sigmaMed1;
1144    TVector<r_4> band1;
1145    band1 = medianOfSpectra(Range(iCh),Range(f1320,f1345)).CompactAllDimensions();
1146    MeanSigma(band1,meanMed1,sigmaMed1);
1147    double meanMed2;
1148    double sigmaMed2;
1149    TVector<r_4> band2;
1150    band2 = medianOfSpectra(Range(iCh),Range(f1355,f1380)).CompactAllDimensions();
1151    MeanSigma(band2,meanMed2,sigmaMed2);
1152    meanMed(iCh) = (meanMed1+meanMed2)*0.5;
1153  } 
1154  meanMed.Print(cout);
[562]1155  cout << endl;
1156
[553]1157 
1158  //Compute the sigma in the range 1320MHz-1380MHz
1159  if (para.debuglev_>9){
1160    cout << "Compute the sigma in the range 1320MHz-1380MHz" << endl;
1161  }
1162  TVector<r_4>sigmaMed(NUMBER_OF_CHANNELS);
[560]1163  sa_size_t redf1320=(sa_size_t)((1320.0-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*para.nSliceInFreq_);
1164  sa_size_t redf1380=(sa_size_t)((1380.0-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*para.nSliceInFreq_);
[553]1165
1166  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
1167    double meanSigma;
1168    double sigmaSigma;
1169    TVector<r_4> band;
1170    band = sigmaRedMtx(Range(iCh),Range(redf1320,redf1380)).CompactAllDimensions();
1171    MeanSigma(band,meanSigma,sigmaSigma);
1172    meanSigma *= sqrt(para.nSliceInFreq_); //to scale to orignal spectra
1173    sigmaMed(iCh) = meanSigma;
1174  }
1175  sigmaMed.Print(cout);
[562]1176  cout << endl;
1177
[553]1178 
1179 
1180  if (para.debuglev_>9){
1181    cout << "Compute medianOfSpectraNorm" << endl;
1182  }
1183  TMatrix<r_4> medianOfSpectraNorm(medianOfSpectra,false); //do not share the data...
1184  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
1185    medianOfSpectraNorm.Row(iCh) -= meanMed(iCh);
1186    medianOfSpectraNorm.Row(iCh) /= sigmaMed(iCh);
1187  }
1188
1189 
1190
[544]1191  {//Save the result
1192    stringstream tmp;
1193    tmp << nSpectra;
1194    string fileName = para.outPath_+"/medianDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
[552]1195    cout << "Save median based on " <<  nSpectra << " cycles " << endl;
[542]1196    POutPersist fos(fileName);
[544]1197
1198    string tag = "median";
1199    fos << PPFNameTag(tag) << medianOfSpectra;
[553]1200
1201    tag = "medianNorm";
1202    fos << PPFNameTag(tag) << medianOfSpectraNorm;
1203   
1204
[544]1205    tag = "meanmedred";
1206    fos << PPFNameTag(tag) << meanRedMtx;
1207    tag = "sigmamedred";
1208    fos << PPFNameTag(tag) << sigmaRedMtx;
[552]1209    tag = "cycleVsfreq";
1210   
1211    TArray<r_4> tarr(tableOfSpectra(Range::all(),Range::all(),Range(0,nSpectra-1)));
1212    fos << PPFNameTag(tag) << tarr;
[540]1213  }
1214}
[544]1215
[540]1216//-------------------------------------------------------
[544]1217int main(int narg, char* arg[]) {
[540]1218 
1219  int rc = 0; //return code
1220  string msg; //message used in Exceptions
1221
1222
[544]1223
1224  //default value for initial parameters (see Para structure on top of the file)
[540]1225  string debuglev = "0";
[544]1226  string action = "meanDiffOnOff";
[540]1227  string inputPath = "."; 
1228  string outputPath = "."; 
1229  string sourceName = "Abell85";
[541]1230  string ppfFile;
[544]1231  string nSliceInFreq = "32";
[560]1232  string typeOfCalib="perRun";
[561]1233  string calibFreq = "1346";
[562]1234  string calibBandFreq="6.25";
1235  string mxcycles;
1236
[540]1237  //  bool okarg=false;
1238  int ka=1;
[554]1239  while (ka<narg) {
[551]1240    if (strcmp(arg[ka],"-h")==0) {
[560]1241      cout << "Usage:  -act [meanRawDiffOnOff]|medianRawDiffOnOff|meanCalibBAODiffOnOff\n"
[562]1242           << " -mxcycles <number> (max. number of cycles to be treated)\n"
[561]1243           << " -calibfreq <number> (cf. freq. used by calibration operation)\n"
[562]1244           << " -calibbandfreq <number> (cf. band of freq. used by calibration operation)\n"
[577]1245           << " -src [Abell85]\n" 
1246           << " -inPath [.]|<top_root_dir of the ppf file>\n" 
[551]1247           << " (ex. /sps/baoradio/AmasNancay/JEC/\n " 
1248           << " -outPath [.]|<dir of the output> \n"
1249           << " -nSliceInFreq [32]|<number of bin in freq. to cumulate>\n"
[561]1250           << " -ppfFile <generic name of the input ppf files> (ex. diffOnOffRaw)\n"
[551]1251           << " -debug <level> "
1252           << endl;
1253      return 0;
1254    }
1255    else if (strcmp(arg[ka],"-debug")==0) {
[540]1256      debuglev=arg[ka+1];
1257      ka+=2;
1258    }
1259    else if (strcmp(arg[ka],"-act")==0) {
1260      action=arg[ka+1];
1261      ka+=2;
1262    }
[561]1263    else if (strcmp(arg[ka],"-calibfreq")==0) {
1264      calibFreq=arg[ka+1];
1265      ka+=2;
1266    }   
[562]1267    else if (strcmp(arg[ka],"-calibbandfreq")==0) {
1268      calibBandFreq=arg[ka+1];
1269      ka+=2;
1270    }   
1271    else if (strcmp(arg[ka],"-mxcycles")==0) {
1272      mxcycles=arg[ka+1];
1273      ka+=2;
1274    }   
[540]1275    else if (strcmp(arg[ka],"-inPath")==0) {
1276      inputPath=arg[ka+1];
1277      ka+=2;
1278    }
1279    else if (strcmp(arg[ka],"-outPath")==0) {
1280      outputPath=arg[ka+1];
1281      ka+=2;
1282    }
[541]1283    else if (strcmp(arg[ka],"-src")==0) {
[540]1284      sourceName=arg[ka+1];
1285      ka+=2;
1286    }
[541]1287    else if (strcmp(arg[ka],"-ppfFile")==0) {
1288      ppfFile=arg[ka+1];
[540]1289      ka+=2;
1290    }
[544]1291    else if (strcmp(arg[ka],"-nSliceInFreq")==0) {
1292      nSliceInFreq=arg[ka+1];
1293      ka+=2;
1294    }
[540]1295    else ka++;
1296  }//eo while
1297
[544]1298  para.debuglev_   = atoi(debuglev.c_str());
1299  para.inPath_     = inputPath;
1300  para.outPath_    = outputPath;
[540]1301  para.sourceName_ = sourceName;
[544]1302  para.ppfFile_    = ppfFile;
1303  para.nSliceInFreq_ = atoi(nSliceInFreq.c_str());
[561]1304  para.calibFreq_   = calibFreq;
[562]1305  para.calibBandFreq_ = calibBandFreq;
1306  para.rcalibFreq_   = atof(calibFreq.c_str());
1307  para.rcalibBandFreq_ = atof(calibBandFreq.c_str());
1308  if (mxcycles != "") {
1309    para.maxNumberCycles_ = atoi(mxcycles.c_str());
1310  } else {
1311    para.maxNumberCycles_ = std::numeric_limits<int>::max();
1312  }
[540]1313
1314  cout << "Dump Initial parameters ............" << endl;
1315  cout << " action = " << action << "\n"
[562]1316       << " maxNumberCycles = " << para.maxNumberCycles_ << "\n"
[544]1317       << " inputPath = " << para.inPath_  << "\n" 
1318       << " outputPath = " <<  para.outPath_ << "\n"
1319       << " sourceName = " << para.sourceName_ << "\n"
1320       << " ppfFile = " <<  para.ppfFile_ << "\n"
1321       << " nSliceInFreq = " << para.nSliceInFreq_  << "\n"
[561]1322       << " calibFreq = " <<  para.calibFreq_ << "\n"
[562]1323       << " calibBandFreq = " <<  para.calibBandFreq_ << "\n"
[544]1324       << " debuglev = "  << para.debuglev_   << "\n";
[540]1325  cout << "...................................." << endl;
1326
[541]1327  if ( "" == ppfFile ) {
[560]1328    cerr << "mergeAnaFiles.cc: you have forgotten ppfFile option"
[541]1329         << endl;
1330    return 999;
1331  }
1332
1333
[540]1334  try {
1335
[542]1336//     int b,e;
1337//     char *match=regexp("truc0machin","[a-z]+[0-9]*",&b,&e);
1338//     printf("->%s<-\n(b=%d e=%d)\n",match,b,e);
[540]1339
[560]1340    if ( action == "meanRawDiffOnOff" ) {
1341      meanRawDiffOnOffCycles();
1342    } else if (action == "medianRawDiffOnOff") {
1343      medianRawDiffOnOffCycles();
1344    } else if (action == "meanCalibBAODiffOnOff") {
1345      meanCalibBAODiffOnOffCycles();
[544]1346    } else {
1347      msg = "Unknown action " + action;
1348      throw msg;
[540]1349    }
1350
[544]1351
[540]1352  }  catch (std::exception& sex) {
1353    cerr << "mergeRawOnOff.cc std::exception :"  << (string)typeid(sex).name() 
1354         << "\n msg= " << sex.what() << endl;
1355    rc = 78;
1356  }
1357  catch ( string str ) {
1358    cerr << "mergeRawOnOff.cc Exception raised: " << str << endl;
1359  }
1360  catch (...) {
1361    cerr << "mergeRawOnOff.cc catched unknown (...) exception  " << endl; 
1362    rc = 79; 
1363  } 
1364
1365  return 0;
1366
1367}
Note: See TracBrowser for help on using the repository browser.