Skip to content
Snippets Groups Projects
get_LookUp_Values.C 3.58 KiB
Newer Older
/*This code produces a lookup table necessary to convert drift times to
  drift distances in the shms drift chambers
*/

#define NPLANES 12
Yero1990's avatar
Yero1990 committed
#define TOTAL_BINS 274
void get_LookUp_Values() {

   
  //Read Run Number from txt file
  int run_NUM;
Yero1990's avatar
Yero1990 committed
  Long64_t num_evts;        //added
  string input_file;   //added

  TString f0 = "input_RUN.txt";
  ifstream infile(f0);
Yero1990's avatar
Yero1990 committed
  infile >> input_file >> run_NUM >> num_evts;
 
  //Open root file containing drift time histos
Yero1990's avatar
Yero1990 committed
  TFile *f = new TFile(Form("../root_files/run%d/shms_tzero_corr_histos.root", run_NUM),"READ");
 
  //Define histogram array
  TH1F *h[NPLANES];
 
  //Define the number Drift Chamber planes
  TString plane_names[NPLANES]={"1u1", "1u2", "1x1", "1x2", "1v1", "1v2", "2v2", "2v1", "2x2", "2x1", "2u2", "2u1"};
 
  //Declare bin properties
  int bin_t0[NPLANES];
  int bin_final[NPLANES];					/*Array to store the bin number corresponding to last bin*/
  int bin_Content[NPLANES];          /*Array to store the content (# events) corresponding to the bin with maximum content*/
  double binContent_TOTAL[NPLANES];     /*Array to store sum of all bin contents for each plane*/
  double binSUM[NPLANES];
  int bin;
  int binx;
  double lookup_value[NPLANES];           /*Array to store lookup values for each plane*/ 
  
  //Create an output file to store lookup values  
  ofstream ofs;
  TString lookup_table = Form("../../../PARAM/SHMS/DC/pdriftmap_run%d_NEW.param", run_NUM);
  ofs.open (lookup_table);
 
 
  //Set headers for subsequent columns of data
  ofs << Form("; Lookup Table: RUN %d", run_NUM) << "\n";
Yero1990's avatar
Yero1990 committed
  ofs << "; number of bins in time to distance lookup table" << "\n";
  ofs << Form("pdriftbins = %d", TOTAL_BINS+1) << "\n";
Yero1990's avatar
Yero1990 committed
  ofs << "; number of 1st bin in table in ns" << "\n";
  ofs << "pdrift1stbin=0" << "\n";
  ofs << "; bin size in ns" << "\n";
Yero1990's avatar
Yero1990 committed
  ofs << "pdriftbinsz=1" << "\n";
 
 
 
  //Loop over each plane of shms Drift Chambers (DC1 & DC2)

  for (int ip=0; ip<NPLANES; ip++){
   
Yero1990's avatar
Yero1990 committed
    TString drift_time_histo = "plane_"+plane_names[ip]+"drifttime"; 

    //Get drift time histograms from root file
    h[ip] = (TH1F*)f->Get(drift_time_histo);

    //Get bin corresponding to t0 = 0 ns
    bin_t0[ip] = h[ip]->GetXaxis()->FindBin(0.0);
   
    //Get final bin 
    bin_final[ip] = bin_t0[ip] + TOTAL_BINS;
   
   
   
    //Find total BIN Content over entire integration range
    binContent_TOTAL[ip] = 0; //set counter to zero

    for (bin = bin_t0[ip]; bin <= bin_final[ip]; bin ++ ) {
     
      bin_Content[ip] = h[ip] -> GetBinContent(bin);
     
      binContent_TOTAL[ip] = bin_Content[ip] + binContent_TOTAL[ip];
     
      //   cout << "Bin: " << bin << endl;
      //   cout << "Content " << bin_Content[ip] << endl;
      //   cout << "Content SUM : " << binContent_TOTAL[ip] << endl;
    }
   
    TString headers = "pwc" + plane_names[ip] + "fract=";      
    ofs << headers;	  
   
    //Calculate LookUp Value
   
    binSUM[ip] = 0.0;
    int bin_count = 0;
   
    for (bin = bin_t0[ip]; bin <= bin_final[ip]; bin++) {
     
      bin_Content[ip] = h[ip] -> GetBinContent(bin);
      binSUM[ip] = binSUM[ip] + bin_Content[ip];
     
     
      lookup_value[ip] = binSUM[ip] / binContent_TOTAL[ip];
      bin_count = bin_count + 1;
     
Yero1990's avatar
Yero1990 committed
      if (bin_count <= 16 ) {
	ofs << setprecision(5) << lookup_value[ip] << fixed << ",";
      }
     
Yero1990's avatar
Yero1990 committed
      else if (bin_count >16 && bin_count < 274) {
	ofs << setprecision(5) << lookup_value[ip] << ((bin_count+1) % 20 ? "," : "\n") << fixed;