Changeset 544


Ignore:
Timestamp:
Oct 5, 2011, 9:51:55 AM (13 years ago)
Author:
campagne
Message:

introduce median calculous (jec)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • BAORadio/AmasNancay/mergeRawOnOff.cc

    r542 r544  
    3939//-----------------------------------------------
    4040const sa_size_t NUMBER_OF_CHANNELS = 2;
    41 const sa_size_t  NUMBER_OF_FREQ = 8192;
     41const sa_size_t NUMBER_OF_FREQ     = 8192;
     42const sa_size_t TOTAL_NUM_CYCLES   = 10000;
    4243const r_4    LOWER_FREQUENCY = 1250.0; //MHz
    4344const r_4    TOTAL_BANDWIDTH = 250.0; //MHz
    4445//-----------------------------------------------
     46//Input parameters
     47struct Param {
     48  int debuglev_;      //debug
     49  string inPath_;     //root directory of the input files
     50  string outPath_;    //output files are located here
     51  string sourceName_; //source name & subdirectory of the input files
     52  string ppfFile_;    //generic name of the input files
     53  int nSliceInFreq_;  //used by reduceSpectra() fnc
     54} para;
     55//--------------------------------------------------------------
     56//Utility functions
     57//--------------------------------------------------------------
    4558char *regexp (const char *string, const char *patrn, int *begin, int *end) {   
    4659        int i, w=0, len;                 
     
    6275        return word;
    6376}
    64 //------------------------------------------------
    65 
    66 
    67 struct Param {
    68   int debuglev_;
    69   string inPath_;
    70   string outPath_;
    71   string sourceName_;
    72   string ppfFile_;
    73 } para;
    74 //-----
     77//-------
    7578sa_size_t round_sa(r_4 r) {
    7679  return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5));
     
    145148   
    146149    if (b != 0) return false;
    147     if (e != aStr.size()) return false;
     150    if (e != (int)aStr.size()) return false;
    148151    return true;
    149152
     
    153156};
    154157//-------------------------------------------------------
     158//Rebin in frequence + compute mean and sigma
     159void reduceSpectra(const TMatrix<r_4>& specMtxInPut,
     160                    TMatrix<r_4>& meanMtx,
     161                    TMatrix<r_4>& sigmaMtx) {
     162  sa_size_t nSliceFreq = para.nSliceInFreq_;
     163  sa_size_t deltaFreq =  NUMBER_OF_FREQ/nSliceFreq;
     164  for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){
     165    sa_size_t freqLow= iSlice*deltaFreq;
     166    sa_size_t freqHigh= freqLow + deltaFreq -1;
     167    for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){
     168      TVector<r_4> reducedRow;
     169      reducedRow = specMtxInPut.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions();
     170      double mean;
     171      double sigma;
     172      MeanSigma(reducedRow,mean,sigma);
     173      meanMtx(iCh,iSlice) = mean;
     174      sigmaMtx(iCh,iSlice) = sigma;
     175    }//eo loop on channels
     176  }//eo loop on slices
     177}
    155178//-------------------------------------------------------
     179//Compute the mean of Raw spectra and also the mean/sigma of rebinned spectra
     180//Used like:
     181//
    156182void meanOnCycles() throw(string) {
    157183  list<string> listOfFiles;
     
    166192 
    167193  StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects
    168   TMatrix<r_4> sumOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
     194  TMatrix<r_4> meanOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
    169195  uint_4 nSpectra=0;
    170196  //Loop on files
     
    193219      //How to see if the GetObject is ok?? Ask Reza
    194220      nSpectra++;
    195       sumOfSpectra+=aSpec;
     221      meanOfSpectra+=aSpec;
    196222    }//eo loop on spectra in a file
    197223  }//eo loop on files
    198224 
    199225  //Normalisation
    200   if(nSpectra>0)sumOfSpectra/=(r_4)(nSpectra);
     226  if(nSpectra>0)meanOfSpectra/=(r_4)(nSpectra);
     227
     228  //Compute the reduced version of the mean and sigma
     229  TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
     230  TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
     231  reduceSpectra(meanOfSpectra,meanRedMtx,sigmaRedMtx);
    201232
    202233  {//Save the result
     
    205236    string fileName = para.outPath_+"/meanDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
    206237    cout << "Save mean based on " <<  nSpectra << " cycles " << endl;
     238    POutPersist fos(fileName);
     239
    207240    string tag = "mean";
     241    fos << PPFNameTag(tag) << meanOfSpectra;
     242    tag = "meanred";
     243    fos << PPFNameTag(tag) << meanRedMtx;
     244    tag = "sigmared";
     245    fos << PPFNameTag(tag) << sigmaRedMtx;
     246  }
     247}
     248//-------------------------------------------------------
     249//Compute the median of Raw spectra and also the mean/sigma of rebinned spectra
     250//Used like:
     251//
     252void medianOnCycles() throw(string) {
     253  list<string> listOfFiles;
     254  string directoryName;
     255  directoryName = para.inPath_ + "/" + para.sourceName_;
     256
     257  //Make the listing of the directory
     258  listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_);
     259 
     260  list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd;
     261  iFileEnd = listOfFiles.end();
     262 
     263
     264  TArray<r_4> tableOfSpectra(NUMBER_OF_FREQ,NUMBER_OF_CHANNELS,TOTAL_NUM_CYCLES); //TOTAL_NUM_CYCLES should be large enough...
     265
     266  StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects
     267  uint_4 nSpectra=0;
     268  //Loop on files
     269  for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) {
     270    if (para.debuglev_>90){
     271      cout << "load file <" << *iFile << ">" << endl;
     272    }
     273    PInPersist fin(*iFile);
     274    vector<string> vec = fin.GetNameTags();
     275    list<string> listOfSpectra;
     276    //Keep only required PPF objects
     277    std::remove_copy_if(
     278                        vec.begin(), vec.end(), back_inserter(listOfSpectra),
     279                        not1(match)
     280                        );
     281   
     282    iSpecEnd = listOfSpectra.end();
     283    listOfSpectra.sort(stringCompare);
     284    //Loop of spectra matrix
     285    for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd;  ++iSpec){
     286      if (para.debuglev_>90){
     287        cout << " spactra <" << *iSpec << ">" << endl;
     288      }
     289      TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
     290      fin.GetObject(aSpec,*iSpec);
     291
     292      if ((sa_size_t)nSpectra < TOTAL_NUM_CYCLES ) {
     293        //STORE THE spectra
     294        //      tableOfSpectra(Range::all(),Range::all(),Range(nSpectra)).Show();
     295        //      aSpec.Show();
     296        tableOfSpectra(Range::all(),Range::all(),Range(nSpectra)) = aSpec;
     297//      aSpec.Show();
     298//      cout << "deb 1" << endl;
     299//      tmp = aSpec;
     300//      cout << "deb 2" << endl;
     301      } else {
     302        stringstream tmp;
     303        tmp<<nSpectra;
     304        throw "FATAL: SIZE ERROR: too many cycles:"+tmp.str();
     305      }
     306      nSpectra++;
     307    }//eo loop on spectra in a file
     308  }//eo loop on files
     309 
     310  //Resize
     311
     312  nSpectra--;//For further selection in Range
     313  if (para.debuglev_>90){
     314    cout << "Cycles < 0," <<  nSpectra << ">" << endl;
     315  }
     316 
     317
     318  TMatrix<r_4> medianOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ);
     319  //Compute the median for each freq. and Channel
     320  for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){
     321    for (sa_size_t freq =0; freq<NUMBER_OF_FREQ; freq++){
     322      TVector<r_4> tmp0(tableOfSpectra(Range(freq),Range(iCh),Range(0,nSpectra)).CompactAllDimensions());
     323      vector<r_4> tmp;
     324      tmp0.FillTo(tmp);
     325      nth_element(tmp.begin(),tmp.begin()+tmp.size()/2,tmp.end());
     326      medianOfSpectra(iCh,freq) = *(tmp.begin()+tmp.size()/2);
     327    }
     328  }
     329
     330
     331  //Compute the reduced version of the mean and sigma
     332  TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
     333  TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_);
     334  reduceSpectra(medianOfSpectra,meanRedMtx,sigmaRedMtx);
     335
     336  {//Save the result
     337    stringstream tmp;
     338    tmp << nSpectra;
     339    string fileName = para.outPath_+"/medianDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf";
     340    cout << "Save median based on " <<  (nSpectra+1) << " cycles " << endl;
    208341    POutPersist fos(fileName);
    209     fos << PPFNameTag(tag) << sumOfSpectra;
    210   }
    211 }
     342
     343    string tag = "median";
     344    fos << PPFNameTag(tag) << medianOfSpectra;
     345    tag = "meanmedred";
     346    fos << PPFNameTag(tag) << meanRedMtx;
     347    tag = "sigmamedred";
     348    fos << PPFNameTag(tag) << sigmaRedMtx;
     349  }
     350}
     351
    212352//-------------------------------------------------------
    213 int main(int narg, char* arg[])
    214 {
     353int main(int narg, char* arg[]) {
    215354 
    216355  int rc = 0; //return code
     
    218357
    219358
     359
     360  //default value for initial parameters (see Para structure on top of the file)
    220361  string debuglev = "0";
    221   string action = "default";
     362  string action = "meanDiffOnOff";
    222363  string inputPath = ".";
    223364  string outputPath = ".";
    224365  string sourceName = "Abell85";
    225366  string ppfFile;
     367  string nSliceInFreq = "32";
    226368 
    227369 
     
    253395      ka+=2;
    254396    }
     397    else if (strcmp(arg[ka],"-nSliceInFreq")==0) {
     398      nSliceInFreq=arg[ka+1];
     399      ka+=2;
     400    }
    255401    else ka++;
    256402  }//eo while
    257403
    258   para.debuglev_ = atoi(debuglev.c_str());
    259   para.inPath_   = inputPath;
    260   para.outPath_  = outputPath;
     404  para.debuglev_   = atoi(debuglev.c_str());
     405  para.inPath_     = inputPath;
     406  para.outPath_    = outputPath;
    261407  para.sourceName_ = sourceName;
    262   para.ppfFile_ = ppfFile;
     408  para.ppfFile_    = ppfFile;
     409  para.nSliceInFreq_ = atoi(nSliceInFreq.c_str());
    263410
    264411  cout << "Dump Initial parameters ............" << endl;
    265412  cout << " action = " << action << "\n"
    266        << " inputPath = " << inputPath << "\n"
    267        << " outputPath = " << outputPath << "\n"
    268        << " sourceName = " << sourceName << "\n"
    269        << " ppfFile = " << ppfFile << "\n"
    270        << " debuglev = "  << debuglev  << "\n";
     413       << " inputPath = " << para.inPath_  << "\n"
     414       << " outputPath = " <<  para.outPath_ << "\n"
     415       << " sourceName = " << para.sourceName_ << "\n"
     416       << " ppfFile = " <<  para.ppfFile_ << "\n"
     417       << " nSliceInFreq = " << para.nSliceInFreq_  << "\n"
     418       << " debuglev = "  << para.debuglev_   << "\n";
    271419  cout << "...................................." << endl;
    272420
     
    284432//     printf("->%s<-\n(b=%d e=%d)\n",match,b,e);
    285433
    286     if ( action == "default" ) {
     434    if ( action == "meanDiffOnOff" ) {
    287435      meanOnCycles();
    288     }
     436    } else if (action == "medianDiffOnOff") {
     437      medianOnCycles();
     438    } else {
     439      msg = "Unknown action " + action;
     440      throw msg;
     441    }
     442
    289443
    290444  }  catch (std::exception& sex) {
Note: See TracChangeset for help on using the changeset viewer.