| 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
 | 
|---|
| 2 | //                               Eric Aubourg
 | 
|---|
| 3 | //                               Christophe Magneville
 | 
|---|
| 4 | //                               Reza Ansari
 | 
|---|
| 5 | 
 | 
|---|
| 6 | /*   Calcul de statistique simples sur TOI  
 | 
|---|
| 7 | 
 | 
|---|
| 8 | ----------------   Exemple d'appel  ---------------------
 | 
|---|
| 9 | csh> toistat -start 104385384 -end 104399964 -range -500,500 \
 | 
|---|
| 10 |             -intoi boloMuV_26 -wtoi 8192 -wclean 512,5 \
 | 
|---|
| 11 |             inputbolo.fits filt.fits xx.ppf
 | 
|---|
| 12 | */
 | 
|---|
| 13 | 
 | 
|---|
| 14 | 
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "machdefs.h"
 | 
|---|
| 17 | #include <math.h>
 | 
|---|
| 18 | #include <unistd.h>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "toimanager.h"
 | 
|---|
| 21 | #include "cgt.h"
 | 
|---|
| 22 | #include "fitstoirdr.h"
 | 
|---|
| 23 | #include "fitstoiwtr.h"
 | 
|---|
| 24 | #include "simtoipr.h"
 | 
|---|
| 25 | #include "simoffset.h"
 | 
|---|
| 26 | #include "simcleaner.h"
 | 
|---|
| 27 | #include "nooppr.h"
 | 
|---|
| 28 | #include "timing.h"
 | 
|---|
| 29 | #include "histinit.h"
 | 
|---|
| 30 | #include "ntuple.h"
 | 
|---|
| 31 | #include "fitsntuple.h"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "psighand.h"
 | 
|---|
| 34 | #include <stdexcept>
 | 
|---|
| 35 | 
 | 
|---|
| 36 | void Usage(bool fgerr)
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   cout << endl;
 | 
|---|
| 39 |   if (fgerr) {
 | 
|---|
| 40 |     cout << " toistat : Argument Error ! toistat -h for usage " << endl; 
 | 
|---|
| 41 |     exit(1);
 | 
|---|
| 42 |   }
 | 
|---|
| 43 |   else {
 | 
|---|
| 44 |     cout << " toistat : produce a stat NTuple (mean,sigma) from a TOI" << endl;
 | 
|---|
| 45 |     cout << "\n Usage : toistat [-intoi toiname] [-flag mask] \n " 
 | 
|---|
| 46 |          << "         [-start snb] [-end sne] [-snimplicit] \n" 
 | 
|---|
| 47 |          << "         [-wtoi sz] [-wclean wsz,nbw] [-range min,max] [-cleannsig nsig] \n"
 | 
|---|
| 48 |          << "         [-outppf] [-noprstat] [-useseqbuff] \n"
 | 
|---|
| 49 |          << "         [-sepflg sepFlagFile] inFitsName outFileName \n" 
 | 
|---|
| 50 |          << "   -start snb : sets the start sample num \n"
 | 
|---|
| 51 |          << "   -end sne : sets the end sample num \n"
 | 
|---|
| 52 |          << "   -snimplicit : sampleNum are implicit in fits files (def=no) \n"
 | 
|---|
| 53 |          << "   -range min,max : sets the acceptable range for intoi \n"
 | 
|---|
| 54 |          << "              default= -16000,16000\n"
 | 
|---|
| 55 |          << "   -intoi toiName : select input TOI name (def bolo)\n"
 | 
|---|
| 56 |          << "   -flag mask : set TOI flag mask (def = 0 - no flag) \n"
 | 
|---|
| 57 |          << "   -sepflg sepFlagFileName: sets separate flag file (Level2)\n" 
 | 
|---|
| 58 |          << "   -wtoi sz : sets TOISeqBuff buffer size (def= 8192)\n"
 | 
|---|
| 59 |          << "   -wclean wsz,nbw : sets cleaner window parameters (256,5) \n"
 | 
|---|
| 60 |          << "   -cleannsig nsig : Sets cleanner ThrNSig (default = 999999.) \n"
 | 
|---|
| 61 |          << "   -outppf : Write the NTuple in PPF format (default: FITS) \n"
 | 
|---|
| 62 |          << "   -noprstat : DO NOT PrintStat with ProcSampleCounter \n"
 | 
|---|
| 63 |          << "   -useseqbuff : Use TOISeqBuffered TOI's (default: TOISegmented) \n"
 | 
|---|
| 64 |          << endl;
 | 
|---|
| 65 |   }
 | 
