Index: /BAORadio/AmasNancay/trunk/etude_mergeAna.pic
===================================================================
--- /BAORadio/AmasNancay/trunk/etude_mergeAna.pic	(revision 563)
+++ /BAORadio/AmasNancay/trunk/etude_mergeAna.pic	(revision 563)
@@ -0,0 +1,80 @@
+
+set source  $1
+set ncycles $2
+
+openppf onoffsurvey_${source}-${ncycles}Cycles.ppf
+print onoffevol
+
+set defatt "font=helvetica,bold,20 fixedfonsize"
+set axedefatt  "font=helvetica,bold,30 fixedfonsize grid"
+graphicatt "$defatt"
+setaxesatt "$axedefatt"
+
+newwin 1 2
+n/pl onoffevol.onoffRaw0%cycle ! ! "blue cpts notit nsta"
+settitle "Raw ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles " ' ' $defatt
+n/pl onoffevol.onoffRaw1%cycle ! ! "red cpts notit nsta"
+setaxelabels "cycle" "I[1346MHz/6.25MHz] (a.u)" $axedefatt
+
+newwin 1 2
+n/proj hRaw0 onoffevol.onoffRaw0 ! ! ! "blue cpts notit"
+settitle "Raw ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles " ' ' $defatt
+n/proj hRaw1 onoffevol.onoffRaw1 ! ! ! "red cpts notit"
+
+
+
+	
+newwin 1 2
+n/pl onoffevol.onoffRun0%cycle ! ! "blue cpts notit nsta"
+settitle "ON-OFF Calib Run ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+n/pl onoffevol.onoffRun1%cycle ! ! "red cpts notit nsta"
+setaxelabels "cycle" "I[1346MHz/6.25MHz] (a.u)" $axedefatt
+
+newwin 1 2
+n/proj hRun0 onoffevol.onoffRun0 ! ! ! "blue cpts notit"
+settitle "ON-OFF Calib Run ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+n/proj hRun1 onoffevol.onoffRun1 ! ! ! "red cpts notit"
+
+
+newwin 1 2
+n/pl onoffevol.onoffCycle0%cycle ! ! "blue cpts notit nsta"
+settitle "ON-OFF Calib Cycle ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+n/pl onoffevol.onoffCycle1%cycle ! ! "red cpts notit nsta"
+setaxelabels "cycle" "I[1346MHz/6.25MHz] (a.u)" $axedefatt
+
+newwin 1 2
+n/proj hCycle0 onoffevol.onoffCycle0 ! ! ! "blue cpts notit"
+settitle "ON-OFF Calib Cycle ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+n/proj hCycle1 onoffevol.onoffCycle1 ! ! ! "red cpts notit"
+
+
+
+graphicatt "xylimits=1250,1500,-0.01,0.01"
+del snocal0 snocal1
+objaoper meanNoCalib row 0 snocal0
+objaoper meanNoCalib row 1 snocal1
+newwin 1 1
+plot2d snocal0 (n/8192)*250+1250 val n>0 "blue cpts notit nsta"
+plot2d snocal1 (n/8192)*250+1250 val n>0 "red same cpts notit nsta"
+settitle "Raw ON-OFF  ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+setaxelabels "Freq. (MHz)" "I (a.u)" $axedefatt
+
+
+
+del sruncal0 sruncal1
+objaoper meanPerRunCalib row 0 sruncal0
+objaoper meanPerRunCalib row 1 sruncal1
+newwin 1 1
+plot2d sruncal0 (n/8192)*250+1250 val n>0 "blue cpts notit nsta"
+plot2d sruncal1 (n/8192)*250+1250 val n>0 "red same cpts notit nsta"
+settitle "ON-OFF Calib Run  ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+setaxelabels "Freq. (MHz)" "I (a.u)" $axedefatt
+
+newwin 1 1
+del scyclecal0 scyclecal1
+objaoper meanPerCycleCalib row 0 scyclecal0
+objaoper meanPerCycleCalib row 1 scyclecal1
+plot2d scyclecal0 (n/8192)*250+1250 val n>0 "blue cpts notit nsta"
+plot2d scyclecal1 (n/8192)*250+1250 val n>0 "red same cpts notit nsta"
+settitle "ON-OFF Calib Cycle  ${source} Ch 0 (blue) Ch 1 (red) ${ncycles}cycles" ' ' $defatt
+setaxelabels "Freq. (MHz)" "I (a.u)" $axedefatt
Index: /BAORadio/AmasNancay/trunk/mergeAnaFiles.cc
===================================================================
--- /BAORadio/AmasNancay/trunk/mergeAnaFiles.cc	(revision 562)
+++ /BAORadio/AmasNancay/trunk/mergeAnaFiles.cc	(revision 563)
@@ -36,9 +36,15 @@
 
 //-----------------------------------------------
