#include "GEvent.h"
#include "Elec.h"
#include "Trap.h"
ClassImp(GEvent)
  //TRandom aa(3);
void piksr2(int n, float arr[], int brr[])
{
        int i,j;
        float a;
	int b;

        for (j=1;j<n;j++) {
                a=arr[j];
                b=brr[j];
                i=j;
                while (i > 0 && arr[i-1] > a) {
                        arr[i]=arr[i-1];
                        brr[i]=brr[i-1];
                        i--;
                }
                arr[i]=a;
                brr[i]=b;
        }
}

void piksr2(int n, int arr[], int brr[])
{
        int i,j;
        int a;
	int b;

        for (j=1;j<n;j++) {
                a=arr[j];
                b=brr[j];
                i=j;
                while (i > 0 && arr[i-1] > a) {
                        arr[i]=arr[i-1];
                        brr[i]=brr[i-1];
                        i--;
                }
                arr[i]=a;
                brr[i]=b;
        }
}


void piksr(int n, int arr[])
{
        int i,j;
        int a;

        for (j=1;j<n;j++) {
                a=arr[j];
                i=j;
                while (i > 0 && arr[i-1] > a) {
                        arr[i]=arr[i-1];
                        i--;
                }
                arr[i]=a;
        }
}

float distance2(float x1,float y1,float x2,float y2)
{
float y,x;
x=pow((x1-x2),2);
y=pow((y1-y2),2);
return(sqrt(x+y));
}

void swoo(char *a, char *b) {
  char c = *a;
  *a = *b; *b = c;
}

void swooip(float *in, int s) {
  char *sr, b;
  while(s--) {
    sr=(char *)in;
    swoo(&sr[0], &sr[3]);
    swoo(&sr[1], &sr[2]);
    in++;
  }
}                          

float lin(float x0,float y0,float x1,float y1,float x)
{
float y;
y=((y1-y0)/(x1-x0))*(x-x0)+y0;
return y;
}

float entp(struct steprec *event)
{
float nostrips=1;
float pitch=50;
double celidel; 
float z,which;
// printf("=%f; =%f ;=%f",event->data[0],event->data[0]*1e4,event->data[0]*1e4/pitch); 
 event->data[0]=(float)(modf(event->data[0]*1e4/(pitch*nostrips),&celidel))*pitch*nostrips;
 z=event->data[1];
 event->data[1]=(float)(event->data[2]*1e4)+4640;
 if(event->data[1]<0) event->data[1]=fabs(event->data[1]);  
 event->data[1]+=1;
 event->data[2]=(float)(z*1e4);
 event->data[8]=(float)(event->data[8]*1e4);
 return((float)celidel);
}

void printdrift(struct drift *d)
{
int i,j;
printf("drift strip=%d : entry(%f,%f), exit(%f,%f), dist=%f, de=%fn",d->strip,d->x[0],d->y[0],d->x[1],d->y[1],d->dist,d->de);
}

