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

Last change on this file since 2330 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
Line 
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 "ntuple.h"
14#include "sopemtx.h"
15
16#include "flagtoidef.h"
17
18
19SimpleOffsetEstimator::SimpleOffsetEstimator(int mwsz, int nptfit, int degpol)
20 : poly((degpol > 1)?degpol+1:2)
21{
22 SetParams(mwsz, nptfit, degpol);
23 totnscount = 0;
24 totnbblock = 0;
25 bmincut = bmaxcut = -9999.;
26 bgalcut = false;
27 ns_flgcut = ns_bgalcut = 0;
28 npb_fitpoly = 0;
29 SavePolyNTuple();
30}
31
32SimpleOffsetEstimator::~SimpleOffsetEstimator()
33{
34}
35
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
52void SimpleOffsetEstimator::PrintStatus(::ostream & os)
53{
54 os << "\n ------------------------------------------------------ \n"
55 << " SimpleOffsetEstimator::PrintStatus() - MeanWSize= " << mWSz << " NPtFit="
56 << nPtFit << " DegPoly=" << degPol << endl;
57 if (bgalcut)
58 os << " bGalCut = Yes , bGalMin = " << bmincut << " bGalMax= " << bmaxcut << endl;
59 else os << " bGalCut = No " << endl;
60 TOIProcessor::PrintStatus(os);
61 os << " ProcessedSampleCount=" << ProcessedSampleCount()
62 << " NS_FlgCut= " << ns_flgcut << " NS_BGalCut= " << ns_bgalcut
63 << " NPb_FitPoly= " << npb_fitpoly << endl;
64 os << " ------------------------------------------------------ " << endl;
65}
66
67void SimpleOffsetEstimator::init()
68{
69 cout << "SimpleOffsetEstimator::init" << endl;
70 declareInput("in");
71 declareInput("bgal");
72 declareOutput("offset");
73 declareOutput("out");
74 declareOutput("incopie");
75 declareOutput("bgalcopie");
76 // declareOutput("mean_y");
77 // declareOutput("sig_y");
78 // declareOutput("mean_x");
79 name = "SimpleOffsetEstimator";
80}
81
82void SimpleOffsetEstimator::run()
83{
84 int snb = getMinIn();
85 int sne = getMaxIn();
86
87 bool fgoffset = checkOutputTOIIndex(0);
88 bool fgbgal = checkInputTOIIndex(1);
89 bool fgout = checkOutputTOIIndex(1);
90 bool fgincopie = checkOutputTOIIndex(2);
91 bool fgbgalcopie = checkOutputTOIIndex(3);
92 // bool fgmeany = checkOutputTOIIndex(4);
93 // bool fgsigy = checkOutputTOIIndex(5);
94 // bool fgmeanx = checkOutputTOIIndex(6);
95
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 }
101 if (bgalcut && !fgbgal) {
102 cerr << " SimpleOffsetEstimator::run() - Input TOI bgal not connected! "
103 << endl;
104 throw ParmError("SimpleOffsetEstimator::run() Input TOI bgal not connected!");
105 }
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 }
111 if (!fgoffset && !fgout) {
112 cerr << " SimpleOffsetEstimator::run() - No output TOI (offset/in-offset) connected!"
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
120 char * nomsnt[] = {"sncur", "sn0", "meanx", "meany", "sigy",
121 "a0", "a1", "a2", "ycur", "nok", "nbblkok"};
122 NTuple xntp(11, nomsnt);
123
124 char ans[20];
125
126 try {
127
128 // Vecteurs pour les donnees et les sorties
129 int wsize = mWSz;
130
131 int hisblk = nPtFit/2+1;
132 Vector vinhist(wsize*hisblk);
133 TVector<uint_8> vfghist(wsize*hisblk);
134 Vector voff(wsize);
135 Vector vout(wsize);
136 Vector vinc(wsize);
137 TVector<uint_8> vfgc(wsize);
138 Vector bgal(wsize);
139
140
141 // Pour le fit
142 Vector errCoef(degPol+1);
143
144 Vector X(nPtFit+1);
145 Vector X0(nPtFit+1);
146 Vector Y(nPtFit+1);
147 Vector YErr(nPtFit+1);
148
149 // Variables diverses
150 int k,kb,j,klast;
151 klast = snb-1;
152 totnbblock = 0;
153
154
155 int nbblkok = 0;
156
157 bool fginiXYdone = false;
158
159 double sn0 = 0.;
160 double nok = 0.;
161 double mean = 0.;
162 double sig = 0.;
163 double meanx = 0.;
164 double sn_last = 0.;
165 double y_last = 0.;
166
167 double last_meanok = 0.;
168 double last_sigok = 1.;
169 double meanx_forlast = 0.;
170 bool fg_last_meansig = false;
171
172 // Boucle sur les sampleNum
173 // 1ere partie, on traite par paquets de wsize
174 int nblk = (sne-snb+1)/wsize;
175 for(kb=0; kb<nblk; kb++) {
176 // cout << " SimpleOffsetEstimator::run - Loop " << kb << " NbBlkOK= "
177 // << nbblkok << " <CR> to continue / q --> QUIT ... \n" ;
178 // gets(ans); if (ans[0] == 'q') break;
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) ) );
184
185 getData(0, k, wsize, vin.Data(), vfg.Data());
186 if (fgbgal) {
187 getData(1, k, wsize, bgal.Data());
188 if (fgbgalcopie) putData(3, k, wsize, bgal.Data());
189 }
190
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
198 // Calcul moyenne et sigma du bloc
199 nok = 0.; meanx = 0.;
200 mean = 0.; sig = 0.;
201 meanx_forlast = 0.;
202 for(j=0; j<wsize; j++) {
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; }
207 mean += vin(j);
208 sig += vin(j)*vin(j);
209 meanx += k+j;
210 nok++;
211 }
212
213
214 if (!fginiXYdone) {
215 if (nok > 0.5) {
216 mean /= nok;
217 meanx /= nok;
218 sig = sig/nok-mean*mean;
219 }
220 X = RegularSequence((kb+0.5)*wsize, (double)wsize);
221 Y = mean;
222 YErr = (nok > 0.5) ? sqrt(mean) : 1.;
223 fginiXYdone = true;
224 }
225
226 if (nok > 3.5) {
227 mean /= nok;
228 meanx /= nok;
229 sig = sig/nok-mean*mean;
230 if (sig < 1.e-10*mean) sig = 1.e-10*mean;
231 if (sig < 1.e-39) sig = 1.e-39;
232 int kk = nbblkok%nPtFit;
233 nbblkok++;
234 Y(kk) = mean;
235 YErr(kk) = sig;
236 X(kk) = meanx;
237 last_meanok = mean;
238 last_sigok = sig;
239 }
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 }
247
248
249 if (((nok>3.5) || fg_last_meansig) && (nbblkok >= nPtFit) ) {
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);
254 if (smax < 1.e-39) smax = 1.e-39;
255 YErr(nPtFit) = smax/10.;
256 X(nPtFit) = sn_last;
257 sn0 = (double)(k-nPtFit*wsize*0.5);
258 X0 = X;
259 X0 -= sn0;
260 FitPoly(X0, Y, YErr);
261 }
262 else {
263 if (nbblkok < 2) {
264 sn0 = k+1;
265 poly(0) = mean;
266 for(int jj=1; jj<=degPol; jj++) poly(jj) = 0.;
267 }
268 else if (nbblkok < nPtFit) {
269 poly(0) = 0.5*(Y(nbblkok-1)+Y(0));
270 poly(1) = (Y(nbblkok-1) - Y(0))/(X(nbblkok-1)-X(0));
271 sn0 = 0.5*(X(nbblkok-1)+X(0));
272 for(int jj=2; jj<=degPol; jj++) poly(jj) = 0.;
273 }
274 sn_last = sn0;
275 y_last = poly(sn_last-sn0);
276 }
277
278 if (ntpoly) { // Remplissage du XNTuple de controle
279 float xnt[12];
280 xnt[0] = k;
281 xnt[1] = sn0;
282 xnt[2] = meanx;
283 xnt[3] = mean;
284 xnt[4] = sig;
285 xnt[5] = poly(0);
286 xnt[6] = poly(1);
287 xnt[7] = poly(2);
288 xnt[8] = ApplyPoly(k-sn0);
289 xnt[9] = nok;
290 xnt[10] = nbblkok;
291 xntp.Fill(xnt);
292 }
293
294
295 if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data());
296
297 if (kb < nPtFit/2) continue;
298 Vector vinc;
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);
308 bgal.ReSize(wszt);
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
324 // Calcul des valeurs d'offset en sortie
325 for(j=0; j<wsize; j++)
326 voff(j) = ApplyPoly(k+j-sn0);
327 sn_last = k+wsize;
328 y_last = ApplyPoly(sn_last-sn0);
329 if (fgoffset) putData(0, k, wsize, voff.Data());
330 if (fgout) {
331 vinc -= voff;
332 putData(1, k, wsize, vinc.Data(), vfgc.Data());
333 }
334
335 klast+=wsize;
336 totnscount+=wsize;
337 totnbblock++;
338 // cout << " SimpleOffset::run " << totnbblock << " totnscount " << totnscount << endl;
339 } // Fin boucle sur les samples, par pas de wsize
340
341 // 3eme partie, on traite la fin du bloc d'echantillons si necessaire
342 if (klast < sne) {
343 wsize = sne-klast;
344 Vector vin(wsize);
345 voff.ReSize(wsize);
346 vout.ReSize(wsize);
347 TVector<uint_8> vfg(wsize);
348 k = klast+1;
349 getData(0, k, wsize, vin.Data(), vfg.Data());
350
351 if (fgbgal) {
352 getData(1, k, wsize, bgal.Data());
353 if (fgbgalcopie) putData(3, k, wsize, bgal.Data());
354 }
355
356 for(j=0; j<wsize; j++)
357 voff(j) = ApplyPoly(k+j-sn0);
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 }
364
365 /*
366 if (fgmeany) {
367 vout = mean;
368 putData(4, k, wsize, vout.Data());
369 }
370 if (fgsigy) {
371 vout = sig;
372 putData(5, k, wsize, vout.Data());
373 }
374
375 if (fgmeanx) {
376 vout = meanx;
377 putData(6, k, wsize, vout.Data());
378 }
379 */
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}
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.