+//Usage
+//
+// ./Objs/mergeAnaFiles -act meanCalibBAODiffOnOff -inPath /sps/baoradio/AmasNancay/JEC/ -src Abell85 -ppfFile dataRaw -debug 1 -mxcycles 500
+//
+//
+//
+//-----------------------------------------------
 const sa_size_t NUMBER_OF_CHANNELS = 2;
 const sa_size_t NUMBER_OF_FREQ     = 8192;
 const r_4    LOWER_FREQUENCY       = 1250.0; //MHz
 const r_4    TOTAL_BANDWIDTH       = 250.0;  //MHz
-//-----------------------------------------------
 //Input parameters
 struct Param {
@@ -68,5 +74,7 @@
 		 sa_size_t chHigh, 
 		 TVector<r_4>& vec){
+  
   sa_size_t nr = mtx.NRows();
+  
   for (sa_size_t ir=0; ir<nr; ir++){
     TVector<r_4> tmp(mtx(Range(ir),Range(chLow,chHigh)),false);
@@ -242,123 +250,5 @@
 //-------------------------------------------------------
 //Compute the mean of Diff ON-OFF BAO-calibrated spectra and also the mean/sigma of rebinned spectra 
-//Used like: 
 //
-// void meanCalibBAODiffOnOffCycles() throw(string) {
-
-//   list<string> listOfFiles;
-//   string directoryName;
-//   directoryName = para.inPath_ + "/" + para.sourceName_;
-
-//   //Make the listing of the directory
-//   listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
-  
-//   list<string>::const_iterator iFile, iFileEnd, iSpecOff, iSpecOffEnd, iSpecOn, iSpecOnEnd;
-//   iFileEnd = listOfFiles.end();
-  
-
-//   //Loop on files
-//   uint_4 nRuns=0;
-//   TArray<r_4> tableOfSpectra(NUMBER_OF_FREQ,NUMBER_OF_CHANNELS,para.maxNumberCycles_); //para.maxNumberCycles_ should be large enough...
-
-
-//   for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
-//     if (para.debuglev_>90){
-//       cout << "load file <" << *iFile << ">" << endl;
-//     }
-
-//     vector<string> tokens;
-//     split(*File,"_",tokens);
-//     string dateOfRun = tokens[1];
-//     string srcLower = tokens[2];
-//     if (para.debuglev_>90){
-//       cout << "date <" << dateOfRun << ">" << endl;
-//     }
-    
-//     PInPersist fin(*iFile);
-//     vector<string> vec = fin.GetNameTags();
-
-//     if (para.typeOfCalib_ == "perRun") {
-//       ///////////////////
-//       //make the calibration of the mean of all Off and On of the run and perform the difference
-
-//       vector<string> modeList;
-//       modeList.push_back("On");
-//       modeList.push_back("Off");
-//       vector<string>::const_iterator iMode;
-//       map<string, TMatrix<r_4> > spectreModeCollect;
-
-//       for (iMode = modeList.begin(); iMode!=modeList.end(); ++iMode) {
-// 	///////////////////
-// 	//
-// 	//Compute the mean of the mode
-// 	//
-// 	list<string> listOfSpectra;
-// 	//Keep only required PPF objects
-// 	string matchstr = "specRaw"+(*iMode)+"[0-9]+"; 
-// 	std::remove_copy_if(
-// 			    vec.begin(), vec.end(), back_inserter(listOfSpectra),
-// 			    not1(StringMatch(matchstr))
-// 			    );
-	
-// 	listOfSpectra.sort(stringCompare);
-// 	iSpecEnd = listOfSpectra.end();
-// 	TMatrix<r_4> meanOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
-// 	uint_4 nSpectra=0;
-// 	//Loop of spectra matrix
-// 	for (iSpec = listOfSpectra.begin(); iSpec!=iSpecEnd;  ++iSpec){
-// 	  if (para.debuglev_>90){
-// 	    cout << " spactra <" << *iSpec << ">" << endl;
-// 	  }
-// 	  TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
-// 	  fin.GetObject(aSpec,*iSpec);
-// 	  //How to see if the GetObject is ok?? Ask Reza
-// 	  nSpectra++;
-// 	  meanOfSpectra+=aSpec;
-// 	}//end loop Off
-// 	//Mean
-// 	if(nSpectra>0)meanOfSpectra=(r_4)(nSpectra);
-
-// 	//BAO Calibrator
-// 	string calibFileName = directoryName + "/" 
-// 	  + "calib_" + dateOfRun + "_" + srcLower + "_"+(*iMode)+"_" 
-// 	  + para.calibFreq_ +"MHz-All.txt";
-// 	if(debuglev_>0) cout << "Read Calib file " << calibFileName << endl;
-// 	ifstream ifs(calibFileName.c_str());
-// 	if ( ! ifs.is_open() ) {
-// 	  rc = 999;
-// 	  throw calibFileName + " cannot be opened...";
-// 	}	
-// 	TVector<r_4> calibBAOfactors;
-// 	sa_size_t nr,nc; //values read 
-// 	calibBAOfactorsOff.ReadASCII(ifs,nr,nc);
-// 	if(debuglev_>9){
-// 	  cout << "(nr,nc): "<< nr << "," << nc << endl;
-// 	  calibBAOfactors.Print(cout);
-// 	}
-	
-// 	for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
-// 	  meanOfSpectra(Range(iCh), Range::all()) /=calibBAOfactors(iCh);
-// 	}
-// 	//storage
-// 	spectreModeCollect.insert(pair<string, TMatrix<r_4> >(*iMode,TMatrix<r_4>(meanOfSpectra,false))); //do not share data (cf. SOPHYA) 
-//       }//end of mode
-
-//       //Take the difference ON-OFF in current run
-//       TMatrix<r_4>diffOnOff = spectreModeCollect["On"]-spectreModeCollect["Off"];
-
-
-//     } else if (para.typeOfCalib_ == "perCycle") {
-//       //perform the calibration of the OFF and ON per cycle, then make the mean and take the diff
-//     } else {
-//       string msg="FATAL (meanCalibBAODiffOnOffCycles); unknown calibration mode " 
-// 	+ para.typeOfCalib_ ;
-//       throw(msg);
-//     }  
-
-
-//     nRuns++;
-    
-//   }//eo loop on spectra in a file
-// }
 void meanCalibBAODiffOnOffCycles() throw(string) {
 
@@ -378,12 +268,16 @@
   TMatrix<r_4> meanDiffONOFF_perCycleCalib(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);    //set to 0
   char* onffTupleName[7]={"cycle",
-			  "onoffRaw0","onoffRun0","onoffCycle0",
-			  "onoffRaw1","onoffRun1","onoffCycle1"};
+			  "onoffRaw0","onoffRaw1",
+			  "onoffRun0","onoffRun1",
+			  "onoffCycle0","onoffCycle1"};
   NTuple onoffevolution(7,onffTupleName);
   r_4 xnt[7];
 
   //Lower and Higher freq. bin to perform mean follow up
-  sa_size_t chLow = freqToChan(para.rcalibFreq_ - (para.rcalibBandFreq_*0.5));
+  sa_size_t chLow  = freqToChan(para.rcalibFreq_ - (para.rcalibBandFreq_*0.5));
   sa_size_t chHigh = freqToChan(para.rcalibFreq_ + (para.rcalibBandFreq_*0.5));
+  if (para.debuglev_>90){
+    cout << "freq. band for follow up [" <<  chLow << ", "<< chHigh << "]" << endl;
+  }
   
   //Loop on files/run
@@ -501,5 +395,4 @@
       double mean,sigma;
       MeanSigma(coef,mean,sigma);
-      cout << "Mean: " << mean << " sigma " << sigma << endl;
       calibBAOfactors_Off_Run_Ch0.Column(1).SetCst(mean);
     }
@@ -533,5 +426,5 @@
       double mean,sigma;
       MeanSigma(coef,mean,sigma);
-      cout << "Mean: " << mean << " sigma " << sigma << endl;
+      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
       calibBAOfactors_Off_Run_Ch1.Column(1).SetCst(mean);
     }