/*
 GEvent::EventEnergy()
{
Int_t lenght=EnergyLoss.GetSize();
for(i=0;i<lenght) energy+=
}*/
 GEvent::GEvent(Float_t x) 
{
  //  Constructor:
  //	Float_t x ; selects the shift in um for the strips readot x=x+shift
  //                This is used for selecting the read out strip.
   Shift=x;
   Lenght=10;
 DriftStrip=TArrayI(Lenght); 
 EnergyLoss=TArrayF(Lenght); 
 EpX=TArrayF(Lenght);
 ExpX=TArrayF(Lenght);
 EpY=TArrayF(Lenght);
 ExpY=TArrayF(Lenght);
 PathLenght=TArrayF(Lenght);
 histop =new TClonesArray("TH1F",Lenght);
 histon =new TClonesArray("TH1F",Lenght);
};



 GEvent::GEvent(Char_t *name, Char_t *newname, Float_t pitch,Float_t shift, Float_t offset,Int_t byteorder,Int_t print)
{ 
Shift=shift; 
DoProccesedFile(name,newname,offset,pitch,byteorder,print);
}

 GEvent::~GEvent()
{
  histop->Delete();
  histon->Delete();
}

 void GEvent::DoProccesedFile(Char_t *name,Char_t *newname, Float_t offset, Float_t Pitch, Int_t ByteOrder, Int_t print)
{
float coords[100][4];
double celidel,ostanek;
int no,sign,j=0,before=0,k;
float fhs,nhs,dif;
float x,y,i=0,cond,x0,y0,dist,di,ch,charge;
struct steprec fevent,nevent,event;
struct drift d[100];
struct event_header eh;
FILE *out,*in;
if((in=fopen(name,"rb+"))==NULL) exit(0);
if((out=fopen(newname,"wb+"))==NULL) exit(0);


while(no=fread(&event,sizeof(event),1,in)) 
   { 
     if(ByteOrder){swooip((float *)&event,sizeof(event)/sizeof(float));}
     event.data[0]+=offset;
     //printrec(&event,1);
     if(event.step==0) {
       if(before){ 
     eh.samno=fevent.samno;
     eh.track=fevent.track;
     eh.lenght=j;
     //    if(hpnt){ swooip((float *)&eh,3); swooip((float *)&d[1],j*sizeof(struct drift)/sizeof(float)); }
     fwrite(&eh,sizeof(struct event_header),1,out);
     fwrite(&d[1],sizeof(struct drift),j,out);
       }
     j=0;
     for(k=0;k<20;k++) {d[k].x[0]=0; d[k].x[1]=0; d[k].y[0]=0; d[k].y[1]=0; d[k].strip=0; d[k].dist=0; d[k].de=0;}
     if(print) printf("n\n");  
     fevent=event; fhs=entp(&fevent); 
     if(print) printf("div=%d x=%f y=%f n",j,fevent.data[0]+fhs*Pitch,fevent.data[1]); 
     }
     
     if(event.step>0) {
       before=1;
       nevent=event;
       nhs=entp(&nevent); //printrec(&nevent,1);
       
       charge=nevent.data[7];
       x=fevent.data[0]+fhs*Pitch;
       y=fevent.data[1];
       dist=distance2(x,y,nevent.data[0]+nhs*Pitch,nevent.data[1]);
       dif=fhs-nhs;
       // printf("charge=%f dist=%fn",charge,dist);
       //printrec(&nevent,1);
     if(event.data[6]!=0 && dif!=0)
       {
	 if(fhs>nhs) {
	   dif=fhs-nhs;
	   while((fhs-i)>nhs)
	     {
	       x0=x; y0=y;
               if(fhs>=0) x=(fhs-i)*Pitch;
	       y=lin(fevent.data[0]+fhs*Pitch,fevent.data[1],nevent.data[0]+nhs*Pitch,nevent.data[1],x); 
	       di=distance2(x,y,x0,y0);
	       ch=di/dist*charge; j++;
	       if(x<x0) {ostanek=Pitch*modf((double)(x0/Pitch),&celidel); 
		 d[j].x[0]=(ostanek!=0?ostanek:Pitch); d[j].x[1]=0; }
	       // d[j].x[0]=x0; d[j].x[1]=x;  
	       d[j].y[0]=y0; d[j].y[1]=y; 
	       d[j].strip=(int)((x+x0)/(2*Pitch)); d[j].de=ch*ekv; d[j].dist=di; 
	       if(print)   printdrift(&d[j]);
	       //       	       printf("div=%d x=%f y=%f di=%f ch=%f        %f %f %f n",j,x,y,di,ch,fhs,nhs,i);  
	       i++;
	     }
	 }
	 if(fhs<nhs) {
	   dif=nhs-fhs;
	   while((fhs+i)<nhs)
	     {
	       x0=x; y0=y;
	       if(fhs>=0) x=(fhs+(i+1))*Pitch;
	       y=lin(fevent.data[0]+fhs*Pitch,fevent.data[1],nevent.data[0]+nhs*Pitch,nevent.data[1],x); 
	       di=distance2(x,y,x0,y0);
	       ch=di/dist*charge; j++;
	       if(x>x0) {ostanek=Pitch*modf((double)(x0/Pitch),&celidel); 
		 d[j].x[0]=(ostanek!=0?ostanek:0); d[j].x[1]=Pitch; }
	       
	       //d[j].x[0]=x0; d[j].x[1]=x;  
	       d[j].y[0]=y0; d[j].y[1]=y; 
	       d[j].strip=(int)((x+x0)/(2*Pitch)); d[j].de=ch*ekv; d[j].dist=di; 
	       if(print)   {printdrift(&d[j]); printf("div=%d x=%f y=%f di=%f ch=%f          %f %f %fn",j,x,y,di,ch,fhs,nhs,i);}
  	       i++;
	     }
	 }
	 i=0;
       }
    j++;
     di=distance2(x,y,nevent.data[0]+nhs*Pitch,nevent.data[1]);
     if(dist!=0) ch=di/dist*charge; else ch=0;
     d[j].x[0]=x; d[j].x[1]=nevent.data[0]+nhs*Pitch;
     if(d[j].x[0]<d[j].x[1]) {
	  d[j].x[1]=Pitch*modf((double)(d[j].x[1]/Pitch),&celidel);  
	  d[j].x[0]=Pitch*modf((double)(d[j].x[0]/Pitch),&celidel);
     }
     if(d[j].x[0]>d[j].x[1]) { 
	  d[j].x[1]=Pitch*modf((double)(d[j].x[1]/Pitch),&celidel);
	  ostanek=Pitch*modf((double)(d[j].x[0]/Pitch),&celidel);
	  d[j].x[0]=ostanek!=0?ostanek:Pitch;}
     
     d[j].y[0]=y; d[j].y[1]=nevent.data[1]; 
     d[j].strip=(int)((nevent.data[0]+nhs*Pitch+x)/(2*Pitch)); d[j].de=ch*ekv; d[j].dist=di; 
     if(print) printdrift(&d[j]);
     if(print)     printf("div=%d x=%f y=%f di=%f ch=%f n",j,nevent.data[0]+nhs*Pitch,nevent.data[1],di,ch); 
     fevent=nevent;
     fhs=nhs;

    
     }
     
   }

 fclose(out);  
}


 Int_t GEvent::Next(FILE *in,Int_t print)
{
 Int_t i,ok=1;
 struct event_header eh;
 struct drift *d;
 ok=fread(&eh,sizeof(struct event_header),1,in);
 d=(struct drift*) malloc(sizeof(struct drift)*eh.lenght);
 ok=fread(d,sizeof(struct drift),eh.lenght,in);
 if(print) printf("Event no=%d :::: track no=%d :::: event lenght=%dn",eh.samno,eh.track,eh.lenght);
 Lenght=eh.lenght;
 DriftStrip=TArrayI(eh.lenght); 
 EnergyLoss=TArrayF(eh.lenght); 
 EpX=TArrayF(eh.lenght);
 ExpX=TArrayF(eh.lenght);
 EpY=TArrayF(eh.lenght);
 ExpY=TArrayF(eh.lenght);
 PathLenght=TArrayF(eh.lenght);
 histop =new TClonesArray("TH1F",Lenght);
 histon =new TClonesArray("TH1F",Lenght);

 for(i=0;i<eh.lenght;i++) 
   {
     DriftStrip[i]=d[i].strip; EnergyLoss[i]=d[i].de; PathLenght[i]=d[i].dist;
     EpY[i]=d[i].y[0]; ExpY[i]=d[i].y[1]; EpX[i]=d[i].x[0]+Shift; ExpX[i]=d[i].x[1]+Shift; 
     if(print) printf("%d (%f %f),(%f %f) %fn",DriftStrip[i],EpX[i],EpY[i],ExpX[i], ExpY[i],EnergyLoss[i]);
   }
 if(print) printf("n");
return ok;
}

 void GEvent::NextX(Float_t width, Float_t depth,Float_t pos,Int_t print)
{
Event++; Lenght=1; DriftStrip[0]=2; 
EpX[0]=width*pos+Shift; ExpX[0]=EpX[0];
EpY[0]=depth*pos+1; ExpY[0]=EpY[0];
EnergyLoss[0]=60000;
PathLenght[0]=1;

if(print) printf("%d %d (%f %f),(%f %f) %f %fn",Event,DriftStrip[0],EpX[0],EpY[0],ExpX[0], ExpY[0],PathLenght[0],EnergyLoss[0]);
}


 void GEvent::AddStrip(Int_t number, TH1F *hisp,TH1F *hisn)
{
  if(number>DriftStrip.GetSize()) printf("Array out of bondsn");
  else {
    //Strips[number]=strip;
    TClonesArray &entryp = *histop;
    TClonesArray &entryn = *histon;
    new(entryp[number]) TH1F();
    new(entryn[number]) TH1F();
    hisp->Copy(*entryp[number]);
    hisn->Copy(*entryn[number]);
  }
}
//______________________________________________________________________________
 void GEvent::Reset()
{
histop->Clear(); 
histon->Clear();
  //delete histop; //Change Made on 6.12.1999
  //delete histon;
}

 void GEvent::DrawHisto(Int_t i,char *option)
{
Int_t max;
  if(! strcmp(option,"p")) ((TH1F *)histop->At(i))->Draw();
  if(! strcmp(option,"n")) ((TH1F *)histon->At(i))->Draw();
  if(! strcmp(option,"pn")) 
    { 
      TH1F *gg=new TH1F("sumpn","Sum of p an n",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
      gg->Add(((TH1F *)histop->At(i)),((TH1F *)histon->At(i)),1,1); gg->DrawCopy();
      delete gg;
    }
}


 void GEvent::GetHitMap(Int_t *hitmap)
{
Int_t i,j;
Int_t clsiz=0;
Int_t *hit= new Int_t[Lenght+1];
 for(i=0;i<Lenght;i++) hit[i]=DriftStrip[i];
piksr(Lenght,hit);
 if(Lenght==1) 
   {hitmap[0]=hit[0];clsiz++;} 
  else 
      {
   hitmap[0]=hit[0]; clsiz++;
   for(i=1;i<Lenght;i++) if(i<Lenght && hit[i]!=hit[i-1]) {hitmap[clsiz]=hit[i]; clsiz++;}
      }
 if(clsiz==0) {clsiz=1; hitmap[0]=hit[0];} 
delete [] hit;
}

 Int_t GEvent::ClusterSize()
{
return ClusterSize(Lenght,DriftStrip.GetArray());
}


 Int_t GEvent::ClusterSize(Int_t lenght, Int_t *driftstrip)
{
Int_t i,j;
Int_t clsiz=0;
Int_t *hit= new Int_t[lenght];
for(i=0;i<lenght;i++) {hit[i]=driftstrip[i]; }//printf("%d ",hit[i]);}
piksr(lenght,hit);
 if(lenght==1) {clsiz++;} else {clsiz++;
   for(i=1;i<lenght;i++) if(i<lenght && hit[i]!=hit[i-1]) clsiz++;
 }
 if(clsiz==0) {clsiz=1;} 
delete [] hit;
return clsiz;
}

 void GEvent::Signals(Float_t *x,Int_t *y,Trap *tn,Trap *tp,TH1F *trn,TH1F *trp,Elec *el,Int_t shaper)
{
Int_t i=0,j,k;
Int_t clsiz=ClusterSize();
Float_t max;
GetHitMap(y);
TH1F *hisp=new TH1F("chp","+ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hisn=new TH1F("chn","-ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hist=new TH1F("cht","all",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));

for(i=0;i<clsiz;i++) 
  {
    for(j=0;j<Lenght;j++)
    {
      if(y[i]==DriftStrip[j]) 
          { 
		hisp->Add((TH1F*)histop->At(j));
		hisn->Add((TH1F*)histon->At(j));	      
	  }
      
    }
    tn->Trapping(hisn,trn); 
    tp->Trapping(hisp,trp);   

     hist->Add(&trn[3]);  
     hist->Add(&trp[3]);
     // el->preamp(15e-12,50,hist,50e-9);
     // el->CRshape(1e-12,2.5e4,2.5e4,hist);
      // el->RCshape(1e-12,2.5e4,2.5e4,hist);
      //el->RCshape(1e-12,2.5e4,2.5e4,hist);

      el->preamp(hist);
      el->CRshape(hist);
      el->RCshape(hist);
      if(shaper) el->RCshape(hist);
      //       hist->DrawCopy();
     // for(j=1;j<hist->GetNbinsX();j++) )x[i]=max>hist->GetBinContent(j)?max:hist->GetBinContent(j);
      k=hist->GetXaxis()->FindBin(el->GetPeakTime()); x[i]=hist->GetBinContent(k); 
     //x[i]=hist->Integral(1,hist->GetNbinsX()-1);
    hist->Reset();
    hisp->Reset();
    hisn->Reset();
  }
delete hist;
delete hisn;
delete hisp;
piksr2(clsiz,x,y);
}

///////////////////////////////////////////////////////////////////////////////
//									    //
// Calculates the signals and timing                                        //
//									    //
//////////////////////////////////////////////////////////////////////////////
 void GEvent::Signals(GSignals *sig,Int_t *y,Trap *tn,Trap *tp,TH1F *trn,TH1F *trp,Elec *el,Int_t shaper)
{
Int_t i=0,j,k,ts=0;
Int_t timeslots=sig->GetN();
Int_t clsiz=ClusterSize();
Float_t *x=new Float_t [timeslots*clsiz]; 
Float_t max;
GetHitMap(y);
TH1F *hisp=new TH1F("chp","+ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hisn=new TH1F("chn","-ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hist=new TH1F("cht","all",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));

    for(i=0;i<clsiz;i++) 
      {
	for(j=0;j<Lenght;j++)
	  {
	    if(y[i]==DriftStrip[j]) 
	      { 
		hisp->Add((TH1F*)histop->At(j));
		hisn->Add((TH1F*)histon->At(j));	      
	      }
      
	  }
    tn->Trapping(hisn,trn); 
    tp->Trapping(hisp,trp);   

     hist->Add(&trn[3]);  
     hist->Add(&trp[3]);
     // el->preamp(15e-12,50,hist,50e-9);
     // el->CRshape(1e-12,2.5e4,2.5e4,hist);
      // el->RCshape(1e-12,2.5e4,2.5e4,hist);
      //el->RCshape(1e-12,2.5e4,2.5e4,hist);

      el->preamp(hist);
      el->CRshape(hist);
      el->RCshape(hist);
      if(shaper) el->RCshape(hist);
      //       hist->DrawCopy();
     // for(j=1;j<hist->GetNbinsX();j++) )x[i]=max>hist->GetBinContent(j)?max:hist->GetBinContent(j);
     for(ts=0;ts<timeslots;ts++)
       {
	 if((sig->GetPeakTimes())[ts]<=0.) x[ts*clsiz+i]=hist->GetMaximum(); else {
      k=hist->GetXaxis()->FindBin((sig->GetPeakTimes())[ts]);
      x[ts*clsiz+i]=hist->GetBinContent(k); }
      //      printf("cluster size =%d, timeslots=%d :: timeslot=%d, clsiz=%d,  index=%d, amplitude=%fn",clsiz,timeslots,ts,i,k,x[ts*clsiz+i]);
     //x[i]=hist->Integral(1,hist->GetNbinsX()-1);
       }
    hist->Reset();
    hisp->Reset();
    hisn->Reset();
      }
delete hist;
delete hisn;
delete hisp;
//printf("Adding the Evetnsn");
for(i=0;i<timeslots;i++) sig->AddEvent(i,clsiz,&x[i*clsiz]);
//printf("Cleaning Upn");
delete [] x;
//piksr2(clsiz,x,y);
}

///////////////////////////////////////////////////////////////////////////////
//									    //
// Calculates the signals and timing for interpolated strips                //
//									    //
//////////////////////////////////////////////////////////////////////////////
 void GEvent::SignalsI(GSignals *sigi,Float_t *transfer,Int_t *y,Trap *tn,Trap *tp,TH1F *trn,TH1F *trp,Elec *el,Int_t shaper)
{
GSignals *sig;
sig=sigi;
SignalsI(sigi,sig,transfer,y,tn,tp,trn,trp,el,shaper);
}

 void GEvent::SignalsI(GSignals *sigi,GSignals *sig,Float_t *transfer,Int_t *y,Trap *tn,Trap *tp,TH1F *trn,TH1F *trp,Elec *el,Int_t shaper)
{
Int_t i=0,j=0,k=0,ts=0;
Int_t timeslots=sig->GetN();
Int_t clsiz=ClusterSize();
Float_t *x=new Float_t [timeslots*clsiz]; 
Float_t max;

GetHitMap(y);

TH1F *hisp=new TH1F("chp","+ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hisn=new TH1F("chn","-ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hist=new TH1F("cht","all",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));

  for(i=0;i<clsiz;i++) 
      {
	for(j=0;j<Lenght;j++)
	  {
	    if(y[i]==DriftStrip[j]) 
	      { 
		hisp->Add((TH1F*)histop->At(j));
		hisn->Add((TH1F*)histon->At(j));	      
	      }
      
	  }
    tn->Trapping(hisn,trn); 
    tp->Trapping(hisp,trp);   

     hist->Add(&trn[3]);  
     hist->Add(&trp[3]);


      el->preamp(hist);
      el->CRshape(hist);
      el->RCshape(hist);
      if(shaper) el->RCshape(hist);
      //       hist->DrawCopy();
     // for(j=1;j<hist->GetNbinsX();j++) )x[i]=max>hist->GetBinContent(j)?max:hist->GetBinContent(j);
     for(ts=0;ts<timeslots;ts++)
       {
	 if((sig->GetPeakTimes())[ts]<=0.) x[ts*clsiz+i]=hist->GetMaximum(); else {
      k=hist->GetXaxis()->FindBin((sig->GetPeakTimes())[ts]);
      x[ts*clsiz+i]=hist->GetBinContent(k); }
      //      printf("cluster size =%d, timeslots=%d :: timeslot=%d, clsiz=%d,  index=%d, amplitude=%fn",clsiz,timeslots,ts,i,k,x[ts*clsiz+i]);
     //x[i]=hist->Integral(1,hist->GetNbinsX()-1);
       }
    hist->Reset();
    hisp->Reset();
    hisn->Reset();
      }
delete hist;
delete hisn;
delete hisp;
//printf("Adding the Evetnsn");
/////////////////////////////////////////////
Int_t odd=0,Clsiz,l=0;
for(i=0;i<clsiz;i++) odd+=(Int_t) TMath::Odd((Int_t) y[i]); 
Clsiz=clsiz+3*odd;
k=0;
//printf("Clsiz=%d clsiz=%dn",Clsiz,clsiz);
//for(i=0;i<clsiz;i++) printf("y[%d]=%dn",i,y[i]); 
Int_t *hitmap=new Int_t[Clsiz];
Int_t *index=new Int_t[Clsiz];

 for(i=0;i<clsiz;i++) 
   if(TMath::Odd((Int_t) y[i])) 
     {	
       l=1;
       for(j=-3;j<=3;j+=2) {hitmap[k]=y[i]+j; index[k]=(i+1)*10+l; l++; k++;}
     }
   else
     {hitmap[k]=y[i]; index[k]=(i+1)*10; k++;}

 piksr2(Clsiz,hitmap,index);   

 Int_t CLSize=ClusterSize(Clsiz,hitmap); //printf("cluster size=%d ",CLSize);
Float_t *signals=new Float_t[timeslots*CLSize];
Int_t cls=0,pos=0;
Float_t multiply=1;
Int_t trans,posit;

//printf("n");
trans=index[0]%10;
posit=index[0]/10-1;
 multiply=trans==0?1:transfer[trans-1]; //printf("trans=%d posit=%d multiply=%fn",trans,posit,multiply);
 //y[0]=hitmap[0];
for(ts=0;ts<timeslots;ts++) signals[ts*CLSize]=x[ts*clsiz+posit]*multiply; 
for(i=1;i<Clsiz;i++)
  { 
    if(hitmap[i]!=hitmap[i-1]) 
      {
	cls++;
	//y[cls]=hitmap[cls];
	trans=index[i]%10;
	posit=index[i]/10-1;
	multiply=trans==0?1:transfer[trans-1]; //printf("trans=%d posit=%d multiply=%fn",trans,posit,multiply);
	for(ts=0;ts<timeslots;ts++) signals[ts*CLSize+cls]=x[ts*clsiz+posit]*multiply;  
      }	
    else
      {
	trans=index[i]%10;
	posit=index[i]/10-1;
	multiply=trans==0?1:transfer[trans-1];//printf("trans=%d posit=%d multiply=%fn",trans,posit,multiply);
	for(ts=0;ts<timeslots;ts++) signals[ts*CLSize+cls]+=x[ts*clsiz+posit]*multiply;  
      }	
  }


//printf("n calculated cluseter size = %dn",cls+1);
//for(i=0;i<Clsiz;i++) printf("%d %dn",hitmap[i],index[i]);
// for(i=0;i<CLSize;i++) {for(ts=0;ts<timeslots;ts++) printf("%f ",signals[ts*CLSize+i]); printf("n");}
for(i=0;i<timeslots;i++) 
  {
    sigi->AddEvent(i,CLSize,&signals[i*CLSize]); 
    if(sig!=sigi) sig->AddEvent(i,clsiz,&x[i*clsiz]);
  }
//printf("Cleaning Upn");
delete [] hitmap;
delete [] index;
delete [] x;
delete [] signals;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


 void GEvent::Signals(Float_t *x,Int_t *y)
{
Int_t i=0,j;
Int_t clsiz=ClusterSize();
GetHitMap(y);
TH1F *hisp=new TH1F("chp","+ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hisn=new TH1F("chn","-ch",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
TH1F *hist=new TH1F("cht","all",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));

for(i=0;i<clsiz;i++) 
  {
    for(j=0;j<Lenght;j++)
    {
      if(y[i]==DriftStrip[j]) 
          { 
		hisp->Add((TH1F*)histop->At(j));
		hisn->Add((TH1F*)histon->At(j));	      
	  }
      
    }
    hist->Add(hisp); 
    hist->Add(hisn);
    // hist->DrawCopy();
    // Elec::preamp(15e-12,50,hist,25e-9,0);
    x[i]=hist->Integral(1,hist->GetNbinsX()-1);
    hist->Reset();
    hisp->Reset();
    hisn->Reset();
  }
delete hist;
delete hisn;
delete hisp;
piksr2(clsiz,x,y);
}

 void GEvent::GetHisto(Int_t i,TH1F *his,char *option)
{
Int_t max;
  if(! strcmp(option,"p")) ((TH1F *)histop->At(i))->Copy(*his);
  if(! strcmp(option,"n")) ((TH1F *)histon->At(i))->Copy(*his);
  if(! strcmp(option,"pn")) 
    { 
      TH1F *gg=new TH1F("sumpn","Sum of p an n",((TH1F *)histop->At(i))->GetNbinsX(),0,((TH1F *)histop->At(i))->GetNbinsX()*((TH1F *)histop->At(i))->GetBinWidth(1));
      gg->Add(((TH1F *)histop->At(i)),((TH1F *)histon->At(i)),1,1); gg->Copy(*his);
      delete gg;
    }
}

 void GEvent::PrintEvent()
{
Int_t i;
 for(i=0;i<Lenght;i++) 
printf("%d (%f %f),(%f %f) %fn",DriftStrip[i],EpX[i],EpY[i],ExpX[i], ExpY[i],EnergyLoss[i]);
}













ROOT page - Class index - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.