Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:15:07

0001 int Fe55_main (  const char * dir, const char * outName = "Fe_50V" );
0002 #ifndef __CINT__
0003 
0004 #include "../Bias.cpp"
0005 
0006 #include <assert.h>
0007 #include <time.h>
0008 #include <TRandom.h>
0009 #include "TMinuit.h"
0010 
0011 
0012 class rPad {
0013 private:
0014     TVirtualPad *fPad;
0015 public:
0016     rPad(): fPad(gPad){}
0017     ~rPad(){ if (fPad) fPad->cd(); }
0018 };
0019 // CTE parameters *****************************************************

0020 //                    1 3 5 7 

0021 const double CteX = 0.9999999; //0.999997;

0022 const double CteY = 0.9999999;
0023 
0024 // other constants ++++++++++++++++++++++++++++++++++++++++++++++++++++

0025 
0026 class Co {
0027 public:
0028 // canvas constants

0029     static const float left_margin;
0030     static const float right_margin;
0031     static const float top_margin;
0032     static const float bot_margin;
0033     
0034 //what to do flags

0035     static const int doFit;
0036     static const int doCTE;
0037 
0038 // Fe55 lines and related constants

0039     static const int Nsrch;
0040     static const int Nxtail;
0041     static const int Nytail;
0042     static const int NXpix;
0043     static const int NYpix;
0044     static const int NBtot;
0045     static const int Nsumh;
0046     static const double KcutL;
0047     static const double KcutR;
0048     static const double Ea;
0049     static const double Eb;
0050     static const double w_pair;
0051     static const double Fano;
0052     static const double NeKa;
0053     static const double NeKb;
0054 // geometry & absorbtion

0055     static const double Depth; //sensor thickness in micron

0056     static const double Dchan; //N-channel thickness in microns

0057     static const double DelD;  // thickness ratio

0058     static const double Tau_a; //absorbtion 

0059     static const double Tau_b; //absorbtion

0060 //electrical

0061     static const double Iqmu;  // 1/q*mu in (kOhm*cm)*micron**-3

0062     static const double Qtke;  // q/(2*k*e_0)in V*micron

0063     static const double Ro;   // Si resistivity in kOhm*cm

0064     static const double Na;   // acceptor density in microm^-3

0065     static const double Nd;   // dopant density in microm^-3

0066     static const double Cpn;  // pn-junction correction factor

0067     static const double Vde;  // depletion voltage

0068     static const double mobil_e; 
0069     static const double v_sat_e; 
0070 
0071 // run constant

0072     static const double NTlow;
0073     static const double NTcnt;
0074     static const double Gain;  // representative gain

0075     static const double ConvGain[16];
0076 
0077 };
0078 
0079 const float Co::left_margin =  (float)0.06;
0080 const float Co::right_margin = (float)0.006;
0081 const float Co::top_margin =   (float)0.01;
0082 const float Co::bot_margin =   (float)0.04;
0083 
0084 const int Co::doFit = 1;
0085 const int Co::doCTE = 0;
0086 
0087 const int Co::Nsrch = 3;
0088 const int Co::Nxtail = 0;
0089 const int Co::Nytail = 0;
0090 const int Co::NXpix = Co::Nsrch + Co::Nxtail;
0091 const int Co::NYpix = Co::Nsrch + Co::Nytail;
0092 const int Co::NBtot = Co::NXpix*Co::NYpix;
0093 const int Co::Nsumh = Co::NBtot + 1;
0094 
0095 const double Co::KcutL =  1500.;  // e-

0096 const double Co::KcutR =  1700.;  // e-

0097 const double Co::Ea= 5897.; //in eV

0098 const double Co::Eb = 6490.; //in eV

0099 const double Co::w_pair = 3.68; //in eV/e-

0100 const double Co::Fano = 0.12;
0101 const double Co::NeKa = Co::Ea/Co::w_pair; //in electrons

0102 const double Co::NeKb = Co::Eb/Co::w_pair; //in electrons

0103 
0104 const double Co::Depth = 100.; //sensor thickness in microns

0105 const double Co::Dchan = 1.;    //N-channel thickness in microns

0106 const double Co::DelD = Co::Dchan/Co::Depth;  // thickness ratio

0107 const double Co::Tau_a = 28.8/Co::Depth; //absorbtion (in microns)

0108 const double Co::Tau_b = 37.9/Co::Depth; //absorbtion (in microns)

0109 
0110 const double Co::Iqmu=4.6296;  // 1/q*mu in (kOhm*cm)*micron**-3

0111 const double Co::Qtke=0.7717e-3;  // q/(2*k*e_0)in V*micron

0112 const double Co::Ro=3.;  // Si resistivity in kOhm*cm

0113 const double Co::Na=Co::Iqmu/Co::Ro; // 1.54 for 3 kOhm*cm, density in microm^-3

0114 //const double Co::Na=1.54;   // 1.54 for 3 kOhm*cm, density in microm^-3

0115 //const double Co::Na=1.155;  // for 4 kOhm*cm, density in microm^-3

0116 //const double Co::Na=0.924;  // for 5 kOhm*cm, density in microm^-3

0117 const double Co::Nd=1.6e+3;  //dopant density in microm^-3

0118 const double Co::Cpn=1.-Co::DelD*Co::DelD-Co::DelD*Co::DelD*Co::Nd/Co::Na;  //junction correction factor

0119 const double Co::Vde=Co::Qtke*Co::Na*Co::Depth*Co::Depth*Co::Cpn;  //depletion voltage

0120 
0121 const double Co::mobil_e = 900.;  //in micron**2/(ns*V) 

0122 const double Co::v_sat_e = 118.;  //in micron/ns 

0123 
0124 const double Co::NTlow = -3.0;  //1.2;   // Noise Threshold low

0125 const double Co::NTcnt = 5.0;   // Noise Threshold count

0126 const double Co::Gain  = 3.0;   // in e-/adu