|---|
| 66 |   if (fgerr) exit(1); 
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | int main(int narg, char** arg) {
 | 
|---|
| 70 | 
 | 
|---|
| 71 |   if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   cout << "toistat starting - Decoding arguments " << " narg=" << narg << endl;
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   bool fgsetstart = false;
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   bool fgprstat = true;
 | 
|---|
| 78 |   bool fgsegmented = true;
 | 
|---|
| 79 |   bool fgoutppf = false;
 | 
|---|
| 80 |   bool snimplicit = false;
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   bool fgmask = false;
 | 
|---|
| 83 |   uint_8 flagmask = 0;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   int dbglev = 0;
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   int wtoi = 8192;
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   int nmax = 10;
 | 
|---|
| 90 |   int istart = 0;
 | 
|---|
| 91 |   int iend = 0;
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   double range_min = -16000;
 | 
|---|
| 94 |   double range_max = 16000.;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   // cleaner parameters
 | 
|---|
| 97 |   int clean_wsz = 256;
 | 
|---|
| 98 |   int clean_nbw = 5;
 | 
|---|
| 99 |   double clean_nsig = 999999.;
 | 
|---|
| 100 | 
 | 
|---|
| 101 | // Fichier de flag separe / Level2 / Reza 18/6/2002
 | 
|---|
| 102 |   string sepflagfile;   
 | 
|---|
| 103 |   bool sepflg = false;  
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   // File names 
 | 
|---|
| 106 |   string infile;
 | 
|---|
| 107 |   string outfile;
 | 
|---|
| 108 |   string intoi = "bolo"; 
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   if (narg < 3) Usage(true);
 | 
|---|
| 111 |   int ko=1;
 | 
|---|
| 112 |   // decoding arguments 
 | 
|---|
| 113 |   for(int ia=1; ia<narg; ia++) {
 | 
|---|
| 114 |     if (strcmp(arg[ia],"-start") == 0) {
 | 
|---|
| 115 |       if (ia == narg-1) Usage(true);  // -start est suivi d'un argument 
 | 
|---|
| 116 |       istart = atoi(arg[ia+1]); ia++;
 | 
|---|
| 117 |       fgsetstart = true;
 | 
|---|
| 118 |     }    
 | 
|---|
| 119 |     else if (strcmp(arg[ia],"-end") == 0) {
 | 
|---|
| 120 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 121 |       iend = atoi(arg[ia+1]); ia++;
 | 
|---|
| 122 |     }    
 | 
|---|
| 123 |     else if (strcmp(arg[ia],"-snimplicit") == 0)  snimplicit = true;
 | 
|---|
| 124 |     else if (strcmp(arg[ia],"-wtoi") == 0) {
 | 
|---|
| 125 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 126 |       wtoi = atoi(arg[ia+1]); ia++;
 | 
|---|
| 127 |     }    
 | 
|---|
| 128 |     else if (strcmp(arg[ia],"-wclean") == 0) {
 | 
|---|
| 129 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 130 |       sscanf(arg[ia+1],"%d,%d",  &clean_wsz, &clean_nbw);  ia++;
 | 
|---|
| 131 |     }    
 | 
|---|
| 132 |     else if (strcmp(arg[ia],"-cleannsig") == 0) {
 | 
|---|
| 133 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 134 |       clean_nsig = atof(arg[ia+1]); ia++;
 | 
|---|
| 135 |     }    
 | 
|---|
| 136 |     else if (strcmp(arg[ia],"-range") == 0) {
 | 
|---|
| 137 |       if (ia == narg-1) Usage(true);
 | 
|---|
| 138 |       sscanf(arg[ia+1],"%lf,%lf",&range_min, &range_max);
 | 
|---|
| 139 |       ia++;
 | 
|---|
| 140 |     }    
 | 
|---|
| 141 |     else if (strcmp(arg[ia],"-intoi") == 0) {
 | 
|---|
| 142 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 143 |       intoi = arg[ia+1]; ia++;
 | 
|---|
| 144 |     }    
 | 
|---|
| 145 |     else if (strcmp(arg[ia],"-flag") == 0) {
 | 
|---|
| 146 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 147 |       flagmask = atol(arg[ia+1]); 
 | 
|---|
| 148 |       fgmask = true; ia++;
 | 
|---|
| 149 |     }    
 | 
|---|
| 150 |     else if (strcmp(arg[ia],"-sepflg") == 0) {
 | 
|---|
| 151 |       if (ia == narg-1) Usage(true);  
 | 
|---|
| 152 |       sepflagfile = arg[ia+1]; 
 | 
|---|
| 153 |       sepflg = true; ia++;
 | 
|---|
| 154 |     }    
 | 
|---|
| 155 |     else if (strcmp(arg[ia],"-outppf") == 0)  fgoutppf = true;
 | 
|---|
| 156 | 
 | 
|---|
| 157 |     else if (strcmp(arg[ia],"-noprstat") == 0)  fgprstat = false;
 | 
|---|
| 158 |     else if (strcmp(arg[ia],"-prstat") == 0)  fgprstat = true;
 | 
|---|
| 159 |     else if (strcmp(arg[ia],"-useseqbuff") == 0)  fgsegmented = false;
 | 
|---|
| 160 | 
 | 
|---|
| 161 |     else { ko = ia; break; }  // Debut des noms
 | 
|---|
| 162 |   }
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   if (iend < istart) iend = istart+wtoi*(nmax+5);
 | 
|---|
| 165 |   if ((narg-ko) < 2)  Usage(true); 
 | 
|---|
| 166 |   infile = arg[ko];
 | 
|---|
| 167 |   outfile = arg[ko+1];
 | 
|---|
| 168 |   //  outppfname = arg[ko+2];
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   cout << " Initializing SOPHYA ... " << endl;
 | 
|---|
| 171 |   SophyaInit();
 | 
|---|
| 172 |   //  SophyaConfigureSignalhandling(true);
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   InitTim();
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   cout << ">>>> toistat: Infile= " << infile << " outFile=" 
 | 
|---|
| 177 |        << outfile << endl; 
 | 
|---|
| 178 |   cout << "  iStart= " << istart << " iEnd= " << iend << endl;
 | 
|---|
| 179 |   cout << ">>>> InTOIName= " << intoi << endl;
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   try {
 | 
|---|
| 182 |     TOIManager* mgr = TOIManager::getManager();
 | 
|---|
| 183 |   
 | 
|---|
| 184 |     //  mgr->setRequestedSample(11680920,11710584);
 | 
|---|
| 185 |     //  mgr->setRequestedSample(104121000, 104946120);
 | 
|---|
| 186 |     if (fgsetstart) 
 | 
|---|
| 187 |       mgr->setRequestedSample(istart, iend);
 | 
|---|
| 188 | 
 | 
|---|
| 189 |     cout << "> Creating FITSTOIReader object - InFile=" << infile << endl;
 | 
|---|
| 190 |     FITSTOIReader r(infile); 
 | 
|---|
| 191 |     if (snimplicit) {
 | 
|---|
| 192 |       cout << " Setting Implicit SN flag for reader: r.setImplicitSN()" << endl;
 | 
|---|
| 193 |       r.setImplicitSN();
 | 
|---|
| 194 |     }
 | 
|---|
| 195 |     if (sepflg) {
 | 
|---|
| 196 |       cout << " Setting separate flag file for InTOI_bolo File=" << sepflagfile 
 | 
|---|
| 197 |            << " (Flags=FlgToiSpike, FlgToiSource)" << endl;
 | 
|---|
| 198 |       vector<FlagToiDef> flgcol;
 | 
|---|
| 199 |       flgcol.push_back(FlgToiSpike);
 | 
|---|
| 200 |       flgcol.push_back(FlgToiSource);
 | 
|---|
| 201 |       r.setFlagFile(sepflagfile, flgcol);
 | 
|---|
| 202 |     }
 | 
|---|
| 203 | 
 | 
|---|
| 204 |     cout << "> Creating SimpleCleaner() " << endl;
 | 
|---|
| 205 |     SimpleCleaner cleaner(clean_wsz, clean_nbw);
 | 
|---|
| 206 |     cout << " Setting Range for cleaner: " << range_min 
 | 
|---|
| 207 |          << " - " << range_max << endl;
 | 
|---|
| 208 |     cleaner.SetRange(range_min, range_max);
 | 
|---|
| 209 |     cleaner.SetCleanForMeanThrNSig(clean_nsig);
 | 
|---|
| 210 |     if (fgmask) {
 | 
|---|
| 211 |       cout << " Setting FlagMask: cleaner.SetFlagMask( " << hex 
 | 
|---|
| 212 |            << flagmask << dec << "/" << flagmask << " ) " << endl; 
 | 
|---|
| 213 |       cleaner.SetFlagMask(flagmask);
 | 
|---|
| 214 |     }
 | 
|---|
| 215 |     cleaner.FillMeanSigNTuple(true);
 | 
|---|
| 216 | 
 | 
|---|
| 217 |     CGT plombier(fgsegmented, wtoi);
 | 
|---|
| 218 |     //    plombier.SetDebugLevel(dbglev);
 | 
|---|
| 219 | 
 | 
|---|
| 220 |     cout << "> Connecting Processors through plombier ... " << endl;
 | 
|---|
| 221 |     string inname = "in";
 | 
|---|
| 222 |     string outname = "out";
 | 
|---|
| 223 |     plombier.Connect(r, intoi, cleaner, inname);
 | 
|---|
| 224 | 
 | 
|---|
| 225 |     cout << "> Plombier status before start" << endl;
 | 
|---|
| 226 |     cout << plombier ; 
 | 
|---|
| 227 | 
 | 
|---|
| 228 |     cout << cleaner;
 | 
|---|
| 229 | 
 | 
|---|
| 230 |     PrtTim("starting processors");
 | 
|---|
| 231 |     mgr->startAll();
 | 
|---|
| 232 |     //    plombier.Start();
 | 
|---|
| 233 | 
 | 
|---|
| 234 | 
 | 
|---|
| 235 |     // ------------------- Impression continu de stat ------------------------ 
 | 
|---|
| 236 |     if (fgprstat) {
 | 
|---|
| 237 |       ProcSampleCounter<SimpleCleaner> stats(cleaner);
 | 
|---|
| 238 |       stats.InfoMessage() = "toistat/Info";
 | 
|---|
| 239 |       stats.PrintStats();
 | 
|---|
| 240 |     }
 | 
|---|
| 241 |     // -----------------------------------------------------------------------
 | 
|---|
| 242 | 
 | 
|---|
| 243 |     mgr->waitForAll();
 | 
|---|
| 244 |     PrtTim("End threads");
 | 
|---|
| 245 |     
 | 
|---|
| 246 |     cout << cleaner;
 | 
|---|
| 247 | 
 | 
|---|
| 248 |     cleaner.GetMeanSigNTuple().Info()["TOIFILE"] = infile;
 | 
|---|
| 249 |     cleaner.GetMeanSigNTuple().Info()["TOINAME"] = intoi;
 | 
|---|
| 250 |   
 | 
|---|
| 251 |     if (fgoutppf) {
 | 
|---|
| 252 |       cout << " \n --------------------------------------------------------- " << endl;
 | 
|---|
| 253 |       cout << " Saving Stats (mean-sigma) NTuple to PPF file " << outfile << endl;
 | 
|---|
| 254 |       POutPersist pos(outfile);
 | 
|---|
| 255 |       pos << cleaner.GetMeanSigNTuple();
 | 
|---|
| 256 |       cout << " \n --------------------------------------------------------- " << endl;
 | 
|---|
| 257 |     }
 | 
|---|
| 258 |     else {
 | 
|---|
| 259 |       cout << " \n --------------------------------------------------------- " << endl;
 | 
|---|
| 260 |       cout << " Saving Stats (mean-sigma NTuple to FITS file " << outfile << endl;
 | 
|---|
| 261 |       FitsOutFile  fos(outfile, FitsFile::clear);
 | 
|---|
| 262 |       fos << cleaner.GetMeanSigNTuple();
 | 
|---|
| 263 |       cout << " \n --------------------------------------------------------- " << endl;      
 | 
|---|
| 264 |     }
 | 
|---|
| 265 |   }
 | 
|---|
| 266 |   catch (PThrowable & exc) {
 | 
|---|
| 267 |     cerr << "\n toistat: Catched Exception \n" << (string)typeid(exc).name() 
 | 
|---|
| 268 |          << " - Msg= " << exc.Msg() << endl;
 | 
|---|
| 269 |   }
 | 
|---|
| 270 |   catch (const std::exception & sex) {
 | 
|---|
| 271 |     cerr << "\n toistat: Catched std::exception \n" 
 | 
|---|
| 272 |          << (string)typeid(sex).name() << endl; 
 | 
|---|
| 273 |   }
 | 
|---|
| 274 |   catch (...) {
 | 
|---|
| 275 |     cerr << "\n toistat: some other exception was caught ! " << endl;
 | 
|---|
| 276 |   }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |   return(0);
 | 
|---|
| 279 | }
 | 
|---|