// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL // Eric Aubourg // Christophe Magneville // Reza Ansari #include "config.h" #include "array.h" #include "simoffset.h" #include #include "toimanager.h" #include "pexceptions.h" #include "ctimer.h" #include "xntuple.h" #include "flagtoidef.h" SimpleOffsetEstimator::SimpleOffsetEstimator(int mwsz, int nptfit, int degpol) : poly((degpol > 1)?degpol:1) { mWSz = (mwsz > 8) ? mwsz : 8; nPtFit = (nptfit > degpol+2) ? nptfit : degpol+2; if (nPtFit%2 == 0) nPtFit++; degPol = (degpol > 1)?degpol:1; totnscount = 0; totnbblock = 0; SavePolyNTuple(); } SimpleOffsetEstimator::~SimpleOffsetEstimator() { } void SimpleOffsetEstimator::PrintStatus(::ostream & os) { os << "\n ------------------------------------------------------ \n" << " SimpleOffsetEstimator::PrintStatus() - MeanWSize= " << mWSz << " NPtFit=" << nPtFit << " DegPoly=" << degPol << " poly.Degre()=" << poly.Degre() << endl; TOIProcessor::PrintStatus(os); os << " ProcessedSampleCount=" << ProcessedSampleCount() << endl; os << " ------------------------------------------------------ " << endl; } void SimpleOffsetEstimator::init() { cout << "SimpleOffsetEstimator::init" << endl; declareInput("in"); declareOutput("offset"); declareOutput("out"); declareOutput("incopie"); declareOutput("poly_a0"); declareOutput("poly_a1"); declareOutput("poly_a2"); declareOutput("poly_sn0"); declareOutput("mean_y"); declareOutput("sig_y"); declareOutput("mean_x"); name = "SimpleOffsetEstimator"; } void SimpleOffsetEstimator::run() { int snb = getMinIn(); int sne = getMaxIn(); bool fgoffset = checkOutputTOIIndex(0); bool fgout = checkOutputTOIIndex(1); bool fgincopie = checkOutputTOIIndex(2); bool fga0 = checkOutputTOIIndex(3); bool fga1 = checkOutputTOIIndex(4); bool fga2 = checkOutputTOIIndex(5); bool fgsn0 = checkOutputTOIIndex(6); bool fgmeany = checkOutputTOIIndex(7); bool fgsigy = checkOutputTOIIndex(8); bool fgmeanx = checkOutputTOIIndex(9); if (!checkInputTOIIndex(0)) { cerr << " SimpleOffsetEstimator::run() - Input TOI (in) not connected! " << endl; throw ParmError("SimpleOffsetEstimator::run() Input TOI (in) not connected!"); } if (!fgoffset && !fgout) { cerr << " SimpleOffsetEstimator::run() - No Output TOI (offset/in-offset) connected! " << endl; throw ParmError(" SimpleOffsetEstimator::run() No output TOI (offset/in-offset) connected!"); } cout << " SimpleOffsetEstimator::run() SNRange=" << snb << " - " << sne << endl; // NTuple pour sauvegarde des coeff de poly char * nomsnt[] = {"sncur", "sn0", "meanx", "meany", "sigy", "a0", "a1", "a2", "ycur"}; XNTuple xntp(0, 9, 0, 0, nomsnt); try { // Vecteurs pour les donnees et les sorties int wsize = mWSz; int hisblk = nPtFit/2+1; Vector vinhist(wsize*hisblk); TVector vfghist(wsize*hisblk); Vector voff(wsize); Vector vout(wsize); Vector vinc(wsize); TVector vfgc(wsize); // Pour le fit Vector errCoef(degPol+1); Vector X(nPtFit+1); Vector X0(nPtFit+1); Vector Y(nPtFit+1); Vector YErr(nPtFit+1); // Variables diverses int k,kb,j,klast; klast = snb-1; totnbblock = 0; int nbblkok = 0; bool fginiXYdone = false; double sn0 = 0.; double nok = 0.; double mean = 0.; double sig = 0.; double meanx = 0.; double sn_last = 0.; double y_last = 0.; // Boucle sur les sampleNum // 1ere partie, on traite par paquets de wsize int nblk = (sne-snb+1)/wsize; for(kb=0; kb vfg( vfghist(Range((kb%hisblk)*wsize, 0, wsize) ) ); getData(0, k, wsize, vin.Data(), vfg.Data()); // Calcul moyenne et sigma du bloc nok = 0.; meanx = 0.; mean = 0.; sig = 0.; for(j=0; j 0.5) { mean /= nok; meanx /= nok; sig = sig/nok-mean*mean; } X = RegularSequence((kb+0.5)*wsize, (double)wsize); Y = mean; YErr = (nok > 0.5) ? sqrt(mean) : 1.; fginiXYdone = true; } if (nok > 3.5) { mean /= nok; meanx /= nok; sig = sig/nok-mean*mean; if (sig < 1.e-6*mean) sig = 1.e-6*mean; int kk = nbblkok%nPtFit; nbblkok++; Y(kk) = mean; YErr(kk) = sig; X(kk) = meanx; } if ((nok>3.5) && (nbblkok >= nPtFit) ) { // On force le fit a garder une certaine continuite Y(nPtFit) = y_last; double smin, smax; YErr(Range(0,0,nPtFit)).MinMax(smin, smax); YErr(nPtFit) = smax/10.; X(nPtFit) = sn_last; sn0 = (double)(k-nPtFit*wsize*0.5); X0 = X; X0 -= sn0; poly.Fit(X0,Y,YErr,degPol,errCoef); } else { if (nbblkok < 2) { sn0 = k+1; poly[0] = mean; for(int jj=1; jj<=poly.Degre(); jj++) poly[jj] = 0.; } else if (nbblkok < nPtFit) { poly[0] = 0.5*(Y(nbblkok-1)+Y(0)); poly[1] = (Y(nbblkok-1) - Y(0))/(X(nbblkok-1)-X(0)); sn0 = 0.5*(X(nbblkok-1)+X(0)); for(int jj=2; jj<=poly.Degre(); jj++) poly[jj] = 0.; } sn_last = sn0; y_last = poly(sn_last-sn0); } if (ntpoly) { // Remplissage du XNTuple de controle float xnt[10]; xnt[0] = k; xnt[1] = sn0; xnt[2] = meanx; xnt[3] = mean; xnt[4] = sig; xnt[5] = poly[0]; xnt[6] = poly[1]; xnt[7] = poly[2]; xnt[8] = poly(k-sn0); xntp.Fill(NULL, xnt, NULL, NULL); } /* if (nbblkok < 8) { cout << "------ DBG-X " << nbblkok << "," << nok << " degre=" << poly.Degre() << endl; cout << "DBG-A X0=" << X0 << endl; cout << "DBG-A Y=" << Y << endl; cout << "DBG-A YErr=" << YErr << endl; cout << "DBG-A poly= " << poly << endl; } */ if (fgincopie) putData(2, k, wsize, vin.Data(), vfg.Data()); if (kb < nPtFit/2) continue; Vector vinc; TVector vfgc; int kbs = kb-nPtFit/2; k = kbs*wsize+snb; if (kb == nblk-1) { int wszt = wsize*hisblk; voff.ReSize(wszt); vout.ReSize(wszt); vinc.ReSize(wszt); vfgc.ReSize(wszt); int jb = 0; for(int kbsc=kbs; kbsc vfg(wsize); k = klast+1; getData(0, k, wsize, vin.Data(), vfg.Data()); for(j=0; j