0127 const double Co::ConvGain[16]={
0128     1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,    //2.32, 2.93,

0129     1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
0130 // 112-09 TS3-SK

0131 //1.9946, 1.9831, 1.9521, 1.9543, 1.9677, 1.9709, 1.9733, 1.9844,

0132 //2.0202, 1.9952, 2.0233, 1.9689, 2.0233, 1.9845, 2.0416, 2.0035  };

0133 // 112-01 TS3-JWY

0134 //3.0159, 3.0040, 3.5802, 3.0541, 2.9406, 3.0012, 3.0181, 2.9704,      // ch2 with bad CTE

0135 //2.9568, 3.0146, 2.9646, 2.9576, 2.9881, 3.0488, 3.0515, 3.1203 };

0136 
0137     //int NKhit=0;

0138     //double AvHit[Co::NXpix][Co::NYpix]={{0.}};

0139 // Fit Functions ******************************************************

0140 //*********************************************************************

0141 // par[]:

0142 // 0 - total/integral amplitude

0143 // 1 - x-center 

0144 // 2 - y-center

0145 // 3 - sigma 

0146 
0147 double  G2DI ( double *v, double *par )  
0148 {
0149     const double bin_w2 = 0.5;
0150     double x = v[0];
0151     double y = v[1];
0152     double sigma;
0153     if ( par[3] <  0.000001 ) { sigma = 1.e-6; }  //&& par[3] > -0.000001 

0154     else { sigma = par[3]; }
0155 
0156     double Xmin = (x - bin_w2 - par[1])/sigma;
0157     double Xmax = (x + bin_w2 - par[1])/sigma;
0158     double Ymin = (y - bin_w2 - par[2])/sigma;
0159     double Ymax = (y + bin_w2 - par[2])/sigma;
0160     double fitval  =    par[0];
0161           fitval *= TMath::Freq(Xmax) - TMath::Freq(Xmin);
0162           fitval *= TMath::Freq(Ymax) - TMath::Freq(Ymin);
0163     return fitval;
0164 }
0165 
0166 // cluster finder *******************************************

0167 // sequential clusters **************************************

0168 // individual cluster

0169 class Hits;
0170 class OneHit{
0171 public:
0172     int Flag;
0173     
0174     static int Nrpix1;
0175     static double Acut1;
0176     static double AcutL1;
0177     static int jy_c;         // current row index

0178     static double * p_crow;  // current row (internal) pointer

0179     static double * p_arow;  // antecedent row (internal) pointer

0180     static int * Seed;       // pointer to Hits Seed array

0181     //    double Amp[Wpad][Wtime];

0182     
0183     int    ixfirst,  jyfirst;  //first pixel in the seed

0184     int    ixmax,  jymax;      //pixel with max amplitude

0185     int    ixl, ixr;           //left and right index of the zone

0186     int    jylast;
0187     int    ixcntr, jycntr;     //zone center

0188     double xhit, yhit;         //centroid coordinates

0189     double Amax;
0190     double SumA, xA, yA;
0191     int Npix;          //number of pixels abouve threshold

0192     int Nxpix;         //number of pixels in X-direction

0193     int Nypix;         //number of pixels in Y-direction

0194     
0195     OneHit(){Flag=0;};
0196     void Init( int ix, int jy );
0197     int AddPix( int ix, int jy );
0198     void Done( void ){Flag=1;};
0199     ~OneHit(){};
0200 };
0201 int OneHit::Nrpix1=0;
0202 double OneHit::Acut1=0.;
0203 double OneHit::AcutL1=0.;
0204 int OneHit::jy_c=0;       // current row index

0205 double * OneHit::p_crow=0;  //for current row array

0206 double * OneHit::p_arow=0;  //for antecedent row array

0207 int * OneHit::Seed=0;     //pointer to Hits Seed array

0208 
0209 // cluster collection and management

0210 class Hits{
0211 public:
0212     static const int Wpad;  //x coord

0213     static const int Wtime; //y coord

0214     const int Nrpix;
0215     const int maxHit;
0216     const int maxSeed;
0217     const double Noise;
0218     const double Threshold;
0219     const double Acut;
0220     const double AcutL;
0221 
0222     int NSeeds;
0223     int NHits;
0224     int jy_c;       // current row index

0225     double * prow;  // current row pointer

0226     double * crow;  // current row array

0227     double * arow;  // antecedent row array

0228 
0229     OneHit * hit1;  //for hit array[maxHit];

0230     int * Seed;     //for seed array[maxSeed];

0231     
0232 void GetRow(double * p_row, int yc);
0233 void ClusterMatch(void);
0234 void ClusterSeed(void);
0235 Hits( int nx, int ny, double ANoise, double thresh);
0236 //~Hits(){};

0237 ~Hits(){
0238     delete [] crow; crow=0;
0239     delete [] arow; arow=0;
0240     delete [] hit1; hit1=0;
0241     delete [] Seed; Seed=0;
0242 }
0243 
0244 };
0245 const int Hits::Wpad=3;
0246 const int Hits::Wtime=3;
0247 
0248 // OneHit & Hits implementation

0249 void OneHit::Init( int ix, int jy )
0250 {
0251     double amp;
0252     ixfirst=ix;
0253     ixmax=ix;
0254     printf(" OneHit::Init  %i %i \n", ix, jy);
0255     if (ix>0) {
0256         amp=*(p_crow+ix-1);
0257         if (amp > AcutL1) ixfirst=ix-1;
0258         printf(" OneHit::Init  %i %f %f \n", ixfirst, AcutL1, amp );
0259     }
0260     jyfirst=jy;
0261     jymax=jy;
0262     jylast=jyfirst+Hits::Wtime-1;
0263     jycntr=jyfirst+Hits::Wtime/2;
0264 
0265     Npix=1;
0266     Nxpix=1;
0267     Nypix=1;
0268     Amax=SumA=0.;
0269     xA=0.;
0270     int nn=ixfirst+Hits::Wpad;
0271     nn = nn<Nrpix1 ? nn : Nrpix1;
0272     for (int i=ixfirst; i<nn; i++){
0273         amp=*(p_crow+i);
0274         //if (amp<AcutL1) break;

0275         if (amp > Amax) {Amax=amp; ixmax=i;}
0276         Nxpix++;
0277         SumA+= amp;
0278         xA+=i*amp;
0279         *(p_crow+i)=0.;
0280     }
0281     yA=jy*SumA;
0282     // check the antedecent row

0283     double aSumA=0;
0284     Nypix++;
0285     for (int i=ixfirst; i<nn; i++){
0286         amp=*(p_arow+i);
0287         //if (amp<AcutL1) break;

0288         if (amp > Amax) {Amax=amp; ixmax=i;}
0289         Nxpix++;
0290         aSumA+= amp;
0291         xA+=i*amp;
0292         *(p_arow+i)=0.;
0293     }
0294     if (aSumA>SumA) {jymax=jy-1;}
0295     yA+=(jy-1)*aSumA;
0296     SumA+=aSumA;
0297     xhit=xA/SumA;
0298     yhit=yA/SumA;
0299     jyfirst=jy-1;
0300     
0301 
0302     ixl=ixfirst;
0303     ixr=ixfirst+Hits::Wpad;
0304     ixcntr=ixl+Hits::Wpad/2;
0305     if (ixr>Nrpix1) {ixr=Nrpix1;}
0306     printf(" OneHit::Init  %i %i %f %f %f \n", ixl,ixr, SumA, xhit, yhit );
0307 }
0308 
0309 int OneHit::AddPix( int ix, int jy)
0310 {
0311     if (jy>jylast) {Flag=1; return 1;}
0312     double amp = *(p_crow+ix);
0313     SumA += amp;
0314     if (amp > Amax) {Amax=amp;}
0315     xA+=ix*amp;
0316     yA+=jy*amp;
0317     *(p_crow+ix)=0;
0318     return 0;
0319 }
0320 
0321 
0322 Hits::Hits( int nx, int ny, double ANoise, double thresh):
0323 Nrpix(nx),
0324 maxHit((nx/Wpad+1)*(ny/Wtime+1)),
0325 maxSeed(Wtime*nx/Wpad),
0326 Noise(ANoise),
0327 Threshold(thresh),
0328 Acut(Noise*Threshold),
0329 AcutL(Noise*2.0),
0330 NSeeds(0),
0331 NHits(0),
0332 jy_c(0),
0333 prow(0)
0334 {
0335     crow = new double[Nrpix];
0336     arow = new double[Nrpix];
0337     hit1 = new OneHit[maxHit];
0338     Seed = new int[maxSeed];
0339     memset( crow, 0, Nrpix);
0340     memset( arow, 0, Nrpix);
0341     memset( Seed, 0, maxSeed);
0342 }; // end of Hits constructor

0343 
0344 void Hits::GetRow(double * p_row, int yc)
0345 {
0346     if (!prow) { for (int i=0; i<Nrpix; i++) {arow[i]= crow[i];} }
0347     prow=p_row;
0348     jy_c=yc;
0349     for (int i=0; i<Nrpix; i++) {crow[i]= *(prow+i);}
0350 }
0351 
0352 void Hits::ClusterMatch(void)
0353 {
0354     int full=0;
0355     if (!NSeeds) return;
0356 //    printf(" Hits::ClusterMatch NSeeds= %i  \n", NSeeds);

0357     for (int i=0; i<NSeeds; i++){
0358         int hind=Seed[i];
0359         int lind=hit1[hind].ixl;
0360         int rind=hit1[hind].ixr;
0361         int ixcnt=0;
0362 //        printf(" Hits::ClusterMatch l,r= %i %i %i %i \n", lind, rind, NSeeds, i);

0363        for (int ii=lind; ii<rind; ii++) {
0364 //           printf(" Hits::ClusterMatch ii,crow= %i %f \n", ii, crow[ii]);

0365             if (crow[ii]<AcutL) {ixcnt++;}
0366             full=hit1[hind].AddPix(ii,jy_c);
0367             if (full) break;
0368             crow[ii]=0.; //clear the pixel

0369         }
0370         if (!ixcnt)  {
0371             hit1[hind].Done();
0372             for (int ii=i+1; ii<NSeeds; ii++) {Seed[ii-1]=Seed[ii];}
0373             NSeeds--;
0374             i--;
0375         }
0376     }
0377 }
0378 
0379 void Hits::ClusterSeed(void)
0380 {
0381     for (int i=0; i<Nrpix; i++) {
0382         if (crow[i]<Acut) continue;
0383         if (NSeeds >= maxSeed) break;
0384         if (NHits >= maxHit) break;
0385         Seed[NSeeds]=NHits;  //Seed points to a Hit index

0386         hit1[NHits].Init(i,jy_c);
0387         NSeeds++;
0388         NHits++;
0389  //       printf(" Hits::ClusterSeed i,NSeeds,NHits= %i %i %i \n", i, Nseeds, NHits);

0390     }
0391 }
0392 
0393 // High Amplitude Seed order ******************************************

0394 //*********************************************************************

0395 class Qhit{
0396 public:
0397     int ixb,   jyb;    
0398     void Set(int ix, int jy){ixb=ix; jyb=jy;};
0399     Qhit(){};
0400     ~Qhit(){};
0401 };
0402 
0403 class Ohit{
0404 public:
0405     const int Xmax;
0406     const int Xmin;
0407     const int Ymax;
0408     const int Ymin;
0409     const double Noise;
0410     const double lowT;
0411     const double cntT;
0412     const double Cntr;           // = Ncore/2.;

0413     const int nxb,   nyb;
0414     const long Jmax;
0415     double * pbuf;
0416     char * flagbuf;
0417     int Flag;
0418     int isoFlag; //isolated pixel flag

0419 
0420     enum { Ncore   = Co::Nsrch };
0421     enum { NXsrch  = Co::NXpix };
0422     enum { NYsrch  = Co::NYpix };
0423     double Amp[NXsrch][NYsrch];
0424     int    ixb,  jyb;    //center of the search area in the buffer

0425     double xhit, yhit;   //centroid local coordinates, relative zone corner

0426     double xhitG, yhitG;   //centroid global/image coordinates

0427     double xfitG, yfitG;   //centroid global fitted coordinates

0428     double Amax;
0429     double Aseed;
0430     double Sum;
0431     double SumCTE;
0432     double SumOne; //sum of isolated pixels

0433     double rms;
0434     double MRatio;
0435     double ARatio;
0436     int Npix;          //number of pixels abouve threshold

0437     int NpixH;         //number of pixels abouve High threshold

0438     int Nxpix;         //number of pixels in X-direction

0439     int Nypix;         //number of pixels in Y-direction 

0440 
0441     enum { NZ   = 4 };
0442     int Zind;
0443     int NKhit[NZ];
0444     double AvHit[NZ][NXsrch][NYsrch];
0445     double A2Hit[NZ][NXsrch][NYsrch];
0446 
0447     TCanvas *cF;
0448     TH2F * f2D;
0449     TF2 * fit;
0450     enum { Npar2DI = 4};
0451     double FitPar[Npar2DI];
0452     double FitErr[Npar2DI];
0453     int fitflag;
0454     const int Nfree;
0455     double chi2;
0456     double chiR;
0457     double Sumf;
0458     double xfit;
0459     double yfit;
0460     double sfit;
0461     double efit;
0462 
0463     void FitX( void );  // 2D cluster fit TCanvas *cF 

0464     void Cluster( int ix, int jy, const int pass=0  );
0465     void Clear( void );
0466     Ohit(int max_X, int min_X, int max_Y, int min_Y, double ANoise,
0467             int nx, int ny, double * buf,   
0468             TCanvas * pcF, TH2F * pf2D, TF2 * pfit );   
0469     //Ohit(int max_X, int min_X, int max_Y, int min_Y, double ANoise,

0470     //      int nx, int ny, double * buf);

0471     ~Ohit(){delete [] flagbuf; flagbuf = 0;};
0472 };
0473 
0474 Ohit::Ohit( int max_X, int min_X, int max_Y, int min_Y, double ANoise,
0475             int nx, int ny, double * buf,   
0476             TCanvas * pcF, TH2F * pf2D, TF2 * pfit ):
0477     Xmax(max_X),
0478     Xmin(min_X),
0479     Ymax(max_Y),
0480     Ymin(min_Y),
0481     Noise(ANoise),
0482     lowT(Co::NTlow*Noise),
0483     cntT(Co::NTcnt*Noise),
0484     Cntr(Ncore/2.),
0485     nxb(nx),
0486     nyb(ny),
0487     Jmax(nxb*nyb),
0488     pbuf(buf),
0489     Flag(-100),
0490     isoFlag(-100),
0491     cF(pcF),
0492     f2D(pf2D),
0493     fit(pfit),
0494     fitflag(-1),
0495     Nfree(NXsrch*NYsrch-Npar2DI),
0496     chi2(-1.0),
0497     chiR(-1.0),
0498     Sumf(-1.0),
0499     xfit(-1.0),
0500     yfit(-1.0),
0501     sfit(-1.0),
0502     efit(-1.0)
0503 {
0504     if ( pcF==0)  cF = new TCanvas("Fit2D", "Fit2D", 300,10,700,650);
0505     if ( f2D==0) f2D = new TH2F("f2D","fit2", NXsrch, 0, NXsrch, NYsrch, 0, NYsrch);
0506     if ( fit==0) fit = new TF2("fitG2DI", G2DI, 0., NXsrch, 0., NYsrch, Npar2DI);
0507     for (int i=0;i<NZ;i++){
0508         NKhit[i]=0;
0509         for (int y=0; y<NYsrch; y++){
0510             for (int x=0; x<NXsrch; x++){
0511                     AvHit[i][x][y] =0.;
0512                     A2Hit[i][x][y] =0.;
0513             }
0514         }
0515     }
0516     flagbuf =new char[Jmax];
0517     memset( flagbuf, 0, Jmax);
0518 
0519 } // end of Ohit constructor

0520 
0521 void Ohit::Cluster(int ix, int jy, const int pass )
0522 {
0523     ixb=ix;
0524     jyb=jy;
0525     Flag = -1000;
0526     isoFlag = -1000;
0527     xhit = 0.;
0528     yhit = 0.;
0529     xhitG = 0.;
0530     yhitG = 0.;
0531     xfitG = 0.;
0532     yfitG = 0.;
0533     Amax = 0.;
0534     Sum  = 0.;
0535     SumCTE = 0.;
0536     SumOne = 0.;
0537     rms = 0.;
0538     ARatio = 0.;
0539     Npix = 0;
0540     NpixH= 0;
0541     fitflag=-1;
0542     chi2=-1.0;
0543     chiR=-1.0;
0544     Sumf=-1.0;
0545     xfit=-1.0;
0546     yfit=-1.0;
0547     sfit=-1.0;
0548     efit=-1.0;
0549 
0550     int js = jyb*nxb + ixb;
0551     if (js < 0 || js >= Jmax) return; 
0552     Aseed = pbuf[js];
0553     if ( Aseed < 3.*Noise ) return;
0554 
0555     Flag = 0;
0556 
0557     char pileup=0;
0558     const int center = Ncore/2;
0559     for (int y=0; y<NYsrch; y++){
0560         int ybuf = jyb + y - center;
0561         if ( ybuf < Ymin || ybuf >= Ymax) {Flag = -1; continue;}
0562         for (int x=0; x<NXsrch; x++){
0563             int xbuf = ixb + x - center;
0564             if ( xbuf < Xmin || xbuf >= Xmax) {Flag = -1; continue;}
0565             int jb = ybuf*nxb + xbuf;
0566             double amp = 0.;
0567             if (Flag == 0 ) { amp = pbuf[jb]; }  
0568             Sum += amp;  
0569             if (pass) { flagbuf[jb]++;}
0570             if ( flagbuf[jb] >1 ) {pileup = 1;}
0571             if (  amp > cntT ) { NpixH++; }
0572             if ( (amp > lowT) || pass ) {
0573                 Npix++;
0574                 //Sum += amp;

0575                 xhit += x*amp;
0576                 yhit += y*amp;
0577                 double ampC = amp;
0578                 ampC /=(TMath::Power(CteX, xbuf)*TMath::Power(CteY, ybuf));
0579                 SumCTE += ampC; 
0580             }
0581             if ( amp > Amax ) { Amax = amp; }
0582             Amp[x][y] = amp;
0583         }
0584     }
0585 
0586     if (pass) { return; }
0587     if ( pileup ) {Flag = -200;  return;}  //printf("Pileup detected \n");

0588     if ( Sum < 100.) {Flag = -100;  return;}  //printf("Sum<100 \n");

0589     MRatio = Amax/Sum;
0590     ARatio = 0.125*(Sum/Amax - 1.);
0591     xhit /=Sum; 
0592     yhit /=Sum;
0593     xhitG= ixb + xhit - Cntr;
0594     yhitG= jyb + yhit - Cntr;
0595 
0596     double rmsx2 = 0.;
0597     double rmsy2 = 0.;
0598     for (int y=0; y<NYsrch; y++){
0599         for (int x=0; x<NXsrch; x++){
0600             double amp=Amp[x][y];
0601             if (amp <0.) continue;
0602             rmsx2 += (x - xhit)*(x-xhit)*amp/Sum;
0603             rmsy2 += (y - xhit)*(y-xhit)*amp/Sum;
0604         }
0605     }
0606     rms = sqrt(rmsx2+rmsy2);
0607 
0608 // isolated pixel row flag

0609     isoFlag=0;
0610     SumOne = 0.;
0611     
0612     for(int x = 0; x < NXsrch; x++){
0613         for(int y = 0; y < NYsrch; y++) {
0614             if (y == center ) continue;
0615             if (Amp[x][y] > cntT) {isoFlag++;}
0616         }
0617     }
0618     
0619     int preC = center-1;
0620     if (preC<0) preC=0;
0621     
0622     for(int x = 0; x < preC; x++) {
0623         if (Amp[x][center] > cntT) {isoFlag++;}
0624     }
0625     
0626     if (!isoFlag) {
0627         for (int x = preC; x < NXsrch; x++) {
0628             SumOne += Amp[x][center];
0629         }
0630     }
0631     
0632     //if ( Sum < Co::KcutL && Sum > Co::KcutR ) { return; }

0633     //Zind=0;

0634     //if (ixb > nxb/2) Zind=1; 

0635     //NKhit[Zind]++;

0636     //for (int y=0; y<NYsrch; y++){

0637     //  for (int x=0; x<NXsrch; x++){

0638     //      double amp=Amp[x][y];  ///Sum

0639     //      AvHit[Zind][x][y] += amp;

0640     //      A2Hit[Zind][x][y] += amp*amp;

0641     //  }

0642     //}

0643     //Zind=2;

0644     //if (jyb > nyb/2) Zind=3; 

0645     //NKhit[Zind]++;

0646     //for (int y=0; y<NYsrch; y++){

0647     //  for (int x=0; x<NXsrch; x++){

0648     //      double amp=Amp[x][y];  ///Sum

0649     //      AvHit[Zind][x][y] += amp;

0650     //      A2Hit[Zind][x][y] += amp*amp;

0651     //  }

0652     //}

0653     //

0654 
0655     return;
0656 }
0657 
0658 void Ohit::FitX( void )
0659 {
0660 // 2D fit 

0661 
0662     for (int y=1; y<=NYsrch; y++){
0663             //cout << " y= " << y ;

0664         for (int x=1; x<=NXsrch; x++){
0665             double amp = Amp[x-1][y-1];
0666             if (amp < 0.) amp=0.;
0667             f2D->SetBinContent(x, y, amp);
0668             double StErr  = Noise;
0669             double StErr2 = Noise*Noise; 
0670             if ( amp > 3.*Noise ){
0671                 StErr2 += amp * Co::Gain * Co::Fano;
0672                 StErr = sqrt(StErr2);
0673             }
0674             f2D->SetBinError  (x, y, StErr);
0675             //cout << "  "   << amp;

0676         }
0677             //cout << endl;

0678     }
0679     //cF->cd(1);

0680     //f2D->Draw("lego");

0681 
0682     fit->SetParameter(0, Sum);
0683     //fit->SetParLimits(0,0.,2000.);

0684     fit->SetParameter(1, xhit);  
0685     fit->SetParameter(2, yhit); //fit->SetParLimits(2,0.,1000.);

0686     fit->SetParameter(3, rms);  
0687     //fit->SetParLimits(3,0.,8.);

0688     //cout<<" "<<endl;

0689     //cout << " Initial parameters: " << endl; 

0690     //cout << " N="<<Ncore<<" Npix="<<Npix<<" Aseed="<<Aseed<<endl;

0691     //for (int jp = 0; jp < Npar2DI; jp++){

0692     //  cout <<" par "<<jp<<" = "  << fit->GetParameter(jp)<< endl;

0693     //}

0694 
0695     fitflag = f2D->Fit("fitG2DI","Q"); //LNQ

0696     chi2 = fit->GetChisquare();
0697     chiR = chi2/(double)Nfree;
0698     Sumf = fit->GetParameter(0);
0699     xfit = fit->GetParameter(1);
0700     yfit = fit->GetParameter(2);
0701     sfit = fit->GetParameter(3);
0702     if (sfit<0.) sfit = -sfit;
0703     efit = fit->GetParError(3);
0704     if (efit<0.) efit = -efit;
0705     if ( fitflag == 0 ) {
0706         xhitG= ixb + xhit - Cntr;
0707         yhitG= jyb + yhit - Cntr;
0708         xfitG= ixb + xfit - Cntr;
0709         yfitG= jyb + yfit - Cntr;
0710     }
0711 
0712     //cout << " fit status = " << fitflag << endl;

0713     //cout << " Chi**2 = " << chi2  << endl;

0714     //cout << " Npar/Npix = "   << Npar2DI << "/" << Npix << endl;

0715     //for (int jp = 0; jp < Npar2DI; jp++){

0716     //  FitPar[jp]=fit->GetParameter(jp);

0717     //  FitErr[jp]=fit->GetParError(jp);

0718     //  cout <<" par "<<jp<<" = "<<FitPar[jp]<<" +- "<<FitErr[jp]<< endl;

0719     //  if ( FitPar[jp] < 0.) cout << " WARNING! " << FitPar[jp] << endl;

0720     //}

0721     //if ( fitflag != 0 ) { 

0722     //  fit->SetParameter(0, Sum);  

0723     //  //fit->SetParLimits(0,0.,2000.);

0724     //  fit->SetParameter(1, xhit);  

0725     //  fit->SetParameter(2, yhit); 

0726     //  fit->SetParameter(3, rms);  

0727     //  fit->SetParLimits(3,0.,8.);

0728     //  cout<<" *** Debug failed fit ***"<<endl;

0729     //  cout << " Initial parameters: " << endl; 

0730     //  cout << " N="<<Ncore<<" Npix="<<Npix<<" Aseed="<<Aseed<<endl;

0731     //  for (int jp = 0; jp < Npar2DI; jp++){

0732     //      cout <<" par "<<jp<<" = "  << fit->GetParameter(jp)<< endl;

0733     //  }

0734     //  fitflag = f2D->Fit("fitG2DI","LNV"); //Q

0735     //  TH2F * f2Df= new TH2F("f2Df","fit2f", NXsrch, 0, NXsrch, NYsrch, 0, NYsrch);

0736     //  double q[2];

0737     //  for (int y=1; y<=NYsrch; y++){

0738     //          cout << " y= " << y ;

0739     //          q[1] = (double)y-0.5; // bin center

0740     //      for (int x=1; x<=NXsrch; x++){

0741     //          q[0] = (double)x-0.5; // bin center

0742     //          double AmD = Amp[x-1][y-1];

0743     //          AmD -= G2DI(q, FitPar);  

0744     //          f2Df->SetBinContent(x, y, AmD);

0745     //          cout <<"  "<<Amp[x-1][y-1]<<" / "<<AmD;

0746     //      }

0747     //          cout << endl;

0748     //  }

0749     //  cF->cd(2);

0750     //  f2Df->Draw("lego");

0751 
0752     //  cF->Update();

0753     //  while(!gSystem->ProcessEvents()){gSystem->Sleep(400);};

0754     //  delete f2Df;

0755     //} //  end fit flag IF 

0756 
0757     //cF->Update();

0758     //while(!gSystem->ProcessEvents()){gSystem->Sleep(400);};

0759 
0760     return;
0761 }  // end Ohit::FitX         

0762          
0763 void Ohit::Clear()
0764 {
0765     if (Flag < -500 ) return;
0766     int Nc = Ncore/2;
0767     for (int i=0; i<NYsrch; i++){
0768             int xbuf = ixb + i - Nc;
0769         for (int j=0; j<NXsrch; j++){
0770             int ybuf = jyb + j - Nc;
0771             int jb = ybuf*nxb + xbuf;
0772             if ( (xbuf >= Xmin && xbuf < Xmax) &&
0773                  (ybuf >= Ymin && ybuf < Ymax)) pbuf[jb]=0.;
0774         }
0775     }
0776 }
0777 
0778 //*********************************************************************

0779 int PeakRange(TH1D * hist, double * maxpos, double * Rmin, double * Rmax) 
0780 {
0781     const double Fract = 0.04;
0782     int maxbin =    hist->GetMaximumBin();
0783     *maxpos =       hist->GetBinCenter(maxbin);
0784     double maxval = hist->GetBinContent(maxbin);    
0785 
0786     cout << " maxbin=" << maxbin << " maxval=" <<maxval <<endl;
0787     cout << " max/maxpos=" << *maxpos << endl;
0788 
0789     int bin = maxbin;
0790     while (bin>0 && hist->GetBinContent(bin) > Fract*maxval) bin--;
0791     *Rmin = hist->GetBinCenter(bin);
0792     cout << " Rmin=" << *Rmin << " A(Rmin)=" << hist->GetBinContent(bin) << endl;
0793 
0794     int xlast   = hist->GetXaxis()->GetLast();
0795     bin = maxbin;
0796     while (bin<xlast && hist->GetBinContent(bin) > Fract*maxval) bin++;
0797     bin++;
0798     *Rmax = hist->GetBinCenter(bin);
0799     cout << " Rmax=" << *Rmax << " A(Rmax)=" << hist->GetBinContent(bin) << endl;
0800 
0801     return 0;
0802 }
0803 //---------------------------------------------------------------------

0804 // SimX class ---------------------------------------------------------

0805 class SimX: public FileF {
0806 public:
0807 // canvas constants

0808     static const float left_margin;
0809     static const float right_margin;
0810     static const float top_margin;
0811     static const float bot_margin;
0812 
0813 // constants for simulation

0814     static const int bend;
0815     const int Nmatr;
0816     const int Msz;
0817     const int Centr;
0818     double * matrix;
0819 
0820     static const int Nray;     // number of X-rays

0821     static const double SiDi;      // in pixels

0822     static const double Sigma0;      // in pixels

0823      double SiPzS2;    // SiDi*sqrt(2.)

0824     static const double Vop;      // in V

0825     static const double Vde;      // in V

0826     //static const double Na;      // acceptor micron^-3

0827     //static const double Nd;      // donor micron^-3

0828     //static const double Ra;      // ratio del/d

0829     static const double Cpn;      // in V

0830     static const double Vplus;      // in V

0831     static const double Vmins;      // in V

0832     static const double Tmax; 
0833     static const double t_sat;
0834 
0835     const int tbl_sz;
0836 
0837 // some constants (initiated from Dev)

0838     int XminSearch;
0839     int XmaxSearch;
0840     int YminSearch;
0841     int YmaxSearch;
0842     double PixSz;
0843 
0844     int Flag;
0845 
0846     static SimX * ptrSimA;  //needed for FCN 

0847 
0848     string dname;
0849     string filename;
0850     string OutFN;
0851 
0852     unsigned short * bufsi;
0853 
0854     TH1D * hsiAmp;
0855     TH1D * hsiAmx;
0856     TH1D * hsiAle;
0857     TH1D * hsiXl;
0858     TH1D * hsiYl;
0859     TH1D * hsiZl;
0860     TH1D * hsiTl;
0861 
0862 public:
0863     SimX (  string dir_name );
0864     void Simulator( void );
0865     void PlotSim( void );
0866     //int create_Table();

0867     int OutSimF( const char * fOutName  );
0868     ~SimX(){ delete []bufsi; bufsi = 0; };
0869 };
0870 
0871 // initialization of static members

0872 const float SimX::left_margin =  (float)0.06;
0873 const float SimX::right_margin = (float)0.006;
0874 const float SimX::top_margin =   (float)0.01;
0875 const float SimX::bot_margin =   (float)0.04;
0876 
0877 const int    SimX::bend = 1;
0878 const int    SimX::Nray = 8000; //number of X-rays

0879 const double SimX::SiDi = 0.355;   //diffusion sigma in pixels

0880 const double SimX::Sigma0 = 0.07;  //initial size in pixels

0881 //const double SimX::SiPzS2 = SimX::SiDi*sqrt(2.);    // SiDi*sqrt(2.)

0882 const double SimX::Vop=81.;      // in V

0883 const double SimX::Vde=Co::Vde;      // 10.648 in V

0884 //const double SimX::Na=1.54;      // acceptor concentration

0885 //const double SimX::Nd=1.6e+3;     // donor concentration

0886 //const double SimX::Ra=1.e-4;      // (del/d)**2

0887 const double SimX::Cpn=Co::Cpn;      
0888 const double SimX::Vplus=SimX::Vop +SimX::Vde*(2.*Co::Cpn -1.); // in V

0889 const double SimX::Vmins=SimX::Vop -SimX::Vde;      // in V

0890 const double SimX::Tmax=log(SimX::Vplus/SimX::Vmins);      
0891 const double SimX::t_sat=2.*Co::mobil_e*SimX::Vde/(Co::v_sat_e*Co::Depth);      
0892 
0893 //pixels:   1.0  0.593 0.555 0.519 0.481 0.444 0.407 0.370 0.333 0.296 0.259 0.222

0894 //microns: 13.5  8.0   7.5   7.0   6.5   6.0   5.5   5.0   4.5   4.0   3.5   3.0

0895 
0896 
0897     SimX * SimX::ptrSimA = 0; //needed for FCN 

0898 
0899 SimX::SimX( string dir_name ):
0900     Nmatr(2* bend +1),        
0901     Msz(Nmatr*Nmatr),
0902     Centr(Msz/2),
0903     tbl_sz(Nmatr+4),
0904     dname(dir_name),
0905     bufsi(0)
0906 {
0907     ptrSimA = this;
0908 
0909     cout<<" Sim Param Sigma Diffusion: "<<SiDi<<'\n';
0910     cout<<" Sim Param Gain:            "<<Co::Gain<<'\n';
0911     cout<<" Sim Param Ne Ka:           "<<Co::NeKa<<'\n';
0912     cout<<" Sim Param Ne Kb:           "<<Co::NeKb<<'\n';
0913     cout<<" Sim Cluster matrix bend:   "<<bend <<'\n';
0914     cout<<" Sim Cluster matrix Nmatr:  "<<Nmatr <<'\n';
0915     cout<<" Sim Vop:                   "<<Vop <<'\n';     
0916     cout<<" Sim Vde:                   "<<Vde <<'\n';     
0917     //cout<<" Sim Na:                    "<<Co::Na <<'\n';

0918     //cout<<" Sim Nd:                    "<<Co::Nd <<'\n';

0919     cout<<" Sim DelD:                  "<<Co::DelD <<'\n';
0920     cout<<" Sim Cpn:                   "<<Co::Cpn<<'\n';      
0921     cout<<" Sim Vplus:                 "<<Vplus  <<'\n';
0922     cout<<" Sim Vmins:                 "<<Vmins  <<'\n';
0923     cout<<" Sim Tmax:                  "<<Tmax   <<'\n';     
0924     cout<<" Sim t_sat:                 "<<t_sat  <<'\n';      
0925 //

0926     hsiAmp = new TH1D( "siAmp", "siAmp", 1000, 0., 1000.);
0927     hsiAmp->SetStats(1);
0928     hsiAmx = new TH1D( "siAmx", "siAmx", 1000, 0., 1000.);
0929     hsiAmx->SetStats(1);
0930     hsiAle = new TH1D( "siAle", "siAle", 1000, 0., 1.);
0931     hsiAle->SetStats(1);
0932     hsiXl = new TH1D( "siXl", "siXl", 200, -0.5, 1.5);
0933     hsiXl->SetStats(1);
0934     hsiYl = new TH1D( "siYl", "siYl", 200, -0.5, 1.5);
0935     hsiYl->SetStats(1);
0936     hsiZl = new TH1D( "siZl", "siZl", 120, 0., 1.20);
0937     hsiZl->SetStats(1);
0938     hsiTl = new TH1D( "siTl", "siTl", 120, 0., 1.20);
0939     hsiTl->SetStats(1);
0940 
0941 //

0942         matrix = new double[Msz];
0943 
0944 // file list:  ********************************************************

0945     FileList FL(dname);
0946     if (FL.Nfile <= 0) {Flag= -10; return;}
0947     
0948 
0949 // bias file loop: ****************************************************

0950     FL.FName_Iter=FL.FName.begin();
0951     cout << "Sim starts with bias file:" << *FL.FName_Iter << endl;
0952     for (FL.FName_Iter=FL.FName.begin(); FL.FName_Iter != FL.FName.end(); FL.FName_Iter++ ) 
0953     {
0954         filename = *FL.FName_Iter;  // dname + "/" +

0955         cout << "SimX File: " << *FL.FName_Iter << endl;
0956         if ( Open(filename.c_str()) ) continue;
0957 
0958         while( !iEOF ){              
0959             if ( hdu > MaxP )   break;
0960             if ( Read() )    continue;
0961             if ( naxis != 2) continue; 
0962 
0963 // get device description ************************

0964 
0965             DataStr * Dev = DataStr::Instance(nx,ny);
0966             XminSearch = Dev->minX() + 8;
0967             XmaxSearch = Dev->maxX() - 8;
0968             YminSearch = Dev->minY() + 8;
0969             YmaxSearch = Dev->maxY() - 8;
0970             PixSz = Dev->PixSz();
0971             cout<<" Sim Param Pixel Size: "<<PixSz<<'\n';
0972             cout<<" Sim Param SigmDiff:   "<<SiDi*PixSz<<'\n';
0973 
0974             if (bufsi == 0) bufsi = new unsigned short[npixels];
0975             for (unsigned long i=0; i<npixels; i++){
0976                             //bufsi[i] = buffer[i];

0977                             bufsi[i] = 5700. + gRandom->Gaus(0.,5.137);
0978             }
0979 
0980             Simulator();
0981 
0982             //output fits file 

0983             //char tmpstr[200];

0984             //sprintf( tmpstr, "SimGV_%i_", (int)(SiDi*PixSz*10));

0985             //OutFN =  tmpstr + *FL.FName_Iter;

0986             //printf(" Output file: %s \n", OutFN.c_str());

0987 
0988 
0989             //OutSimF( OutFN.c_str());

0990 
0991         } //end while

0992 
0993     } //end file loop

0994 
0995 }; //end SimX constuctor 

0996 
0997 //*********************************************************************

0998 void SimX::PlotSim( void )
0999 {
1000 // X-ray simulation plots ******************************************

1001      TCanvas    *   SimP = new TCanvas( "SimP", "SimP", 55, 15, 900, 800);
1002      SimP->SetBorderMode  (0);      //to remove border

1003      SimP->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

1004      SimP->SetRightMargin (right_margin);   //to move plot to the right 0.05

1005      SimP->SetTopMargin   (top_margin);   //to use more space 0.07

1006      SimP->SetBottomMargin(bot_margin);   //default

1007      SimP->SetFrameFillColor(0);
1008      SimP->Divide(2,4);
1009      SimP->Update();
1010 
1011      SimP->SetFillStyle(4100);
1012      SimP->SetFillColor(0);
1013 
1014     int Entry;
1015 
1016     Entry = (int)hsiAmp->GetEntries();
1017     if ( Entry > 1 ) {
1018         SimP->cd(1);
1019         hsiAmp->Draw();
1020     }
1021 
1022     Entry = (int)hsiAmx->GetEntries();
1023     if ( Entry > 1 ) {
1024         SimP->cd(2);
1025         hsiAmx->Draw();
1026     }
1027 
1028     Entry = (int)hsiXl->GetEntries();
1029     if ( Entry > 1 ) {
1030         SimP->cd(3);
1031         hsiXl->Draw();
1032     }
1033 
1034     Entry = (int)hsiYl->GetEntries();
1035     if ( Entry > 1 ) {
1036         SimP->cd(4);
1037         hsiYl->Draw();
1038     }
1039 
1040     Entry = (int)hsiZl->GetEntries();
1041     if ( Entry > 1 ) {
1042         SimP->cd(5);
1043         hsiZl->Draw();
1044     }
1045 
1046     Entry = (int)hsiTl->GetEntries();
1047     if ( Entry > 1 ) {
1048         SimP->cd(6);
1049         hsiTl->Draw();
1050     }
1051 
1052     Entry = (int)hsiAle->GetEntries();
1053     if ( Entry > 1 ) {
1054         SimP->cd(7);
1055         hsiAle->Draw();
1056     }
1057 
1058      SimP->Update();
1059      SimP->Print("SimP.pdf");
1060 
1061     return;
1062 } //end PlotSim function

1063     
1064 
1065 //int SimX::create_Table()

1066 //{

1067 //   

1068 //   const char *ttype1[tbl_sz] = {"id", "Amp", "Xd", "Yd","Matrix"};

1069 //   const char *tform1[tbl_sz] = {"1I", "1E", "1E", "1E", "NmatrE"};  //NmatrE

1070 //   const char *tunit1[tbl_sz] = {"", "", "","",""};

1071 // 

1072 //     // int fits_create_tbl(fitsfile *fptr, int tbltype, long nrows, int tfields,

1073 //    //char *ttype[],char *tform[], char *tunit[], char *extname, int *status)

1074 //

1075 //

1076 //   /* Add the first binary table extension. */

1077 //   fits_create_tbl(fptr, BINARY_TBL, 1L, tbl_sz, (char **)ttype1, (char **)tform1,

1078 //       (char **)tunit1, NULL, &status);

1079 //

1080 //  fits_write_key (fptr, TSTRING, "TDISP1", "I8", 

1081 //                "recommended display format", &status);

1082 //  fits_write_key (fptr, TSTRING, "TDISP2", "F8.3", 

1083 //                "recommended display format", &status);

1084 //  fits_write_key (fptr, TSTRING, "TDISP3", "F8.3", 

1085 //                "recommended display format", &status);

1086 //  fits_write_key (fptr, TSTRING, "TDISP4", "F8.3", 

1087 //                "recommended display format", &status);

1088 //  fits_write_key (fptr, TSTRING, "TDISP5", "F8.3", 

1089 //                "recommended display format", &status);

1090 //

1091 ///* stuff data one row at a time into a binary table in the FITS file */

1092 ///* Count3 and Count4 ignored */

1093 //// int fits_write_col / ffpcl

1094 ////      (fitsfile *fptr, int datatype, int colnum, long firstrow,

1095 ////       long firstelem, long nelements, DTYPE *array, > int *status)

1096 //   long nx =1;

1097 //   for (int row = 1; row <= Nmatr; row++)

1098 //   {

1099 //  fits_write_col (fptr, TINT, 1, row, 1, 1, row, &status);

1100 //  fits_write_col (fptr, TFLOAT, 2, row, 1, 1, Amp, &status);

1101 //  fits_write_col (fptr, TFLOAT, 3, row, 1, 1, Xd, &status);

1102 //  fits_write_col (fptr, TFLOAT, 4, row, 1, 1, Yd, &status);

1103 //  fits_write_col (fptr, TUSHORT, 5, row, nx, Nmatr,matrix, &status);

1104 //  nx += Nmatr;

1105 //   }

1106 //

1107 //}

1108 //

1109 
1110 void SimX::Simulator( void )
1111 {
1112 const double Xrange = XmaxSearch-XminSearch;
1113 const double Yrange = YmaxSearch-YminSearch;
1114 
1115 for (int ray=0; ray<Nray; ray++){
1116 
1117 // X-ray coordinates (in pixels):

1118     double xs = (double)XminSearch + Xrange*gRandom->Rndm();
1119     double ys = (double)YminSearch + Yrange*gRandom->Rndm();
1120     int ixs=(int)xs;
1121     int jys=(int)ys;
1122     //int Jind = jys *nx + ixs;

1123     double lxs = xs - ixs; //local x

1124     double lys = ys - jys; //local y

1125     hsiXl->Fill(lxs);
1126     hsiYl->Fill(lys);
1127         
1128 // cluster amplitude in e-

1129     double tau;
1130     double Amp;
1131     double ab = gRandom->Rndm();
1132     if (ab < 0.12) {Amp = Co::NeKb; tau=Co::Tau_b;}
1133     else           {Amp = Co::NeKa; tau=Co::Tau_a;}
1134     Amp = Co::NeKa; tau=Co::Tau_a; //only Ka

1135 
1136 // z/d - coordinate (depth of conversion), z=0 window

1137     double lz = 2.; 
1138     while (lz > 1.){
1139         lz = gRandom->Exp(tau);
1140     }
1141     hsiZl->Fill(lz);
1142 
1143     double drti = log( Vplus/(Vmins+2.*Vde*lz*Cpn) ) + t_sat*(1.-lz);
1144     drti /= (Tmax+t_sat);
1145     if (drti < 0.) {drti = 0.;}
1146     else           {drti = sqrt (drti);}
1147     hsiTl->Fill(drti);
1148     double sigma_z = SiDi*drti;
1149     sigma_z = sqrt (sigma_z*sigma_z + Sigma0*Sigma0);
1150     SiPzS2 = sigma_z*sqrt(2.);    
1151 
1152     
1153     // Diffuse Cluster

1154 
1155     for (int i=0; i<Msz; i++) {matrix[i]=0.;}  // Cluster Matrix Init

1156 
1157     double Asim=0.;
1158     double dyl;
1159     double dyh;
1160     double sigY;
1161     double dxl;
1162     double dxh;
1163     double sigX;
1164     for(int i = 0; i < Nmatr ; i++){
1165         int ly = i - bend;
1166             dyl = (double)ly - lys;
1167             dyh = dyl + 1.;
1168             dyl /= SiPzS2;
1169             dyh /= SiPzS2;
1170             sigY = TMath::Erf(dyh)-TMath::Erf(dyl);
1171             sigY /= 2.;
1172         //printf("  iy:%i, %i ", i,ly);

1173         //printf("  iy:%i, %i %f %f %f \n", i,ly, dyh, dyl, sigY);

1174         for(int k = 0; k < Nmatr ; k++){
1175             int lx = k - bend;
1176                 dxl = (double)lx - lxs;
1177                 dxh = dxl + 1.;
1178                 dxl /= SiPzS2;
1179                 dxh /= SiPzS2;
1180                 sigX = TMath::Erf(dxh)-TMath::Erf(dxl);
1181                 sigX /= 2.;
1182             double signal = Amp*sigX*sigY;
1183             double sim_sig = signal;
1184             if (signal > 5.) {  //signal Poisson variation 

1185                 double ran_sig = signal * Co::Fano;
1186                 sim_sig = gRandom->PoissonD(ran_sig);
1187                 sim_sig += signal*(1.-Co::Fano);
1188             } //signal Poisson variation 

1189             if ( sim_sig < 0.0) sim_sig=0.;
1190             sim_sig /= Co::Gain;
1191             Asim += sim_sig;
1192             int gx = lx + ixs;
1193             int gy = ly + jys;
1194             int jj = gy *nx + gx;
1195             int idx = i * Nmatr + k;
1196             matrix[idx]=sim_sig;
1197             bufsi[jj] +=sim_sig; 
1198             //printf(" ix=%i %f %f %f %f %f \n ", lx, dxl, dxh, matrix[idx], signal, sigX);

1199             //printf("  %f %d %d ", matrix[idx], buffer[jj], bufsi[jj]);

1200         }
1201             //printf(" \n ");

1202     }
1203     //printf("  Asim =%fe- Xamp=%f \n", Asim, matrix[Msz/2]);

1204     hsiAmp->Fill(Asim);
1205     hsiAmx->Fill(matrix[Msz/2]);
1206     
1207     dyl = (-bend - lys)/SiPzS2;
1208     dyh = (Nmatr - bend -lys)/SiPzS2;
1209     sigY = TMath::Erf(dyh)-TMath::Erf(dyl);
1210     sigY /= 2.;
1211     dxl = (-bend - lxs)/SiPzS2;
1212     dxh = (Nmatr - bend -lys)/SiPzS2;
1213     sigX = TMath::Erf(dyh)-TMath::Erf(dyl);
1214     sigX /= 2.;
1215     double sigLF = (1.-sigX*sigY)*100.; // leaked fraction in %

1216     hsiAle->Fill(sigLF);
1217 
1218 } //end of ray loop

1219 
1220     return;
1221 }  //end SimX::Simulator

1222 //-----------------------------------------------------------------------------

1223 
1224 int SimX::OutSimF( const char * fOutName  )
1225 {
1226 
1227     fitsfile *fSimOut;      /* pointer to the output FITS file */ 
1228     //int status = 0;

1229     status = 0;
1230 
1231     char foutname[200]; // = "Output.fits";      

1232     //strcpy (foutname,  dname.c_str());  

1233     //strcat (foutname, "\\");

1234     strcpy (foutname, fOutName);
1235     //strcat (foutname, ".fits");

1236 
1237     fits_create_file(&fSimOut, foutname, &status); /* create new FITS file */
1238     if ( status ) {printf(" create_file status error: %i \n", status); return -6;}
1239      
1240     //  Copy keywords from existing HDU into empty HDU of fOutName file

1241     fits_copy_header( fptr, fSimOut, &status);
1242     if ( status ) {printf(" copy header error: %i \n", status); return -6;}
1243     // don't copy, just bare header

1244     //const int naxis =2;

1245     //long naxes[naxis];

1246     //naxes[0]=nx;

1247     //naxes[1]=ny;

1248     //npixels = nx*ny;

1249     //fits_create_img( fSimOut, USHORT_IMG, naxis, naxes, &status);

1250     //if ( status ) {printf(" write_img status error: %i \n", status); return -6;}

1251 
1252     /* write the array of unsigned short to the FITS file */
1253     fits_write_img( fSimOut, TUSHORT, fpixel, npixels, bufsi, &status);
1254     if ( status ) {printf(" write_img status error: %i \n", status); return -6;}
1255      
1256     fits_close_file(fSimOut, &status);  
1257     if ( status ) {printf(" close_file status error: %i \n", status); return -6;}
1258 
1259 
1260     return 0;
1261 } //end SimX::OutSimF

1262 //*********************************************************************

1263 
1264 //---------------------------------------------------------------------

1265 // Fe55 class ---------------------------------------------------------

1266 class Fe55: public FileF {
1267 public:
1268 // canvas constants

1269     static const float left_margin;
1270     static const float right_margin;
1271     static const float top_margin;
1272     static const float bot_margin;
1273 
1274 // Fe55 lines and related constants

1275     static const int Npar;  //fit parameters

1276 
1277 // some "constants" (initiated from Dev)

1278     int XminSearch;
1279     int XmaxSearch;
1280     int YminSearch;
1281     int YmaxSearch;
1282     double PixSz;
1283     double AminSearch[MaxP];
1284     double ANoise[MaxP];  
1285     const double HighCut;
1286 
1287     int Nchan;
1288     int ch_idx;
1289 
1290     int Flag;
1291 
1292     string dname;
1293     string filename;
1294     const char * OutExt;
1295     FILE * pFlist;
1296     FILE * pFreg;
1297     FILE * pFcat;
1298 
1299 
1300     const vector<double> * avbuf;
1301     //vector <double> bufzs;

1302     double * bufzs;
1303     double * bz_save;
1304 
1305 // FFT 

1306     const int doFFT;
1307     FFT * Col;
1308     FFT * Row;
1309 
1310 //histos

1311     TH1D * hblc;
1312     enum { Nhs = 16 };
1313     TH1D * hs[MaxP][Nhs];
1314     TH1D * hfe[MaxP];
1315     TH1D * hmap[MaxP];
1316     TH1D * hpile[MaxP];
1317     TH1D * hclean[MaxP];
1318     TH1D * hYpro[MaxP];
1319     TH1D * hXpro[MaxP];
1320     TH1D * hXtst[MaxP];
1321     TH2D * hYX[MaxP];
1322     TH1F * hNp[MaxP];
1323     TH1F * hNph[MaxP];
1324     TH1F * horm[MaxP];
1325     TH1F * hra[MaxP];
1326     TH1F * hspr[MaxP];
1327 
1328     TH1D * hXf[MaxP][7];
1329     TH1D * hSig;
1330 
1331     enum { Nham = Co::Nsumh };  // 10 };

1332     TH1D * ham[MaxP][Nham];
1333     TH1D * hXstat[MaxP];
1334 
1335     enum { Ncte = 33 };
1336     TH1F * xcte[MaxP][Ncte];
1337     TH1F * ycte[MaxP][Ncte];
1338     enum { Zone = 2 };
1339     int NgrX;
1340     int NgrY;
1341 
1342     enum { NZ   = 4 };
1343     int Zind;
1344     double NKhit[MaxP][NZ];
1345     double AvHit[MaxP][NZ][Co::NXpix][Co::NYpix];
1346     double A2Hit[MaxP][NZ][Co::NXpix][Co::NYpix];
1347     TH2D * AvrHit[MaxP];  //[Zone*Zone] 

1348     TH1D * hpixA[MaxP][Co::NXpix][Co::NYpix];
1349 
1350     TH2F * ClustQ[MaxP];
1351     TH2F * Xcte[MaxP];
1352     TH2F * Ycte[MaxP];
1353 
1354     TH1D * hitSumA[MaxP];  //test clustering

1355 public:
1356     Fe55 (  string dir_name, 
1357             const char * outName, 
1358             const double * Noise, 
1359             const vector<double> * b_avbuf,
1360             const int dofft );
1361     void BaLiC ( DataStr * Dev);
1362     void BaLiS ( DataStr * Dev);
1363     void PlotBase( void );
1364     void Catalogs( void );
1365     static double  GGF ( double *v, double *par );  //fiting function

1366     static double  Gauss ( double *v, double *par );  //fiting function

1367     void FitG(TH1D * hm, double * Amp, double * Shift, double * Noise);
1368     void PlotXfit( void );
1369     void PlotSpectra( void );
1370     void PlotXlines( void );
1371     void PlotCTE( void );
1372     void PlotAvHit(void);
1373     void PlotProfiles(void);
1374     void Plot_FFT();
1375     void PlotHits(void);
1376     //int OpenOutF ( char * fOutName = "Out_bufzs" );

1377     //int OutFitsF ( void );

1378     ~Fe55();
1379 };
1380 
1381 // initialization of static members

1382 const float Fe55::left_margin =  (float)0.06;
1383 const float Fe55::right_margin = (float)0.006;
1384 const float Fe55::top_margin =   (float)0.01;
1385 const float Fe55::bot_margin =   (float)0.04;
1386 
1387 const int    Fe55::Npar = 4; 
1388 
1389 Fe55::~Fe55(){ 
1390     //delete [] bufzs; bufzs = 0;

1391     //if (doFFT) {

1392     //  delete    Col; Col = 0;

1393     //  delete    Row; Row = 0;

1394     //}

1395     //Col = 0;

1396     //Row = 0;

1397 }
1398 
1399 void Fe55::Plot_FFT()
1400 {
1401      //const float left_margin = (float)0.04;

1402      //const float right_margin = (float)0.001;

1403      //const float top_margin = (float)0.01;

1404      //const float bot_margin = (float)0.04;

1405 
1406     TCanvas *   Tr[MaxP];
1407 for (int u=0; u<2; u++){  //Nchan

1408     //if ( avbuf[u].size() < npixels) continue;

1409     char tiname[50];
1410     char PDFname[50];
1411     sprintf(tiname, "FeFFT %i", u);
1412     sprintf(PDFname, "Fe55FFT_%i.pdf", u);
1413     if ( u > MaxP )   break;
1414      Tr[u] = new TCanvas( tiname, tiname, 200+4*u, 10+2*u, 800, 600);
1415      Tr[u]->SetBorderMode  (1);         //to remove border

1416      Tr[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

1417      Tr[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

1418      Tr[u]->SetTopMargin   (top_margin);   //to use more space 0.07

1419      Tr[u]->SetBottomMargin(bot_margin);   //default

1420      Tr[u]->SetFrameFillColor(0);
1421      Tr[u]->Divide(1,2); 
1422 
1423 /***************** Plot Power Spectrum for Rows *************/
1424      printf(" Start draw Power Spectrum for Rows\n\n");
1425      Tr[u]->cd(1);
1426      gPad->SetLogy();
1427      gPad->SetLogx();
1428 
1429      do {
1430      int Npsd = Row->PSDav[u].size();
1431      int Nfrq = Row->Freq.size();
1432      printf(" RowPSD points =%i RowFreq size=%i *** \n", Npsd, Nfrq);
1433      if ( Npsd <= 3 ) continue;
1434      int nn = Npsd -3;
1435      if ( Nfrq < nn ) continue;
1436      TGraph * std6 = new TGraph( nn, &Row->Freq[0], &Row->PSDav[u][0]);
1437      std6->SetTitle("Rows");
1438      std6->GetYaxis()->SetTitle("PSD ");
1439      std6->GetXaxis()->SetTitle("k/N, 1/pixel");
1440      std6->SetMarkerColor(4);
1441      std6->SetMarkerStyle(21);
1442      std6->Draw("AP");
1443          cout << "i" << " - " << "Freq" << " - " << " RowPSD" << endl;
1444      for (int i=0; i<Npsd; i++){
1445          cout << i << ". - " << Row->Freq[i] << " - " << Row->PSDav[u][i] << endl;
1446      }
1447      } while(0);
1448 
1449 /*********************Plot Power Spectrum for Columns*********/
1450      Tr[u]->cd(2);
1451      gPad->SetLogy();
1452      gPad->SetLogx();
1453      do {
1454      int Npsd = Col->PSDav[u].size();
1455      int Nfrq = Col->Freq.size();
1456      printf(" ColPSD points =%i ColFreq size=%i *** \n", Npsd, Nfrq);
1457      if ( Npsd <= 3 ) continue;
1458      int nn = Npsd -3;
1459      if ( Nfrq < nn ) continue;
1460      TGraph * std7 = new TGraph( nn, &Col->Freq[0], &Col->PSDav[u][0]);
1461      std7->SetTitle("Columns");
1462      std7->GetYaxis()->SetTitle("PSD ");
1463      std7->GetXaxis()->SetTitle("k/N, 1/pixel");
1464      std7->SetMarkerColor(6);
1465      std7->SetMarkerStyle(21);
1466      std7->Draw("AP");
1467      } while(0);
1468 
1469     Tr[u]->Update();
1470     Tr[u]->Print(PDFname);
1471 }
1472 
1473 /***************** Plot subtraction histos *************/
1474 
1475     TCanvas *   Tb = new TCanvas( "subtract", "subtract", 250, 20, 800, 600);
1476      Tb->SetBorderMode  (1);            //to remove border

1477      Tb->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

1478      Tb->SetRightMargin (right_margin); //to move plot to the right 0.05

1479      Tb->SetTopMargin   (top_margin);   //to use more space 0.07

1480      Tb->SetBottomMargin(bot_margin);   //default

1481      Tb->SetFrameFillColor(0);
1482      Tb->Divide(2,2); 
1483 
1484     int Entry = (int)Row->hbuf[0]->GetEntries();
1485     if ( Entry >1 ) {
1486         Tb->cd(1);
1487         gPad->SetLogy();
1488         gStyle->SetOptFit(1);
1489         Row->hbuf[0]->Draw();
1490         Row->hbuf[0]->Fit("gaus");
1491 
1492     }
1493     
1494     Entry = (int)Row->hbuf[1]->GetEntries();
1495     if ( Entry >1 ) {
1496         Tb->cd(2);
1497         gPad->SetLogy();
1498         gStyle->SetOptFit(1);
1499         Row->hbuf[1]->Draw();
1500         Row->hbuf[1]->Fit("gaus");
1501     }
1502 
1503     Entry = (int)Row->hbuf[2]->GetEntries();
1504     if ( Entry >1 ) {
1505         Tb->cd(3);
1506         gPad->SetLogy();
1507         Row->hbuf[2]->Draw();
1508     }
1509 
1510     Entry = (int)Row->hbuf[3]->GetEntries();
1511     if ( Entry >1 ) {
1512         Tb->cd(4);
1513         gPad->SetLogy();
1514         Row->hbuf[3]->Draw();
1515     }
1516 
1517      return;
1518 } // end Fe55::Plot_FFT

1519 
1520 
1521 void Fe55::BaLiC ( DataStr * Dev )
1522 {
1523     //  if (avbuf == 0){avbuf = new double[npixels]; memset(avbuf,0,npixels);} 

1524     printf( "\n");
1525     printf( "******* BaLiC  is invoked for HDU=%i channel=%i ******\n",hdu, ch_idx);
1526     double NoiseCut = 5.*ANoise[ch_idx];
1527     double act = 0;
1528     double b_shift = 0;
1529     double b_shift1 = 0;
1530     double b_rms = 0.;
1531     double peak = 0.;
1532     hblc->Reset();
1533     for (int iy=Dev->minY(); iy<Dev->maxY(); iy++) {
1534         for (int ix=Dev->minX(); ix<Dev->maxX(); ix++) {
1535           int j = iy*nx + ix;
1536           bufzs[j] = (double)buffer[j] - avbuf[ch_idx][j];
1537           if ( bufzs[j] < NoiseCut ) {  //TMath::Abs(bufzs[j])

1538               hblc->Fill( bufzs[j] );
1539               b_shift += bufzs[j];
1540               act+=1.;
1541           }
1542         }
1543     }
1544     if (act > 0.2*npixels ) {
1545         b_shift /= act;
1546         FitG(hblc, &peak, &b_shift1, &b_rms);
1547         printf ("GausAv=%f rms=%f \n", b_shift1, b_rms);
1548         b_shift = b_shift1;
1549     }
1550     else b_shift=0.;
1551 
1552     printf(" shift= %f act=%f \n",  b_shift, act);
1553 
1554     for (int iy=0; iy<ny; iy++) {
1555         for (int ix=0; ix<nx; ix++) {
1556           int j = iy*nx + ix;
1557           bufzs[j] -= b_shift;
1558           if ( Dev->IsOver(ix, iy) ) continue;
1559           hfe[ch_idx]->Fill( bufzs[j] );
1560         }
1561     }
1562 } // end Base Line Correction 

1563 
1564 void Fe55::BaLiS ( DataStr * Dev )
1565 {
1566     printf("\n");
1567     printf("*********# BaLiS  is invoked for HDU=%i ch_idx=%i #********\n", hdu, ch_idx);
1568     printf("*********# BaLiS: Dev->: Ominx=%i Omax=%i minX=%i maxX=%i NoverX=%i \n",
1569            Dev->OmiX(), Dev->OmaX(), Dev->minX(), Dev->maxX(), Dev->NoverX() );
1570 
1571     double b_shift = 0.;
1572     double b_rms   = 0.;
1573     double peak = 0.;
1574 
1575 // Fill input histo 

1576     for (int iy=0; iy<ny; iy++) {
1577         for (int ix=0; ix<nx; ix++) {
1578             long j = iy*nx + ix;
1579             hs[ch_idx][0]->Fill( bufzs[j] );
1580         }
1581     }
1582 // Overscan subtraction 

1583     hs[ch_idx][2]->Reset();
1584     for (int iy=0; iy<ny; iy++) {
1585         double a_over = 0.;
1586         for (int ix=Dev->OmiX(); ix<Dev->OmaX(); ix++) {
1587             long j = iy*nx + ix;
1588             a_over += bufzs[j]/Dev->NoverX();
1589         }
1590         for (int ix=0; ix<nx; ix++) {
1591             long j = iy*nx + ix;
1592                 bufzs[j] = bufzs[j] - a_over;
1593             if ( Dev->IsOverX(ix) ) hs[ch_idx][1]->Fill( bufzs[j] );
1594             else {
1595                 hs[ch_idx][2]->Fill( bufzs[j] );
1596             }
1597         }
1598     }
1599     ////b_shift = hs[ch_idx][1]->GetMean();

1600     ////b_rms   = hs[ch_idx][1]->GetRMS();

1601     ////printf (" OS: shift=%f rms=%f \n", b_shift, b_rms);

1602     FitG(hs[ch_idx][2], &peak, &b_shift, &b_rms);
1603     printf (" OS: GausAv=%f rms=%f \n", b_shift, b_rms);
1604     
1605    // goto skipIter;

1606     
1607 // Iterative Part

1608     for ( int it=0; it<4; it++){
1609         hs[ch_idx][3+2*it]->Reset();
1610         hs[ch_idx][4+2*it]->Reset();
1611 
1612   // active row pixel average subtraction 

1613     double RowCut = 4.*b_rms;
1614     //if (it == 0) RowCut = 60000.;  //10.*b_rms;

1615     for (int iy=0; iy<ny; iy++) {
1616         double a_row = 0.;
1617         double n_row = 0.;
1618         // row loops, active pixels only

1619         for (int ix=Dev->minX(); ix<Dev->maxX(); ix++) {
1620             int j = iy*nx + ix;
1621             bufzs[j] -= b_shift;
1622             if ( !it || (bufzs[j] < RowCut) ) {a_row += bufzs[j]; n_row +=1.;}
1623         }
1624         if (n_row > 0.5*nx ) a_row /= n_row;
1625         else a_row = 0.;
1626 
1627         for (int ix=Dev->minX(); ix<Dev->maxX(); ix++) {
1628             int j = iy*nx + ix;
1629             bufzs[j] -= a_row;
1630             hs[ch_idx][4+2*it]->Fill( bufzs[j]);
1631         }
1632     }
1633     //b_shift = hs[ch_idx][4+2*it]->GetMean();  

1634     //b_rms   = hs[ch_idx][4+2*it]->GetRMS();

1635     //printf (" Column: shift=%f rms=%f \n", b_shift, b_rms);

1636     FitG(hs[ch_idx][4+2*it], &peak, &b_shift, &b_rms);
1637     printf ("Fit Noise Itr%d : Row: shift=%f rms=%f \n",it, b_shift,b_rms);
1638 
1639 // all column pixel average subtraction 

1640     double ColCut = 4.*b_rms;
1641     //if (it == 0) ColCut = 60000.;  //10.*b_rms;

1642     for (int ix=0; ix<nx; ix++) {
1643         double a_col = 0.;
1644         double n_col = 0.;
1645        // column loops, without column overscan

1646         for (int iy=Dev->minY(); iy<Dev->maxY(); iy++) {
1647             long j = iy*nx + ix;
1648             bufzs[j] -= b_shift;
1649             if ( !it || (bufzs[j] < ColCut) ) {a_col += bufzs[j]; n_col +=1.;}
1650         }
1651         if (n_col > 0.5*ny ) a_col /= n_col;
1652         else a_col = 0.;
1653 
1654         for (int iy=Dev->minY(); iy<Dev->maxY(); iy++) {
1655             long j = iy*nx + ix;
1656             bufzs[j] -= a_col;
1657             //if ( Dev->IsOver(ix,iy) ) continue;  //for SIX

1658             hs[ch_idx][3+2*it]->Fill( bufzs[j]);
1659         }
1660         //end column loops  

1661     }
1662 
1663     FitG(hs[ch_idx][3+2*it], &peak, &b_shift, &b_rms);
1664     printf ("Fit Noise Itr%d : Col: shift=%f rms=%f \n",it, b_shift,b_rms);
1665 
1666     } //end of "itteration" loop

1667 
1668 //skipIter:

1669 //    cout <<" Iterations are skipped "<< endl;

1670     cout <<" BaLiS noise for hdu="<<hdu<< " : " <<b_rms << endl;
1671     ANoise[ch_idx] = b_rms; 
1672         for (int iy=0; iy<ny; iy++) {
1673             for (int ix=0; ix<nx; ix++) {
1674                 long j = iy*nx + ix;
1675                 if ( Dev->IsOver(ix, iy) ) continue;
1676                 double amp = bufzs[j] - b_shift;
1677                 bufzs[j] = amp;
1678                 hfe[ch_idx]->Fill( bufzs[j]);
1679                 hs[ch_idx][13]->Fill( bufzs[j]);
1680             }
1681         }
1682 
1683     // Prepare to record in the file

1684     //for (int ix=0; ix<nx; ix++) {

1685     //  for (int iy=0; iy<ny; iy++) { 

1686     //      long j = iy*nx + ix;

1687     //      bufzs[j] += 1000.;

1688     //      if ( bufzs[j] < 0. ) bufzs[j] = 0.;

1689     //      if ( bufzs[j] > 30000.) bufzs[j] = 30000.;

1690     //      //if ( bufzs[j] < 0.5*b_rms ) bufzs[j] = 0.;

1691     //      //if ( bufzs[j] > 2000. ) bufzs[j] = 0.;

1692     //  }

1693     //}

1694 
1695 } // end Base Line Subtraction

1696 
1697 void Fe55::PlotBase( void )
1698 {
1699     printf("AIK in PlotBase\n");
1700     TCanvas *   BaL[MaxP]={0};
1701     rPad __f;
1702 int Entry;
1703 for (int u=0; u<Nchan; u++){
1704         //if ( avbuf[u].size() < npixels) continue;

1705         char tiname[50];
1706         sprintf(tiname, "BaLiSu %i", u);
1707         if ( u > MaxP )   break;
1708 
1709 // set bias canvas 1 ****************************************************

1710     BaL[u] = new TCanvas( tiname, tiname, 100+5*u, 100+5*u, 900, 800);
1711     //fprintf(stderr," PlotBase -- %d -Canvas ID --------%d ------- \n",u,BaL[u]->GetCanvasID());

1712     BaL[u]->SetFrameFillColor(0);
1713     BaL[u]->SetBorderMode  (0);         //to remove border

1714     BaL[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

1715     BaL[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

1716     BaL[u]->SetTopMargin   (top_margin);   //to use more space 0.07

1717     BaL[u]->SetBottomMargin(bot_margin);   //default

1718     BaL[u]->Divide(2,6);
1719 
1720     BaL[u]->SetFillStyle(4100);
1721     BaL[u]->SetFillColor(0);
1722     
1723     for (int i = 0; i<3; i++){
1724         if ( hs[u][i] == 0) continue;
1725         Entry = (int)hs[u][i]->GetEntries();
1726         if ( Entry < 1 ) continue;
1727         BaL[u]->cd(i+1);
1728         gPad->SetLogy(); 
1729         hs[u][i]->Draw();
1730     }
1731     
1732     Entry = (int)hs[u][13]->GetEntries();
1733     if ( Entry >1 ) {
1734         BaL[u]->cd(4);
1735         gPad->SetLogy();
1736         hs[u][13]->Draw();
1737     }
1738 
1739     for (int i = 3; i<11; i++){
1740         if ( hs[u][i] == 0) continue;
1741         Entry = (int)hs[u][i]->GetEntries();
1742         if ( Entry < 1 ) continue;
1743         BaL[u]->cd(i+2);
1744         gPad->SetLogy();
1745         hs[u][i]->Draw();
1746     }
1747     BaL[u]->Update();
1748     char uname[40];
1749     sprintf(uname,"BaLiSu_%i.pdf", u);
1750     //BaL[u]->Print(uname);

1751 
1752 } // end "section" canvas loop

1753 
1754     return;
1755 } //end PlotBase    

1756 
1757 void Fe55::Catalogs( void )
1758 {
1759     // create x-ray list file

1760     string listName = filename;
1761     basic_string <char>::size_type indexL;
1762     indexL = listName.find ( ".fits");
1763     listName = listName.replace ( indexL , 5 , "-xray.txt" );
1764     printf("Output list File: %s \n", listName.c_str());
1765     if (access(listName.c_str(), F_OK) != -1) {
1766         remove(listName.c_str());
1767     }
1768     pFlist = fopen(listName.c_str(),"a+t");
1769     if(pFlist == 0){
1770         printf("File %s can't be open \n", listName.c_str());
1771         Flag = -1;
1772         return;
1773     }
1774     fprintf(pFlist, "No  x   y   x-fit  y-fit  y-fcorr sum-reg XIP \n");
1775 
1776 // create region file

1777     string regName = filename;
1778     basic_string <char>::size_type indexF;
1779     indexF = regName.find ( ".fits");
1780     regName = regName.replace ( indexF , 5 , ".reg" );
1781     printf("Output reg File: %s \n", regName.c_str());
1782     if (access(regName.c_str(), F_OK) != -1) {
1783         remove(regName.c_str());
1784     }
1785     pFreg = fopen(regName.c_str(),"a+t");
1786     if(pFreg == 0){
1787         printf("File %s can't be open \n", regName.c_str());
1788         Flag = -1;
1789         return;
1790     }
1791     
1792 // create catalog file

1793     string catName = filename;
1794     basic_string <char>::size_type indexC;
1795     indexC =  catName.find ( ".fits");
1796     catName = catName.replace ( indexF , 5 , "_catalog.xml" );
1797     printf("Output cat File: %s \n", catName.c_str());
1798     if (access(catName.c_str(), F_OK) != -1) {
1799         remove(catName.c_str());
1800     }
1801     pFcat = fopen(catName.c_str(),"a+t");
1802     if(pFcat == 0){
1803         printf("File %s can't be open \n", catName.c_str());
1804         Flag = -1;
1805         return;
1806     }
1807     fprintf(pFcat, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<VOTABLE version=\"1.1\">\n<RESOURCE>\n");
1808     fprintf(pFcat, "<TABLE>\n");
1809     fprintf(pFcat, "<DESCRIPTION># X-ray catalog\n</DESCRIPTION>\n");
1810     fprintf(pFcat, "<FIELD name=\"PhysicalX\" >\n<DESCRIPTION>Physical X</DESCRIPTION>\n</FIELD>\n");
1811     fprintf(pFcat, "<FIELD name=\"PhysicalY\" >\n<DESCRIPTION>Physical Y</DESCRIPTION>\n</FIELD>\n");
1812     fprintf(pFcat, "<FIELD name=\"ImageX\" >\n<DESCRIPTION>Image X</DESCRIPTION>\n</FIELD>\n");
1813     fprintf(pFcat, "<FIELD name=\"ImageY\" >\n<DESCRIPTION>Image y coordinates</DESCRIPTION>\n</FIELD>\n");
1814     fprintf(pFcat, "<FIELD name=\"Amp Sum\" >\n<DESCRIPTION>Amplitude Sum</DESCRIPTION>\n</FIELD>\n");
1815     fprintf(pFcat, "<FIELD name=\"ChannelNumber\" >\n<DESCRIPTION>CCD Segment</DESCRIPTION>\n</FIELD>\n");
1816     fprintf(pFcat, "<FIELD name=\"Amp0,0\" >\n<DESCRIPTION>Amplitude 0,0</DESCRIPTION>\n</FIELD>\n");
1817     fprintf(pFcat, "<FIELD name=\"Amp1,0\" >\n<DESCRIPTION>Amplitude 1,0</DESCRIPTION>\n</FIELD>\n");
1818     fprintf(pFcat, "<FIELD name=\"Amp2,0\" >\n<DESCRIPTION>Amplitude 2,0</DESCRIPTION>\n</FIELD>\n");
1819     fprintf(pFcat, "<FIELD name=\"Amp0,1\" >\n<DESCRIPTION>Amplitude 0,1</DESCRIPTION>\n</FIELD>\n");
1820     fprintf(pFcat, "<FIELD name=\"Amp1,1\" >\n<DESCRIPTION>Amplitude 1,1</DESCRIPTION>\n</FIELD>\n");
1821     fprintf(pFcat, "<FIELD name=\"Amp2,1\" >\n<DESCRIPTION>Amplitude 2,1</DESCRIPTION>\n</FIELD>\n");
1822     fprintf(pFcat, "<FIELD name=\"Amp0,2\" >\n<DESCRIPTION>Amplitude 0,2</DESCRIPTION>\n</FIELD>\n");
1823     fprintf(pFcat, "<FIELD name=\"Amp1,2\" >\n<DESCRIPTION>Amplitude 1,2</DESCRIPTION>\n</FIELD>\n");
1824     fprintf(pFcat, "<FIELD name=\"Amp2,2\" >\n<DESCRIPTION>Amplitude 2,2</DESCRIPTION>\n</FIELD>\n");
1825     fprintf(pFcat, "<DATA>\n<TABLEDATA>\n");
1826     
1827     return;
1828 }  //end Catalogs

1829 
1830 Fe55::Fe55 (    string dir_name,
1831         const char * outName,
1832         const double * Noise,
1833         const vector<double> * b_avbuf,
1834         const int dofft ):
1835     HighCut(1200.),
1836     Nchan(0),
1837     Flag(-1),
1838     dname(dir_name),
1839     OutExt(outName),
1840     avbuf(b_avbuf),
1841     bufzs(0),
1842     doFFT(dofft),
1843     Col(0),
1844     Row(0)
1845 {
1846     for (int i=0; i<MaxP; i++) { ANoise[i] =0.;}
1847     if (b_avbuf !=0) {              // for BaLiC and not for BaLiS

1848         //avbuf[0].assign(b_avbuf->begin(),b_avbuf->end()); 

1849         for (int i=0; i<MaxP; i++) {
1850             //avbuf[i].assign(b_avbuf[i].begin(),b_avbuf[i].end()); 

1851             AminSearch[i] = 10.*Noise[i];
1852             ANoise[i] = Noise[i];
1853             printf(" ANoise[%i](noise rms) = %f \n", i, ANoise[i]);
1854         }
1855     }
1856 
1857     for (int i=0; i<MaxP; i++) {
1858         for (int j=0; j<NZ; j++) {
1859             NKhit[i][j]=0.;
1860             for (int ix=0; ix<Co::NXpix; ix++) {
1861                 for (int iy=0; iy<Co::NYpix; iy++) {
1862                             AvHit[i][j][ix][iy] = 0.;
1863                             A2Hit[i][j][ix][iy] = 0.;
1864                 }
1865             }
1866         }
1867     }
1868 // Base Line Subtraction Histograms ***********************************

1869     char title[40];
1870     char hname[50];
1871     sprintf(title, "base line correction");
1872     hblc = new TH1D( title, title, 800, -400., 400.);
1873     hblc->SetStats(1);
1874     for (int u=0; u<MaxP; u++){
1875         for (int i=0; i<Nhs; i++) {hs[u][i] = 0;}
1876         sprintf(title, "hs %i 0", u);
1877         sprintf(hname, "Raw Data");
1878         hs[u][0] = new TH1D( title, hname, 2000, 0., 4000.);
1879         hs[u][0]->GetXaxis()->SetNdivisions(505);
1880         hs[u][0]->SetStats(1);
1881 
1882         sprintf(title, "hs %i 1", u);
1883         sprintf(hname, "Pre+Overscan after overscan subtr");
1884         hs[u][1] = new TH1D( title, hname, 800, -400., 400.);
1885         hs[u][1]->SetStats(1);
1886 
1887         sprintf(title, "hs %i 2", u);
1888         sprintf(hname, "Active after overscan subtr");
1889         hs[u][2] = new TH1D( title, hname, 2000, -400., 1600.);
1890         hs[u][2]->SetStats(1);
1891 
1892         for (int it=0; it<4; it++){
1893              sprintf(title, "hs %i %i", u, 3+2*it);
1894              sprintf(hname, "Column %i", it+1);
1895              hs[u][3+2*it] = new TH1D( title, hname, 2000, -400., 1600.);
1896              hs[u][3+2*it]->GetXaxis()->SetNdivisions(505);
1897              hs[u][3+2*it]->SetStats(1);
1898 
1899              sprintf(title, "hs %i %i", u, 4+2*it);
1900              sprintf(hname, "Row %i ", it+1);
1901              hs[u][4+2*it] = new TH1D( title, hname, 2000, -400., 1600.);
1902              hs[u][4+2*it]->GetXaxis()->SetNdivisions(505);
1903              hs[u][4+2*it]->SetStats(1);
1904 
1905          }
1906 
1907          sprintf(title, "hs13 %i",u);
1908          sprintf(hname, "Active final");
1909          hs[u][13] = new TH1D( title, hname, 2000, -400., 1600.);
1910          hs[u][13]->GetXaxis()->SetNdivisions(505);
1911          hs[u][13]->SetStats(1);
1912 
1913 // histos for map sorted clusters   ******************************

1914         sprintf(title, "hNp_%i", u);
1915         sprintf(hname, "Npix %i", u);
1916         hNp[u] = new TH1F(title,hname,32,0.,32.);
1917         hNp[u]->SetStats(1); 
1918         sprintf(title, "hNph_%i", u);
1919         sprintf(hname, "NpHT %i", u);
1920         hNph[u] = new TH1F(title,hname,32,0.,32.);
1921         hNph[u]->SetStats(1); 
1922         sprintf(title, "hOrm_%i", u);
1923         sprintf(hname, "Orm %i", u);
1924         horm[u] = new TH1F(title,hname, 40,0.,2.);
1925         horm[u]->SetStats(1);
1926         sprintf(title, "hra_%i", u);
1927         sprintf(hname, "Ratio %i", u);
1928         hra[u] = new TH1F(title,hname, 240,0.,1.05);
1929         //hra[u]->SetStats(1111);

1930         hra[u]->SetStats(0);
1931         sprintf(title, "hspr_%i", u);
1932         sprintf(hname, "SPR %i", u);
1933         hspr[u] = new TH1F(title,hname, 240,0.,1.2);
1934         //hspr[u]->SetStats(1111);

1935         hspr[u]->SetStats(0);
1936 
1937          for (int i=0; i<Nham; i++){
1938             sprintf(title, "Ch%i_cluster_%i", u, i);
1939         switch( i )
1940         {
1941             case 0: 
1942                 sprintf(hname, "Ch%i all pixels", u);
1943                 break;
1944             case 1: 
1945                 sprintf(hname, "Ch%i 1 pixel", u);
1946                 break;
1947             case 5: 
1948                 sprintf(hname, "Ch%i 5-%i pixels", u, Nham-1);
1949                 break;
1950             default:
1951                 sprintf(hname, "Ch%i %i pixels", u, i);
1952                 break;
1953         }
1954             ham[u][i] = new TH1D( title, hname, 750, 0., 1500.);
1955             ham[u][i]->GetXaxis()->SetNdivisions(505);
1956             ham[u][i]->SetStats(1);
1957         }
1958         
1959         sprintf(title, "Seq_hit_Ch%i_cluster_SumA", u);
1960         sprintf(hname, "Ch%i_SumA", u);
1961         hitSumA[u] = new TH1D( title, hname, 150, 0., 1500.); //1000, 0., 10000.

1962         hitSumA[u]->GetXaxis()->SetNdivisions(505);
1963         hitSumA[u]->SetStats(1);
1964 
1965         // Fe55 Analysis histograms: ******************************************

1966         sprintf(hname, "pix_amp_%i", u);
1967         hfe[u] = new TH1D(hname,hname,2000,-200.,9800.);
1968         hfe[u]->SetStats(1);
1969         sprintf(hname, "map_amp_%i", u);
1970         hmap[u] = new TH1D(hname,hname,2000,-200.,9800.);
1971         hmap[u]->SetStats(1);
1972         sprintf(hname, "pileup_sum_%i", u);
1973         hpile[u] = new TH1D(hname,hname,1000, 0., 10000.);
1974         hpile[u]->SetStats(1);
1975         sprintf(hname, "clean_sum_%i", u);
1976         hclean[u] = new TH1D(hname,hname,150, 0., 1500.); //1000, 0., 10000.

1977         hclean[u]->SetStats(1);
1978         sprintf(hname, "Y_profile_%i", u);
1979         hYpro[u] = new TH1D(hname,hname,1600, 400., 1200.);
1980         hYpro[u]->SetStats(1);
1981         sprintf(hname, "X_profile_%i", u);
1982         hXpro[u] = new TH1D(hname,hname,800, 0., 1600.);
1983         hXpro[u]->SetStats(1);
1984         sprintf(hname, "X_fit_%i", u);
1985         hXtst[u] = new TH1D(hname,hname,200, 0.5, 2.5);
1986         hXtst[u]->SetStats(1);
1987         sprintf(hname, "YX_plot_%i", u);
1988         hYX[u] = new TH2D(hname,hname,80, 600., 920., 100, 500, 1500.);
1989         hYX[u]->SetStats(0);
1990         //*************

1991         
1992         sprintf(title, "X-loss %i", u);
1993         sprintf(hname, "X-loss %i", u);
1994         hXstat[u] = new TH1D( title, hname, 10, 0., 10.);
1995         hXstat[u]->GetXaxis()->SetNdivisions(505);
1996         hXstat[u]->SetStats(0);
1997     
1998         sprintf(title, "Serial Transfer %i", u);
1999         sprintf(hname, "Serial Transfer %i", u);
2000         Xcte[u] = new TH2F( title, hname, 50, 0., 550., 400, 0., 2000.);
2001         Xcte[u]->SetStats(0);
2002         sprintf(title, "Parallel Transfer %i", u);
2003         sprintf(hname, "Parallel Transfer %i", u);
2004         Ycte[u] = new TH2F( title, hname,101, 0., 2020., 400, 0., 2000.);
2005         Ycte[u]->SetStats(0);
2006         sprintf(title, "ClustQ %i", u);
2007         sprintf(hname, "ClustQ %i", u);
2008         ClustQ[u] = new TH2F( title, hname, 50, 0., 550., 101, 0., 2020.);
2009         ClustQ[u]->SetStats(0);
2010 
2011         for (int i=0; i<Ncte; i++){
2012             char xtitle[40];
2013             char ytitle[40];
2014             sprintf(xtitle, " %i binx %i", u, i);
2015             sprintf(ytitle, " %i biny %i", u, i);
2016             xcte[u][i] = new TH1F( xtitle, xtitle, 400, 0., 2000.);
2017             ycte[u][i] = new TH1F( ytitle, ytitle, 400, 0., 2000.);
2018             xcte[u][i]->SetStats(1);
2019             ycte[u][i]->SetStats(1);
2020         }
2021 
2022 // average cluster and pixel amp distributions

2023         //for (int i=0; i<Zone*Zone; i++){

2024             sprintf(title, "AvHit %i", u);  //, i)

2025             AvrHit[u] = new TH2D(title,title, Co::NXpix, 0., Co::NXpix, Co::NYpix, 0., Co::NYpix);
2026             AvrHit[u]->SetStats(1);
2027         //}

2028         for (int x=0; x<Co::NXpix; x++) {
2029             for (int y=0; y<Co::NYpix; y++) {
2030                 sprintf(title, "ClusterPix_%i_%i%i", u, x, y);
2031                 hpixA[u][x][y] = new TH1D(title,title,750,-50,700);
2032                 hpixA[u][x][y]->GetXaxis()->SetNdivisions(505);
2033                 hpixA[u][x][y]->SetStats(1);
2034             }
2035         }
2036 
2037      hXf[u][0] = new TH1D("chi2","chi2", 100, 0., 100.);
2038      hXf[u][0]->SetStats(0);
2039      hXf[u][6] = new TH1D("chiR","chiR", 100, 0., 50.);
2040      hXf[u][6]->SetStats(0);
2041      hXf[u][1] = new TH1D("SumF","SumF", 500, 0., 2000.);
2042      hXf[u][1]->SetStats(1);
2043      hXf[u][2] = new TH1D("xfit","xfit", 200, 0.5, 2.5);
2044      hXf[u][2]->SetStats(0);
2045      hXf[u][3] = new TH1D("yfit","yfit", 200, 0.5, 2.5);
2046      hXf[u][3]->SetStats(0);
2047      hXf[u][4] = new TH1D("sigma","sigma", 200, 0., 20.0);
2048      hXf[u][4]->SetStats(0);
2049      hXf[u][5] = new TH1D("SigErr","SigErr", 200, 0., 1.);
2050      hXf[u][5]->SetStats(0);
2051 
2052     } // MaxP loop

2053     
2054     hSig = new TH1D("sigma","sigma", 200, 0., 20.0);
2055     hSig->SetStats(0);
2056 
2057 
2058 // Fe55 files:  *******************************************************

2059     FileList FL(dname);
2060     if (FL.Nfile <= 0) {Flag= -1; return;}
2061     
2062 // file loop: *********************************************************

2063     Hits * SegHits[MaxP];
2064     TCanvas * cF = new TCanvas("Fit2D", "Fit2D", 300,10,700,650);
2065     TH2F   * f2D = new TH2F("f2D","fit2", Co::NXpix, 0, Co::NXpix, Co::NYpix, 0, Co::NYpix);
2066     enum { Npar2DI = 4};
2067     TF2    * fit = new TF2("fitG2DI", G2DI, 0., Co::NXpix, 0., Co::NYpix, Npar2DI);
2068     cF->Divide(1,2); 
2069 
2070     int Nf=0;
2071     int FFTinit=0;
2072     const char *colr[8]={"white","black","red","green","blue","cyan","magenta","yellow"};
2073 
2074     for (FL.FName_Iter=FL.FName.begin(); FL.FName_Iter != FL.FName.end(); FL.FName_Iter++, Nf++)
2075     {
2076         int Nray = 0;
2077         ch_idx=-1;
2078         filename = *FL.FName_Iter; //  dname + "/" +

2079         printf(" Fe55: opening file %s \n",filename.c_str());
2080         if ( Open(filename.c_str()) ) continue;
2081         //OpenOutF();

2082         Catalogs();
2083         
2084         while( !iEOF ){
2085             if ( hdu > MaxP )   break;
2086             if ( Read() )    continue;
2087             if ( hdu == 1){
2088                 for (int i=0; i<Nkey; i++){
2089                     double v; 
2090                     int ir = getValue( KeyList[i], &v );
2091                     if (!i) v -= T1995; //convert CTIME to root time

2092                     if (!ir) Vval[i].push_back( v ); 
2093                 }
2094             }
2095             if ( naxis != 2) continue;
2096 
2097             DataStr * Dev = DataStr::Instance(nx,ny);
2098             XminSearch = Dev->minX() + 4;
2099             XmaxSearch = Dev->maxX() - 4;
2100             YminSearch = Dev->minY() + 4;
2101             YmaxSearch = Dev->maxY() - 4;
2102             PixSz = Dev->PixSz();
2103 
2104             if ( !Nf ){
2105                 Nchan++;
2106                 if (doFFT && (!FFTinit) ) {
2107                     Col = new FFT(  "Fe55 Col",
2108                                     Dev->minY(), Dev->maxY(),
2109                                     Dev->minX(), Dev->maxX(),
2110                                     nx, 1 );
2111                     Row = new FFT(  "Fe55 Row",
2112                                     Dev->minX(), Dev->maxX(),
2113                                     Dev->minY(), Dev->maxY(),
2114                                     1, nx, 87 );  //, 3.944 );  

2115                     FFTinit=1;
2116                 }
2117             }
2118 
2119             if (bufzs == 0) {
2120                 bufzs = new double[npixels];
2121                 memset(bufzs, 0, npixels * sizeof(double));
2122                 bz_save = new double[npixels];
2123                 memset(bz_save, 0, npixels * sizeof(double));
2124             }
2125             ch_idx++;
2126             if (ch_idx != 0) continue;
2127             //if (ch_idx>7) break;

2128 
2129 //          BaLiC ( Dev );

2130 
2131             for (unsigned long i=0; i<npixels; i++){
2132                             bufzs[i] = (double)buffer[i];}
2133             printf(" before BaLiS File: %s  \n", filename.c_str() );
2134             BaLiS ( Dev );
2135             printf(" ANoise[%i](noise rms) = %f \n", ch_idx, ANoise[ch_idx]);
2136             //break;

2137 
2138             if (doFFT) { 
2139                 Col->DTrans(bufzs, ch_idx, 0); 
2140                 Row->DTrans(bufzs, ch_idx, 1); 
2141                 //Col->DTrans(bufzs, 1, 0); 

2142                 Row->DTrans(bufzs, 0, 0); 
2143             }
2144 
2145             double AminSrch = 20.*ANoise[ch_idx];
2146             //OutFitsF( bufzs );

2147 
2148 //*************  gain correction ***************************************

2149 //

2150              double gainC = Co::ConvGain[ch_idx];
2151             //AminSrch *= gainC;

2152             //for (unsigned long i=0; i<npixels; i++){

2153             //              bufzs[i] *= gainC;}

2154 
2155             //*************  multimap ordering ***************************************

2156             Qhit qhi;
2157             typedef pair <double, Qhit> AmapPair;
2158             multimap <double, Qhit, greater<double> >  Amap;
2159             multimap <double, Qhit, greater<double> >::iterator AIter;
2160             multimap <double, Qhit>::size_type Asize;
2161             Amap.clear();
2162 
2163             printf(" Start sequentional clustering: ch=%i  \n", ch_idx );
2164             SegHits[ch_idx]= new Hits(nx, ny, ANoise[ch_idx], 20.0 );
2165             OneHit::Nrpix1=SegHits[ch_idx]->Nrpix;
2166             OneHit::Acut1 =SegHits[ch_idx]->Acut;
2167             OneHit::AcutL1 =SegHits[ch_idx]->AcutL;
2168             for (int iy=YminSearch; iy<YmaxSearch; iy++) {
2169                 int j0 = iy*nx;
2170                 double * pb = &bufzs[j0];
2171                 SegHits[ch_idx]->GetRow(pb,iy);
2172                 OneHit::p_crow=SegHits[ch_idx]->crow;  // current row pointer

2173                 OneHit::p_arow=SegHits[ch_idx]->arow;  // antecedent row pointer

2174                 OneHit::jy_c=iy;    // current row index

2175                 SegHits[ch_idx]->ClusterMatch();
2176                 SegHits[ch_idx]->ClusterSeed();
2177             }
2178             printf(" Done sequentional clustering: ch=%i Nhits=%i \n", ch_idx, SegHits[ch_idx]->NHits);
2179             printf(" Hits Dump: %i %i %i %i %i \n", Hits::Wpad, Hits::Wtime,
2180                    SegHits[ch_idx]->Nrpix, SegHits[ch_idx]->maxHit, SegHits[ch_idx]->maxSeed);
2181             printf(" Hits Dump: %f %f %f %f \n", SegHits[ch_idx]->Noise,
2182                    SegHits[ch_idx]->Threshold, SegHits[ch_idx]->Acut, SegHits[ch_idx]->AcutL);
2183             printf(" Hits Dump: %i %i %i  \n", SegHits[ch_idx]->NSeeds, SegHits[ch_idx]->NHits,
2184             SegHits[ch_idx]->jy_c);
2185             
2186             for (int ih=0; ih<SegHits[ch_idx]->NHits; ih++){
2187                 hitSumA[ch_idx]->Fill( SegHits[ch_idx]->hit1[ih].SumA );
2188                 fprintf(pFreg, "# tile %i\nimage; box %i %i %i %i 0 # color = %s text={%6.0f}\n", ch_idx+1, SegHits[ch_idx]->hit1[ih].ixcntr + 1, SegHits[ch_idx]->hit1[ih].jycntr + 1, Hits::Wpad, Hits::Wtime, colr[2], SegHits[ch_idx]->hit1[ih].SumA);
2189 
2190             }
2191             delete SegHits[ch_idx]; SegHits[ch_idx]=0;
2192             
2193             for (int iy=YminSearch; iy<YmaxSearch; iy++) {
2194                 for (int ix=XminSearch; ix<XmaxSearch; ix++) {
2195                   int j = iy*nx + ix;
2196                   double amp = bufzs[j];
2197                   if (amp < AminSrch ) continue;
2198                   qhi.Set(ix, iy);
2199                   Amap.insert( AmapPair( amp, qhi ) );
2200                 }
2201             }
2202             Asize = Amap.size();
2203             printf(" File: %s  Ch=%i MapSize:%lu \n", filename.c_str(), ch_idx, (long unsigned)Asize);
2204 
2205             NgrX = (Dev->maxX() - Dev->minX())/(Ncte-1);
2206             NgrY = (Dev->maxY() - Dev->minY())/(Ncte-1);
2207             Ohit hit( XmaxSearch, XminSearch, YmaxSearch, YminSearch, ANoise[ch_idx],
2208                         nx, ny, bufzs, cF, f2D, fit);
2209             for (unsigned long i=0; i<npixels; i++){ bz_save[i]=bufzs[i]; }
2210             for ( AIter = Amap.begin(); AIter != Amap.end(); AIter++ ){
2211                 int x = AIter->second.ixb;
2212                 int y = AIter->second.jyb;
2213                 hit.Cluster(x, y, 1);
2214                 hit.Clear();
2215             }
2216             for (unsigned long i=0; i<npixels; i++){ bufzs[i]=bz_save[i]; }
2217 
2218             printf(" ** second pass \n");
2219             for ( AIter = Amap.begin(); AIter != Amap.end(); AIter++ ){
2220                 double amp = AIter -> first;
2221                 int x = AIter->second.ixb;
2222                 int y = AIter->second.jyb;
2223                 hmap[ch_idx]->Fill( amp );
2224                 hit.Cluster(x, y);
2225                 int lHit = (hit.Npix > 0) && (hit.Npix<=Ohit::NXsrch*Ohit::NYsrch); //(hit.Npix<4);

2226                 int lMnK = 0;  //(hit.Sum*gainC > Co::KcutL) && (hit.Sum*gainC < Co::KcutR);

2227                 if (hit.Flag == -200) hpile[ch_idx]->Fill( hit.Sum );
2228                 if (hit.Flag < 0) { hit.Clear(); continue;}
2229                 //assert(lHit);

2230                 if (!lHit) continue;
2231                 hclean[ch_idx]->Fill( hit.Sum );
2232                 hXstat[ch_idx]->Fill(0.1);
2233                 hNp[ch_idx]->Fill(hit.NpixH );
2234                 //ham[ch_idx][0]->Fill( hit.Sum*gainC  );

2235                 //ham[ch_idx][hit.NpixH]->Fill( hit.Sum*gainC  );

2236                 ham[ch_idx][0]->Fill( hit.Sum*gainC );
2237                 ham[ch_idx][hit.NpixH]->Fill( hit.Sum*gainC );
2238                 Xcte[ch_idx]->Fill( x, hit.SumCTE*gainC );
2239                 Ycte[ch_idx]->Fill( y, hit.SumCTE*gainC );
2240                 // CTE histos

2241                 if ( Co::doCTE ) {  //6

2242                     int indx=x/NgrX;
2243                     int indy=y/NgrY;
2244                     xcte[ch_idx][indx]->Fill( hit.SumCTE*gainC );
2245                     ycte[ch_idx][indy]->Fill( hit.SumCTE*gainC );
2246                 }
2247                if ( lMnK ) {
2248                     hXstat[ch_idx]->Fill(1.1);
2249                     hNph[ch_idx]->Fill(hit.NpixH );
2250                     horm[ch_idx]->Fill(hit.rms);
2251                     hra[ch_idx]->Fill(hit.MRatio);
2252                     ClustQ[ch_idx]->Fill( hit.ixb, hit.jyb );
2253                     for (int i=0; i<Ohit::NXsrch; i++) {
2254                           for (int j=0; j<Ohit::NYsrch; j++) {
2255                             if (hit.Amp[i][j]>0.1) hspr[ch_idx]->Fill(hit.Amp[i][j]/hit.Sum);
2256                           }
2257                       }
2258 
2259                     // Average Hit

2260                         Zind=0;
2261                         if (x > nx/2) Zind=1; 
2262                         NKhit[ch_idx][Zind]++;
2263                         for (int iy=0; iy<hit.NYsrch; iy++){
2264                             for (int ix=0; ix<hit.NXsrch; ix++){
2265                                 double ai=hit.Amp[ix][iy]*gainC;  ///Sum

2266                                 AvHit[ch_idx][Zind][ix][iy] += ai;
2267                                 A2Hit[ch_idx][Zind][ix][iy] += ai*ai;
2268                             }
2269                         }
2270                         Zind=2;
2271                         if (y > ny/2) Zind=3; 
2272                         NKhit[ch_idx][Zind]++;
2273                         for (int iy=0; iy<hit.NYsrch; iy++){
2274                             for (int ix=0; ix<hit.NXsrch; ix++){
2275                                 double aj=hit.Amp[ix][iy]*gainC;  ///Sum

2276                                 AvHit[ch_idx][Zind][ix][iy] += aj;
2277                                 A2Hit[ch_idx][Zind][ix][iy] += aj*aj;
2278                             }
2279                         }
2280 
2281                 }  // if Ka,b

2282                 //  cluster profile histos

2283 //                if (hit.NpixH>4) {  //!hit.isoFlag

2284 //                    //ham[ch_idx][0]->Fill( hit.Sum*gainC );  //hit.SumOne*gainC );

2285 //                    for (int i=0;i<Ohit::NXsrch;i++) {

2286 //                        for (int j=0;j<Ohit::NYsrch;j++) {

2287 //                            hpixA[ch_idx][i][j]->Fill(hit.Amp[i][j]*gainC);

2288 //                        }

2289 //                    }

2290 //                }

2291 
2292                 if ( Co::doFit
2293                      && hit.Npix > 3
2294                      && hit.Npix<=Ohit::NXsrch*Ohit::NYsrch ) {
2295                     hit.FitX();
2296                     hXstat[ch_idx]->Fill(2.1);
2297                     if (hit.fitflag == 0) {
2298                         hXstat[ch_idx]->Fill(3.1);
2299                         hXf[ch_idx][0]->Fill( hit.chi2);
2300                         hXf[ch_idx][6]->Fill( hit.chiR);
2301                         if ( hit.chiR    < 40.     ) {  //chi2

2302                             hXstat[ch_idx]->Fill(4.1);
2303                             hXf[ch_idx][1]->Fill( hit.Sumf);
2304                             if ( (hit.Sumf >  500.) &&
2305                                  (hit.Sumf < 1500.) ) {
2306                                 hXf[ch_idx][2]->Fill( hit.xfit);
2307                                 hXf[ch_idx][3]->Fill( hit.yfit);
2308                                 hXf[ch_idx][4]->Fill( hit.sfit*PixSz);
2309                                 hXf[ch_idx][5]->Fill( hit.efit*PixSz);
2310                                 if ( hit.sfit*PixSz > 3.){
2311                                     hYpro[ch_idx]->Fill( hit.yfitG);
2312                                     hXpro[ch_idx]->Fill( hit.xfitG);
2313                                     hXtst[ch_idx]->Fill( hit.xfitG);
2314                                     hYX[ch_idx]->Fill(hit.yfitG, hit.xfitG);
2315                                     for (int i=0;i<Ohit::NXsrch;i++) {
2316                                         for (int j=0;j<Ohit::NYsrch;j++) {
2317                                             hpixA[ch_idx][i][j]->Fill(hit.Amp[i][j]*gainC);
2318                                         }
2319                                     }
2320                                     //Output to list file

2321                                     Nray++;
2322                                     fprintf(pFlist, " %i %f %f %f %f %f %f %i \n",
2323                                             Nray, hit.xhitG+ch_idx*nx, hit.yhitG, hit.xfitG+ch_idx*nx, hit.yfitG, hit.yfitG, hit.Sum, 0);
2324 
2325                                 }
2326                             }
2327                         }
2328                     }
2329                 }  // cluster fit

2330 
2331                 //Output to region file

2332                 fprintf(pFreg, "# tile %i\nimage; box %i %i %i %i 0 # color = %s text={%6.0f}\n", ch_idx+1, hit.ixb + 1, hit.jyb + 1, Ohit::NXsrch, Ohit::NYsrch, colr[3], hit.Sum);
2333                 //Output to catalog file

2334                 double lX=(double)hit.xhit+1.0;  //hit.ixb + 1.0;  +1.0; <- to ds9 coordinate

2335                 double lY=(double)hit.yhit+1.0;  //hit.jyb + 1.0;

2336                 int lRegion = (lX > 1) && (lY > 1) && (lX < nx) && (lY < ny);
2337                 if ( (abs(ltm1_1) > 0.001) && (abs(ltm2_2) > 0.001) ) {   //&& lRegion

2338                     double physX = (lX - ltv1)/ltm1_1;
2339                     double physY = (lY - ltv2)/ltm2_2;
2340                     if(ch_idx + 1 > 8){physX--; physY++;} //correction for channels 8- 16

2341                     fprintf(pFcat, "<TR><TD>%f</TD><TD>%f</TD><TD>%i</TD><TD>%i</TD><TD>%6.0f</TD><TD>%i</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD><TD>%6.0f</TD></TR>\n", physX, physY, hit.ixb + 1, hit.jyb + 1, hit.Sum, ch_idx + 1, hit.Amp[0][0], hit.Amp[1][0], hit.Amp[2][0], hit.Amp[0][1], hit.Amp[1][1], hit.Amp[2][1], hit.Amp[0][2], hit.Amp[1][2], hit.Amp[2][2]);
2342                     //Nfree, fitflag, chi2, chiR, Sumf, xfit, yfit, sfit, efit

2343                 }
2344                
2345                 hit.Clear();
2346             }
2347             printf(" ** segment %i done \n", ch_idx);
2348             
2349             //hit.~Ohit();

2350             //for (int y=0; y<Co::NYpix; y++){

2351             //  for (int x=0; x<Co::NXpix; x++){

2352             //      hit.AvHit[x][y] /= hit.NKhit;

2353             //      AvrHit[ch_idx]->SetBinContent(x+1,y+1,hit.AvHit[x][y]);

2354             //  }

2355             //}

2356 
2357         } //end HDU while loop

2358 
2359         //OutFitsF( bufzs );

2360         Close();
2361         fclose(pFlist);    //Close list file

2362         pFlist = 0;
2363         
2364         fclose(pFreg);  //Close region file

2365         pFreg = 0;
2366         
2367         //print last part of catalog

2368         fprintf(pFcat,"</TABLEDATA>\n</DATA>\n</TABLE>\n</RESOURCE>\n</VOTABLE>");
2369         fclose(pFcat);  //Close catalog file

2370         pFcat = 0;
2371         
2372 
2373         //CloseOut();

2374 
2375     } //end files loop

2376 
2377 // Average FFT vectors +++++++++++++++++++++++++++++++++++++++++++++++++++++

2378 
2379     if (doFFT) {
2380         for (int u=0; u<2; u++){   //Nchan

2381             for(int i =1; i < Col->Nfreq; i++) {Col->PSDav[u][i] /= Nf;}
2382             for(int i =1; i < Row->Nfreq; i++) {Row->PSDav[u][i] /= Nf;}
2383         }
2384     }       
2385 //  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2386 
2387     Flag = 0;
2388     return;
2389 } // end Fe55 constructor

2390 
2391 
2392 
2393 //*********************************************************************

2394 // par[]

2395 // 0 - amplitude of Gauss1

2396 // 1 - center of Gauss1

2397 // 2 - sigma noise in electrons

2398 // 3 - amplitude of Gaus2

2399 // 4 - center of Gaus2 <----- calculated from par[1]

2400 
2401 double  Fe55::GGF ( double *v, double *par )  
2402 {
2403     static const double GNorm = 1./TMath::Sqrt(2.*TMath::Pi());
2404     static const double sa = Co::Ea * Co::Fano/Co::w_pair;
2405     static const double sb = Co::Eb * Co::Fano/Co::w_pair;
2406     double scale = par[1]*Co::w_pair/Co::Ea;  //in adu/e-

2407     double par4 = par[1]*Co::Eb/Co::Ea;
2408 
2409     double x = v[0];
2410     double N2 = par[2]*par[2];   //noise in e- squared

2411     double s1 = TMath::Sqrt(N2 + sa)*scale;
2412     double s2 = TMath::Sqrt(N2 + sb)*scale;
2413     double argG1 = (x - par[1])/s1;
2414     double argG2 = (x - par4)/s2;
2415     double fitval = par[0]*GNorm*TMath::Exp(-0.5*argG1*argG1)/s1 +
2416             par[3]*GNorm*TMath::Exp(-0.5*argG2*argG2)/s2; 
2417 
2418     //printf(" %f %f %f %f %f %f %f %f %f %f %f  \n", 

2419     //  x, fitval, par[0], par[1], par[2], par[3], 

2420     //  scale, s1, s2, argG1, argG2 );

2421 
2422     return fitval;
2423 }
2424 
2425 void Fe55::PlotSpectra( void )
2426 {
2427     rPad __f;
2428 
2429     TCanvas *   Zs[MaxP]={0};
2430     for (int ch_idx=0; ch_idx<Nchan; ch_idx++){
2431         char tiname[50];
2432         sprintf(tiname, "Spectra_%i", ch_idx);
2433         if ( ch_idx > MaxP )   break;
2434 // zero subtracted X-ray hits ******************************************

2435          Zs[ch_idx] = new TCanvas( tiname, tiname, 30, 10, 900, 800);
2436          Zs[ch_idx]->SetBorderMode  (0);      //to remove border

2437          Zs[ch_idx]->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

2438          Zs[ch_idx]->SetRightMargin (right_margin);   //to move plot to the right 0.05

2439          Zs[ch_idx]->SetTopMargin   (top_margin);   //to use more space 0.07

2440          Zs[ch_idx]->SetBottomMargin(bot_margin);   //default

2441          Zs[ch_idx]->SetFrameFillColor(0);
2442          Zs[ch_idx]->Divide(2,3);
2443 
2444          //TColor * c150 = new TColor(150,1.0,0.0,0.0); 

2445          Zs[ch_idx]->SetFillStyle(4100);
2446          Zs[ch_idx]->SetFillColor(0);
2447 
2448          Zs[ch_idx]->cd(1);
2449          if ( hfe[ch_idx]->GetEntries() > 1. ) {
2450              gPad->SetLogy(); 
2451              gStyle->SetOptFit(1);
2452              hfe[ch_idx]->SetLineColor(4);
2453              hfe[ch_idx]->Draw();
2454              hfe[ch_idx]->Fit("gaus");
2455              if ( hmap[ch_idx]->GetEntries() > 1. ) {
2456                  //gPad->SetLogy();

2457                  hmap[ch_idx]->SetLineColor(6);
2458                  hmap[ch_idx]->Draw("same");
2459              }
2460          }
2461 
2462          Zs[ch_idx]->cd(2);
2463          if ( hclean[ch_idx]->GetEntries() > 1. ) {
2464              gPad->SetLogy();
2465              hclean[ch_idx]->SetLineColor(8);
2466              hclean[ch_idx]->Draw();
2467          }
2468 
2469          Zs[ch_idx]->cd(3);
2470          if ( hpile[ch_idx]->GetEntries() > 1. ) {
2471              gPad->SetLogy(); 
2472              hpile[ch_idx]->SetLineColor(6);
2473              hpile[ch_idx]->Draw();
2474          }
2475         
2476         Zs[ch_idx]->cd(4);
2477         if ( hYpro[ch_idx]->GetEntries() > 1. ) {
2478             gPad->SetLogy();
2479             hYpro[ch_idx]->SetLineColor(6);
2480             hYpro[ch_idx]->Draw();
2481         }
2482         
2483         Zs[ch_idx]->cd(5);
2484         if ( hXpro[ch_idx]->GetEntries() > 1. ) {
2485             //gPad->SetLogy();

2486             hXpro[ch_idx]->SetLineColor(6);
2487             hXpro[ch_idx]->Draw();
2488         }
2489 
2490         Zs[ch_idx]->cd(6);
2491         if ( hYX[ch_idx]->GetEntries() > 1. ) {
2492             //gPad->SetLogy();

2493             hYpro[ch_idx]->SetLineColor(4);
2494             hYX[ch_idx]->Draw();
2495         }
2496         
2497 //        Zs[ch_idx]->cd(7);

2498 //        if ( hXtst[ch_idx]->GetEntries() > 1. ) {

2499 //            //gPad->SetLogy();

2500 //            hXtst[ch_idx]->SetLineColor(6);

2501 //            hXtst[ch_idx]->Draw();

2502 //        }

2503         
2504          Zs[ch_idx]->Update();
2505         sprintf(tiname,"Spectra_%i.pdf", ch_idx);
2506          Zs[ch_idx]->Print(tiname);
2507     }
2508 } //end PlotSpectra function

2509     
2510 double  Fe55::Gauss ( double *v, double *par )  
2511 {
2512 
2513     double x = v[0];
2514     double s1 = par[2];
2515     if (s1 < 0.000001) s1=0.000001;
2516     double GNorm = 1./(TMath::Sqrt(2.*TMath::Pi())*s1);
2517     double argG1 = (x - par[1])/s1;
2518     double fitval = par[0]*GNorm*TMath::Exp(-0.5*argG1*argG1);
2519     return fitval;
2520 }
2521 
2522 void Fe55::FitG (TH1D * hm, double * Amp, double * Shift, double * Noise)
2523 {
2524 //fit range *******************************************************************

2525     double maxpos;
2526     double Rmin;
2527     double Rmax;
2528     PeakRange( hm, &maxpos, &Rmin, &Rmax); 
2529     cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax <<endl;
2530     cout << endl;
2531 //*****************************************************************************

2532 
2533     const char fitname[] = "fitGauss";
2534     TF1 * fit = new TF1(fitname, Fe55::Gauss, Rmin, Rmax, 3);
2535 
2536     double maxval = hm->GetEntries(); 
2537     if (maxval < 1.) return;
2538         printf("\n Fit of %s \n",hm->GetTitle());
2539 //      printf(" : Sum of weights = %f  Entries = %f \n", i, sw, maxval);   

2540     double par0 = maxval*0.75;
2541     double par2 = (Rmax - Rmin)/4.;  //ANoise[0];

2542     fit->SetParameter(0, par0); 
2543     fit->SetParameter(1, maxpos);   
2544     fit->SetParameter(2, par2);
2545     cout << " par0=" << par0 <<  endl;
2546     cout << " par1=" << maxpos << " par2=" << par2 << endl;
2547     cout << "fitname=" << fitname << endl;
2548     int fitflag = hm->Fit(fitname,"NS");
2549     cout << " FitFlag=" << fitflag << endl;
2550     *Amp   = fit->GetParameter(0);
2551     *Shift   = fit->GetParameter(1);
2552     *Noise = fit->GetParameter(2);
2553 
2554     return;
2555 }
2556 
2557 
2558 void Fe55::PlotAvHit(void)
2559 {
2560     string foName = "AvHit_";
2561     foName += OutExt;
2562     foName += ".txt";
2563     printf(" Output File: %s  \n", foName.c_str());
2564     FILE * pFile = fopen (foName.c_str(),"wt");
2565     if (pFile == 0) {printf(" File %s cann't be open \n", foName.c_str()); Flag=-1; return;}
2566     fprintf (pFile,"// %s  input files from: %s \n", foName.c_str(), dname.c_str());
2567 
2568 for (int u=0; u<Nchan; u++){
2569         if ( u > MaxP )   break;
2570         // Average Hit

2571         fprintf (pFile," \n *** channel: %i  \n", u);
2572 
2573     for (int zind=0; zind<NZ; zind++) {
2574             double Nev=NKhit[u][zind];
2575             fprintf (pFile," \n Zone: %i  Nev=%f \n", zind, Nev);
2576             if (Nev < 100) continue;
2577             for (int y=0; y<Co::NYpix; y++){
2578                 fprintf (pFile," y=%i: ", y);
2579                 for (int x=0; x<Co::NXpix; x++){
2580                     double amp=AvHit[u][zind][x][y]/Nev;
2581                     double am2=A2Hit[u][zind][x][y]/Nev;
2582                     double rms = am2-amp*amp;
2583                     if (rms <0.) rms=0.;
2584                     rms=sqrt(rms);
2585                     rms/=sqrt(Nev);
2586                 fprintf (pFile," %f+/-%f  ", amp, rms);
2587                 }
2588                 fprintf (pFile,"\n");
2589             }
2590                 fprintf (pFile,"\n");
2591 
2592     }
2593 
2594 }
2595     fclose( pFile );
2596 
2597 //TCanvas   *   Ch[MaxP]={0};

2598 //      char tiname[50];

2599 //      sprintf(tiname, "Ch %i", u);

2600 //

2601 //// cluster canvas ************************************************* 

2602 //   Ch[u] = new TCanvas( tiname, tiname, 50+5*u, 50+5*u, 1000, 800);

2603 //   Ch[u]->SetBorderMode  (0);            //to remove border

2604 //   Ch[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

2605 //   Ch[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

2606 //   Ch[u]->SetTopMargin   (top_margin);   //to use more space 0.07

2607 //   Ch[u]->SetBottomMargin(bot_margin);   //default

2608 //   Ch[u]->SetFrameFillColor(0);

2609 //   Ch[u]->Divide(1,1);

2610 //

2611 //   Ch[u]->SetFillStyle(4100);

2612 //   Ch[u]->SetFillColor(0);

2613 //

2614 //// average hit canvas ************************************************* 

2615 //   //for (int i=0; i<Zone*Zone; i++){

2616 //      // Ch[u]->cd(1);  //2*i+1

2617 //      ////gPad->SetLogy(); 

2618 //      //gPad->SetGrid(1,0);

2619 //      //gPad->SetGridy();

2620 //      //gPad->SetFillColor(0);

2621 //      //gPad->SetBorderSize(2);

2622 //      //AvrHit[u]->GetXaxis()->SetNdivisions(505);

2623 //      //AvrHit[u]->GetYaxis()->SetNdivisions(505);

2624 //      //AvrHit[u]->Draw("colz");

2625 //

2626 //      Ch[u]->cd(2);  //2*i+2

2627 //      gPad->SetFillColor(0);

2628 //      gPad->SetBorderSize(2);

2629 //      AvrHit[u]->Draw("lego");

2630 //   //}

2631 //

2632 //      Ch[u]->Update();

2633 
2634 
2635     return;
2636 
2637 } //end PlotAvHit    

2638 
2639 void Fe55::PlotXlines()
2640 {
2641     double maxpos;
2642     double Rmin;
2643     double Rmax;
2644     
2645 //    double NKa =0.;

2646 //    double EKa   = 0.;

2647 //    double Noise = 0.;

2648 //    double NKb   =0.;

2649 //    double era   = 0.;

2650 //    double ca = 0.;

2651 //    double rab = 0.;

2652 //    int fitflag = -100.;

2653 
2654     // output file: *******************************************************

2655     string foName = "fit4_";
2656     foName += OutExt;
2657     foName += ".txt";
2658     printf(" Output File: %s  \n", foName.c_str());
2659     FILE * pFile = fopen (foName.c_str(),"wt");
2660     if (pFile == 0) {printf(" File %s cann't be open \n", foName.c_str()); Flag=-1; return;}
2661     fprintf (pFile,"// %s  input files from: %s \n", foName.c_str(), dname.c_str());
2662 
2663 // Fe55 Segments Summary  ************************************************* 

2664      TCanvas    * Cs = new TCanvas( "Fe55", "Fe55", 145, 5, 1000, 800);
2665      Cs->SetBorderMode  (0);            //to remove border

2666      Cs->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

2667      Cs->SetRightMargin (right_margin); //to move plot to the right 0.05

2668      Cs->SetTopMargin   (top_margin);   //to use more space 0.07

2669      Cs->SetBottomMargin(bot_margin);   //default

2670      Cs->SetFrameFillColor(0);
2671      Cs->Divide(4,4);
2672 
2673      Cs->SetFillStyle(4100);
2674      Cs->SetFillColor(0);
2675 
2676     for (int u=0; u < Nchan; u++){
2677         //fit range********************************************************************

2678         PeakRange( ham[u][0], &maxpos, &Rmin, &Rmax);
2679         
2680         double iGain = 1.;
2681         if ( maxpos > 0.000001) iGain = Co::NeKa/maxpos;
2682         double par2 = ANoise[u]*iGain;  //for STA --> 78.; e2v --> 20.

2683         cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax << endl;
2684         cout << " gain estimate =" << iGain << " e-/adu" << endl;
2685         cout << " noise =" << par2 << " e-" << endl;
2686         double pKb = maxpos*Co::Eb/Co::Ea;
2687         if ( pKb > Rmax ) {
2688             Rmax = pKb + (Rmax - maxpos);
2689             cout <<  " new Rmax= " << Rmax << endl;
2690         }
2691         fprintf (pFile,"//Peak:  maxpos= %f  Rmin=%f  Rmax=%f \n", maxpos, Rmin, Rmax);
2692         fprintf (pFile,"// Channel %d estimates:  gain= %f e-/adu; noise=%f e-; rms = %f adu  \n",
2693                                    u, iGain, par2, ANoise[u]);
2694         
2695         const char * fitname = "fitGG";
2696         TF1 * fit = new TF1(fitname, Fe55::GGF, Rmin, Rmax, Npar);
2697         
2698         double maxval = ham[u][0]->GetEntries(); //ham[u][i]->GetBinContent(maxbin);

2699         if (maxval < 1.) continue;
2700         double sw = ham[u][0]->GetSumOfWeights();
2701         double bw = ham[u][0]->GetBinWidth(1);
2702         printf("\n Fit of %s \n",ham[u][0]->GetTitle());
2703         printf(" ham%i[0]: Sum of weights = %f  Entries = %f  BinW = %f\n",
2704                u, sw, maxval, bw);
2705         double par0 = maxval*0.75*bw;  //bw because of the root "feature"

2706         double par3 = maxval*0.25*bw;
2707         fit->SetParameter(0, par0);
2708         fit->SetParameter(1, maxpos);
2709         fit->SetParameter(2, par2);
2710         fit->SetParameter(3, par3);
2711         cout << " par0=" << par0 <<  endl;
2712         cout << " par1=" << maxpos << " par2=" << par2 << endl;
2713         cout << " par3=" << par3 <<  endl;
2714         cout << "fitname=" << fitname << endl;
2715         
2716         double NKa =0.;
2717         double EKa   = 0.;
2718         double Noise = 0.;
2719         double NKb   =0.;
2720         double era   = 0.;
2721         double ca = 0.;
2722         double rab = 0.;
2723         int fitflag = -100.; 
2724         
2725         fprintf (pFile,"//flag   N Ka     N Kb    Ka,adu   er(Ka) noise,e   Ca    Ra/b \n");
2726         if ( ham[u][0]->GetEntries() > 1 ) {
2727             Cs->cd(u+1);
2728             gPad->SetLogy(1); 
2729             gPad->SetGrid(1,0);
2730             gPad->SetFillColor(0);
2731             gPad->SetBorderSize(2);
2732             ham[u][0]->SetStats(0);
2733             ham[u][0]->SetTitle("");
2734             ham[u][0]->GetXaxis()->SetNdivisions(505);
2735             ham[u][0]->GetXaxis()->CenterTitle();
2736             ham[u][0]->GetXaxis()->SetTitle("Cluster Total Amplitude, a.d.u.");
2737             ham[u][0]->GetXaxis()->SetLabelSize(0.05);
2738             ham[u][0]->GetXaxis()->SetTitleSize(0.05);
2739             ham[u][0]->GetYaxis()->SetNdivisions(505);
2740             ham[u][0]->GetYaxis()->CenterTitle();
2741             ham[u][0]->GetYaxis()->SetTitle("Number of clusters");
2742             ham[u][0]->GetYaxis()->SetLabelSize(0.05);
2743             ham[u][0]->GetYaxis()->SetTitleSize(0.05);
2744             ham[u][0]->GetYaxis()->SetTitleOffset(1.3);
2745  
2746             //ham[u][0]->Draw();

2747             gPad->SetBit(kMustCleanup);
2748             gPad->GetListOfPrimitives()->Add(ham[u][0],"");
2749             gPad->Modified(kTRUE);
2750 
2751             fitflag = ham[u][0]->Fit(fitname,"LR");   //IB 

2752             cout << " FitFlag=" << fitflag << endl;
2753             NKa   = fit->GetParameter(0)/bw;
2754             EKa   = fit->GetParameter(1);
2755             Noise = fit->GetParameter(2);
2756             NKb   = fit->GetParameter(3)/bw;
2757             era   = fit->GetParError(1);
2758             ca = EKa > 0.1 ? (Co::Ea/Co::w_pair)/EKa : 0.;
2759             rab = NKa > 1  ? (double)NKb/NKa : 0.;
2760         }
2761         fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %6.2f, %6.2f, %7.4f, %5.2f,", 
2762                    fitflag,   NKa,   NKb,   EKa,   era, Noise,    ca,   rab);
2763         //fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %7.2f, %5.2f, %5.2f, %5.2f, %5.2f,", 

2764         //             fitflag,   NKa,   NKb,   EKa,   EKb, Noise,    ca,    cb,   rab);

2765         fprintf (pFile," }  // all clusters \n");
2766     }
2767 
2768     Cs->Update();
2769     Cs->Print("Fe55_all.pdf");
2770 //** End Fe55 Segments Summary  *********************************************** 

2771     
2772     rPad __f;
2773     printf("In PlotXlines\n");
2774     TCanvas *   Cl[MaxP]={0};
2775     TCanvas *   Xbin[MaxP]={0}; 
2776     TCanvas *   Ybin[MaxP]={0};
2777     TCanvas *   CTEgr[MaxP]={0};
2778 
2779     for (int u=0; u<Nchan; u++){
2780         char tiname[50];
2781         sprintf(tiname, "Fe55_%i", u);
2782         if ( u > MaxP )   break;
2783 
2784 // cluster canvas ************************************************* 

2785      Cl[u] = new TCanvas( tiname, tiname, 50+5*u, 50+5*u, 1000, 800);
2786      Cl[u]->SetBorderMode  (0);            //to remove border

2787      Cl[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

2788      Cl[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

2789      Cl[u]->SetTopMargin   (top_margin);   //to use more space 0.07

2790      Cl[u]->SetBottomMargin(bot_margin);   //default

2791      Cl[u]->SetFrameFillColor(0);
2792      Cl[u]->Divide(4,3);
2793 
2794      Cl[u]->SetFillStyle(4100);
2795      Cl[u]->SetFillColor(0);
2796 
2797 //fit range ********************************************************************

2798     PeakRange( ham[u][0], &maxpos, &Rmin, &Rmax); 
2799     
2800     double iGain = 1.;
2801     if ( maxpos > 0.000001) iGain = Co::NeKa/maxpos;
2802     double par2 = ANoise[u]*iGain;  //for STA --> 78.; e2v --> 20. 

2803     cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax << endl;
2804     cout << " gain estimate =" << iGain << " e-/adu" << endl;
2805     cout << " noise =" << par2 << " e-" << endl;
2806     double pKb = maxpos*Co::Eb/Co::Ea;
2807     if ( pKb > Rmax ) {
2808         Rmax = pKb + (Rmax - maxpos);
2809         cout <<  " new Rmax= " << Rmax << endl;
2810     }
2811     fprintf (pFile,"//Peak:  maxpos= %f  Rmin=%f  Rmax=%f \n", maxpos, Rmin, Rmax);
2812     fprintf (pFile,"//Estimates:  gain= %f  noise=%f   \n", iGain, par2);
2813 
2814     const char * fitname = "fitGG";
2815     TF1 * fit = new TF1(fitname, Fe55::GGF, Rmin, Rmax, Npar); 
2816 //*****************************************************************************

2817 
2818     fprintf (pFile,"// Channel %d Noise rms = %f, %f e- \n", u, ANoise[u], par2);
2819     fprintf (pFile,"//flag   N Ka     N Kb    Ka,adu   er(Ka) noise,e   Ca    Ra/b \n");
2820 
2821     for (int i=10; i<Nham; i++) ham[u][9]->Add(ham[u][i]);
2822 
2823     for (int i=0; i < 10; i++){
2824         double maxval = ham[u][i]->GetEntries(); //ham[u][i]->GetBinContent(maxbin);

2825         if (maxval < 1.) continue;
2826         double sw = ham[u][i]->GetSumOfWeights();
2827         double bw = ham[u][i]->GetBinWidth(1);
2828         printf("\n Fit of %s \n",ham[u][i]->GetTitle());
2829         printf(" ham%i[%i]: Sum of weights = %f  Entries = %f  BinW = %f\n", 
2830                         u, i, sw, maxval, bw);  
2831         double par0 = maxval*0.75*bw;  //bw because of the root "feature"

2832         double par3 = maxval*0.25*bw;
2833         fit->SetParameter(0, par0); 
2834         fit->SetParameter(1, maxpos);   
2835         fit->SetParameter(2, par2);
2836         fit->SetParameter(3, par3);  
2837         cout << " par0=" << par0 <<  endl;
2838         cout << " par1=" << maxpos << " par2=" << par2 << endl;
2839         cout << " par3=" << par3 <<  endl;
2840         cout << "fitname=" << fitname << endl;
2841 
2842         double NKa =0.;
2843         double EKa   = 0.;
2844         double Noise = 0.;
2845         double NKb   =0.;
2846         double era   = 0.;
2847         double ca = 0.;
2848         double rab = 0.;
2849         int fitflag = -100.; 
2850 
2851         if ( ham[u][i]->GetEntries() > 1 ) {
2852             Cl[u]->cd(i+1);
2853             gPad->SetLogy(1); 
2854             gPad->SetGrid(1,0);
2855             gPad->SetFillColor(0);
2856             gPad->SetBorderSize(2);
2857             ham[u][i]->SetStats(0);
2858             ham[u][i]->SetTitle("");
2859             ham[u][i]->GetXaxis()->SetNdivisions(505);
2860             ham[u][i]->GetXaxis()->CenterTitle();
2861             ham[u][i]->GetXaxis()->SetTitle("Cluster Total Amplitude, a.d.u.");
2862             ham[u][i]->GetXaxis()->SetLabelSize(0.05);
2863             ham[u][i]->GetXaxis()->SetTitleSize(0.05);
2864             ham[u][i]->GetYaxis()->SetNdivisions(505);
2865             ham[u][i]->GetYaxis()->CenterTitle();
2866             ham[u][i]->GetYaxis()->SetTitle("Number of clusters");
2867             ham[u][i]->GetYaxis()->SetLabelSize(0.05);
2868             ham[u][i]->GetYaxis()->SetTitleSize(0.05);
2869             ham[u][i]->GetYaxis()->SetTitleOffset(1.3);
2870  
2871             // ham[u][i]->Draw();

2872             gPad->SetBit(kMustCleanup);
2873             gPad->GetListOfPrimitives()->Add(ham[u][i],"");
2874             gPad->Modified(kTRUE);
2875 
2876             fitflag = ham[u][i]->Fit(fitname,"LR");   //IB 

2877             cout << " FitFlag=" << fitflag << endl;
2878             NKa   = fit->GetParameter(0)/bw;
2879             EKa   = fit->GetParameter(1);
2880             Noise = fit->GetParameter(2);
2881             if ( (i>1) & (i<10) ) Noise /= TMath::Sqrt((double)i);
2882             NKb   = fit->GetParameter(3)/bw;
2883             era   = fit->GetParError(1);
2884             ca = EKa > 0.1 ? (Co::Ea/Co::w_pair)/EKa : 0.;
2885             rab = NKa > 1  ? (double)NKb/NKa : 0.;
2886         }
2887         fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %6.2f, %6.2f, %7.4f, %5.2f,",
2888                    fitflag,   NKa,   NKb,   EKa,   era, Noise,    ca,   rab);
2889         //fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %7.2f, %5.2f, %5.2f, %5.2f, %5.2f,", 

2890         //             fitflag,   NKa,   NKb,   EKa,   EKb, Noise,    ca,    cb,   rab);

2891         if (i) fprintf (pFile," }  // %i pixel clusters \n", i);
2892         else fprintf (pFile," }  // all clusters \n");
2893     }
2894      
2895     if ( hNp[u]->GetEntries() > 1 ) {
2896         Cl[u]->cd(11);
2897         gPad->SetLogy(0);
2898         gPad->SetGrid(1,0);
2899         gPad->SetFillColor(0);
2900         gPad->SetBorderSize(2);
2901         //gPad->SetLogy(); 

2902         hNp[u]->SetStats(0);
2903         hNp[u]->SetTitle("");
2904         //hNp[u]->SetLineColor(4); 

2905         //hNp[u]->SetMarkerColor(4);

2906         //hNp[u]->SetMarkerStyle(21);

2907         hNp[u]->GetXaxis()->SetNdivisions(505);
2908         hNp[u]->GetXaxis()->CenterTitle();
2909         hNp[u]->GetXaxis()->SetTitle("Number of fired pixels");
2910         hNp[u]->GetXaxis()->SetLabelSize(0.05);
2911         hNp[u]->GetXaxis()->SetTitleSize(0.06);
2912         hNp[u]->GetYaxis()->SetNdivisions(505);
2913         hNp[u]->GetYaxis()->CenterTitle();
2914         hNp[u]->GetYaxis()->SetTitle("Number of clusters");
2915         hNp[u]->GetYaxis()->SetLabelSize(0.05);
2916         hNp[u]->GetYaxis()->SetTitleSize(0.06);
2917         hNp[u]->GetYaxis()->SetTitleOffset(1.7);
2918 
2919         hNp[u]->Draw();
2920     }
2921     fprintf (pFile,"\n Npix LT: entries=%f mean=%f rms=%f \n", 
2922         hNp[u]->GetEntries(), hNp[u]->GetMean(), hNp[u]->GetRMS() );
2923 
2924     //if ( hNph[u]->GetEntries() > 1 ) {

2925     //  Cl[u]->cd(8);

2926     //  gPad->SetLogy(0);

2927     //  gPad->SetGrid(1,0);

2928     //  gPad->SetFillColor(0);

2929     //  gPad->SetBorderSize(2);

2930     //  //gPad->SetLogy(); 

2931     //  hNph[u]->SetStats(0);

2932     //  hNph[u]->SetTitle("");

2933     //  //hNph[u]->SetLineColor(4); 

2934     //  //hNph[u]->SetMarkerColor(4);

2935     //  //hNph[u]->SetMarkerStyle(21);

2936     //  hNph[u]->GetXaxis()->SetNdivisions(505);

2937     //  hNph[u]->GetXaxis()->CenterTitle();

2938     //  hNph[u]->GetXaxis()->SetTitle("Number of fired pixels");

2939     //  hNph[u]->GetXaxis()->SetLabelSize(0.05);

2940     //  hNph[u]->GetXaxis()->SetTitleSize(0.06);

2941     //  hNph[u]->GetYaxis()->SetNdivisions(505);

2942     //  hNph[u]->GetYaxis()->CenterTitle();

2943     //  hNph[u]->GetYaxis()->SetTitle("Number of HT clusters");

2944     //  hNph[u]->GetYaxis()->SetLabelSize(0.05);

2945     //  hNph[u]->GetYaxis()->SetTitleSize(0.06);

2946     //  hNph[u]->GetYaxis()->SetTitleOffset(1.7);

2947 
2948     //  hNph[u]->Draw();

2949     //}

2950     fprintf (pFile,"\n Npix HT: entries=%f mean=%f rms=%f \n", 
2951         hNph[u]->GetEntries(), hNph[u]->GetMean(), hNph[u]->GetRMS() );
2952 
2953     fprintf (pFile,"\n Xstat: %f, %f, %f, %f, %f \n", 
2954         hXstat[u]->GetBinContent(1),  hXstat[u]->GetBinContent(2),
2955         hXstat[u]->GetBinContent(3),  hXstat[u]->GetBinContent(4),
2956         hXstat[u]->GetBinContent(5));
2957     printf ("\n Xstat: %f, %f, %f, %f, %f \n", 
2958         hXstat[u]->GetBinContent(1),  hXstat[u]->GetBinContent(2),
2959         hXstat[u]->GetBinContent(3),  hXstat[u]->GetBinContent(4),
2960         hXstat[u]->GetBinContent(5) );
2961 
2962     //Cl[u]->cd(9);

2963     //gPad->SetLogy(0); 

2964     //gPad->SetGrid(1,0);

2965     //hspr[u]->Draw();

2966 
2967     // Cl[u]->Modified();

2968     Cl[u]->Update();
2969     char uname[40];
2970     sprintf(uname,"Cl_Fe55_%i.pdf", u);
2971     Cl[u]->Print(uname);
2972 
2973     if ( Co::doCTE) {
2974     // CTE_X fit canvas *************************************************

2975     sprintf(tiname, "Xbin_%i", u);
2976     Xbin[u] = new TCanvas( tiname, tiname, 100+2*u, 75+2*u, 1000, 800);
2977     Xbin[u]->SetBorderMode  (0);            //to remove border

2978     Xbin[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

2979     Xbin[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

2980     Xbin[u]->SetTopMargin   (top_margin);   //to use more space 0.07

2981     Xbin[u]->SetBottomMargin(bot_margin);   //default

2982     Xbin[u]->SetFrameFillColor(0);
2983     Xbin[u]->Divide(8,4);
2984 
2985     Xbin[u]->SetFillStyle(4100);
2986     Xbin[u]->SetFillColor(0);
2987 
2988     cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax << endl;
2989 
2990     double Sa = 0.;
2991     double X  = 0.;
2992     double X2 = 0.;
2993     double Y  = 0.;
2994     double XY = 0.;
2995     fprintf (pFile,"  \n");
2996     double xptX[Ncte]={0.};
2997     double xerX[Ncte]={0.};
2998     double yptX[Ncte]={0.};
2999     double yerX[Ncte]={0.};
3000     int NptX = 0;
3001     for (int i=0; i<(Ncte-1); i++){
3002         double maxval = xcte[u][i]->GetEntries(); //xcte[i]->GetBinContent(maxbin);

3003         if (maxval < 1.) continue;
3004         double sw = xcte[u][i]->GetSumOfWeights();
3005         printf("\n Fit of %s \n",xcte[u][i]->GetTitle());
3006         printf(" xcte[%i]: Sum of weights = %f  Entries = %f \n", i, sw, maxval);   
3007         double par0 = maxval*0.75;
3008         double par3 = par0/10.;  //maxval/4.;

3009         fit->SetParameter(0, par0); 
3010         fit->SetParLimits(0,0.,par0*100.);
3011         fit->SetParameter(1, maxpos);
3012         fit->SetParLimits(1,0.,maxpos*10.);
3013         fit->SetParameter(2, par2);
3014         fit->SetParLimits(2,0.,par2*100.);
3015         fit->SetParameter(3, par3);
3016         //fit->SetParLimits(3,0.,par3*100.);

3017         cout << " par0=" << par0 <<  endl;
3018         cout << " par1=" << maxpos << " par2=" << par2 << endl;
3019         cout << " par3=" << par3 << endl;
3020 
3021         Xbin[u]->cd(1+i);
3022         gPad->SetLogy(); 
3023         gPad->SetGrid(1,0);
3024         gPad->SetFillColor(0);
3025         gPad->SetBorderSize(2);
3026         xcte[u][i]->Draw();
3027         int fitflag = xcte[u][i]->Fit("fitGG","LIBr");
3028         cout << " FitFlag=" << fitflag << endl;
3029         double NKa   = fit->GetParameter(0);
3030         double EKa   = fit->GetParameter(1);
3031         double Noise = fit->GetParameter(2);
3032         double NKb   = fit->GetParameter(3);
3033         //double EKb   = fit->GetParameter(4);

3034         double era   = fit->GetParError(1);
3035         //double erb   = fit->GetParError(4);

3036         double ca = EKa > 0.1 ? (Co::Ea/Co::w_pair)/EKa : 0.;
3037         //double cb = EKb > 0.1 ? (Co::Eb/Co::w_pair)/EKb : 0.;

3038         double rab = NKa > 1  ? (double)NKb/NKa : 0.;
3039         fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %6.2f, %6.2f, %5.2f, %5.2f,", 
3040                    fitflag,   NKa,   NKb,   EKa,   era, Noise,    ca,   rab);
3041         //fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %7.2f, %5.2f, %5.2f, %5.2f, %5.2f,", 

3042         //             fitflag,   NKa,   NKb,   EKa,   EKb, Noise,    ca,    cb,   rab);

3043         fprintf (pFile," }  // Xbin# %i %i\n", u, i);
3044         double s2 =(EKa/era)*(EKa/era); 
3045         if (fitflag == 0 ) {
3046             Sa += s2;
3047             X  += (i*NgrX+1)*s2;
3048             X2 += (i*NgrX+1)*(i*NgrX+1)*s2;
3049             Y  += log(EKa)*s2;
3050             XY += (i*NgrX+1)*log(EKa)*s2;
3051             xptX[NptX]=i*NgrX+1;
3052             yptX[NptX]=EKa;
3053             yerX[NptX]=era;
3054             NptX++;
3055         }
3056     }
3057     double detX = Sa*X2 - X*X;
3058     double aX=0.;
3059     double bX=0.;
3060     double ebX=0.;
3061     if ( TMath::Abs(detX) > 0.000001) {
3062         aX = (Y*X2 - X*XY)/detX;
3063         bX = (Sa*XY - X*Y)/detX;
3064         ebX = Sa/detX;
3065         if (ebX > 0) {ebX = sqrt(ebX);}
3066     }
3067     double Xcte_fit = exp(bX);
3068     double eCTEx = TMath::Abs(Xcte_fit)*ebX;
3069     double A0X = exp(aX)*NgrX*(1.-Xcte_fit)/(1.-TMath::Power(Xcte_fit,NgrX));
3070     fprintf (pFile," \n CTEX %12.8f +/- %12.8f  \n", Xcte_fit, eCTEx );
3071     fprintf (pFile," \n NgrX=%i NptX=%i A0=%f \n", NgrX, NptX, A0X );
3072     fprintf (pFile," \n CteXin=%f CteCorr=%f CteXnew=%f \n", CteX, Xcte_fit, CteX*Xcte_fit);
3073 
3074     Xbin[u]->Update();
3075     sprintf(uname,"Xbin_Fe55_%i.pdf", u);
3076     Xbin[u]->Print(uname);
3077     //delete Xbin[u];

3078 //

3079 // CTE_Y fit canvas ************************************************* 

3080     sprintf(tiname, "Ybin_%i", u);
3081     Ybin[u] = new TCanvas( tiname, tiname, 150+2*u, 100+2*u, 1000, 800);
3082     Ybin[u]->SetBorderMode  (0);            //to remove border

3083     Ybin[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

3084     Ybin[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

3085     Ybin[u]->SetTopMargin   (top_margin);   //to use more space 0.07

3086     Ybin[u]->SetBottomMargin(bot_margin);   //default

3087     Ybin[u]->SetFrameFillColor(0);
3088     Ybin[u]->Divide(8,4);
3089 
3090     Ybin[u]->SetFillStyle(4100);
3091     Ybin[u]->SetFillColor(0);
3092 
3093     cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax << endl;
3094 
3095     Sa = 0.;
3096     X  = 0.;
3097     X2 = 0.;
3098     Y  = 0.;
3099     XY = 0.;
3100     fprintf (pFile,"  \n");
3101     double xptY[Ncte]={0.};
3102     double xerY[Ncte]={0.};
3103     double yptY[Ncte]={0.};
3104     double yerY[Ncte]={0.};
3105     int NptY=0;
3106     for (int i=0; i<(Ncte-1); i++){
3107         double maxval = ycte[u][i]->GetEntries(); //ycte[u][i]->GetBinContent(maxbin);

3108         if (maxval < 1.) continue;
3109         double sw = ycte[u][i]->GetSumOfWeights();
3110         printf("\n Fit of %s \n",ycte[u][i]->GetTitle());
3111         printf(" ycte[%i]: Sum of weights = %f  Entries = %f \n", i, sw, maxval);   
3112         double par0 = maxval*0.75;
3113         double par3 = par0/10.;  //maxval/4.;

3114         fit->SetParameter(0, par0);
3115         fit->SetParLimits(0, 0.,par0*100.);
3116         fit->SetParameter(1, maxpos);
3117         fit->SetParLimits(1, 0.,maxpos*10.);
3118         fit->SetParameter(2, par2);
3119         fit->SetParLimits(2, 0.,par2*100.);
3120         fit->SetParameter(3, par3);
3121         //fit->SetParLimits(3, 0.,par3*100.);

3122         cout << " par0=" << par0 <<  endl;
3123         cout << " par1=" << maxpos << " par2=" << par2 << endl;
3124         cout << " par3=" << par3 << endl;
3125 
3126         Ybin[u]->cd(1+i);
3127         gPad->SetLogy(); 
3128         gPad->SetGrid(1,0);
3129         gPad->SetFillColor(0);
3130         gPad->SetBorderSize(2);
3131         ycte[u][i]->Draw();
3132         int fitflag = ycte[u][i]->Fit("fitGG","LIBr");
3133         cout << " FitFlag=" << fitflag << endl;
3134         double NKa   = fit->GetParameter(0);
3135         double EKa   = fit->GetParameter(1);
3136         double Noise = fit->GetParameter(2);
3137         double NKb   = fit->GetParameter(3);
3138         //double EKb   = fit->GetParameter(4);

3139         double era   = fit->GetParError(1);
3140         //double erb   = fit->GetParError(4);

3141         double ca = EKa > 0.1 ? (Co::Ea/Co::w_pair)/EKa : 0.;
3142         //double cb = EKb > 0.1 ? (Co::/Co::w_pair)/EKb : 0.;

3143         double rab = NKa > 1  ? (double)NKb/NKa : 0.;
3144         fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %6.2f, %6.2f, %5.2f, %5.2f,", 
3145                    fitflag,   NKa,   NKb,   EKa,   era, Noise,    ca,   rab);
3146         //fprintf (pFile," {  %i, %7.1f, %7.1f, %7.2f, %7.2f, %5.2f, %5.2f, %5.2f, %5.2f,", 

3147         //             fitflag,   NKa,   NKb,   EKa,   EKb, Noise,    ca,    cb,   rab);

3148         fprintf (pFile," }  // Xbin# %i \n", i);
3149         double s2 =(EKa/era)*(EKa/era); 
3150         if (fitflag == 0 ) {
3151             Sa += s2;
3152             X  += (i*NgrY+1)*s2;
3153             X2 += (i*NgrY+1)*(i*NgrY+1)*s2;
3154             Y  += log(EKa)*s2;
3155             XY += (i*NgrY+1)*log(EKa)*s2;
3156             xptY[NptY]=i*NgrY+1;
3157             yptY[NptY]=EKa;
3158             yerY[NptY]=era;
3159             NptY++;
3160         }
3161     }
3162     double detY = Sa*X2 - X*X;
3163     double aY=0.;
3164     double bY=0.;
3165     double ebY=0.;
3166     if ( TMath::Abs(detY) > 0.000001) {
3167         aY = (Y*X2 - X*XY)/detY;
3168         bY = (Sa*XY - X*Y)/detY;
3169         ebY = Sa/detY;
3170         if (ebY > 0.) {ebY =sqrt(ebY);}
3171     }
3172     double Ycte_fit = exp(bY);
3173     double eCTEy = TMath::Abs(Ycte_fit)*ebY;
3174     double A0Y = exp(aY)*NgrY*(1.-Ycte_fit)/(1.-TMath::Power(Ycte_fit,NgrY));
3175     fprintf (pFile," \n CTEY %12.8f +/- %12.8f  \n", Ycte_fit, eCTEy );
3176     fprintf (pFile," \n NgrY=%i NptY=%i A0y=%f \n", NgrY, NptX, A0Y );
3177     fprintf (pFile," \n CteYin=%f CteCorr=%f CteYnew=%f \n", CteY, Ycte_fit, CteY*Ycte_fit);
3178 
3179     delete Ybin[u];
3180     //Ybin[u]->Update();

3181     //sprintf(uname,"Ybin_Fe55_%i.pdf", u);

3182     //Ybin[u]->Print(uname);

3183 
3184 // CTE Graph canvas ************************************************* 

3185     sprintf(tiname, "CTE_%i", u);
3186     CTEgr[u] = new TCanvas( tiname, tiname, 110+2*u, 80+2*u, 1000, 800);
3187     CTEgr[u]->SetBorderMode  (0);            //to remove border

3188     CTEgr[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

3189     CTEgr[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

3190     CTEgr[u]->SetTopMargin   (top_margin);   //to use more space 0.07

3191     CTEgr[u]->SetBottomMargin(bot_margin);   //default

3192     CTEgr[u]->SetFrameFillColor(0);
3193     CTEgr[u]->Divide(2,2);
3194 
3195     CTEgr[u]->SetFillStyle(4100);
3196     CTEgr[u]->SetFillColor(0);
3197     gStyle->SetPalette(1);
3198 
3199     CTEgr[u]->cd(1);
3200     gPad->SetGrid(1,0);
3201     gPad->SetGridy();
3202     gPad->SetFillColor(0);
3203     gPad->SetBorderSize(2);
3204     Xcte[u]->GetXaxis()->SetNdivisions(505);
3205     Xcte[u]->GetXaxis()->CenterTitle();
3206     Xcte[u]->GetXaxis()->SetTitle("Column number");
3207     Xcte[u]->GetXaxis()->SetLabelSize(0.04);
3208     Xcte[u]->GetXaxis()->SetTitleSize(0.04);
3209     Xcte[u]->GetYaxis()->SetNdivisions(505);
3210     Xcte[u]->GetYaxis()->CenterTitle();
3211     Xcte[u]->GetYaxis()->SetTitle("X-ray signal, a.d.u.");
3212     Xcte[u]->GetYaxis()->SetLabelSize(0.04);
3213     Xcte[u]->GetYaxis()->SetTitleSize(0.04);
3214     Xcte[u]->GetYaxis()->SetTitleOffset(1.4);
3215     Xcte[u]->Draw("colz");
3216 
3217     CTEgr[u]->cd(3);
3218     gPad->SetFillColor(0);
3219     gPad->SetBorderSize(2);
3220     TGraph * Xgr = new TGraphErrors(NptX, xptX, yptX, xerX, yerX);  // Ncte-1

3221     Xgr->SetTitle("serial transfer graph"); 
3222     Xgr->SetMarkerColor(4);
3223     Xgr->SetMarkerStyle(21);
3224     Xgr->GetXaxis()->SetNdivisions(505);
3225     Xgr->GetXaxis()->CenterTitle();
3226     Xgr->GetXaxis()->SetTitle("Column number");
3227     Xgr->GetXaxis()->SetLabelSize(0.04);
3228     Xgr->GetXaxis()->SetTitleSize(0.04);
3229     Xgr->GetYaxis()->SetNdivisions(505);
3230     Xgr->GetYaxis()->CenterTitle();
3231     Xgr->GetYaxis()->SetTitle("K_{#alpha} signal, a.d.u.");
3232     Xgr->GetYaxis()->SetLabelSize(0.04);
3233     Xgr->GetYaxis()->SetTitleSize(0.04);
3234     Xgr->GetYaxis()->SetTitleOffset(1.4);
3235     Xgr->Draw("ALP");
3236 //fit X curve

3237     double xmin = 1.;       
3238     TF1 * fitX = new TF1("fitX","exp([0]+[1]*x)", xmin, XmaxSearch);
3239     fitX->SetParameter(0, aX);
3240     fitX->SetParameter(1, bX);
3241     fitX->SetLineWidth(2);
3242     fitX->Draw("SAME");
3243 
3244     CTEgr[u]->cd(2);
3245     gPad->SetGrid(1,0);
3246     gPad->SetGridy();
3247     gPad->SetFillColor(0);
3248     gPad->SetBorderSize(2);
3249     Ycte[u]->GetXaxis()->SetNdivisions(505);
3250     Ycte[u]->GetXaxis()->CenterTitle();
3251     Ycte[u]->GetXaxis()->SetTitle("Row number");
3252     Ycte[u]->GetXaxis()->SetLabelSize(0.04);
3253     Ycte[u]->GetXaxis()->SetTitleSize(0.04);
3254     Ycte[u]->GetYaxis()->SetNdivisions(505);
3255     Ycte[u]->GetYaxis()->CenterTitle();
3256     Ycte[u]->GetYaxis()->SetTitle("X-ray signal, a.d.u.");
3257     Ycte[u]->GetYaxis()->SetLabelSize(0.04);
3258     Ycte[u]->GetYaxis()->SetTitleSize(0.04);
3259     Ycte[u]->GetYaxis()->SetTitleOffset(1.4);
3260     Ycte[u]->Draw("colz");
3261 
3262     CTEgr[u]->cd(4);
3263     gPad->SetFillColor(0);
3264     gPad->SetBorderSize(2);
3265     TGraph * Ygr = new TGraphErrors(NptY, xptY, yptY, xerY, yerY);
3266     Ygr->SetMarkerColor(4);
3267     Ygr->SetMarkerStyle(21);
3268     Ygr->SetTitle("parallel transfer graph"); 
3269     Ygr->GetXaxis()->SetNdivisions(505);
3270     Ygr->GetXaxis()->CenterTitle();
3271     Ygr->GetXaxis()->SetTitle("Row number");
3272     Ygr->GetXaxis()->SetLabelSize(0.04);
3273     Ygr->GetXaxis()->SetTitleSize(0.04);
3274     Ygr->GetYaxis()->SetNdivisions(505);
3275     Ygr->GetYaxis()->CenterTitle();
3276     Ygr->GetYaxis()->SetTitle("K_{#alpha} signal, a.d.u.");
3277     Ygr->GetYaxis()->SetLabelSize(0.04);
3278     Ygr->GetYaxis()->SetTitleSize(0.04);
3279     Ygr->GetYaxis()->SetTitleOffset(1.4);
3280     Ygr->Draw("ALP");
3281 //fit Y curve

3282     TF1 * fitY = new TF1("fitY","exp([0]+[1]*x)", xmin, YmaxSearch);
3283     fitY->SetParameter(0, aY);
3284     fitY->SetParameter(1, bY);
3285     fitY->SetLineWidth(2);
3286     fitY->Draw("SAME");
3287 
3288     CTEgr[u]->Update();
3289 //  CTEgr[u]->Print("CTEgr.pdf");

3290 ////*****************************************************************************

3291 }
3292 //  End: printf( "Jumped to End. \n" );

3293 
3294 } //end channel loop

3295 
3296     fclose( pFile );
3297     
3298     return;
3299 } //end PlotXlines function

3300 
3301 
3302 //*********************************************************************

3303 void Fe55::PlotXfit( void )
3304 {
3305     double maxpos;
3306     double Rmin;
3307     double Rmax;
3308     
3309 //    double NKa =0.;

3310 //    double EKa   = 0.;

3311 //    double Noise = 0.;

3312 //    double NKb   =0.;

3313 //    double era   = 0.;

3314 //    double ca = 0.;

3315 //    double rab = 0.;

3316     int fitflag = -100.;
3317     
3318     rPad __f;
3319     printf("In PlotXfit\n");
3320     // Xfit Segments Summary  *************************************************

3321     TCanvas * Cx = new TCanvas( "Xfit", "Xfit", 145, 5, 1000, 800);
3322     Cx->SetBorderMode  (0);            //to remove border

3323     Cx->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

3324     Cx->SetRightMargin (right_margin); //to move plot to the right 0.05

3325     Cx->SetTopMargin   (top_margin);   //to use more space 0.07

3326     Cx->SetBottomMargin(bot_margin);   //default

3327     Cx->SetFrameFillColor(0);
3328     Cx->Divide(6,3);
3329     
3330     Cx->SetFillStyle(4100);
3331     Cx->SetFillColor(0);
3332     
3333     for (int u=0; u <16 ; u++){  //Nchan

3334         hSig->Add(hXf[u][4]);
3335         if ( hXf[u][4]->GetEntries() > 1 ) {
3336             Cx->cd(u+1);
3337             //gPad->SetLogy();

3338             gPad->SetGrid(1,0);
3339             gPad->SetFillColor(0);
3340             gPad->SetBorderSize(0);
3341             hXf[u][4]->SetStats(0);
3342             hXf[u][4]->SetTitle("");
3343             //hXf[u][4]->SetLineColor(4);

3344             //hXf[u][4]->SetMarkerColor(4);

3345             //hXf[u][4]->SetMarkerStyle(21);

3346             //hXf[u][4]->GetXaxis()->SetNdivisions(505);

3347             hXf[u][4]->GetXaxis()->SetTitle("#sigma, #mum");
3348             hXf[u][4]->GetXaxis()->SetLabelSize(0.05);
3349             hXf[u][4]->GetXaxis()->SetTitleSize(0.05);
3350             hXf[u][4]->GetYaxis()->SetNdivisions(505);
3351             hXf[u][4]->GetYaxis()->SetTitle("Number of clusters");
3352             hXf[u][4]->GetYaxis()->SetLabelSize(0.05);
3353             hXf[u][4]->GetYaxis()->SetTitleSize(0.05);
3354             hXf[u][4]->GetYaxis()->SetTitleOffset(1.3);
3355             
3356             hXf[u][4]->Draw();
3357             
3358             //gPad->SetBit(kMustCleanup);

3359             //gPad->GetListOfPrimitives()->Add(ham[u][0],"");

3360             //gPad->Modified(kTRUE);

3361             
3362         } //entries

3363     }  //channels

3364     
3365     Cx->cd(17);
3366     //gPad->SetLogy();

3367     gPad->SetGrid(1,0);
3368     gPad->SetFillColor(0);
3369     gPad->SetBorderSize(0);
3370     hSig->SetStats(0);
3371     hSig->SetTitle("");
3372     //hSig->SetLineColor(4);

3373     //hSig->SetMarkerColor(4);

3374     //hSig->SetMarkerStyle(21);

3375     //hSig->GetXaxis()->SetNdivisions(505);

3376     hSig->GetXaxis()->SetTitle("#sigma, #mum");
3377     hSig->GetXaxis()->SetLabelSize(0.05);
3378     hSig->GetXaxis()->SetTitleSize(0.05);
3379     hSig->GetYaxis()->SetNdivisions(505);
3380     hSig->GetYaxis()->SetTitle("Number of clusters");
3381     hSig->GetYaxis()->SetLabelSize(0.05);
3382     hSig->GetYaxis()->SetTitleSize(0.05);
3383     hSig->GetYaxis()->SetTitleOffset(1.3);
3384     
3385     hSig->Draw();
3386     
3387     Cx->Update();
3388     Cx->Print("Xfit_all.pdf");
3389     
3390     // individual segments ***********************************************

3391     char tiname[50];
3392     TCanvas *   Cfit[MaxP]={0};
3393     
3394     for (int u=0; u<Nchan; u++){
3395         sprintf(tiname, "Fit %i", u);
3396         if ( u > MaxP )   break;
3397         sprintf(tiname, "Xfit_%i", u);
3398         
3399         // X-ray cluster fit canvas ******************************************

3400         Cfit[u] = new TCanvas( tiname, tiname, 50+2*u, 10+2*u, 900, 800);
3401         Cfit[u]->SetBorderMode  (0);      //to remove border

3402         Cfit[u]->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3403         Cfit[u]->SetRightMargin (right_margin);   //to move plot to the right 0.05

3404         Cfit[u]->SetTopMargin   (top_margin);   //to use more space 0.07

3405         Cfit[u]->SetBottomMargin(bot_margin);   //default

3406         Cfit[u]->SetFrameFillColor(0);
3407         Cfit[u]->Divide(2,3);
3408         Cfit[u]->Update();
3409         
3410         Cfit[u]->SetFillStyle(4100);
3411         Cfit[u]->SetFillColor(0);
3412         gStyle->SetOptFit();
3413         
3414         Cfit[u]->cd(1);
3415         gPad->SetFillColor(0);
3416         gPad->SetBorderSize(2);
3417         //  if ( hXf[u][0]->GetEntries() > 1 ) {

3418         //      //gPad->SetLogy();

3419         //      hXf[u][0]->SetStats(0);

3420         //      hXf[u][0]->SetTitle("");

3421         //      //hXf[u][0]->SetLineColor(4);

3422         //      //hXf[u][0]->SetMarkerColor(4);

3423         //      //hXf[u][0]->SetMarkerStyle(21);

3424         //      hXf[u][0]->GetXaxis()->SetNdivisions(505);

3425         //      hXf[u][0]->GetXaxis()->SetTitle("#chi^{2}");

3426         //      hXf[u][0]->GetXaxis()->SetLabelSize(0.05);

3427         //      hXf[u][0]->GetXaxis()->SetTitleSize(0.05);

3428         //      hXf[u][0]->GetYaxis()->SetNdivisions(505);

3429         //      hXf[u][0]->GetYaxis()->SetTitle("Number of clusters");

3430         //      hXf[u][0]->GetYaxis()->SetLabelSize(0.05);

3431         //      hXf[u][0]->GetYaxis()->SetTitleSize(0.05);

3432         //      hXf[u][0]->GetYaxis()->SetTitleOffset(1.3);

3433         //

3434         //      hXf[u][0]->Draw();

3435         //  }

3436         
3437         if ( hXf[u][6]->GetEntries() > 1 ) {
3438             //gPad->SetLogy();

3439             hXf[u][6]->SetStats(0);
3440             hXf[u][6]->SetTitle("");
3441             //hXf[u][6]->SetLineColor(4);

3442             //hXf[u][6]->SetMarkerColor(4);

3443             //hXf[u][6]->SetMarkerStyle(21);

3444             hXf[u][6]->GetXaxis()->SetNdivisions(505);
3445             hXf[u][6]->GetXaxis()->SetTitle("#chi^{2}R");
3446             hXf[u][6]->GetXaxis()->SetLabelSize(0.05);
3447             hXf[u][6]->GetXaxis()->SetTitleSize(0.05);
3448             hXf[u][6]->GetYaxis()->SetNdivisions(505);
3449             hXf[u][6]->GetYaxis()->SetTitle("Number of clusters");
3450             hXf[u][6]->GetYaxis()->SetLabelSize(0.05);
3451             hXf[u][6]->GetYaxis()->SetTitleSize(0.05);
3452             hXf[u][6]->GetYaxis()->SetTitleOffset(1.3);
3453             
3454             hXf[u][6]->Draw();
3455         }
3456         
3457         Cfit[u]->cd(2);
3458         gPad->SetFillColor(0);
3459         gPad->SetBorderSize(2);
3460         if ( hXf[u][1]->GetEntries() > 1 ) {
3461             //gPad->SetLogy();

3462             hXf[u][1]->SetStats(0);
3463             hXf[u][1]->SetTitle("");
3464             //hXf[u][1]->SetLineColor(4);

3465             //hXf[u][1]->SetMarkerColor(4);

3466             //hXf[u][1]->SetMarkerStyle(21);

3467             hXf[u][1]->GetXaxis()->SetNdivisions(505);
3468             hXf[u][1]->GetXaxis()->SetTitle("Fitted cluster amplitude, a.d.u.");
3469             hXf[u][1]->GetXaxis()->SetLabelSize(0.05);
3470             hXf[u][1]->GetXaxis()->SetTitleSize(0.05);
3471             hXf[u][1]->GetYaxis()->SetNdivisions(505);
3472             hXf[u][1]->GetYaxis()->SetTitle("Number of clusters");
3473             hXf[u][1]->GetYaxis()->SetLabelSize(0.05);
3474             hXf[u][1]->GetYaxis()->SetTitleSize(0.05);
3475             hXf[u][1]->GetYaxis()->SetTitleOffset(1.3);
3476             
3477             hXf[u][1]->Draw();
3478             
3479             //fit *************************************************************************

3480             PeakRange( hXf[u][1], &maxpos, &Rmin, &Rmax);
3481             
3482             double iGain = 1.;
3483             if ( maxpos > 0.000001) iGain = Co::NeKa/maxpos;
3484             double par2 = ANoise[u]*iGain;  //for STA --> 78.; e2v --> 20.

3485             cout << " maxpos=" << maxpos << " Rmin=" << Rmin << " Rmax" << Rmax << endl;
3486             cout << " gain estimate =" << iGain << " e-/adu" << endl;
3487             cout << " noise =" << par2 << " e-" << endl;
3488             double pKb = maxpos*Co::Eb/Co::Ea;
3489             if ( pKb > Rmax ) {
3490                 Rmax = pKb + (Rmax - maxpos);
3491                 cout <<  " new Rmax= " << Rmax << endl;
3492             }
3493             //fprintf (pFile,"//Peak:  maxpos= %f  Rmin=%f  Rmax=%f \n", maxpos, Rmin, Rmax);

3494             //fprintf (pFile,"//Estimates:  gain= %f  noise=%f   \n", iGain, par2);

3495             
3496             const char * fitname = "fitGG";
3497             TF1 * fit = new TF1(fitname, Fe55::GGF, Rmin, Rmax, Npar);
3498             
3499             //fprintf (pFile,"// Channel %d Noise rms = %f, %f e- \n", u, ANoise[u], par2);

3500             //fprintf (pFile,"//flag   N Ka     N Kb    Ka,adu   er(Ka) noise,e   Ca    Ra/b \n");

3501             
3502             
3503             double maxval = hXf[u][1]->GetEntries(); //ham[u][i]->GetBinContent(maxbin);

3504             if (maxval < 1.) continue;
3505             double sw = hXf[u][1]->GetSumOfWeights();
3506             double bw = hXf[u][1]->GetBinWidth(1);
3507             printf("\n Fit of %s \n",hXf[u][0]->GetTitle());
3508             printf(" hXf%i[0]: Sum of weights = %f  Entries = %f  BinW = %f\n",
3509                    u, sw, maxval, bw);
3510             double par0 = maxval*0.75*bw;  //bw because of the root "feature"

3511             double par3 = maxval*0.25*bw;
3512             fit->SetParameter(0, par0);
3513             fit->SetParameter(1, maxpos);
3514             fit->SetParameter(2, par2);
3515             fit->SetParameter(3, par3);
3516             cout << " par0=" << par0 <<  endl;
3517             cout << " par1=" << maxpos << " par2=" << par2 << endl;
3518             cout << " par3=" << par3 <<  endl;
3519             cout << "fitname=" << fitname << endl;
3520             
3521             fitflag = hXf[u][1]->Fit(fitname,"LR");   //IB

3522             cout << " FitFlag=" << fitflag << endl;
3523 //            NKa   = fit->GetParameter(0)/bw;

3524 //            EKa   = fit->GetParameter(1);

3525 //            Noise = fit->GetParameter(2);

3526 //            NKb   = fit->GetParameter(3)/bw;

3527 //            era   = fit->GetParError(1);

3528 //            ca = EKa > 0.1 ? (Co::Ea/Co::w_pair)/EKa : 0.;

3529 //            rab = NKa > 1  ? (double)NKb/NKa : 0.;

3530             
3531         }
3532         
3533         Cfit[u]->cd(3);
3534         gPad->SetFillColor(0);
3535         gPad->SetBorderSize(2);
3536         if ( hXf[u][2]->GetEntries() > 1 ) {
3537             //gPad->SetLogy();

3538             hXf[u][2]->SetStats(0);
3539             hXf[u][2]->SetTitle("");
3540             //hXf[u][2]->SetLineColor(4);

3541             //hXf[u][2]->SetMarkerColor(4);

3542             //hXf[u][2]->SetMarkerStyle(21);

3543             hXf[u][2]->GetXaxis()->SetTitle("X coordinate, sub pixel");
3544             hXf[u][2]->GetXaxis()->SetLabelSize(0.05);
3545             hXf[u][2]->GetXaxis()->SetTitleSize(0.05);
3546             hXf[u][2]->GetYaxis()->SetNdivisions(505);
3547             hXf[u][2]->GetYaxis()->SetTitle("Number of clusters");
3548             hXf[u][2]->GetYaxis()->SetLabelSize(0.05);
3549             hXf[u][2]->GetYaxis()->SetTitleSize(0.05);
3550             hXf[u][2]->GetYaxis()->SetTitleOffset(1.3);
3551             
3552             hXf[u][2]->Draw();
3553         }
3554         
3555         Cfit[u]->cd(4);
3556         gPad->SetFillColor(0);
3557         gPad->SetBorderSize(2);
3558         if ( hXf[u][3]->GetEntries() > 1 ) {
3559             //gPad->SetLogy();

3560             hXf[u][3]->SetStats(0);
3561             hXf[u][3]->SetTitle("");
3562             //hXf[u][3]->SetLineColor(4);

3563             //hXf[u][3]->SetMarkerColor(4);

3564             //hXf[u][3]->SetMarkerStyle(21);

3565             hXf[u][3]->GetXaxis()->SetTitle("Y coordinate, sub pixel");
3566             hXf[u][3]->GetXaxis()->SetLabelSize(0.05);
3567             hXf[u][3]->GetXaxis()->SetTitleSize(0.05);
3568             hXf[u][3]->GetYaxis()->SetNdivisions(505);
3569             hXf[u][3]->GetYaxis()->SetTitle("Number of clusters");
3570             hXf[u][3]->GetYaxis()->SetLabelSize(0.05);
3571             hXf[u][3]->GetYaxis()->SetTitleSize(0.05);
3572             hXf[u][3]->GetYaxis()->SetTitleOffset(1.3);
3573             
3574             hXf[u][3]->Draw();
3575         }
3576         
3577         Cfit[u]->cd(5);
3578         gPad->SetFillColor(0);
3579         gPad->SetBorderSize(2);
3580         if ( hXf[u][4]->GetEntries() > 1 ) {
3581             //gPad->SetLogy(); 

3582             hXf[u][4]->SetStats(0);
3583             hXf[u][4]->SetTitle("");
3584             //hXf[u][4]->SetLineColor(4); 

3585             //hXf[u][4]->SetMarkerColor(4);

3586             //hXf[u][4]->SetMarkerStyle(21);

3587             hXf[u][4]->GetXaxis()->SetTitle("#sigma, #mum");
3588             hXf[u][4]->GetXaxis()->SetLabelSize(0.05);
3589             hXf[u][4]->GetXaxis()->SetTitleSize(0.05);
3590             hXf[u][4]->GetYaxis()->SetNdivisions(505);
3591             hXf[u][4]->GetYaxis()->SetTitle("Number of clusters");
3592             hXf[u][4]->GetYaxis()->SetLabelSize(0.05);
3593             hXf[u][4]->GetYaxis()->SetTitleSize(0.05);
3594             hXf[u][4]->GetYaxis()->SetTitleOffset(1.3);
3595             
3596             hXf[u][4]->Draw();
3597         }
3598         
3599         Cfit[u]->cd(6);
3600         if ( hXf[u][5]->GetEntries() > 1 ) {
3601             //gPad->SetLogy(); 

3602             hXf[u][5]->SetStats(0);
3603             hXf[u][5]->SetTitle("");
3604             //hXf[u][5]->SetLineColor(4); 

3605             //hXf[u][5]->SetMarkerColor(4);

3606             //hXf[u][5]->SetMarkerStyle(21);

3607             hXf[u][5]->GetXaxis()->SetTitle("#sigma error, #mum");
3608             hXf[u][5]->GetXaxis()->SetLabelSize(0.05);
3609             hXf[u][5]->GetXaxis()->SetTitleSize(0.05);
3610             hXf[u][5]->GetYaxis()->SetNdivisions(505);
3611             hXf[u][5]->GetYaxis()->SetTitle("Number of clusters");
3612             hXf[u][5]->GetYaxis()->SetLabelSize(0.05);
3613             hXf[u][5]->GetYaxis()->SetTitleSize(0.05);
3614             hXf[u][5]->GetYaxis()->SetTitleOffset(1.3);
3615             hXf[u][5]->Draw();
3616         }
3617         
3618         Cfit[u]->Update();
3619         //Cfit->Print("Fe55_Xfit.pdf");

3620     }
3621     
3622     return;
3623 } //end PlotXfit function

3624 
3625 void Fe55::PlotCTE()
3626 {
3627      rPad __f;
3628 
3629 // XCTE canvas ************************************************* 

3630      TCanvas    *   CteXc = new TCanvas( "CteXc", "CteXc", 150, 150, 800, 800);
3631      CteXc->SetBorderMode  (0);      //to remove border

3632      CteXc->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3633      CteXc->SetRightMargin (right_margin);   //to move plot to the right 0.05

3634      CteXc->SetTopMargin   (top_margin);   //to use more space 0.07

3635      CteXc->SetBottomMargin(bot_margin);   //default

3636      CteXc->SetFrameFillColor(0);
3637      CteXc->Divide(4,4);
3638 
3639      gStyle->SetPalette(1);
3640      CteXc->SetFillStyle(4100);
3641      CteXc->SetFillColor(0);
3642 
3643 for (int u=0; u<Nchan; u++){
3644     if ( u > MaxP )   break;
3645      CteXc->cd(u+1);
3646      gPad->SetGrid(1,0);
3647      gPad->SetGridy();
3648      gPad->SetFillColor(0);
3649      gPad->SetBorderSize(2);
3650      Xcte[u]->Draw("colz");
3651 }
3652      CteXc->Update();
3653      CteXc->Print("Fe55_CteXc.pdf");
3654 
3655 // YCTE canvas ************************************************* 

3656      TCanvas    *   CteYc = new TCanvas( "CteYc", "CteYc", 200, 200, 900, 900);
3657      CteYc->SetBorderMode  (0);      //to remove border

3658      CteYc->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3659      CteYc->SetRightMargin (right_margin);   //to move plot to the right 0.05

3660      CteYc->SetTopMargin   (top_margin);   //to use more space 0.07

3661      CteYc->SetBottomMargin(bot_margin);   //default

3662      CteYc->SetFrameFillColor(0);
3663      CteYc->Divide(4,4);
3664 
3665      gStyle->SetPalette(1);
3666      CteYc->SetFillStyle(4100);
3667      CteYc->SetFillColor(0);
3668 
3669 for (int u=0; u<Nchan; u++){
3670     if ( u > MaxP )   break;
3671      CteYc->cd(u+1);
3672      gPad->SetGrid(1,0);
3673      gPad->SetGridy();
3674      gPad->SetFillColor(0);
3675      gPad->SetBorderSize(2);
3676      Ycte[u]->Draw("colz");
3677 }
3678      CteYc->Modified();
3679      CteYc->Update();
3680      CteYc->Print("Fe55_CteYc.pdf");
3681 
3682 // Clusters coordinates canvas ************************************************* 

3683      TCanvas    *   CQ = new TCanvas( "CQ", "CQ", 210, 210, 900, 900);
3684      CQ->SetBorderMode  (0);      //to remove border

3685      CQ->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3686      CQ->SetRightMargin (right_margin);   //to move plot to the right 0.05

3687      CQ->SetTopMargin   (top_margin);   //to use more space 0.07

3688      CQ->SetBottomMargin(bot_margin);   //default

3689      CQ->SetFrameFillColor(0);
3690      CQ->Divide(4,4);
3691 
3692      gStyle->SetPalette(1);
3693      CQ->SetFillStyle(4100);
3694      CQ->SetFillColor(0);
3695 
3696 for (int u=0; u<Nchan; u++){
3697     if ( u > MaxP )   break;
3698      CQ->cd(u+1);
3699      gPad->SetGrid(1,0);
3700      gPad->SetGridy();
3701      gPad->SetFillColor(0);
3702      gPad->SetBorderSize(2);
3703      ClustQ[u]->Draw("colz");
3704 }
3705      CQ->Modified();
3706      CQ->Update();
3707      CQ->Print("Fe55_Q.pdf");
3708 
3709 } //end PlotCTE function

3710 
3711 void Fe55::PlotProfiles()
3712 {
3713     rPad __f;
3714     printf("In PlotProfiles\n");
3715     TCanvas *   Prof[MaxP]={0};
3716     //TCanvas   *   Xbin[MaxP]={0};

3717     //TCanvas   *   Ybin[MaxP]={0};

3718     
3719     for (int u=0; u<Nchan; u++){
3720         char tiname[50];
3721         sprintf(tiname, "Profile_%i", u);
3722         if ( u > MaxP )   break;
3723         
3724         // cluster canvas *************************************************

3725         Prof[u] = new TCanvas( tiname, tiname, 150+5*u, 150+5*u, 1000, 800);
3726         Prof[u]->SetBorderMode  (0);            //to remove border

3727         Prof[u]->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

3728         Prof[u]->SetRightMargin (right_margin); //to move plot to the right 0.05

3729         Prof[u]->SetTopMargin   (top_margin);   //to use more space 0.07

3730         Prof[u]->SetBottomMargin(bot_margin);   //default

3731         Prof[u]->SetFrameFillColor(0);
3732         Prof[u]->Divide(Co::NXpix,Co::NYpix);
3733         
3734         Prof[u]->SetFillStyle(4100);
3735         Prof[u]->SetFillColor(0);
3736         
3737         for (int x=0; x<Co::NXpix;x++){
3738             for (int y=0; y<Co::NYpix; y++){
3739                 Prof[u]->cd(y*Co::NXpix+x+1);
3740                 hpixA[u][x][y]->Draw();
3741             }
3742         }
3743 
3744         Prof[u]->Update();
3745         sprintf(tiname,"Profile_%i.pdf", u);
3746         Prof[u]->Print(tiname);
3747 
3748     }
3749 
3750 } //

3751 
3752 void Fe55::PlotHits()
3753 {
3754     rPad __f;
3755     printf("In PlotHits\n");
3756     // cluster canvas *************************************************

3757     TCanvas * cHits = new TCanvas("Hits", "Hits", 400,20,800,750);
3758     cHits->SetBorderMode  (0);            //to remove border

3759     cHits->SetLeftMargin  (left_margin);  //to move plot to the right 0.14

3760     cHits->SetRightMargin (right_margin); //to move plot to the right 0.05

3761     cHits->SetTopMargin   (top_margin);   //to use more space 0.07

3762     cHits->SetBottomMargin(bot_margin);   //default

3763     cHits->SetFrameFillColor(0);
3764     cHits->Divide(1,Nchan);
3765     
3766     cHits->SetFillStyle(4100);
3767     cHits->SetFillColor(0);
3768         
3769     for (int u=0; u<Nchan; u++){
3770         if ( u > MaxP )   break;
3771         cHits->cd(u+1);
3772         if ( hitSumA[u]->GetEntries() > 1 ) {
3773             hitSumA[u]->Draw();
3774         }
3775     }
3776     
3777     cHits->Update();
3778     cHits->Print("Hits.pdf");
3779     
3780 } // end PlotHits

3781 
3782 int Fe55_main ( const char * dir,
3783         const char * outName )  //= "Fe_50V" )

3784 {
3785 // clean up previous histograms

3786     gDirectory->GetList()->Delete();
3787 
3788 // ********************************************************************

3789     for (int i=0; KeyList[i]; i++) {printf(" %i.%s \n", i, KeyList[i]);}
3790     for (int i=0; i<Nkey; i++) Vval[i].erase( Vval[i].begin(), Vval[i].end() );
3791 
3792 //input directory +++++++++++++++++++++++++++++++++++++++++++++++++++++

3793     string dir_name = dir; // = "C:/DATA/Fe55/20080122-161251/";

3794     //dir_name += outName;

3795 
3796 // Bias files analysis ************************************************

3797 
3798     string  bias_dir = dir_name;
3799     bias_dir += "/bias";
3800     
3801     int doFFt=0;
3802 //  Bias B( bias_dir, outName, doFFt );

3803 //  if (!B.Flag) {

3804 //      B.Plot();

3805 //      if (doFFt) { B.Plot_FFT(); }

3806 //      //B.PrintVal();

3807 //  }

3808     //else {if (B.Flag > -10) return -1;} //flag=-10 if there is no bias data

3809 
3810 // ********************************************************************

3811 // X-ray simulations   ************************************************

3812 
3813 //  SimX SimFe55( bias_dir );

3814 //  SimFe55.PlotSim();

3815 //  return 0;

3816 // ********************************************************************

3817 // Fe55 files analysis ************************************************

3818     
3819     //dir_name += "/Sim_375V2";

3820 
3821     doFFt=0;
3822 //  Fe55 Fe( dir_name, outName, B.bias_rms, B.trbuf, doFFt  );  //for BaLiC

3823     Fe55 Fe( dir_name, outName, 0, 0, doFFt  );   // for BaLiS

3824 
3825     if (Fe.Flag != 0) return -1;
3826     Fe.PlotBase();   //if Base Line Subtraction is used

3827     Fe.PlotSpectra();
3828     Fe.PlotXlines();
3829     //Fe.PlotAvHit();

3830     Fe.PlotProfiles();
3831     if ( Co::doCTE ) {Fe.PlotCTE();}
3832     if ( Co::doFit ) {Fe.PlotXfit();}
3833     if (doFFt) { Fe.Plot_FFT(); }
3834     Fe.PlotHits();
3835 
3836 
3837     
3838     unsigned int VNpnt = Vval[0].size();
3839     unsigned int VVpnt;
3840     if ( !VNpnt ) return 0;
3841 // Vector canvas 1 ****************************************************

3842      const float left_margin = (float)0.01;
3843      const float right_margin = (float)0.001;
3844      const float top_margin = (float)0.00;
3845      const float bot_margin = (float)0.00;
3846 
3847      rPad __f;
3848      TCanvas    *   CT = new TCanvas( "CT", "Bias", 100, 70, 900, 800);
3849      CT->SetBorderMode  (0);      //to remove border

3850      CT->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3851      CT->SetRightMargin (right_margin);  //to move plot to the right 0.05

3852      CT->SetTopMargin   (top_margin);    //to use more space 0.07

3853      CT->SetBottomMargin(bot_margin);    //default

3854      CT->SetFrameFillColor(0);
3855      CT->Divide(1,3);
3856      //TColor * c150 = new TColor(150,1.0,0.0,0.0); 

3857 
3858      gPad->SetFillStyle(4100);
3859      gPad->SetFillColor(0);
3860 
3861      CT->cd(1);
3862      do {
3863      VVpnt = Vval[2].size();
3864      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3865      if ( !VVpnt ) continue;
3866      TGraph * pGe = new TGraph( VNpnt, &Vval[0][0], &Vval[2][0]);
3867      pGe->SetTitle("exposure"); 
3868      pGe->GetXaxis()->SetTimeDisplay(1);
3869      pGe->GetXaxis()->SetLabelOffset((float)0.02);
3870      pGe->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3871      pGe->SetMarkerColor (4);
3872      pGe->SetMarkerStyle (8);
3873      pGe->SetMarkerSize  (1);
3874      pGe->SetLineColor   (4);
3875      pGe->SetLineWidth   (2);
3876      pGe->Draw("ALP");
3877      } while(0);
3878 
3879      CT->cd(2);
3880      do {
3881      VVpnt = Vval[3].size();
3882      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3883      if ( !VVpnt ) continue;
3884      TGraph * pGv = new TGraph( VNpnt, &Vval[0][0], &Vval[3][0]);
3885      pGv->SetTitle("Bias"); 
3886      pGv->GetXaxis()->SetTimeDisplay(1);
3887      pGv->GetXaxis()->SetLabelOffset((float)0.02);
3888      pGv->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3889      pGv->SetMarkerColor (4);
3890      pGv->SetMarkerStyle (8);
3891      pGv->SetMarkerSize  (1);
3892      pGv->SetLineColor   (4);
3893      pGv->SetLineWidth   (2);
3894      pGv->Draw("ALP");
3895      } while(0);
3896 
3897      CT->cd(3);
3898      do {
3899      VVpnt = Vval[4].size();
3900      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3901      if ( !VVpnt ) continue;
3902      TGraph * pGa = new TGraph( VNpnt, &Vval[0][0], &Vval[4][0]);
3903      pGa->SetTitle("Bias current, pA"); 
3904      pGa->GetXaxis()->SetTimeDisplay(1);
3905      pGa->GetXaxis()->SetLabelOffset((float)0.02);
3906      pGa->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3907      pGa->SetMarkerColor (6);
3908      pGa->SetMarkerStyle (8);
3909      pGa->SetMarkerSize  (1);
3910      pGa->SetLineColor   (6);
3911      pGa->SetLineWidth   (2);
3912      pGa->Draw("ALP");
3913      } while(0);
3914 
3915      CT->Modified();
3916      CT->Update();
3917      CT->Print("CT_main.pdf");
3918 
3919 // Vector canvas 2 ***************************************************

3920      TCanvas    *   C2 = new TCanvas( "C2", "Temperature", 150, 100, 900, 800);
3921      C2->SetBorderMode  (0);      //to remove border

3922      C2->SetLeftMargin  (left_margin);   //to move plot to the right 0.14

3923      C2->SetRightMargin (right_margin);  //to move plot to the right 0.05

3924      C2->SetTopMargin   (top_margin);    //to use more space 0.07

3925      C2->SetBottomMargin(bot_margin);    //default

3926      C2->SetFrameFillColor(0);
3927      C2->Divide(1,4);
3928 
3929      gPad->SetFillStyle(4100);
3930      gPad->SetFillColor(0);
3931 
3932      C2->cd(1);
3933      do {
3934      VVpnt = Vval[5].size();
3935      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3936      if ( !VVpnt ) continue;
3937      TGraph * pGTA = new TGraph( VNpnt, &Vval[0][0], &Vval[5][0]);
3938      pGTA->SetTitle("A"); 
3939      pGTA->GetXaxis()->SetTimeDisplay(1);
3940      pGTA->GetXaxis()->SetLabelOffset((float)0.02);
3941      pGTA->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3942      pGTA->SetMarkerColor (4);
3943      pGTA->SetMarkerStyle (8);
3944      pGTA->SetMarkerSize  (1);
3945      pGTA->SetLineColor   (4);
3946      pGTA->SetLineWidth   (2);
3947      pGTA->Draw("ALP");
3948      } while(0);
3949 
3950      C2->cd(2);
3951      do {
3952      VVpnt = Vval[6].size();
3953      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3954      if ( !VVpnt ) continue;
3955      TGraph * pGTB = new TGraph( VNpnt, &Vval[0][0], &Vval[6][0]);
3956      pGTB->SetTitle("B"); 
3957      pGTB->GetXaxis()->SetTimeDisplay(1);
3958      pGTB->GetXaxis()->SetLabelOffset((float)0.02);
3959      pGTB->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3960      pGTB->SetMarkerColor (8);
3961      pGTB->SetMarkerStyle (8);
3962      pGTB->SetMarkerSize  (1);
3963      pGTB->SetLineColor   (8);
3964      pGTB->SetLineWidth   (2);
3965      pGTB->Draw("ALP");
3966      } while(0);
3967 
3968      C2->cd(3);
3969      do {
3970      VVpnt = Vval[7].size();
3971      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3972      if ( !VVpnt ) continue;
3973      TGraph * pGTC = new TGraph( VNpnt, &Vval[0][0], &Vval[7][0]);
3974      pGTC->SetTitle("C"); 
3975      pGTC->GetXaxis()->SetTimeDisplay(1);
3976      pGTC->GetXaxis()->SetLabelOffset((float)0.02);
3977      pGTC->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3978      pGTC->SetMarkerColor (4);
3979      pGTC->SetMarkerStyle (8);
3980      pGTC->SetMarkerSize  (1);
3981      pGTC->SetLineColor   (4);
3982      pGTC->SetLineWidth   (2);
3983      pGTC->Draw("ALP");
3984      } while(0);
3985 
3986      C2->cd(4);
3987      do {
3988      VVpnt = Vval[8].size();
3989      printf(" Vector's Vpoints =%i *** \n", VVpnt);
3990      if ( !VVpnt ) continue;
3991      TGraph * pGTD = new TGraph( VNpnt, &Vval[0][0], &Vval[8][0]);
3992      pGTD->SetTitle("D"); 
3993      pGTD->GetXaxis()->SetTimeDisplay(1);
3994      pGTD->GetXaxis()->SetLabelOffset((float)0.02);
3995      pGTD->GetXaxis()->SetTimeFormat("#splitline{%Y-%m-%d}{%H:%M}"); 
3996      pGTD->SetMarkerColor (14);
3997      pGTD->SetMarkerStyle (8);
3998      pGTD->SetMarkerSize  (1);
3999      pGTD->SetLineColor   (14);
4000      pGTD->SetLineWidth   (2);
4001      pGTD->Draw("ALP");
4002      } while(0);
4003 
4004      C2->Modified();
4005      C2->Update();
4006      C2->Print("C2_main.pdf");
4007     
4008     return 0;
4009 }
4010 
4011 
4012 #endif //__CINT__

4013