[540] | 1 | // Utilisation de SOPHYA pour faciliter les tests ... |
---|
| 2 | #include <regex.h> |
---|
| 3 | //#include <regexp.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | |
---|
| 6 | #include "sopnamsp.h" |
---|
| 7 | #include "machdefs.h" |
---|
| 8 | |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | #include <dirent.h> |
---|
| 11 | #include <matharr.h> |
---|
| 12 | |
---|
| 13 | // include standard c/c++ |
---|
| 14 | #include <iostream> |
---|
| 15 | #include <fstream> |
---|
| 16 | #include <string> |
---|
| 17 | #include <vector> |
---|
| 18 | #include <map> |
---|
| 19 | #include <functional> |
---|
| 20 | #include <algorithm> |
---|
| 21 | #include <numeric> |
---|
| 22 | #include <list> |
---|
| 23 | #include <exception> |
---|
| 24 | |
---|
| 25 | // include sophya mesure ressource CPU/memoire ... |
---|
| 26 | #include "resusage.h" |
---|
| 27 | #include "ctimer.h" |
---|
| 28 | #include "timing.h" |
---|
| 29 | #include "timestamp.h" |
---|
| 30 | #include "strutilxx.h" |
---|
| 31 | #include "ntuple.h" |
---|
| 32 | #include "fioarr.h" |
---|
| 33 | #include "tarrinit.h" |
---|
| 34 | #include "histinit.h" |
---|
| 35 | #include "fitsioserver.h" |
---|
| 36 | #include "fiosinit.h" |
---|
| 37 | #include "ppersist.h" |
---|
| 38 | |
---|
| 39 | //----------------------------------------------- |
---|
[542] | 40 | const sa_size_t NUMBER_OF_CHANNELS = 2; |
---|
[544] | 41 | const sa_size_t NUMBER_OF_FREQ = 8192; |
---|
[553] | 42 | const sa_size_t TOTAL_NUM_CYCLES = 500; |
---|
[560] | 43 | const r_4 LOWER_FREQUENCY = 1250.0; //MHz |
---|
| 44 | const r_4 TOTAL_BANDWIDTH = 250.0; //MHz |
---|
[542] | 45 | //----------------------------------------------- |
---|
[544] | 46 | //Input parameters |
---|
| 47 | struct 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 |
---|
[560] | 54 | string typeOfCalib_;//type of calibration to be done |
---|
[544] | 55 | } para; |
---|
| 56 | //-------------------------------------------------------------- |
---|
| 57 | //Utility functions |
---|
[552] | 58 | |
---|
[553] | 59 | sa_size_t freqTochan(r_4 f){ |
---|
| 60 | return (sa_size_t)((f-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*NUMBER_OF_FREQ); |
---|
| 61 | } |
---|
| 62 | //--------- |
---|
[552] | 63 | class median_of_empty_list_exception:public std::exception{ |
---|
| 64 | virtual const char* what() const throw() { |
---|
| 65 | return "Attempt to take the median of an empty list of numbers. " |
---|
| 66 | "The median of an empty list is undefined."; |
---|
| 67 | } |
---|
| 68 | }; |
---|
| 69 | template<class RandAccessIter> |
---|
| 70 | double median(RandAccessIter begin, RandAccessIter end) |
---|
| 71 | throw(median_of_empty_list_exception){ |
---|
| 72 | if(begin == end){ throw median_of_empty_list_exception(); } |
---|
| 73 | std::size_t size = end - begin; |
---|
| 74 | std::size_t middleIdx = size/2; |
---|
| 75 | RandAccessIter target = begin + middleIdx; |
---|
| 76 | std::nth_element(begin, target, end); |
---|
| 77 | |
---|
| 78 | if(size % 2 != 0){ //Odd number of elements |
---|
| 79 | return *target; |
---|
| 80 | }else{ //Even number of elements |
---|
| 81 | double a = *target; |
---|
| 82 | RandAccessIter targetNeighbor= target-1; |
---|
| 83 | std::nth_element(begin, targetNeighbor, end); |
---|
| 84 | return (a+*targetNeighbor)/2.0; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
[544] | 88 | //-------------------------------------------------------------- |
---|
[540] | 89 | char *regexp (const char *string, const char *patrn, int *begin, int *end) { |
---|
| 90 | int i, w=0, len; |
---|
| 91 | char *word = NULL; |
---|
| 92 | regex_t rgT; |
---|
| 93 | regmatch_t match; |
---|
| 94 | regcomp(&rgT,patrn,REG_EXTENDED); |
---|
| 95 | if ((regexec(&rgT,string,1,&match,0)) == 0) { |
---|
| 96 | *begin = (int)match.rm_so; |
---|
| 97 | *end = (int)match.rm_eo; |
---|
| 98 | len = *end-*begin; |
---|
| 99 | word=(char*)malloc(len+1); |
---|
| 100 | for (i=*begin; i<*end; i++) { |
---|
| 101 | word[w] = string[i]; |
---|
| 102 | w++; } |
---|
| 103 | word[w]=0; |
---|
| 104 | } |
---|
| 105 | regfree(&rgT); |
---|
| 106 | return word; |
---|
| 107 | } |
---|
[544] | 108 | //------- |
---|
[540] | 109 | sa_size_t round_sa(r_4 r) { |
---|
| 110 | return static_cast<sa_size_t>((r > 0.0) ? (r + 0.5) : (r - 0.5)); |
---|
| 111 | } |
---|
| 112 | //----- |
---|
| 113 | string StringToLower(string strToConvert){ |
---|
| 114 | //change each element of the string to lower case |
---|
| 115 | for(unsigned int i=0;i<strToConvert.length();i++) { |
---|
| 116 | strToConvert[i] = tolower(strToConvert[i]); |
---|
| 117 | } |
---|
| 118 | return strToConvert;//return the converted string |
---|
| 119 | } |
---|
| 120 | //----- |
---|
| 121 | bool stringCompare( const string &left, const string &right ){ |
---|
| 122 | if( left.size() < right.size() ) |
---|
| 123 | return true; |
---|
| 124 | for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit ) |
---|
| 125 | if( tolower( *lit ) < tolower( *rit ) ) |
---|
| 126 | return true; |
---|
| 127 | else if( tolower( *lit ) > tolower( *rit ) ) |
---|
| 128 | return false; |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | //----- |
---|
| 132 | list<string> ListOfFileInDir(string dir, string filePettern) throw(string) { |
---|
| 133 | list<string> theList; |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | DIR *dip; |
---|
| 137 | struct dirent *dit; |
---|
| 138 | string msg; string fileName; |
---|
| 139 | string fullFileName; |
---|
| 140 | size_t found; |
---|
| 141 | |
---|
| 142 | if ((dip=opendir(dir.c_str())) == NULL ) { |
---|
| 143 | msg = "opendir failed on directory "+dir; |
---|
| 144 | throw msg; |
---|
| 145 | } |
---|
| 146 | while ( (dit = readdir(dip)) != NULL ) { |
---|
| 147 | fileName = dit->d_name; |
---|
| 148 | found=fileName.find(filePettern); |
---|
| 149 | if (found != string::npos) { |
---|
| 150 | fullFileName = dir + "/"; |
---|
| 151 | fullFileName += fileName; |
---|
| 152 | theList.push_back(fullFileName); |
---|
| 153 | } |
---|
| 154 | }//eo while |
---|
| 155 | if (closedir(dip) == -1) { |
---|
| 156 | msg = "closedir failed on directory "+dir; |
---|
| 157 | throw msg; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | theList.sort(stringCompare); |
---|
| 161 | |
---|
| 162 | return theList; |
---|
| 163 | |
---|
| 164 | } |
---|
| 165 | // |
---|
| 166 | class StringMatch : public unary_function<string,bool> { |
---|
| 167 | public: |
---|
| 168 | StringMatch(const string& pattern): pattern_(pattern){} |
---|
| 169 | bool operator()(const string& aStr) const { |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | int b,e; |
---|
| 173 | regexp(aStr.c_str(),pattern_.c_str(),&b,&e); |
---|
| 174 | |
---|
[541] | 175 | // cout << "investigate " << aStr << " to find " << pattern_ |
---|
| 176 | // << "[" <<b<<","<<e<<"]" |
---|
| 177 | // << endl; |
---|
[540] | 178 | |
---|
| 179 | |
---|
| 180 | if (b != 0) return false; |
---|
[544] | 181 | if (e != (int)aStr.size()) return false; |
---|
[540] | 182 | return true; |
---|
| 183 | |
---|
| 184 | } |
---|
| 185 | private: |
---|
| 186 | string pattern_; |
---|
| 187 | }; |
---|
| 188 | //------------------------------------------------------- |
---|
[544] | 189 | //Rebin in frequence + compute mean and sigma |
---|
| 190 | void reduceSpectra(const TMatrix<r_4>& specMtxInPut, |
---|
| 191 | TMatrix<r_4>& meanMtx, |
---|
| 192 | TMatrix<r_4>& sigmaMtx) { |
---|
| 193 | sa_size_t nSliceFreq = para.nSliceInFreq_; |
---|
| 194 | sa_size_t deltaFreq = NUMBER_OF_FREQ/nSliceFreq; |
---|
| 195 | for (sa_size_t iSlice=0; iSlice<nSliceFreq; iSlice++){ |
---|
| 196 | sa_size_t freqLow= iSlice*deltaFreq; |
---|
| 197 | sa_size_t freqHigh= freqLow + deltaFreq -1; |
---|
| 198 | for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){ |
---|
| 199 | TVector<r_4> reducedRow; |
---|
| 200 | reducedRow = specMtxInPut.SubMatrix(Range(iCh),Range(freqLow,freqHigh)).CompactAllDimensions(); |
---|
| 201 | double mean; |
---|
| 202 | double sigma; |
---|
| 203 | MeanSigma(reducedRow,mean,sigma); |
---|
| 204 | meanMtx(iCh,iSlice) = mean; |
---|
| 205 | sigmaMtx(iCh,iSlice) = sigma; |
---|
| 206 | }//eo loop on channels |
---|
| 207 | }//eo loop on slices |
---|
| 208 | } |
---|
[541] | 209 | //------------------------------------------------------- |
---|
[560] | 210 | //Compute the mean of Diff ON-OFF BAO-calibrated spectra and also the mean/sigma of rebinned spectra |
---|
| 211 | //Used like: |
---|
| 212 | // |
---|
| 213 | void meanCalibBAODiffOnOffCycles() throw(string) { |
---|
| 214 | |
---|
| 215 | list<string> listOfFiles; |
---|
| 216 | string directoryName; |
---|
| 217 | directoryName = para.inPath_ + "/" + para.sourceName_; |
---|
| 218 | |
---|
| 219 | //Make the listing of the directory |
---|
| 220 | listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_); |
---|
| 221 | |
---|
| 222 | list<string>::const_iterator iFile, iFileEnd, iSpecOff, iSpecOffEnd, iSpecOn, iSpecOnEnd; |
---|
| 223 | iFileEnd = listOfFiles.end(); |
---|
| 224 | |
---|
| 225 | //Loop on files |
---|
| 226 | for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) { |
---|
| 227 | if (para.debuglev_>90){ |
---|
| 228 | cout << "load file <" << *iFile << ">" << endl; |
---|
| 229 | } |
---|
| 230 | PInPersist fin(*iFile); |
---|
| 231 | vector<string> vec = fin.GetNameTags(); |
---|
| 232 | |
---|
| 233 | if (para.typeOfCalib_ == "perRun") { |
---|
| 234 | /////////////////// |
---|
| 235 | //make the calibration of the mean of all Off and On of the run and perform the difference |
---|
| 236 | /////////////////// |
---|
| 237 | // |
---|
| 238 | //Compute the mean of the Off |
---|
| 239 | // |
---|
| 240 | list<string> listOfSpectraOff; |
---|
| 241 | //Keep only required PPF objects |
---|
| 242 | std::remove_copy_if( |
---|
| 243 | vec.begin(), vec.end(), back_inserter(listOfSpectra), |
---|
| 244 | not1(match) |
---|
| 245 | ); |
---|
| 246 | |
---|
| 247 | listOfSpectraOff.sort(stringCompare); |
---|
| 248 | iSpecOffEnd = listOfSpectraOff.end(); |
---|
| 249 | TMatrix<r_4> meanOfSpectraOff(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 250 | uint_4 nSpectraOff=0; |
---|
| 251 | //Loop of spectra matrix |
---|
| 252 | for (iSpecOff = listOfSpectraOff.begin(); iSpecOff !=iSpecOffEnd; ++iSpecOff){ |
---|
| 253 | if (para.debuglev_>90){ |
---|
| 254 | cout << " spactra <" << *iSpecOff << ">" << endl; |
---|
| 255 | } |
---|
| 256 | TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 257 | fin.GetObject(aSpec,*iSpecOff); |
---|
| 258 | //How to see if the GetObject is ok?? Ask Reza |
---|
| 259 | nSpectraOff++; |
---|
| 260 | meanOfSpectraOff+=aSpec; |
---|
| 261 | }//end loop Off |
---|
| 262 | //Normalisation |
---|
| 263 | if(nSpectraOff>0)meanOfSpectraOff/=(r_4)(nSpectraOff); |
---|
| 264 | // |
---|
| 265 | //Compute the mean of the On |
---|
| 266 | // |
---|
| 267 | list<string> listOfSpectraOn; |
---|
| 268 | //Keep only required PPF objects |
---|
| 269 | std::remove_copy_if( |
---|
| 270 | vec.begin(), vec.end(), back_inserter(listOfSpectra), |
---|
| 271 | not1(match) |
---|
| 272 | ); |
---|
| 273 | |
---|
| 274 | listOfSpectraOn.sort(stringCompare); |
---|
| 275 | iSpecOnEnd = listOfSpectraOn.end(); |
---|
| 276 | TMatrix<r_4> meanOfSpectraOn(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 277 | uint_4 nSpectraOn=0; |
---|
| 278 | //Loop of spectra matrix |
---|
| 279 | for (iSpecOn = listOfSpectraOn.begin(); iSpecOn !=iSpecOnEnd; ++iSpecOn){ |
---|
| 280 | if (para.debuglev_>90){ |
---|
| 281 | cout << " spactra <" << *iSpecOn << ">" << endl; |
---|
| 282 | } |
---|
| 283 | TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 284 | fin.GetObject(aSpec,*iSpecOn); |
---|
| 285 | //How to see if the GetObject is ok?? Ask Reza |
---|
| 286 | nSpectraOn++; |
---|
| 287 | meanOfSpectraOn+=aSpec; |
---|
| 288 | }//end loop On |
---|
| 289 | //Normalisation |
---|
| 290 | if(nSpectraOn>0)meanOfSpectraOn/=(r_4)(nSpectraOn); |
---|
| 291 | // |
---|
| 292 | //Get the calibration file |
---|
| 293 | // |
---|
| 294 | |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | } else if {para.typeOfCalib_ == "perCycle") { |
---|
| 298 | //perform the calibration of the OFF and ON per cycle, then make the mean and take the diff |
---|
| 299 | } else { |
---|
| 300 | string msg="FATAL (meanCalibBAODiffOnOffCycles); unknown calibration mode " |
---|
| 301 | + para.typeOfCalib_ ; |
---|
| 302 | throw(msg); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | |
---|
| 306 | }//eo loop on spectra in a file |
---|
| 307 | }//eo loop on files |
---|
| 308 | |
---|
| 309 | } |
---|
| 310 | //------------------------------------------------------- |
---|
| 311 | //Compute the mean of Diff ON-OFF Raw spectra and also the mean/sigma of rebinned spectra |
---|
[544] | 312 | //Used like: |
---|
| 313 | // |
---|
[560] | 314 | void meanRawDiffOnOffCycles() throw(string) { |
---|
[540] | 315 | list<string> listOfFiles; |
---|
| 316 | string directoryName; |
---|
| 317 | directoryName = para.inPath_ + "/" + para.sourceName_; |
---|
| 318 | |
---|
[542] | 319 | //Make the listing of the directory |
---|
[541] | 320 | listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_); |
---|
[540] | 321 | |
---|
[541] | 322 | list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd; |
---|
[540] | 323 | iFileEnd = listOfFiles.end(); |
---|
| 324 | |
---|
[542] | 325 | StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects |
---|
[544] | 326 | TMatrix<r_4> meanOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
[542] | 327 | uint_4 nSpectra=0; |
---|
| 328 | //Loop on files |
---|
[540] | 329 | for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) { |
---|
[542] | 330 | if (para.debuglev_>90){ |
---|
| 331 | cout << "load file <" << *iFile << ">" << endl; |
---|
| 332 | } |
---|
[540] | 333 | PInPersist fin(*iFile); |
---|
| 334 | vector<string> vec = fin.GetNameTags(); |
---|
[541] | 335 | list<string> listOfSpectra; |
---|
[542] | 336 | //Keep only required PPF objects |
---|
[540] | 337 | std::remove_copy_if( |
---|
| 338 | vec.begin(), vec.end(), back_inserter(listOfSpectra), |
---|
| 339 | not1(match) |
---|
| 340 | ); |
---|
| 341 | |
---|
[560] | 342 | listOfSpectra.sort(stringCompare); |
---|
[541] | 343 | iSpecEnd = listOfSpectra.end(); |
---|
[542] | 344 | //Loop of spectra matrix |
---|
[541] | 345 | for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd; ++iSpec){ |
---|
[542] | 346 | if (para.debuglev_>90){ |
---|
| 347 | cout << " spactra <" << *iSpec << ">" << endl; |
---|
| 348 | } |
---|
| 349 | TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 350 | fin.GetObject(aSpec,*iSpec); |
---|
| 351 | //How to see if the GetObject is ok?? Ask Reza |
---|
| 352 | nSpectra++; |
---|
[544] | 353 | meanOfSpectra+=aSpec; |
---|
[542] | 354 | }//eo loop on spectra in a file |
---|
| 355 | }//eo loop on files |
---|
| 356 | |
---|
| 357 | //Normalisation |
---|
[544] | 358 | if(nSpectra>0)meanOfSpectra/=(r_4)(nSpectra); |
---|
[541] | 359 | |
---|
[544] | 360 | //Compute the reduced version of the mean and sigma |
---|
| 361 | TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_); |
---|
| 362 | TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_); |
---|
| 363 | reduceSpectra(meanOfSpectra,meanRedMtx,sigmaRedMtx); |
---|
| 364 | |
---|
[542] | 365 | {//Save the result |
---|
| 366 | stringstream tmp; |
---|
| 367 | tmp << nSpectra; |
---|
| 368 | string fileName = para.outPath_+"/meanDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf"; |
---|
| 369 | cout << "Save mean based on " << nSpectra << " cycles " << endl; |
---|
[544] | 370 | POutPersist fos(fileName); |
---|
| 371 | |
---|
[542] | 372 | string tag = "mean"; |
---|
[544] | 373 | fos << PPFNameTag(tag) << meanOfSpectra; |
---|
| 374 | tag = "meanred"; |
---|
| 375 | fos << PPFNameTag(tag) << meanRedMtx; |
---|
| 376 | tag = "sigmared"; |
---|
| 377 | fos << PPFNameTag(tag) << sigmaRedMtx; |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | //------------------------------------------------------- |
---|
[560] | 381 | //Compute the median of Diff ON-OFF Raw spectra and also the mean/sigma of rebinned spectra |
---|
[544] | 382 | //Used like: |
---|
| 383 | // |
---|
[560] | 384 | void medianRawDiffOnOffCycles() throw(string) { |
---|
[544] | 385 | list<string> listOfFiles; |
---|
| 386 | string directoryName; |
---|
| 387 | directoryName = para.inPath_ + "/" + para.sourceName_; |
---|
| 388 | |
---|
| 389 | //Make the listing of the directory |
---|
| 390 | listOfFiles = ListOfFileInDir(directoryName,para.ppfFile_); |
---|
| 391 | |
---|
| 392 | list<string>::const_iterator iFile, iFileEnd, iSpec, iSpecEnd; |
---|
| 393 | iFileEnd = listOfFiles.end(); |
---|
| 394 | |
---|
| 395 | |
---|
| 396 | TArray<r_4> tableOfSpectra(NUMBER_OF_FREQ,NUMBER_OF_CHANNELS,TOTAL_NUM_CYCLES); //TOTAL_NUM_CYCLES should be large enough... |
---|
| 397 | |
---|
| 398 | StringMatch match("specONOFFRaw[0-9]+"); //Tag of the PPF objects |
---|
| 399 | uint_4 nSpectra=0; |
---|
| 400 | //Loop on files |
---|
| 401 | for (iFile = listOfFiles.begin(); iFile != iFileEnd; ++iFile) { |
---|
| 402 | if (para.debuglev_>90){ |
---|
| 403 | cout << "load file <" << *iFile << ">" << endl; |
---|
| 404 | } |
---|
| 405 | PInPersist fin(*iFile); |
---|
| 406 | vector<string> vec = fin.GetNameTags(); |
---|
| 407 | list<string> listOfSpectra; |
---|
| 408 | //Keep only required PPF objects |
---|
| 409 | std::remove_copy_if( |
---|
| 410 | vec.begin(), vec.end(), back_inserter(listOfSpectra), |
---|
| 411 | not1(match) |
---|
| 412 | ); |
---|
| 413 | |
---|
[560] | 414 | listOfSpectra.sort(stringCompare); |
---|
[544] | 415 | iSpecEnd = listOfSpectra.end(); |
---|
| 416 | //Loop of spectra matrix |
---|
[553] | 417 | for (iSpec = listOfSpectra.begin(); iSpec !=iSpecEnd && (sa_size_t)nSpectra < TOTAL_NUM_CYCLES ; ++iSpec){ |
---|
[544] | 418 | if (para.debuglev_>90){ |
---|
| 419 | cout << " spactra <" << *iSpec << ">" << endl; |
---|
| 420 | } |
---|
| 421 | TMatrix<r_4> aSpec(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 422 | fin.GetObject(aSpec,*iSpec); |
---|
| 423 | |
---|
[553] | 424 | tableOfSpectra(Range::all(),Range::all(),Range(nSpectra)) = aSpec; |
---|
| 425 | |
---|
[544] | 426 | nSpectra++; |
---|
| 427 | }//eo loop on spectra in a file |
---|
| 428 | }//eo loop on files |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | |
---|
| 432 | TMatrix<r_4> medianOfSpectra(NUMBER_OF_CHANNELS,NUMBER_OF_FREQ); |
---|
| 433 | //Compute the median for each freq. and Channel |
---|
| 434 | for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; iCh++){ |
---|
| 435 | for (sa_size_t freq =0; freq<NUMBER_OF_FREQ; freq++){ |
---|
[552] | 436 | TVector<r_4> tmp0(tableOfSpectra(Range(freq),Range(iCh),Range(0,nSpectra-1)).CompactAllDimensions()); |
---|
[544] | 437 | vector<r_4> tmp; |
---|
| 438 | tmp0.FillTo(tmp); |
---|
[552] | 439 | medianOfSpectra(iCh,freq) = median(tmp.begin(),tmp.end()); |
---|
[544] | 440 | } |
---|
| 441 | } |
---|
| 442 | |
---|
| 443 | |
---|
| 444 | //Compute the reduced version of the mean and sigma |
---|
| 445 | TMatrix<r_4> meanRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_); |
---|
| 446 | TMatrix<r_4> sigmaRedMtx(NUMBER_OF_CHANNELS,para.nSliceInFreq_); |
---|
| 447 | reduceSpectra(medianOfSpectra,meanRedMtx,sigmaRedMtx); |
---|
| 448 | |
---|
[553] | 449 | |
---|
| 450 | sa_size_t f1320=freqTochan(1320.); |
---|
| 451 | sa_size_t f1345=freqTochan(1345.); |
---|
| 452 | sa_size_t f1355=freqTochan(1355.); |
---|
| 453 | sa_size_t f1380=freqTochan(1380.); |
---|
| 454 | //Compute baseline arround 1350Mhz on [1320-1345] U [1355-1380] |
---|
| 455 | if (para.debuglev_>9){ |
---|
| 456 | cout << "Compute baseline arround 1350Mhz on [1320-1345] U [1355-1380]" << endl; |
---|
| 457 | } |
---|
| 458 | TVector<r_4>meanMed(NUMBER_OF_CHANNELS); |
---|
| 459 | for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){ |
---|
| 460 | double meanMed1; |
---|
| 461 | double sigmaMed1; |
---|
| 462 | TVector<r_4> band1; |
---|
| 463 | band1 = medianOfSpectra(Range(iCh),Range(f1320,f1345)).CompactAllDimensions(); |
---|
| 464 | MeanSigma(band1,meanMed1,sigmaMed1); |
---|
| 465 | double meanMed2; |
---|
| 466 | double sigmaMed2; |
---|
| 467 | TVector<r_4> band2; |
---|
| 468 | band2 = medianOfSpectra(Range(iCh),Range(f1355,f1380)).CompactAllDimensions(); |
---|
| 469 | MeanSigma(band2,meanMed2,sigmaMed2); |
---|
| 470 | meanMed(iCh) = (meanMed1+meanMed2)*0.5; |
---|
| 471 | } |
---|
| 472 | meanMed.Print(cout); |
---|
| 473 | |
---|
| 474 | //Compute the sigma in the range 1320MHz-1380MHz |
---|
| 475 | if (para.debuglev_>9){ |
---|
| 476 | cout << "Compute the sigma in the range 1320MHz-1380MHz" << endl; |
---|
| 477 | } |
---|
| 478 | TVector<r_4>sigmaMed(NUMBER_OF_CHANNELS); |
---|
[560] | 479 | sa_size_t redf1320=(sa_size_t)((1320.0-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*para.nSliceInFreq_); |
---|
| 480 | sa_size_t redf1380=(sa_size_t)((1380.0-LOWER_FREQUENCY)/TOTAL_BANDWIDTH*para.nSliceInFreq_); |
---|
[553] | 481 | |
---|
| 482 | for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){ |
---|
| 483 | double meanSigma; |
---|
| 484 | double sigmaSigma; |
---|
| 485 | TVector<r_4> band; |
---|
| 486 | band = sigmaRedMtx(Range(iCh),Range(redf1320,redf1380)).CompactAllDimensions(); |
---|
| 487 | MeanSigma(band,meanSigma,sigmaSigma); |
---|
| 488 | meanSigma *= sqrt(para.nSliceInFreq_); //to scale to orignal spectra |
---|
| 489 | sigmaMed(iCh) = meanSigma; |
---|
| 490 | } |
---|
| 491 | sigmaMed.Print(cout); |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | if (para.debuglev_>9){ |
---|
| 495 | cout << "Compute medianOfSpectraNorm" << endl; |
---|
| 496 | } |
---|
| 497 | TMatrix<r_4> medianOfSpectraNorm(medianOfSpectra,false); //do not share the data... |
---|
| 498 | for (sa_size_t iCh=0; iCh<NUMBER_OF_CHANNELS; ++iCh){ |
---|
| 499 | medianOfSpectraNorm.Row(iCh) -= meanMed(iCh); |
---|
| 500 | medianOfSpectraNorm.Row(iCh) /= sigmaMed(iCh); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | |
---|
| 504 | |
---|
[544] | 505 | {//Save the result |
---|
| 506 | stringstream tmp; |
---|
| 507 | tmp << nSpectra; |
---|
| 508 | string fileName = para.outPath_+"/medianDiffOnOffRaw_"+StringToLower(para.sourceName_)+"-"+tmp.str()+"Cycles.ppf"; |
---|
[552] | 509 | cout << "Save median based on " << nSpectra << " cycles " << endl; |
---|
[542] | 510 | POutPersist fos(fileName); |
---|
[544] | 511 | |
---|
| 512 | string tag = "median"; |
---|
| 513 | fos << PPFNameTag(tag) << medianOfSpectra; |
---|
[553] | 514 | |
---|
| 515 | tag = "medianNorm"; |
---|
| 516 | fos << PPFNameTag(tag) << medianOfSpectraNorm; |
---|
| 517 | |
---|
| 518 | |
---|
[544] | 519 | tag = "meanmedred"; |
---|
| 520 | fos << PPFNameTag(tag) << meanRedMtx; |
---|
| 521 | tag = "sigmamedred"; |
---|
| 522 | fos << PPFNameTag(tag) << sigmaRedMtx; |
---|
[552] | 523 | tag = "cycleVsfreq"; |
---|
| 524 | |
---|
| 525 | TArray<r_4> tarr(tableOfSpectra(Range::all(),Range::all(),Range(0,nSpectra-1))); |
---|
| 526 | fos << PPFNameTag(tag) << tarr; |
---|
[540] | 527 | } |
---|
| 528 | } |
---|
[544] | 529 | |
---|
[540] | 530 | //------------------------------------------------------- |
---|
[544] | 531 | int main(int narg, char* arg[]) { |
---|
[540] | 532 | |
---|
| 533 | int rc = 0; //return code |
---|
| 534 | string msg; //message used in Exceptions |
---|
| 535 | |
---|
| 536 | |
---|
[544] | 537 | |
---|
| 538 | //default value for initial parameters (see Para structure on top of the file) |
---|
[540] | 539 | string debuglev = "0"; |
---|
[544] | 540 | string action = "meanDiffOnOff"; |
---|
[540] | 541 | string inputPath = "."; |
---|
| 542 | string outputPath = "."; |
---|
| 543 | string sourceName = "Abell85"; |
---|
[541] | 544 | string ppfFile; |
---|
[544] | 545 | string nSliceInFreq = "32"; |
---|
[560] | 546 | string typeOfCalib="perRun"; |
---|
[540] | 547 | |
---|
| 548 | // bool okarg=false; |
---|
| 549 | int ka=1; |
---|
[554] | 550 | while (ka<narg) { |
---|
[551] | 551 | if (strcmp(arg[ka],"-h")==0) { |
---|
[560] | 552 | cout << "Usage: -act [meanRawDiffOnOff]|medianRawDiffOnOff|meanCalibBAODiffOnOff\n" |
---|
| 553 | << " -calibopt [perRun]|perCcyle (cf. in case of calibration to be done)" |
---|
[551] | 554 | << " -src [Abell85]\n -inPath [.]|<top_root_dir of the ppf file>\n" |
---|
| 555 | << " (ex. /sps/baoradio/AmasNancay/JEC/\n " |
---|
| 556 | << " -outPath [.]|<dir of the output> \n" |
---|
| 557 | << " -nSliceInFreq [32]|<number of bin in freq. to cumulate>\n" |
---|
| 558 | << " -ppfFile <generic name of the input ppf files> (ex. diffOnOffRaw)" |
---|
| 559 | << " -debug <level> " |
---|
| 560 | << endl; |
---|
| 561 | return 0; |
---|
| 562 | } |
---|
| 563 | else if (strcmp(arg[ka],"-debug")==0) { |
---|
[540] | 564 | debuglev=arg[ka+1]; |
---|
| 565 | ka+=2; |
---|
| 566 | } |
---|
| 567 | else if (strcmp(arg[ka],"-act")==0) { |
---|
| 568 | action=arg[ka+1]; |
---|
| 569 | ka+=2; |
---|
| 570 | } |
---|
[560] | 571 | else if (strcmp(arg[ka],"-calibopt")==0) { |
---|
| 572 | typeOfCalib=arg[ka+1]; |
---|
| 573 | ka+=2; |
---|
| 574 | } |
---|
[540] | 575 | else if (strcmp(arg[ka],"-inPath")==0) { |
---|
| 576 | inputPath=arg[ka+1]; |
---|
| 577 | ka+=2; |
---|
| 578 | } |
---|
| 579 | else if (strcmp(arg[ka],"-outPath")==0) { |
---|
| 580 | outputPath=arg[ka+1]; |
---|
| 581 | ka+=2; |
---|
| 582 | } |
---|
[541] | 583 | else if (strcmp(arg[ka],"-src")==0) { |
---|
[540] | 584 | sourceName=arg[ka+1]; |
---|
| 585 | ka+=2; |
---|
| 586 | } |
---|
[541] | 587 | else if (strcmp(arg[ka],"-ppfFile")==0) { |
---|
| 588 | ppfFile=arg[ka+1]; |
---|
[540] | 589 | ka+=2; |
---|
| 590 | } |
---|
[544] | 591 | else if (strcmp(arg[ka],"-nSliceInFreq")==0) { |
---|
| 592 | nSliceInFreq=arg[ka+1]; |
---|
| 593 | ka+=2; |
---|
| 594 | } |
---|
[540] | 595 | else ka++; |
---|
| 596 | }//eo while |
---|
| 597 | |
---|
[544] | 598 | para.debuglev_ = atoi(debuglev.c_str()); |
---|
| 599 | para.inPath_ = inputPath; |
---|
| 600 | para.outPath_ = outputPath; |
---|
[540] | 601 | para.sourceName_ = sourceName; |
---|
[544] | 602 | para.ppfFile_ = ppfFile; |
---|
| 603 | para.nSliceInFreq_ = atoi(nSliceInFreq.c_str()); |
---|
[540] | 604 | |
---|
| 605 | cout << "Dump Initial parameters ............" << endl; |
---|
| 606 | cout << " action = " << action << "\n" |
---|
[544] | 607 | << " inputPath = " << para.inPath_ << "\n" |
---|
| 608 | << " outputPath = " << para.outPath_ << "\n" |
---|
| 609 | << " sourceName = " << para.sourceName_ << "\n" |
---|
| 610 | << " ppfFile = " << para.ppfFile_ << "\n" |
---|
| 611 | << " nSliceInFreq = " << para.nSliceInFreq_ << "\n" |
---|
| 612 | << " debuglev = " << para.debuglev_ << "\n"; |
---|
[540] | 613 | cout << "...................................." << endl; |
---|
| 614 | |
---|
[541] | 615 | if ( "" == ppfFile ) { |
---|
[560] | 616 | cerr << "mergeAnaFiles.cc: you have forgotten ppfFile option" |
---|
[541] | 617 | << endl; |
---|
| 618 | return 999; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | |
---|
[540] | 622 | try { |
---|
| 623 | |
---|
[542] | 624 | // int b,e; |
---|
| 625 | // char *match=regexp("truc0machin","[a-z]+[0-9]*",&b,&e); |
---|
| 626 | // printf("->%s<-\n(b=%d e=%d)\n",match,b,e); |
---|
[540] | 627 | |
---|
[560] | 628 | if ( action == "meanRawDiffOnOff" ) { |
---|
| 629 | meanRawDiffOnOffCycles(); |
---|
| 630 | } else if (action == "medianRawDiffOnOff") { |
---|
| 631 | medianRawDiffOnOffCycles(); |
---|
| 632 | } else if (action == "meanCalibBAODiffOnOff") { |
---|
| 633 | meanCalibBAODiffOnOffCycles(); |
---|
[544] | 634 | } else { |
---|
| 635 | msg = "Unknown action " + action; |
---|
| 636 | throw msg; |
---|
[540] | 637 | } |
---|
| 638 | |
---|
[544] | 639 | |
---|
[540] | 640 | } catch (std::exception& sex) { |
---|
| 641 | cerr << "mergeRawOnOff.cc std::exception :" << (string)typeid(sex).name() |
---|
| 642 | << "\n msg= " << sex.what() << endl; |
---|
| 643 | rc = 78; |
---|
| 644 | } |
---|
| 645 | catch ( string str ) { |
---|
| 646 | cerr << "mergeRawOnOff.cc Exception raised: " << str << endl; |
---|
| 647 | } |
---|
| 648 | catch (...) { |
---|
| 649 | cerr << "mergeRawOnOff.cc catched unknown (...) exception " << endl; |
---|
| 650 | rc = 79; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | return 0; |
---|
| 654 | |
---|
| 655 | } |
---|