[2000] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
| 5 | #include "config.h"
|
---|
| 6 |
|
---|
| 7 | #include "array.h"
|
---|
| 8 | #include "simoffset.h"
|
---|
| 9 | #include <math.h>
|
---|
| 10 | #include "toimanager.h"
|
---|
| 11 | #include "pexceptions.h"
|
---|
| 12 | #include "ctimer.h"
|
---|
| 13 | #include "xntuple.h"
|
---|
| 14 |
|
---|
| 15 | #include "flagtoidef.h"
|
---|
| 16 |
|
---|
| 17 | SimpleOffsetEstimator::SimpleOffsetEstimator(int mwsz, int nptfit, int degpol)
|
---|
[2004] | 18 | : poly((degpol > 1)?degpol:1)
|
---|
[2000] | 19 | {
|
---|
| 20 | mWSz = (mwsz > 8) ? mwsz : 8;
|
---|
| 21 | nPtFit = (nptfit > degpol+2) ? nptfit : degpol+2;
|
---|
[2004] | 22 | degPol = (degpol > 1)?degpol:1;
|
---|
[2000] | 23 | totnscount = 0;
|
---|
| 24 | totnbblock = 0;
|
---|
| 25 | SavePolyNTuple();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | SimpleOffsetEstimator::~SimpleOffsetEstimator()
|
---|
| 29 | {
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | void SimpleOffsetEstimator::PrintStatus(::ostream & os)
|
---|
| 33 | {
|
---|
| 34 | os << "\n ------------------------------------------------------ \n"
|
---|
[2004] | 35 | << " SimpleOffsetEstimator::PrintStatus() - MeanWSize= " << mWSz << " NPtFit="
|
---|
| 36 | << nPtFit << " DegPoly=" << degPol << " poly.Degre()=" << poly.Degre() << endl;
|
---|
[2000] | 37 | TOIProcessor::PrintStatus(os);
|
---|
| 38 | os << " ProcessedSampleCount=" << ProcessedSampleCount() << endl;
|
---|
| 39 | os << " ------------------------------------------------------ " << endl;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | void SimpleOffsetEstimator::init()
|
---|
| 43 | {
|
---|
| 44 | cout << "SimpleOffsetEstimator::init" << endl;
|
---|
| 45 | declareInput("in");
|
---|
| 46 | declareOutput("offset");
|
---|
| 47 | declareOutput("out");
|
---|
| 48 | declareOutput("incopie");
|
---|
| 49 | declareOutput("poly_a0");
|
---|
| 50 | declareOutput("poly_a1");
|
---|
| 51 | declareOutput("poly_a2");
|
---|
| 52 | declareOutput("poly_sn0");
|
---|
| 53 | declareOutput("mean_y");
|
---|
| 54 | declareOutput("sig_y");
|
---|
| 55 | declareOutput("mean_x");
|
---|
| 56 | name = "SimpleOffsetEstimator";
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | void SimpleOffsetEstimator::run()
|
---|
| 60 | {
|
---|
| 61 | int snb = getMinIn();
|
---|
| 62 | int sne = getMaxIn();
|
---|
| 63 |
|
---|
| 64 | bool fgoffset = checkOutputTOIIndex(0);
|
---|
| 65 | bool fgout = checkOutputTOIIndex(1);
|
---|
| 66 | bool fgincopie = checkOutputTOIIndex(2);
|
---|
| 67 | bool fga0 = checkOutputTOIIndex(3);
|
---|
| 68 | bool fga1 = checkOutputTOIIndex(4);
|
---|
| 69 | bool fga2 = checkOutputTOIIndex(5);
|
---|
| 70 | bool fgsn0 = checkOutputTOIIndex(6);
|
---|
| 71 | bool fgmeany = checkOutputTOIIndex(7);
|
---|
| 72 | bool fgsigy = checkOutputTOIIndex(8);
|
---|
| 73 | bool fgmeanx = checkOutputTOIIndex(9);
|
---|
| 74 |
|
---|
| 75 | if (!checkInputTOIIndex(0)) {
|
---|
| 76 | cerr << " SimpleOffsetEstimator::run() - Input TOI (in) not connected! "
|
---|
| 77 | << endl;
|
---|
| 78 | throw ParmError("SimpleOffsetEstimator::run() Input TOI (in) not connected!");
|
---|
| 79 | }
|
---|
| 80 | if (!fgoffset && !fgout) {
|
---|
| 81 | cerr << " SimpleOffsetEstimator::run() - No Output TOI (offset/in-offset) connected! "
|
---|
| 82 | << endl;
|
---|
| 83 | throw ParmError(" SimpleOffsetEstimator::run() No output TOI (offset/in-offset) connected!");
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | cout << " SimpleOffsetEstimator::run() SNRange=" << snb << " - " << sne << endl;
|
---|
| 87 |
|
---|
| 88 | // NTuple pour sauvegarde des coeff de poly
|
---|
| 89 | char * nomsnt[] = {"sncur", "sn0", "meanx", "meany", "sigy", "a0", "a1", "a2", "ycur"};
|
---|
| 90 | XNTuple xntp(0, 9, 0, 0, nomsnt);
|
---|
| 91 |
|
---|
| 92 | try {
|
---|
| 93 |
|
---|
| 94 | // Vecteurs pour les donnees et les sorties
|
---|
| 95 | int wsize = mWSz;
|
---|
| 96 |
|
---|
| 97 | Vector vin(wsize);
|
---|
| 98 | Vector voff(wsize);
|
---|
| 99 | Vector vout(wsize);
|
---|
| 100 | TVector<uint_8> vfg(wsize);
|
---|
| 101 |
|
---|
| 102 | // Pour le fit
|
---|
| 103 | Vector errCoef(3);
|
---|
| 104 |
|
---|
| 105 | Vector X(nPtFit);
|
---|
| 106 | Vector X0(nPtFit);
|
---|
| 107 | Vector Y(nPtFit);
|
---|
| 108 | Vector YErr(nPtFit);
|
---|
| 109 |
|
---|
| 110 | // Variables diverses
|
---|
| 111 | int k,i,j,klast;
|
---|
| 112 | int nks = 0;
|
---|
| 113 | klast = snb-1;
|
---|
| 114 | totnbblock = 0;
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | int nbblkok = 0;
|
---|
[2004] | 118 |
|
---|
| 119 | bool fginiXYdone = false;
|
---|
| 120 |
|
---|
[2000] | 121 | double sn0 = 0.;
|
---|
| 122 | double nok = 0.;
|
---|
| 123 | double mean = 0.;
|
---|
| 124 | double sig = 0.;
|
---|
| 125 | double meanx = 0.;
|
---|
| 126 |
|
---|
| 127 | // Boucle sur les sampleNum
|
---|
| 128 | // 1er partie, on traite par paquets de wsize
|
---|
| 129 |
|
---|
| 130 | for(k=snb;k<=sne-wsize+1;k+=wsize) {
|
---|
| 131 | // Lecture d'un bloc de donnees
|
---|
| 132 | getData(0, k, wsize, vin.Data(), vfg.Data());
|
---|
| 133 |
|
---|
| 134 | // Calcul moyenne et sigma du bloc
|
---|
| 135 | nok = 0.; meanx = 0.;
|
---|
| 136 | mean = 0.; sig = 0.;
|
---|
| 137 |
|
---|
| 138 | for(j=0; j<wsize; j++) {
|
---|
| 139 | if ( vfg(j) ) continue;
|
---|
| 140 | mean += vin(j);
|
---|
| 141 | sig += vin(j)*vin(j);
|
---|
| 142 | meanx += k+j;
|
---|
| 143 | nok++;
|
---|
| 144 | }
|
---|
[2004] | 145 |
|
---|
| 146 | sn0 = (double)(k+wsize/2);
|
---|
| 147 |
|
---|
| 148 | if (!fginiXYdone) {
|
---|
| 149 | if (nok > 0.5) {
|
---|
| 150 | mean /= nok;
|
---|
| 151 | meanx /= nok;
|
---|
| 152 | sig = sig/nok-mean*mean;
|
---|
| 153 | }
|
---|
[2000] | 154 | X = RegularSequence(k+wsize*0.5, (double)wsize);
|
---|
| 155 | Y = mean;
|
---|
| 156 | YErr = (nok > 0.5) ? sqrt(mean) : 1.;
|
---|
[2004] | 157 | fginiXYdone = true;
|
---|
[2000] | 158 | }
|
---|
[2004] | 159 |
|
---|
[2000] | 160 | if (nok > 3.) {
|
---|
| 161 | mean /= nok;
|
---|
| 162 | meanx /= nok;
|
---|
| 163 | sig = sig/nok-mean*mean;
|
---|
| 164 | int kk = nbblkok%nPtFit;
|
---|
| 165 | nbblkok++;
|
---|
| 166 | Y(kk) = mean;
|
---|
| 167 | YErr(kk) = sig;
|
---|
| 168 | X(kk) = meanx;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | X0 = X;
|
---|
| 172 | X0 -= sn0;
|
---|
[2004] | 173 | if (nbblkok > poly.Degre()+1)
|
---|
| 174 | poly.Fit(X0,Y,YErr,degPol,errCoef);
|
---|
| 175 | else {
|
---|
| 176 | poly[0] = mean;
|
---|
| 177 | for(int jj=1; jj<=poly.Degre(); jj++) poly[jj] = 0.;
|
---|
| 178 | }
|
---|
| 179 | /*
|
---|
| 180 | if (nbblkok < 8) {
|
---|
| 181 | cout << "------ DBG-X " << nbblkok << "," << nok << " degre=" << poly.Degre() << endl;
|
---|
| 182 | cout << "DBG-A X0=" << X0 << endl;
|
---|
| 183 | cout << "DBG-A Y=" << Y << endl;
|
---|
| 184 | cout << "DBG-A YErr=" << YErr << endl;
|
---|
| 185 | cout << "DBG-A poly= " << poly << endl;
|
---|
| 186 | }
|
---|
| 187 | */
|
---|
[2000] | 188 |
|
---|
| 189 | // Calcul des valeurs d'offset en sortie
|
---|
| 190 | for(j=0; j<wsize; j++)
|
---|
| 191 | voff(j) = poly(k+j-sn0);
|
---|
| 192 |
|
---|
| 193 | if (fgoffset) putData(0, k, wsize, voff.Data());
|
---|
| 194 | if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data());
|
---|
| 195 | if (fgout) {
|
---|
| 196 | vin -= voff;
|
---|
| 197 | putData(1, k, wsize, vin.Data(), vfg.Data());
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if (fga0) {
|
---|
| 201 | vout = poly[0];
|
---|
| 202 | putData(3, k, wsize, vout.Data());
|
---|
| 203 | }
|
---|
| 204 | if (fga1) {
|
---|
| 205 | vout = poly[1];
|
---|
| 206 | putData(4, k, wsize, vout.Data());
|
---|
| 207 | }
|
---|
| 208 | if (fga2) {
|
---|
| 209 | vout = poly[2];
|
---|
| 210 | putData(5, k, wsize, vout.Data());
|
---|
| 211 | }
|
---|
| 212 | if (fgsn0) {
|
---|
| 213 | vout = sn0;
|
---|
| 214 | putData(6, k, wsize, vout.Data());
|
---|
| 215 | }
|
---|
| 216 | if (fgmeany) {
|
---|
| 217 | vout = mean;
|
---|
| 218 | putData(7, k, wsize, vout.Data());
|
---|
| 219 | }
|
---|
| 220 | if (fgsigy) {
|
---|
| 221 | vout = sig;
|
---|
| 222 | putData(8, k, wsize, vout.Data());
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | if (fgmeanx) {
|
---|
| 226 | vout = meanx;
|
---|
| 227 | putData(9, k, wsize, vout.Data());
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | if (ntpoly) { // Remplissage du XNTuple de controle
|
---|
| 231 | char * nomsnt[] = {"sncur", "sn0", "meanx", "meany", "sigy", "a0", "a1", "a2", "ycur"};
|
---|
| 232 | float xnt[10];
|
---|
| 233 | xnt[0] = k;
|
---|
| 234 | xnt[1] = sn0;
|
---|
| 235 | xnt[2] = meanx;
|
---|
| 236 | xnt[3] = mean;
|
---|
| 237 | xnt[4] = sig;
|
---|
| 238 | xnt[5] = poly[0];
|
---|
| 239 | xnt[6] = poly[1];
|
---|
| 240 | xnt[7] = poly[2];
|
---|
| 241 | xnt[8] = poly(k-sn0);
|
---|
| 242 | xntp.Fill(NULL, xnt, NULL, NULL);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | klast+=wsize;
|
---|
| 246 | totnscount+=wsize;
|
---|
| 247 | totnbblock++;
|
---|
| 248 |
|
---|
| 249 | } // Fin boucle sur les samples, par pas de wsize
|
---|
| 250 |
|
---|
| 251 | // 2eme partie, on traite la fin du bloc d'echantillons si necessaire
|
---|
| 252 | if (klast < sne) {
|
---|
| 253 | wsize = sne-klast;
|
---|
| 254 | vin.ReSize(wsize);
|
---|
| 255 | voff.ReSize(wsize);
|
---|
| 256 | vout.ReSize(wsize);
|
---|
| 257 | vfg.ReSize(wsize);
|
---|
| 258 | getData(0, k, wsize, vin.Data(), vfg.Data());
|
---|
| 259 | for(j=0; j<wsize; j++)
|
---|
| 260 | voff(j) = poly(k+j-sn0);
|
---|
| 261 | if (fgoffset) putData(0, k, wsize, voff.Data());
|
---|
| 262 | if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data());
|
---|
| 263 | if (fgout) {
|
---|
| 264 | vin -= voff;
|
---|
| 265 | putData(1, k, wsize, vin.Data(), vfg.Data());
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | if (fga0) {
|
---|
| 269 | vout = poly[0];
|
---|
| 270 | putData(3, k, wsize, vout.Data());
|
---|
| 271 | }
|
---|
| 272 | if (fga1) {
|
---|
| 273 | vout = poly[1];
|
---|
| 274 | putData(4, k, wsize, vout.Data());
|
---|
| 275 | }
|
---|
| 276 | if (fga2) {
|
---|
| 277 | vout = poly[2];
|
---|
| 278 | putData(5, k, wsize, vout.Data());
|
---|
| 279 | }
|
---|
| 280 | if (fgsn0) {
|
---|
| 281 | vout = sn0;
|
---|
| 282 | putData(6, k, wsize, vout.Data());
|
---|
| 283 | }
|
---|
| 284 | if (fgmeany) {
|
---|
| 285 | vout = mean;
|
---|
| 286 | putData(7, k, wsize, vout.Data());
|
---|
| 287 | }
|
---|
| 288 | if (fgsigy) {
|
---|
| 289 | vout = sig;
|
---|
| 290 | putData(8, k, wsize, vout.Data());
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | if (fgmeanx) {
|
---|
| 294 | vout = meanx;
|
---|
| 295 | putData(9, k, wsize, vout.Data());
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | klast+=wsize;
|
---|
| 299 | totnscount+=wsize;
|
---|
| 300 | totnbblock++;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | cout << " SimpleOffsetEstimator::run() - End of processing "
|
---|
| 304 | << " TotNbBlocks= " << totnbblock << " ProcSamples=" << totnscount << endl;
|
---|
| 305 |
|
---|
| 306 | } // Bloc try
|
---|
| 307 |
|
---|
| 308 | catch (PException & exc) {
|
---|
| 309 | cerr << "SimpleOffsetEstimator::run() Catched Exception " << (string)typeid(exc).name()
|
---|
| 310 | << "\n .... Msg= " << exc.Msg() << endl;
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | if (ntpoly) {
|
---|
| 314 | if (ntpolyname.length() < 1) ntpolyname = "simoffset.ppf";
|
---|
| 315 | POutPersist pos(ntpolyname);
|
---|
| 316 | cout << " SimpleOffsetEstimator::run()/Info : Writing poly ntuple to PPF file " << ntpolyname << endl;
|
---|
| 317 | pos << xntp;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|