Index: BAORadio/AmasNancay/analyse.cc
===================================================================
--- BAORadio/AmasNancay/analyse.cc	(revision 514)
+++ BAORadio/AmasNancay/analyse.cc	(revision 523)
@@ -83,4 +83,16 @@
 }
 //-----
+//JEC 22/9/11 comparison, not case sensitive to sort File liste START
+bool stringCompare( const string &left, const string &right ){
+   if( left.size() < right.size() )
+      return true;
+   return false;
+   for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
+      if( tolower( *lit ) < tolower( *rit ) )
+         return true;
+      else if( tolower( *lit ) > tolower( *rit ) )
+         return false;
+}//JEC 22/9/11 comparison, not case sensitive to sort File liste END
+//-----
 list<string> ListOfFileInDir(string dir, string filePettern) throw(string) {
   list<string> theList;
@@ -89,6 +101,5 @@
   DIR *dip;
   struct dirent *dit;
-  string msg;
-  string fileName;
+  string msg;  string fileName;
   string fullFileName;
   size_t found;
@@ -111,4 +122,9 @@
     throw msg;
   }
+  
+  //JEC 22/9/11 START
+  theList.sort(stringCompare);
+  //JEC 22/9/11 END
+
   return theList;
 }
@@ -290,13 +306,13 @@
       ka+=2;
     }
