source: Sophya/trunk/ArchTOIPipe/ProcWSophya/simoffset.cc@ 2392

Last change on this file since 2392 was 2045, checked in by ansari, 23 years ago

Remplacement de l'objet poly (pb plantage ds lors du Fit) par un vecteur - Reza 7/6/2002

File size: 11.8 KB
RevLine 
[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"
[2034]13#include "ntuple.h"
[2045]14#include "sopemtx.h"
[2000]15
16#include "flagtoidef.h"
17
[2045]18
[2000]19SimpleOffsetEstimator::SimpleOffsetEstimator(int mwsz, int nptfit, int degpol)
[2045]20 : poly((degpol > 1)?degpol+1:2)
[2000]21{
[2014]22 SetParams(mwsz, nptfit, degpol);
[2000]23 totnscount = 0;
24 totnbblock = 0;
[2014]25 bmincut = bmaxcut = -9999.;
26 bgalcut = false;
27 ns_flgcut = ns_bgalcut = 0;
[2045]28 npb_fitpoly = 0;
[2000]29 SavePolyNTuple();
30}
31
32SimpleOffsetEstimator::~SimpleOffsetEstimator()
33{
34}
35
[2014]36void SimpleOffsetEstimator::SetParams(int mwsz, int nptfit, int degpol)
37{
38 mWSz = (mwsz > 8) ? mwsz : 8;
39 nPtFit = (nptfit > degpol+2) ? nptfit : degpol+2;
40 if (nPtFit%2 == 0) nPtFit++;
41 degPol = (degpol > 1)?degpol:1;
42
43}
44
45void SimpleOffsetEstimator::SetBGalCut(double bmin, double bmax)
46{
47 bgalcut = true;
48 bmincut = bmin;
49 bmaxcut = bmax;
50}
51
[2000]52void SimpleOffsetEstimator::PrintStatus(::ostream & os)
53{
54 os << "\n ------------------------------------------------------ \n"
[2004]55 << " SimpleOffsetEstimator::PrintStatus() - MeanWSize= " << mWSz << " NPtFit="
[2045]56 << nPtFit << " DegPoly=" << degPol << endl;
[2014]57 if (bgalcut)
58 os << " bGalCut = Yes , bGalMin = " << bmincut << " bGalMax= " << bmaxcut << endl;
59 else os << " bGalCut = No " << endl;
[2000]60 TOIProcessor::PrintStatus(os);
[2014]61 os << " ProcessedSampleCount=" << ProcessedSampleCount()
[2045]62 << " NS_FlgCut= " << ns_flgcut << " NS_BGalCut= " << ns_bgalcut
63 << " NPb_FitPoly= " << npb_fitpoly << endl;
[2000]64 os << " ------------------------------------------------------ " << endl;
65}
66
67void SimpleOffsetEstimator::init()
68{
69 cout << "SimpleOffsetEstimator::init" << endl;
70 declareInput("in");
[2014]71 declareInput("bgal");
[2000]72 declareOutput("offset");
73 declareOutput("out");
74 declareOutput("incopie");
[2014]75 declareOutput("bgalcopie");
76 // declareOutput("mean_y");
77 // declareOutput("sig_y");
78 // declareOutput("mean_x");
[2000]79 name = "SimpleOffsetEstimator";
80}
81
82void SimpleOffsetEstimator::run()
83{
84 int snb = getMinIn();
85 int sne = getMaxIn();
86
87 bool fgoffset = checkOutputTOIIndex(0);
[2030]88 bool fgbgal = checkInputTOIIndex(1);
[2000]89 bool fgout = checkOutputTOIIndex(1);
90 bool fgincopie = checkOutputTOIIndex(2);
[2014]91 bool fgbgalcopie = checkOutputTOIIndex(3);
92 // bool fgmeany = checkOutputTOIIndex(4);
93 // bool fgsigy = checkOutputTOIIndex(5);
94 // bool fgmeanx = checkOutputTOIIndex(6);
95
[2000]96 if (!checkInputTOIIndex(0)) {
97 cerr << " SimpleOffsetEstimator::run() - Input TOI (in) not connected! "
98 << endl;
99 throw ParmError("SimpleOffsetEstimator::run() Input TOI (in) not connected!");
100 }
[2029]101 if (bgalcut && !fgbgal) {
[2014]102 cerr << " SimpleOffsetEstimator::run() - Input TOI bgal not connected! "
103 << endl;
104 throw ParmError("SimpleOffsetEstimator::run() Input TOI bgal not connected!");
105 }
[2029]106 if (fgbgalcopie && !fgbgal) {
107 cerr << " SimpleOffsetEstimator::run() - Output TOI bgalcopie connected without input TOI bgal!"
108 << endl;
109 throw ParmError("SimpleOffsetEstimator::run() Output TOI bgalcopie connected without input TOI bgal!");
110 }
[2000]111 if (!fgoffset && !fgout) {
[2029]112 cerr << " SimpleOffsetEstimator::run() - No output TOI (offset/in-offset) connected!"
[2000]113 << endl;
114 throw ParmError(" SimpleOffsetEstimator::run() No output TOI (offset/in-offset) connected!");
115 }
116
117 cout << " SimpleOffsetEstimator::run() SNRange=" << snb << " - " << sne << endl;
118
119 // NTuple pour sauvegarde des coeff de poly
[2014]120 char * nomsnt[] = {"sncur", "sn0", "meanx", "meany", "sigy",
121 "a0", "a1", "a2", "ycur", "nok", "nbblkok"};
[2034]122 NTuple xntp(11, nomsnt);
[2000]123
[2045]124 char ans[20];
125
[2000]126 try {
127
128 // Vecteurs pour les donnees et les sorties
129 int wsize = mWSz;
130
[2008]131 int hisblk = nPtFit/2+1;
132 Vector vinhist(wsize*hisblk);
133 TVector<uint_8> vfghist(wsize*hisblk);
[2000]134 Vector voff(wsize);
135 Vector vout(wsize);
[2008]136 Vector vinc(wsize);
137 TVector<uint_8> vfgc(wsize);
[2014]138 Vector bgal(wsize);
[2008]139
[2000]140
141 // Pour le fit
[2008]142 Vector errCoef(degPol+1);
[2000]143
[2008]144 Vector X(nPtFit+1);
145 Vector X0(nPtFit+1);
146 Vector Y(nPtFit+1);
147 Vector YErr(nPtFit+1);
[2000]148
149 // Variables diverses
[2008]150 int k,kb,j,klast;
[2000]151 klast = snb-1;
152 totnbblock = 0;
153
154
155 int nbblkok = 0;
[2004]156
157 bool fginiXYdone = false;
158
[2000]159 double sn0 = 0.;
160 double nok = 0.;
161 double mean = 0.;
162 double sig = 0.;
163 double meanx = 0.;
[2008]164 double sn_last = 0.;
165 double y_last = 0.;
166
[2014]167 double last_meanok = 0.;
168 double last_sigok = 1.;
169 double meanx_forlast = 0.;
170 bool fg_last_meansig = false;
171
[2000]172 // Boucle sur les sampleNum
[2008]173 // 1ere partie, on traite par paquets de wsize
174 int nblk = (sne-snb+1)/wsize;
175 for(kb=0; kb<nblk; kb++) {
[2045]176 // cout << " SimpleOffsetEstimator::run - Loop " << kb << " NbBlkOK= "
177 // << nbblkok << " <CR> to continue / q --> QUIT ... \n" ;
178 // gets(ans); if (ans[0] == 'q') break;
[2008]179 k = kb*wsize+snb;
180 // for(k=snb;k<=sne-wsize+1;k+=wsize) {
181 // Lecture d'un bloc de donnees
182 Vector vin( vinhist(Range((kb%hisblk)*wsize, 0, wsize) ) );
183 TVector<uint_8> vfg( vfghist(Range((kb%hisblk)*wsize, 0, wsize) ) );
[2000]184
185 getData(0, k, wsize, vin.Data(), vfg.Data());
[2029]186 if (fgbgal) {
[2014]187 getData(1, k, wsize, bgal.Data());
188 if (fgbgalcopie) putData(3, k, wsize, bgal.Data());
189 }
[2000]190
[2014]191 fg_last_meansig = false;
192
193 if (kb == 0) {
194 last_meanok = vin.Sum()/wsize;
195 last_sigok = vin.SumX2()/wsize - last_meanok*last_meanok;
196 }
197
[2000]198 // Calcul moyenne et sigma du bloc
199 nok = 0.; meanx = 0.;
200 mean = 0.; sig = 0.;
[2014]201 meanx_forlast = 0.;
[2000]202 for(j=0; j<wsize; j++) {
[2014]203 meanx_forlast += k+j;
204 if ( vfg(j) ) { ns_flgcut++; continue; }
205 if (bgalcut && (bgal(j) > bmincut) && (bgal(j) < bmaxcut) )
206 { ns_bgalcut++; continue; }
[2000]207 mean += vin(j);
208 sig += vin(j)*vin(j);
209 meanx += k+j;
210 nok++;
211 }
[2004]212
213
214 if (!fginiXYdone) {
215 if (nok > 0.5) {
216 mean /= nok;
217 meanx /= nok;
218 sig = sig/nok-mean*mean;
219 }
[2008]220 X = RegularSequence((kb+0.5)*wsize, (double)wsize);
[2000]221 Y = mean;
222 YErr = (nok > 0.5) ? sqrt(mean) : 1.;
[2004]223 fginiXYdone = true;
[2000]224 }
[2004]225
[2008]226 if (nok > 3.5) {
[2000]227 mean /= nok;
228 meanx /= nok;
229 sig = sig/nok-mean*mean;
[2014]230 if (sig < 1.e-10*mean) sig = 1.e-10*mean;
231 if (sig < 1.e-39) sig = 1.e-39;
[2000]232 int kk = nbblkok%nPtFit;
233 nbblkok++;
234 Y(kk) = mean;
235 YErr(kk) = sig;
236 X(kk) = meanx;
[2014]237 last_meanok = mean;
238 last_sigok = sig;
[2000]239 }
[2014]240 else {
241 int kk = nbblkok%nPtFit;
242 Y(kk) = last_meanok;
243 YErr(kk) = last_sigok*10.;
244 X(kk) = meanx_forlast/wsize;
245 fg_last_meansig = true;
246 }
[2045]247
248
[2014]249 if (((nok>3.5) || fg_last_meansig) && (nbblkok >= nPtFit) ) {
[2008]250 // On force le fit a garder une certaine continuite
251 Y(nPtFit) = y_last;
252 double smin, smax;
253 YErr(Range(0,0,nPtFit)).MinMax(smin, smax);
[2014]254 if (smax < 1.e-39) smax = 1.e-39;
[2008]255 YErr(nPtFit) = smax/10.;
256 X(nPtFit) = sn_last;
257 sn0 = (double)(k-nPtFit*wsize*0.5);
258 X0 = X;
259 X0 -= sn0;
[2045]260 FitPoly(X0, Y, YErr);
[2008]261 }
[2004]262 else {
[2008]263 if (nbblkok < 2) {
264 sn0 = k+1;
[2045]265 poly(0) = mean;
266 for(int jj=1; jj<=degPol; jj++) poly(jj) = 0.;
[2008]267 }
268 else if (nbblkok < nPtFit) {
[2045]269 poly(0) = 0.5*(Y(nbblkok-1)+Y(0));
270 poly(1) = (Y(nbblkok-1) - Y(0))/(X(nbblkok-1)-X(0));
[2008]271 sn0 = 0.5*(X(nbblkok-1)+X(0));
[2045]272 for(int jj=2; jj<=degPol; jj++) poly(jj) = 0.;
[2008]273 }
274 sn_last = sn0;
275 y_last = poly(sn_last-sn0);
[2004]276 }
[2045]277
[2008]278 if (ntpoly) { // Remplissage du XNTuple de controle
[2014]279 float xnt[12];
[2008]280 xnt[0] = k;
281 xnt[1] = sn0;
282 xnt[2] = meanx;
283 xnt[3] = mean;
284 xnt[4] = sig;
[2045]285 xnt[5] = poly(0);
286 xnt[6] = poly(1);
287 xnt[7] = poly(2);
288 xnt[8] = ApplyPoly(k-sn0);
[2014]289 xnt[9] = nok;
290 xnt[10] = nbblkok;
[2034]291 xntp.Fill(xnt);
[2008]292 }
293
[2045]294
[2008]295 if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data());
296
297 if (kb < nPtFit/2) continue;
[2045]298 Vector vinc;
[2008]299 TVector<uint_8> vfgc;
300 int kbs = kb-nPtFit/2;
301 k = kbs*wsize+snb;
302 if (kb == nblk-1) {
303 int wszt = wsize*hisblk;
304 voff.ReSize(wszt);
305 vout.ReSize(wszt);
306 vinc.ReSize(wszt);
307 vfgc.ReSize(wszt);
[2014]308 bgal.ReSize(wszt);
[2008]309 int jb = 0;
310 for(int kbsc=kbs; kbsc<nblk; kbsc++) {
311 vinc(Range(jb*wsize, 0, wsize)) =
312 vinhist(Range((kbsc%hisblk)*wsize, 0, wsize) ) ;
313 vfgc(Range(jb*wsize, 0, wsize)) =
314 vfghist(Range((kbsc%hisblk)*wsize, 0, wsize) ) ;
315 jb++;
316 }
317 wsize = wszt;
318 }
319 else {
320 vinc = vinhist(Range((kbs%hisblk)*wsize, 0, wsize) ) ;
321 vfgc = vfghist(Range((kbs%hisblk)*wsize, 0, wsize) ) ;
322 }
323
[2000]324 // Calcul des valeurs d'offset en sortie
325 for(j=0; j<wsize; j++)
[2045]326 voff(j) = ApplyPoly(k+j-sn0);
[2008]327 sn_last = k+wsize;
[2045]328 y_last = ApplyPoly(sn_last-sn0);
[2000]329 if (fgoffset) putData(0, k, wsize, voff.Data());
330 if (fgout) {
[2008]331 vinc -= voff;
332 putData(1, k, wsize, vinc.Data(), vfgc.Data());
[2000]333 }
334
335 klast+=wsize;
336 totnscount+=wsize;
337 totnbblock++;
[2045]338 // cout << " SimpleOffset::run " << totnbblock << " totnscount " << totnscount << endl;
[2000]339 } // Fin boucle sur les samples, par pas de wsize
340
[2008]341 // 3eme partie, on traite la fin du bloc d'echantillons si necessaire
[2000]342 if (klast < sne) {
343 wsize = sne-klast;
[2008]344 Vector vin(wsize);
[2000]345 voff.ReSize(wsize);
346 vout.ReSize(wsize);
[2008]347 TVector<uint_8> vfg(wsize);
348 k = klast+1;
[2000]349 getData(0, k, wsize, vin.Data(), vfg.Data());
[2014]350
[2029]351 if (fgbgal) {
[2014]352 getData(1, k, wsize, bgal.Data());
353 if (fgbgalcopie) putData(3, k, wsize, bgal.Data());
354 }
355
[2000]356 for(j=0; j<wsize; j++)
[2045]357 voff(j) = ApplyPoly(k+j-sn0);
[2000]358 if (fgoffset) putData(0, k, wsize, voff.Data());
359 if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data());
360 if (fgout) {
361 vin -= voff;
362 putData(1, k, wsize, vin.Data(), vfg.Data());
363 }
[2014]364
365 /*
[2000]366 if (fgmeany) {
367 vout = mean;
[2014]368 putData(4, k, wsize, vout.Data());
[2000]369 }
370 if (fgsigy) {
371 vout = sig;
[2014]372 putData(5, k, wsize, vout.Data());
[2000]373 }
374
375 if (fgmeanx) {
376 vout = meanx;
[2014]377 putData(6, k, wsize, vout.Data());
[2000]378 }
[2014]379 */
[2000]380
381 klast+=wsize;
382 totnscount+=wsize;
383 totnbblock++;
384 }
385
386 cout << " SimpleOffsetEstimator::run() - End of processing "
387 << " TotNbBlocks= " << totnbblock << " ProcSamples=" << totnscount << endl;
388
389 } // Bloc try
390
391 catch (PException & exc) {
392 cerr << "SimpleOffsetEstimator::run() Catched Exception " << (string)typeid(exc).name()
393 << "\n .... Msg= " << exc.Msg() << endl;
394 }
395
396 if (ntpoly) {
397 if (ntpolyname.length() < 1) ntpolyname = "simoffset.ppf";
398 POutPersist pos(ntpolyname);
399 cout << " SimpleOffsetEstimator::run()/Info : Writing poly ntuple to PPF file " << ntpolyname << endl;
400 pos << xntp;
401 }
402}
[2045]403
404
405double SimpleOffsetEstimator::ApplyPoly(double x)
406{
407 if (degPol == 0) return(poly(0));
408 else if (degPol == 1) return(poly(0)+poly(1)*x);
409 else if (degPol == 2) return(poly(0)+poly(1)*x+poly(2)*x*x);
410 else if (degPol == 3) return(poly(0)+poly(1)*x+poly(2)*x*x+poly(3)*x*x*x);
411 else {
412 double ret = poly(0);
413 double xx = x;
414 for(int k=1; k<degPol+1; k++) { ret += xx*poly(k); xx *= x; }
415 return(ret);
416 }
417}
418
419// Fonction pour faire LinFit d'un polynome
420r_8 rzpoly_f(uint_4 k, r_8 x )
421{
422 if (k == 0) return(1.);
423 else if (k == 1) return(x);
424 else if (k == 2) return(x*x);
425 else if (k == 3) return(x*x*x);
426 else {
427 r_8 ret = x;
428 for(int i=1; i<k; i++) ret *= x;
429 return(ret);
430 }
431}
432
433void SimpleOffsetEstimator::FitPoly(Vector& X0, Vector& Y, Vector& YErr)
434{
435 Vector cpol(degPol+1);
436 Vector errcoef(degPol+1);
437 LinFitter<r_8> lfit;
438 try {
439 lfit.LinFit(X0, Y, YErr, degPol+1, rzpoly_f, cpol, errcoef);
440 poly = cpol;
441 }
442 catch (PException & exc) {
443 if (npb_fitpoly < 50)
444 cout << " -- SimpleOffsetEstimator::FitPoly/ Catched Exception "
445 << (string)typeid(exc).name() << "\n .... Msg= " << exc.Msg() << endl;
446 if (npb_fitpoly < 10)
447 cout << " X0= " << X0 << " Y=" << Y << " YErr=" << YErr << endl;
448 npb_fitpoly++;
449 }
450 return;
451}
452
Note: See TracBrowser for help on using the repository browser.