#ifndef ETA_INTERPOLATION_BASE_H #define ETA_INTERPOLATION_BASE_H #ifdef MYROOT1 #include #include #include #include #endif #include #include "slsInterpolation.h" #include "tiffIO.h" class etaInterpolationBase : public slsInterpolation { protected: float *hhx; float *hhy; int *heta; int nbetaX, nbetaY; double etamin, etamax, etastepX, etastepY; double rangeMin, rangeMax; double *flat; int *hintcorr; public: etaInterpolationBase(int nx=400, int ny=400, int ns=25, int nsy=25, int nb=-1, int nby=-1, double emin=1, double emax=0) : slsInterpolation(nx,ny,ns,nsy), hhx(NULL), hhy(NULL), heta(NULL), nbetaX(nb), nbetaY(nby), etamin(emin), etamax(emax) { // cout << "eb " << nb << " " << emin << " " << emax << endl; // cout << nb << " " << etamin << " " << etamax << endl; if (nbetaX<=0) { //cout << "aaa:" <=etamax) { etamin=-1; etamax=2; } etastepX=(etamax-etamin)/nbetaX; etastepY=(etamax-etamin)/nbetaY; heta=new int[nbetaX*nbetaY]; hhx=new float[nbetaX*nbetaY]; hhy=new float[nbetaX*nbetaY]; rangeMin=etamin; rangeMax=etamax; flat= new double[nSubPixelsX*nSubPixelsY]; hintcorr=new int [nSubPixelsX*nSubPixelsY*nPixelsX*nPixelsY]; }; etaInterpolationBase(etaInterpolationBase *orig): slsInterpolation(orig){ nbetaX=orig->nbetaX; nbetaY=orig->nbetaY; etamin=orig->etamin; etamax=orig->etamax; rangeMin=orig->rangeMin; rangeMax=orig->rangeMax; etastepX=(etamax-etamin)/nbetaX; etastepY=(etamax-etamin)/nbetaY; heta=new int[nbetaX*nbetaY]; memcpy(heta,orig->heta,nbetaX*nbetaY*sizeof(int)); hhx=new float[nbetaX*nbetaY]; memcpy(hhx,orig->hhx,nbetaX*nbetaY*sizeof(float)); hhy=new float[nbetaX*nbetaY]; memcpy(hhy,orig->hhy,nbetaX*nbetaY*sizeof(float)); hintcorr=new int [nSubPixelsX*nSubPixelsY*nPixelsX*nPixelsY]; }; virtual void resetFlatField() { for (int ibx=0; ibx=etamax) { etamin=-1; etamax=2; } rangeMin=etamin; rangeMax=etamax; etastepX=(etamax-etamin)/nbetaX; etastepY=(etamax-etamin)/nbetaY; } return heta; }; int *setFlatField(int *h, int nb=-1, int nby=-1, double emin=1, double emax=0) { return setEta(h, nb, nby, emin, emax); }; int *getFlatField(){return setEta(NULL);}; int *getFlatField(int &nb, int &nby, double &emin, double &emax){ nb=nbetaX; nby=nbetaY; emin=etamin; emax=etamax; return getFlatField(); }; void *writeFlatField(const char * imgname) { float *gm=NULL; gm=new float[nbetaX*nbetaY]; for (int ix=0; ix=1) etamax=emax; if (emin<=0) etamin=emin; if (etamin>=etamax) { etamin=-1; etamax=2; } etastepX=(etamax-etamin)/nbetaX; etastepY=(etamax-etamin)/nbetaY; uint32 nnx; uint32 nny; float *gm=ReadFromTiff(imgname, nnx, nny); /* if (nnx!=nny) { */ /* cout << "different number of bins in x " << nnx << " and y " << nny<< " !"<< endl; */ /* cout << "Aborting read"<< endl; */ /* return 0; */ /* } */ nbetaX=nnx; nbetaY=nny; if (gm) { if (heta) { delete [] heta; delete [] hhx; delete [] hhy; } heta=new int[nbetaX*nbetaY]; hhx=new float[nbetaX*nbetaY]; hhy=new float[nbetaX*nbetaY]; for (int ix=0; ixScale((double)nSubPixels); return hhx; }; float *gethhy() { // hhy->Scale((double)nSubPixels); return hhy; }; void debugSaveAll(int ind=0) { int ibx, iby; char tit[10000]; float tot_eta=0; float *etah=new float[nbetaX*nbetaY]; // int etabins=nbeta; int ibb=0; for (int ii=0; ii=nSubPixelsX) ibx=nSubPixelsX-1; if (iby>=nSubPixelsY) iby=nSubPixelsY-1; if (ibx>=0 && ibx=0 && iby0 && iby>0) cout << ibx << " " << iby << " " << ii << endl; ftest[ibx+iby*nSubPixelsX]+=heta[ii]; } else cout << "Bad interpolation "<< ii << " " << ibx << " " << iby<< endl; } sprintf(tit,"/scratch/ftest_%d.tiff",ind); WriteToTiff(ftest, tit, nSubPixelsX, nSubPixelsY); //int ibx=0, iby=0; tot_eta/=nSubPixelsX*nSubPixelsY; int nbad=0; for (int ii=0; iitot_eta*2.){ etah[ii]=2; nbad++; } else etah[ii]=0; } sprintf(tit,"/scratch/eta_bad_%d.tiff",ind); WriteToTiff(etah, tit, nbetaX, nbetaY); // cout << "Index: " << ind << "\t Bad bins: "<< nbad << endl; //int ibx=0, iby=0; delete [] ftest; delete [] etah; } virtual int *getInterpolatedImage(){ /* int ipx, ipy; */ /* // cout << "ff" << endl; */ /* calcDiff(1, hhx, hhy); //get flat */ /* double avg=0; */ /* for (ipx=0; ipx0) */ /* hintcorr[ibx+iby*nSubPixelsX*nPixelsX]=hint[ibx+iby*nSubPixelsX*nPixelsX]*(avg/flat[ipx+ipy*nSubPixelsX]); */ /* else */ /* hintcorr[ibx+iby*nSubPixelsX*nPixelsX]=hint[ibx+iby*nSubPixelsX*nPixelsX]; */ /* } */ /* } */ return hint; /* return hintcorr; */ }; protected: double calcDiff(double avg, float *hx, float *hy) { //double p_tot=0; double diff=0, d; //double bsize=1./nSubPixels; int nbad=0; double p_tot_x[nSubPixelsX], p_tot_y[nSubPixelsY], p_tot[nSubPixelsX*nSubPixelsY]; double maxdiff=0, mindiff=avg*nSubPixelsX*nSubPixelsY; int ipx, ipy; for (ipy=0; ipy=nSubPixelsX) ipx=nSubPixelsX-1; ipy=hy[ibx+iby*nbetaX]*nSubPixelsY; if (ipy<0) ipy=0; if (ipy>=nSubPixelsY) ipy=nSubPixelsY-1; p_tot[ipx+ipy*nSubPixelsX]+=heta[ibx+iby*nbetaX]; p_tot_y[ipy]+=heta[ibx+iby*nbetaX]; p_tot_x[ipx]+=heta[ibx+iby*nbetaX]; } } // cout << endl << endl; for (ipy=0; ipy5*sqrt(avg) ) nbad++; diff+=d*d; if (dmaxdiff) maxdiff=d; // cout << setprecision(4) << p_tot[ipx+nSubPixels*ipy] << " "; } /* cout << "** " << setprecision(4) << flat_y[ipy]; */ //cout << "\n"; } /* cout << "**" << endl; cout.width(5); */ /* for (ipx=0; ipx2 || nSubPixelsY>2) calcEta(totquad, sDum, etax, etay); getInterpolatedPosition(x,y,etax,etay,corner,int_x,int_y); return; }; virtual void getInterpolatedPosition(int x, int y, double *data, double &int_x, double &int_y) { double sDum[2][2]; double tot, totquad; double etax=0,etay=0; int corner; corner=calcQuad(data, tot, totquad, sDum); if (nSubPixelsX>2 || nSubPixelsY>2 ) calcEta(totquad, sDum, etax, etay); getInterpolatedPosition(x,y,etax,etay,corner,int_x,int_y); return; }; virtual void getInterpolatedPosition(int x, int y, double totquad,int quad,double *cl,double &int_x, double &int_y) { double cc[2][2]; int xoff=0, yoff=0; switch (quad) { case BOTTOM_LEFT: xoff=0; yoff=0; break; case BOTTOM_RIGHT: xoff=1; yoff=0; break; case TOP_LEFT: xoff=0; yoff=1; break; case TOP_RIGHT: xoff=1; yoff=1; break; default: ; } double etax=0, etay=0; if (nSubPixelsX>2 || nSubPixelsY>2) { cc[0][0]=cl[xoff+3*yoff]; cc[1][0]=cl[xoff+3*(yoff+1)]; cc[0][1]=cl[xoff+1+3*yoff]; cc[1][1]=cl[xoff+1+3*(yoff+1)]; calcEta(totquad,cc,etax,etay); } return getInterpolatedPosition(x,y,etax, etay,quad,int_x,int_y); } virtual void getInterpolatedPosition(int x, int y, double totquad,int quad,int *cl,double &int_x, double &int_y) { double cc[2][2]; int xoff=0, yoff=0; switch (quad) { case BOTTOM_LEFT: xoff=0; yoff=0; break; case BOTTOM_RIGHT: xoff=1; yoff=0; break; case TOP_LEFT: xoff=0; yoff=1; break; case TOP_RIGHT: xoff=1; yoff=1; break; default: ; } double etax=0, etay=0; if (nSubPixelsX>2 || nSubPixelsY>2) { cc[0][0]=cl[xoff+3*yoff]; cc[1][0]=cl[xoff+3*(yoff+1)]; cc[0][1]=cl[xoff+1+3*yoff]; cc[1][1]=cl[xoff+1+3*(xoff+1)]; calcEta(totquad,cc,etax,etay); } return getInterpolatedPosition(x,y,etax, etay,quad,int_x,int_y); } virtual void getInterpolatedPosition(int x, int y, double etax, double etay, int corner, double &int_x, double &int_y) { double xpos_eta=0,ypos_eta=0; double dX,dY; int ex,ey; switch (corner) { case TOP_LEFT: dX=-1.; dY=0; break; case TOP_RIGHT: ; dX=0; dY=0; break; case BOTTOM_LEFT: dX=-1.; dY=-1.; break; case BOTTOM_RIGHT: dX=0; dY=-1.; break; default: cout << "bad quadrant" << endl; dX=0.; dY=0.; } if (nSubPixelsX>2 || nSubPixelsY>2 ) { ex=(etax-etamin)/etastepX; ey=(etay-etamin)/etastepY; if (ex<0) { cout << "x*"<< ex << endl; ex=0; } if (ex>=nbetaX) { cout << "x?"<< ex << endl; ex=nbetaX-1; } if (ey<0) { cout << "y*"<< ey << " " << nbetaY << endl; ey=0; } if (ey>=nbetaY) { cout << "y?"<< ey << " " << nbetaY << endl; ey=nbetaY-1; } xpos_eta=(((double)hhx[(ey*nbetaX+ex)])); ypos_eta=(((double)hhy[(ey*nbetaX+ex)])); if (xpos_eta<0 || xpos_eta>1) xpos_eta=-100; else xpos_eta+=dX ;///((double)nSubPixels); if (ypos_eta<0 || ypos_eta>1) ypos_eta=-100; else ypos_eta+=dY ;///((double)nSubPixels); } else { xpos_eta=0.5*dX+0.25; ypos_eta=0.5*dY+0.25; } if (xpos_eta<-1) int_x=-100; else int_x=((double)x) + xpos_eta+0.5; if (ypos_eta<-1) int_y=-100; else int_y=((double)y) + ypos_eta+0.5; } virtual int addToFlatField(double totquad,int quad,int *cl,double &etax, double &etay) { double cc[2][2]; int xoff=0, yoff=0; switch (quad) { case BOTTOM_LEFT: xoff=0; yoff=0; break; case BOTTOM_RIGHT: xoff=1; yoff=0; break; case TOP_LEFT: xoff=0; yoff=1; break; case TOP_RIGHT: xoff=1; yoff=1; break; default: ; } cc[0][0]=cl[xoff+3*yoff]; cc[1][0]=cl[xoff+3*(yoff+1)]; cc[0][1]=cl[xoff+1+3*yoff]; cc[1][1]=cl[xoff+1+3*(yoff+1)]; //calcMyEta(totquad,quad,cl,etax, etay); calcEta(totquad, cc,etax, etay); // cout <<"******"<< etax << " " << etay << endl; return addToFlatField(etax,etay); } virtual int addToFlatField(double totquad,int quad,double *cl,double &etax, double &etay) { double cc[2][2]; int xoff=0, yoff=0; switch (quad) { case BOTTOM_LEFT: xoff=0; yoff=0; break; case BOTTOM_RIGHT: xoff=1; yoff=0; break; case TOP_LEFT: xoff=0; yoff=1; break; case TOP_RIGHT: xoff=1; yoff=1; break; default: ; } cc[0][0]=cl[xoff+3*yoff]; cc[1][0]=cl[(yoff+1)*3+xoff]; cc[0][1]=cl[yoff*3+xoff+1]; cc[1][1]=cl[(yoff+1)*3+xoff+1]; /* cout << cl[0] << " " << cl[1] << " " << cl[2] << endl; */ /* cout << cl[3] << " " << cl[4] << " " << cl[5] << endl; */ /* cout << cl[6] << " " << cl[7] << " " << cl[8] << endl; */ /* cout <<"******"<Fill(etax,etay); #endif #ifndef MYROOT1 int ex,ey; ex=(etax-etamin)/etastepX; ey=(etay-etamin)/etastepY; if (ey=0 && ey>=0) heta[ey*nbetaX+ex]++; #endif return 0; }; }; class eta3InterpolationBase : public virtual etaInterpolationBase { public: eta3InterpolationBase(int nx=400, int ny=400, int ns=25, int nsy=25, int nb=-1, int nby=-1, double emin=1, double emax=0) : etaInterpolationBase(nx, ny, ns, nsy, nb, nby, emin, emax) { cout << "e3ib " << nb << " " << emin << " " << emax << endl; }; eta3InterpolationBase(eta3InterpolationBase *orig): etaInterpolationBase(orig){ }; ////////////////////////////////////////////////////////////////////////////// //////////// /*It return position hit for the event in input */ ////////////// virtual void getInterpolatedPosition(int x, int y, int *data, double &int_x, double &int_y) { double tot; double etax,etay; int corner=calcEta3(data,etax,etay, tot); getInterpolatedPosition(x,y,etax,etay,corner,int_x,int_y); return; }; virtual void getInterpolatedPosition(int x, int y, double *data, double &int_x, double &int_y) { //double sDum[2][2]; double tot; double etax,etay; int corner=calcEta3(data,etax,etay, tot); getInterpolatedPosition(x,y,etax,etay,corner,int_x,int_y); return; }; virtual void getInterpolatedPosition(int x, int y, double totquad,int quad,double *cl,double &int_x, double &int_y) { double etax, etay; if (nSubPixelsX>2 || nSubPixelsY>2 ) { calcEta3(cl,etax,etay, totquad); } return getInterpolatedPosition(x,y,etax, etay,quad,int_x,int_y); } virtual void getInterpolatedPosition(int x, int y, double totquad,int quad,int *cl,double &int_x, double &int_y) { double etax, etay; if (nSubPixelsX>2 || nSubPixelsY>2 ) { calcEta3(cl,etax,etay, totquad); } return getInterpolatedPosition(x,y,etax, etay,quad,int_x,int_y); } virtual void getInterpolatedPosition(int x, int y, double etax, double etay, int corner, double &int_x, double &int_y) { //cout << "**"; double xpos_eta=0,ypos_eta=0; int ex,ey; if (nSubPixelsX>2 || nSubPixelsY>2 ) { #ifdef MYROOT1 xpos_eta=(hhx->GetBinContent(hhx->GetXaxis()->FindBin(etax),hhy->GetYaxis()->FindBin(etay)))/((double)nSubPixelsX); ypos_eta=(hhy->GetBinContent(hhx->GetXaxis()->FindBin(etax),hhy->GetYaxis()->FindBin(etay)))/((double)nSubPixelsY); #endif #ifndef MYROOT1 ex=(etax-etamin)/etastepX; ey=(etay-etamin)/etastepY; if (ex<0) { /* cout << etax << " " << etamin << " "; */ /* cout << "3x*"<< ex << endl; */ ex=0; } if (ex>=nbetaX) { /* cout << etax << " " << etamin << " "; */ /* cout << "3x?"<< ex << endl; */ ex=nbetaX-1; } if (ey<0) { /* cout << etay << " " << etamin << " "; */ /* cout << "3y*"<< ey << endl; */ ey=0; } if (ey>=nbetaY) { /* cout << etay << " " << etamin << " "; */ /* cout << "3y?"<< ey << endl; */ ey=nbetaY-1; } xpos_eta=(((double)hhx[(ey*nbetaX+ex)]));///((double)nSubPixels); ypos_eta=(((double)hhy[(ey*nbetaX+ex)]));///((double)nSubPixels); #endif } else { switch (corner) { case BOTTOM_LEFT: xpos_eta=-0.25; ypos_eta=-0.25; break; case BOTTOM_RIGHT: xpos_eta=0.25; ypos_eta=-0.25; break; case TOP_LEFT: xpos_eta=-0.25; ypos_eta=0.25; break; case TOP_RIGHT: xpos_eta=0.25; ypos_eta=0.25; break; default: xpos_eta=0; ypos_eta=0; } } /* if (xpos_eta<0 || xpos_eta>1) */ /* int_x=-1; */ /* else */ int_x=((double)x) + xpos_eta; /* if (ypos_eta<0 || ypos_eta>1) */ /* int_y=-1; */ /* else */ int_y=((double)y) + ypos_eta; // int_x=5. + xpos_eta; // int_y=5. + ypos_eta; } virtual int addToFlatField(double totquad,int quad,int *cl,double &etax, double &etay) { calcEta3(cl, etax, etay, totquad); return addToFlatField(etax,etay); } virtual int addToFlatField(double totquad,int quad,double *cl,double &etax, double &etay) { calcEta3(cl, etax, etay, totquad); return addToFlatField(etax,etay); } ////////////////////////////////////////////////////////////////////////////////////// virtual int addToFlatField(double *cluster, double &etax, double &etay){ double totquad; calcEta3(cluster, etax, etay, totquad); return addToFlatField(etax,etay); }; virtual int addToFlatField(int *cluster, double &etax, double &etay){ double totquad; calcEta3(cluster, etax, etay, totquad); return addToFlatField(etax,etay); }; virtual int addToFlatField(double etax, double etay){ #ifdef MYROOT1 heta->Fill(etax,etay); #endif #ifndef MYROOT1 int ex,ey; ex=(etax-etamin)/etastepX; ey=(etay-etamin)/etastepY; if (ey=0 && ey>=0) heta[ey*nbetaX+ex]++; #endif return 0; }; }; #endif