@@ -566,5 +459,5 @@
       double mean,sigma;
       MeanSigma(coef,mean,sigma);
-      cout << "Mean: " << mean << " sigma " << sigma << endl;
+      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
       calibBAOfactors_On_Run_Ch0.Column(1).SetCst(mean);
     }
@@ -597,5 +490,5 @@
       double mean,sigma;
       MeanSigma(coef,mean,sigma);
-      cout << "Mean: " << mean << " sigma " << sigma << endl;
+      //      cout << "Mean: " << mean << " sigma " << sigma << endl;
       calibBAOfactors_On_Run_Ch1.Column(1).SetCst(mean);
     }
@@ -623,4 +516,7 @@
     nr = calibBAOfactors_Off_Run_Ch0.NRows();
     for (sa_size_t ir=0; ir<nr; ++ir){
+//       cout << "Calib. Off Run Ch0 cycle ["<< calibBAOfactors_Off_Run_Ch0(ir,0)<<"], val "
+// 	   << calibBAOfactors_Off_Run_Ch0(ir,1) << endl;
+
       calibBAO_Off_Run_Ch0[(int)calibBAOfactors_Off_Run_Ch0(ir,0)]
 	= calibBAOfactors_Off_Run_Ch0(ir,1);
@@ -631,5 +527,5 @@
       calibBAO_Off_Cycle_Ch1[(int)calibBAOfactors_Off_Cycle_Ch1(ir,0)]
 	= calibBAOfactors_Off_Cycle_Ch1(ir,1);
-    }
+    }//eo loop on coef Off
     
     nr = calibBAOfactors_On_Run_Ch0.NRows();