-    if (strcmp(arg[ka],"-act")==0) {
+    else if (strcmp(arg[ka],"-act")==0) {
       action=arg[ka+1];
       ka+=2;
     }
-    if (strcmp(arg[ka],"-inPath")==0) {
+    else if (strcmp(arg[ka],"-inPath")==0) {
       inputPath=arg[ka+1];
       ka+=2;
     }
-    if (strcmp(arg[ka],"-outPath")==0) {
+    else if (strcmp(arg[ka],"-outPath")==0) {
       outputPath=arg[ka+1];
       ka+=2;
@@ -346,4 +362,20 @@
 
 
+  //JEC 21/9/11 Give the input parameters START
+  cout << " action " << action << "\n"
+       << " inputPath = " << inputPath << "\n" 
+       << " outputPath = " << outputPath << "\n"
+       << " sourceName = " << sourceName << "\n"
+       << " scaFile = " << scaFile << "\n"
+       << " dateOfRun = " << dateOfRun << "\n"
+       << " spectraDirectory = " << spectraDirectory << "\n"
+       << " freqBAOCalib = " << freqBAOCalib  << "\n"
+       << " bandWidthBAOCalib = " << bandWidthBAOCalib << "\n"
+       << " debuglev = "  << debuglev  << "\n"
+       << " mode = " << mode << "\n"
+       << " numcycle = " << numcycle << "\n"
+       << " calibrationOpt = " << calibrationOpt << endl;
+  //JEC 21/9/11 Give the input parameters END
+
 
   try {
@@ -356,4 +388,7 @@
     }
     
+
+
+
 
     //
@@ -671,7 +706,18 @@
 	 iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
       tag = "specRaw";
+
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
       stringstream sid;
-      sid << id;
+      sid << (iSpectre->first).second;
       tag += sid.str();
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+
       fos << PPFNameTag(tag) << iSpectre->second;
     }
@@ -687,34 +733,63 @@
     //------------------------------------------
     //Read BAO calibration files
-    TMatrix<r_4> calibBAOfactors;
     sa_size_t nr,nc; //values read 
-    bool first = true;
-
-    for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
-      string calibFileName = "./" + sourceName_ + "/" 
-	+ dateOfRun_ + StringToLower(sourceName_) + "-" + mode + "-" + freqBAOCalibration_ + "MHz";
-      stringstream channel;
-      channel << iCh;
-      calibFileName += "-Ch" + channel.str() + "Cycles.txt";
-      if(debuglev_>0) cout << "Read file " << calibFileName << endl;
-      ifstream ifs(calibFileName.c_str());
-      if ( ! ifs.is_open() ) {
-	rc = 999;
-	throw calibFileName + " cannot be opened...";
-      }	
-      TVector<r_4> aCalib;
-      aCalib.ReadASCII(ifs,nr,nc);
-      if(first){
-	first = false;
-	calibBAOfactors.SetSize(NUMBER_OF_CHANNELS,nr);
-      }
-      calibBAOfactors( Range(iCh), Range::all() ) = aCalib.Transpose();
-      ifs.close();
-    }//end of loop on channels
-
+//JEC 20/9/11 use mean calibration coeff upon all cycles START
+//    bool first = true;
+//    TMatrix<r_4> calibBAOfactors;
+//     for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
+//       string calibFileName = inputPath_+ "/" 
+// 	+ sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) 
+// 	+ "/calib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+// 	+ mode + "_" + freqBAOCalibration_ + "MHz";
+//       stringstream channel;
+//       channel << iCh;
+//       calibFileName += "-Ch" + channel.str() + "Cycles.txt";
+//       if(debuglev_>0) cout << "Read file " << calibFileName << endl;
+//       ifstream ifs(calibFileName.c_str());
+//       if ( ! ifs.is_open() ) {
+// 	rc = 999;
+// 	throw calibFileName + " cannot be opened...";
+//       }	
+//       TVector<r_4> aCalib;
+//       if(debuglev_>9) cout << "Debug 1" << endl;
+//       aCalib.ReadASCII(ifs,nr,nc);
+//       if(debuglev_>9) cout << "Debug 2" << endl;
+//       if(first){
+// 	first = false;
+// 	if(debuglev_>9) cout << "Debug 3" << endl;
+// 	calibBAOfactors.SetSize(NUMBER_OF_CHANNELS,nr);
+// 	if(debuglev_>9) cout << "Debug 4" << endl;
+//       }
+//       calibBAOfactors( Range(iCh), Range::all() ) = aCalib.Transpose();
+//       if(debuglev_>9) cout << "Debug 5" << endl;
+//       ifs.close();
+//     }//end of loop on channels
+
+    
+    string calibFileName = inputPath_+ "/" 
+      + sourceName_ + "/" + dateOfRun_ + StringToLower(sourceName_) 
+      + "/calib_" + dateOfRun_ + "_" + StringToLower(sourceName_) + "_"
+      + mode + "_" + freqBAOCalibration_ + "MHz-All.txt";
+
+    if(debuglev_>0) cout << "Read file " << calibFileName << endl;
+    ifstream ifs(calibFileName.c_str());
+    if ( ! ifs.is_open() ) {
+      rc = 999;
+      throw calibFileName + " cannot be opened...";
+    }	
+    TVector<r_4> calibBAOfactors;
+    if(debuglev_>9) cout << "Debug 1" << endl;
+    calibBAOfactors.ReadASCII(ifs,nr,nc);
+    if(debuglev_>9){
+      cout << "Debug 2: (nr,nc): "<< nr << "," << nc << endl;
+      calibBAOfactors.Print(cout);
+    }
+
+//JEC 20/9/11 use mean calibration coeff upon all cycles END
 
     //
     //spectra corrected by BAO calibration factor 
-    //make it different on Channels and Cycles (1/06/2011)
+    //-----make it different on Channels and Cycles (1/06/2011) OBSOLETE
+    //use mean calibration coeff upon all cycles (20/6/11)
     //warning cycles are numbered from 1,...,N
     //
@@ -724,5 +799,9 @@
       
       for (sa_size_t iCh=0;iCh<NUMBER_OF_CHANNELS;++iCh){
-	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh,icycle-1);
+	//JEC 20/9/11 use mean calibration coeff upon all cycles START
+
+	//	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh,icycle-1);
+	specmtx( Range(iCh), Range::all() ) /= calibBAOfactors(iCh);
+	//JEC 20/9/11 use mean calibration coeff upon all cycles END
       }
     }
@@ -739,9 +818,20 @@
     id=0;
     for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
+
       tag = "specBAOCalib";
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
       stringstream sid;
-      sid << id;
+      sid << (iSpectre->first).second;
       tag += sid.str();
-      fos << PPFNameTag(tag) << (*iSpectre).second;
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+
+      fos << PPFNameTag(tag) << iSpectre->second;
     }
   }//end of save fits
