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