// This is a c++ program that finds optimum delay for a tower and stores in 
// in a file

#include 
#include 
#include 

float meancalc(int cent,float yval[],int offset){
  float meanval=0;
  float N=0;
  int low=50;
  
  if (centmaximum){
	maximum=array[i];// new max
        index=i;
      }
    }
  return index;
}
//====================================
//====================================
//====================================
float optdelay(float arr[],int mx){
  float x=0;
  float time=0;
  x =(float) mx-(0.25*(arr[mx+1]-arr[mx-1])/(.5*(arr[mx+1]+arr[mx-1])-arr[mx]));
  time=16*(x-1);
  return time;
}
//====================================
//====================================
//====================================


timing() {
  
  int j = 1,i = 1,d;
  float mean[800];
  int off=50;
  int cent;
  float delay[800];
  char ch;
  int section;
  ofstream outfile;

  outfile.open("results.txt");
  assert(outfile);  


  // Loop through all towers
  section=5;
  //for (section=1;section<=12;section++)
  for (ch='A';ch<='E';ch++)
  //ch='D';
    for(d=1;d<=12;d++)
  	{
  //d=10;       
          j=1;

          // Write tower name to file
	   outfile << setw(2) << setfill('0') << section << "T" << ch << setw(2) << setfill('0') << d << " ";

 	  // Loop through different runs and find corresponding means for one tower
	  for (int run = 1025; run <=1030;run++)
	    {
               
              mean[j]=0;
	      TString hFile="/N/u1/bullinger/outLaser/pi";
	      hFile+=run;    hFile+=".hist.root";

	      f=new TFile(hFile);
	      assert(f->IsOpen());
 
	      int sec=section,eta=d;
	      char sub=ch;

	      char name[100];
	      sprintf(name,"%02dT%c%02d",sec,sub,eta);
	      //printf("searching for histo '%s'\n",name);
	      
	      TH1F *h=f->Get(name);
	      assert(h);

	      float *y=h->GetArray();
	      cent=h->GetMaximumBin();

	      mean[j]=meancalc(cent,y,off);
     
	      //     printf("mean %f, j =%d\n",mean[j],j);

	      j++;
	    }

	  // Find maximum value in array
	  int max=arrmax(mean);
   	  printf("max =%f\n",max);
         
          // Use maximum value and means for each run to find optimum delay for tower
          // Store optimum delay in delay[]
          delay[i]=optdelay(mean,max);
	  printf("opt delay %f\n",delay[i]);

	  // Write array index and delay[] to output file
	  outfile << setw(2) << setfill('0') << i << " " << delay[i] << endl;

          i++;

	  	}

	  //       outfile.close();
       printf("Done\n");
	  return;

}

//==========================================================================
//==========================================================================
// Next script

#include 
#include 
#include 
#include 
#include 

TH1F *hx[4];
//====================================
//====================================
//====================================

// Creates histogram of centroid frequency with 1 values in a bin
int initHisto(){
  int nblaser = 4000; // Number bins for laser run

  hx[0]=new TH1F("centr","Optimum Delay for Sector 8",nblaser,0,4000);
 
}

//====================================
//====================================
//====================================
int finish(){
  printf("Finish, nCentroids=%d\n",hx[0]->GetEntries());
  hx[0]->SetXTitle("Opt Delay (ns)");
  hx[0]->SetYTitle("Counts");
  hx[0]->Draw(); 
  }
//====================================
//====================================
//====================================
timing2() {
  initHisto();
  gStyle->SetOptStat(1111111);
  int j = 1,i = 1,d;
  
  int off=50;
  int cent;
  float delay[61];
  char ch;
  int section;
  ifstream infile;

  infile.open("results.txt");
  assert(infile);  


  // Loop through all towers
  //  string q;
  char name[100][100];
  //sprintf(name,"%02dT%c%02d",sec,sub,eta);
  //printf("searching for histo '%s'\n",name);

  int nm,f,g,h,i;
  char su,tow;



  while(infile  >> nm >> tow >> su >> f >> delay[i]){
    float centr = delay[i];
    // if (su=='A'||su=='B'||su=='C')
    //if (su=='D'||su=='E')
      hx[0]->Fill(centr);
  sprintf(name[i],"%02d%c%c%02d\n",nm,tow,su,f);
  printf("name is %s delay is %f\n",name[i],delay[i]);
  infile.ignore(10000,'\n');
  i++;
  }

  infile.close();
       printf("Done\n");
       finish();
	  return 0;

}

//==========================================================================
//==========================================================================
// Next script
#include 
#include 
#include 

float meancalc(int cent,float yval[],int offset){
  float meanval=0;
  float N=0;
  int low=50;
  
  if (centmaximum){
	maximum=array[i];// new max
        index=i;
      }
    }
  return index;
}
//====================================
//====================================
//====================================
float optdelay(float arr[],int mx){
  float x=0;
  float time=0;
  x =(float) mx-(0.25*(arr[mx+1]-arr[mx-1])/(.5*(arr[mx+1]+arr[mx-1])-arr[mx]));
  time=16*(x-1);
  return time;
}
//====================================
//====================================
//====================================


timing3() {
  
  int j = 1,i = 1,d;
  float mean[800];
  int off=50;
  int cent;
  float delay[800];
  char ch;
  int section;
  ofstream outfile;

  outfile.open("results.txt");
  assert(outfile);  


  // Loop through all towers
  section=5;
  //for (section=1;section<=12;section++)
  for (ch='A';ch<='E';ch++)
  //ch='D';
    for(d=1;d<=12;d++)
  	{
  //d=10;       
          j=1;

          // Write tower name to file
	   outfile << setw(2) << setfill('0') << section << "T" << ch << setw(2) << setfill('0') << d << " ";

 	  // Loop through different runs and find corresponding means for one tower
	  for (int run = 1025; run <=1030;run++)
	    {
               
              mean[j]=0;
	      TString hFile="/N/u1/bullinger/outLaser/pi";
	      hFile+=run;    hFile+=".hist.root";

	      f=new TFile(hFile);
	      assert(f->IsOpen());
 
	      int sec=section,eta=d;
	      char sub=ch;

	      char name[100];
	      sprintf(name,"%02dT%c%02d",sec,sub,eta);
	      //printf("searching for histo '%s'\n",name);
	      
	      TH1F *h=f->Get(name);
	      assert(h);

	      float *y=h->GetArray();
	      cent=h->GetMaximumBin();

	      mean[j]=meancalc(cent,y,off);
     
	      //     printf("mean %f, j =%d\n",mean[j],j);

	      j++;
	    }

	  // Find maximum value in array
	  int max=arrmax(mean);
   	  printf("max =%f\n",max);
         
          // Use maximum value and means for each run to find optimum delay for tower
          // Store optimum delay in delay[]
          delay[i]=optdelay(mean,max);
	  printf("opt delay %f\n",delay[i]);

	  // Write array index and delay[] to output file
	  outfile << setw(2) << setfill('0') << i << " " << delay[i] << endl;

          i++;

	  	}

	  //       outfile.close();
       printf("Done\n");
	  return;

}