@@ -775,8 +865,17 @@
     for (iSpectre = spectreCollect.begin();iSpectre != iSpectreEnd ; ++iSpectre, ++id) {
       tag = "specBAORTCalib";
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number START
+//       stringstream sid;
+//       sid << id;
+//       tag += sid.str();
+      tag += (iSpectre->first).first;
       stringstream sid;
-      sid << id;
+      sid << (iSpectre->first).second;
       tag += sid.str();
-      fos << PPFNameTag(tag) << (*iSpectre).second;
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      //JEC 20/9/11 modif tag to take into account Mode and Cycle number END
+      fos << PPFNameTag(tag) << iSpectre->second;
     }
   }//end of save fits
@@ -821,7 +920,11 @@
       tag = "specONOFF";
       stringstream sid;
-      sid << id;
+      //JEC 20/9/11      sid << id;
+      sid << iDiff->first;
       tag += sid.str();
-      fos << PPFNameTag(tag) << (*iDiff).second;
+      if(debuglev_>9) {
+ 	cout << "save tag<" << tag << ">" << endl;
+      }
+      fos << PPFNameTag(tag) << iDiff->second;
     }
     //save the mean also
@@ -869,4 +972,8 @@
   iFileEnd = listOfSpecFiles.end();
   map<string, r_4> meanpowerCollect;
+  //JEC 21/9/11 add meanpower for each Channels START
+  map<string, r_4> meanPowerPerChanCollect;
+  //JEC 21/9/11 add meanpower for each Channels END
+
   map<string, r_4>::const_iterator iMeanPow, iMeanPowEnd;
 
@@ -876,4 +983,14 @@
     aSpectrum >> spectre;
     meanpowerCollect[*iFile] = spectre.Sum()/spectre.Size();
+    //JEC 21/9/11 add meanpower for each Channels START
+    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh) {
+      TVector<r_4> specChan(NUMBER_OF_FREQ);
+      specChan = spectre.Row(iCh).Transpose();
+      stringstream tmp;
+      tmp << iCh;
+      string tag = *iFile + "_" + tmp.str();
+      meanPowerPerChanCollect[tag] = specChan.Sum()/specChan.Size();
+    }
+    //JEC 21/9/11 add meanpower for each Channels END
   }//end of for files
   pair<string, r_4> minelement = *min_element(meanpowerCollect.begin(),meanpowerCollect.end(),compare);
@@ -929,6 +1046,25 @@
   {
     vector<r_4> tmp;
+    //JEC 21/9/11 add meanpower for each Channels START
+    vector<r_4> tmpCh0; //for Chan 0
+    vector<r_4> tmpCh1; //for Chan 1
+    //JEC 21/9/11 add meanpower for each Channels END
     for (iFile = listOfSpecFiles.begin(); iFile != iFileEnd; ++iFile) {
+      if (debuglev_>9) {
+	cout << "Gain: save mean power of file: " << *iFile << endl;
+      }
       tmp.push_back(meanpowerCollect[*iFile]);
+      //JEC 21/9/11 add meanpower for each Channels START  
+      stringstream tmp0;
+      tmp0 << (sa_size_t)0;
+      string tag0 = *iFile + "_" + tmp0.str();
+      tmpCh0.push_back(meanPowerPerChanCollect[tag0]);
+      if (NUMBER_OF_CHANNELS>1){
+	stringstream tmp1;
+	tmp1 << (sa_size_t)1;
+	string tag1 = *iFile + "_" + tmp1.str();
+	tmpCh1.push_back(meanPowerPerChanCollect[tag1]);
+      }
+      //JEC 21/9/11 add meanpower for each Channels END
     }
     string fileName;
@@ -937,8 +1073,15 @@
     POutPersist fos(fileName); 
     TVector<r_4> tvtmp(tmp);
-    fos.PutObject(tvtmp,"gainmoni");
-  }
-  
-
+    fos.PutObject(tvtmp,"gainmoni"); //Attention initialement le tag etait "monitor"...
+    //JEC 21/9/11 add meanpower for each Channels START  
+    TVector<r_4> tvtmp0(tmpCh0);
+    fos.PutObject(tvtmp0,"gainmoni0");
+    if (NUMBER_OF_CHANNELS>1){
+      TVector<r_4> tvtmp1(tmpCh1);
+      fos.PutObject(tvtmp1,"gainmoni1");
+    }
+    //JEC 21/9/11 add meanpower for each Channels END
+  }
+  
   cout << "OK gain finished" <<endl;
    return rc;