@@ -643,26 +539,39 @@
       calibBAO_On_Cycle_Ch1[(int)calibBAOfactors_On_Cycle_Ch1(ir,0)]
 	= calibBAOfactors_On_Cycle_Ch1(ir,1);
-    }
+    }//eo loop on coef On
       
-
-    
-// 	for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
-// 	  meanOfSpectra(Range(iCh), Range::all()) /=calibBAOfactors(iCh);
-// 	}
-
     //Loop on cyles
     for (list<int>::iterator ic=commonCycles.begin(); ic!=commonCycles.end(); ++ic){
 
-      if(para.debuglev_>9){
+      //Look if the cycle has been calibrated...
+      bool isCycleCalibrated = 
+	( calibBAO_On_Run_Ch0.count(*ic)    *
+	  calibBAO_On_Run_Ch1.count(*ic)    *
+	  calibBAO_Off_Run_Ch0.count(*ic)   *
+	  calibBAO_Off_Run_Ch1.count(*ic)   *
+	  calibBAO_On_Cycle_Ch0.count(*ic)  *
+	  calibBAO_On_Cycle_Ch1.count(*ic)  *
+	  calibBAO_Off_Cycle_Ch0.count(*ic) *
+	  calibBAO_Off_Cycle_Ch1.count(*ic) ) != 0 ? true : false;
+
+      if(para.debuglev_>9) {
 	cout << "Calibration coefficients for cycle "<<*ic << endl; 
-	cout << "Off Run Ch0 " << calibBAO_Off_Run_Ch0[*ic] << " "
-	     << "Ch1 " << calibBAO_Off_Run_Ch1[*ic] << "\n"
-	     << "On Run Ch0 " << calibBAO_On_Run_Ch0[*ic] << " "
-	     << "Ch1 " << calibBAO_On_Run_Ch1[*ic] << "\n"
-	     << "Off Cycle Ch0 " << calibBAO_Off_Cycle_Ch0[*ic] << " "
-	     << "Ch1 " << calibBAO_Off_Cycle_Ch1[*ic] << "\n"
-	     << "On Cycle Ch0 " << calibBAO_On_Cycle_Ch0[*ic] << " "
-	     << "Ch1 " << calibBAO_On_Cycle_Ch1[*ic] << endl;
-      }
+	if (isCycleCalibrated) {
+	  cout << "Cycle calibrated " << endl;
+	  cout << "Off Run Ch0 " << calibBAO_Off_Run_Ch0[*ic] << " "
+	       << "Ch1 " << calibBAO_Off_Run_Ch1[*ic] << "\n"
+	       << "On Run Ch0 " << calibBAO_On_Run_Ch0[*ic] << " "
+	       << "Ch1 " << calibBAO_On_Run_Ch1[*ic] << "\n"
+	       << "Off Cycle Ch0 " << calibBAO_Off_Cycle_Ch0[*ic] << " "
+	       << "Ch1 " << calibBAO_Off_Cycle_Ch1[*ic] << "\n"
+	       << "On Cycle Ch0 " << calibBAO_On_Cycle_Ch0[*ic] << " "
+	       << "Ch1 " << calibBAO_On_Cycle_Ch1[*ic] << endl;
+	} else {
+	  cout << "Cycle NOT calibrated " << endl;
+	}
+      }//debug
+
+
+      if ( ! isCycleCalibrated ) continue;
       
       string ppftag;
@@ -706,6 +615,8 @@
       meanDiffONOFF_perCycleCalib += diffOnOff_perCycleCalib;
 
+      //
+      totalNumberCycles++;
       //Fill NTuple
-      xnt[0] = *ic;
+      xnt[0] = totalNumberCycles;
       
       TVector<r_4> meanInRange_noCalib(NUMBER_OF_CHANNELS);
@@ -721,10 +632,10 @@
       TVector<r_4> meanInRange_perCycleCalib(NUMBER_OF_CHANNELS);
       meanInRange(diffOnOff_perCycleCalib,chLow,chHigh,meanInRange_perCycleCalib);
-      xnt[3] = meanInRange_perCycleCalib(0);
-      xnt[4] = meanInRange_perCycleCalib(1);
+      xnt[5] = meanInRange_perCycleCalib(0);
+      xnt[6] = meanInRange_perCycleCalib(1);
       
       onoffevolution.Fill(xnt);
 
-      totalNumberCycles++;
+      //Quit if enough
       if (totalNumberCycles >= para.maxNumberCycles_) break;	
 
@@ -735,5 +646,5 @@
   cout << "End of jobs: we have treated " << totalNumberCycles << " cycles" << endl;
   //Normalisation
-  if(totalNumberCycles>0){
+  if(totalNumberCycles > 0){
     meanDiffONOFF_noCalib       /= (r_4)totalNumberCycles;
     meanDiffONOFF_perRunCalib   /= (r_4)totalNumberCycles;
@@ -758,5 +669,7 @@
     tmp << totalNumberCycles;
     string fileName = para.outPath_+"/onoffsurvey_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
+
     POutPersist fos(fileName);
+    cout << "Save output in " << fileName << endl;
 
     string tag = "meanNoCalib";
Index: /BAORadio/AmasNancay/trunk/plotDiffOnOff.pic
===================================================================
--- /BAORadio/AmasNancay/trunk/plotDiffOnOff.pic	(revision 562)
+++ /BAORadio/AmasNancay/trunk/plotDiffOnOff.pic	(revision 563)
@@ -8,5 +8,5 @@
 set action $4
 
-set defatt "font=helvetica,bold,20"
+set defatt "font=helvetica,bold,20 fixedfontsize"
 
 
