Newer
Older
TestStandRepository / Software / AttenuationScan / AttenuationScan.C
@Federica Lionetto Federica Lionetto on 9 Oct 2014 9 KB Added AttenuationScan, to be modified
//================================================
// Author: Federica Lionetto
// Created on: 21/07/2014
//================================================

#include "../Tools/Lib.C"
#include "../Tools/Par.C"
#include "../Tools/Style.C"

// Input: /home/hep/flionett/TestStand/Data/<sensor>/AttenuationScan/RootFiles/<filename without attenuation>-<x>dB-las.root
// Output: /home/hep/flionett/TestStand/AnalysisResults/<sensor>/AttenuationScan/AttenuationScan-<filename without attenuation>-<x>dB-las.root

void AttenuationScan(char *sensor, char *filename, char *externalPath = 0);

int main(int argc, char *argv[])
{
  dca();

  if ((argc == 2) && (string(argv[1]) == "--info"))
  {
    cout << "**************************************************" << endl;

    cout << "Some comments." << endl;
    
    cout << "**************************************************" << endl;

    return 0;
  }
  else if (argc < 3)
  {
    cout << "**************************************************" << endl;

    cout << "Error! Type of sensor or filename missing..." << endl;
    cout << "Please use the following format:" << endl;
    cout << "./AttenuationScan [1] [2] [3]" << endl;
    cout << "with:" << endl;
    cout << "[1] = Type of sensor (Hans410, ...);" << endl;
    cout << "[2] = Filename, excluding the attenuation value and the run type;" << endl;
    cout << "[3] = Additional folder, optional." << endl;
    cout << "Type ./AttenuationScan --info for more information." << endl;
    
    cout << "**************************************************" << endl;

    return 0;
  }
  else
  {
    cout << "Type of sensor: " << argv[1] << endl;
    cout << "Filename: " << argv[2] << endl;
    if (argc == 3)
      AttenuationScan(argv[1],argv[2]);
    else if (argc == 4)
      AttenuationScan(argv[1],argv[2],argv[3]);
    else
    {
      cout << "Error! Too many arguments given..." << endl;
      
      return 0;
    }
    
    return 0;  
  }
}



// Input: /home/hep/flionett/TestStand/Data/<sensor>/AttenuationScan/RootFiles/<filename without attenuation>-<x>dB-las.root
// Output: /home/hep/flionett/TestStand/AnalysisResults/<sensor>/AttenuationScan/AttenuationScan-<filename without attenuation>.root

void AttenuationScan(char *sensor, char *filename, char *externalPath)
{
  cout << "**************************************************" << endl;
  cout << "Performing attenuation scan..." << endl;
  cout << "**************************************************" << endl;

  // Do not comment this line.
  gROOT->ProcessLine("#include <vector>");

  // Do some fanciness to get the directory right.
  string inputDirectory = "~/TestStand/Data/"+string(sensor)+"/AttenuationScan/RootFiles";  
  string outputDirectory = "~/TestStand/AnalysisResults/"+string(sensor)+"/AttenuationScan";
  if (externalPath!=0)
    outputDirectory = string(outputDirectory+"/"+externalPath);
  cout << "The input directory is: " << inputDirectory << endl;
  cout << "The output directory is: " << outputDirectory << endl;

  // Create the outputDirectory directory if it does not exist.
  string path_to_make = "mkdir -p "+outputDirectory;
  system(path_to_make.c_str());

  cout << "Figures stored in: " << outputDirectory+"/Figures/"+filename << endl;

  // Create a directory named Figures inside the directory named outputDirectory if it does not exist.
  string path_to_make_figures = "mkdir -p "+outputDirectory+"/Figures/"+filename;
  system(path_to_make_figures.c_str());

  TString path_to_figures = (string(path_to_make_figures)).substr((string(path_to_make_figures)).find_last_of(' ')+1);

  // Execute findStrip.
  int strip;
  string direction;

  string input_findStrip = inputDirectory+"/"+string(filename)+"-15.5dB-las.root";

  // findStrip(input_findStrip,&strip,&direction);  

  string input_ROOT;
  string output_ROOT = outputDirectory+"/AttenuationScan-"+filename+".root";

  const Float_t mindB = 10.0;
  const Float_t maxdB = 20.0;
  const Float_t stepdB = 0.5;
  const Int_t steps = (Int_t)((maxdB-mindB)/stepdB+1);
  Float_t dB[steps];

  // Open input data ROOT files.
  input_ROOT = inputDirectory+"/"+string(filename)+"-"+"15.5"+"dB-las.root";
  TFile *input = TFile::Open(TString(input_ROOT));

  // Close input data ROOT files.
  input->Close();

  // Open output ROOT file.
  TFile *output = TFile::Open(TString(output_ROOT),"RECREATE");

  // Write output ROOT file.
  output->Write();

  // Close output ROOT file.
  output->Close();

  return;
}















/*

The main performs an attenuation scan by reading the files in the "folder" folder. It first executes findStrip on the file given as argument 2 and then uses this information for performing the scan.

Still to do:
- use the sum of the ADC counts;
- remove outliers and get the mean and the RMS again.

*/

/*

  //==================================================================
  //
  // Perform scan.
  //

  const Float_t mindB = 10.0;
  const Float_t maxdB = 20.0;
  const Float_t stepdB = 0.5;
  const Int_t steps = (Int_t)((maxdB-mindB)/stepdB+1);
  Float_t dB[steps];

  char inFile[200];
  char outFile[200];
  
  sprintf(outFile,"%s/attenuationScan-%s.root",analysis.c_str(),date.c_str());
  std::cout << "Output file: " << outFile << std::endl;

  TFile *output = TFile::Open(outFile,"RECREATE");

  TH1F *hADC[steps];
  TH1F *hsumADC[steps];

  TCanvas *cADC[steps];
  TCanvas *csumADC[steps];

  Float_t meanADC[steps];
  Float_t rmsADC[steps];

  Float_t meanSumADC[steps];
  Float_t rmsSumADC[steps];

  // Continue from here!!!

  for (int step=0;step<steps;step++) {
    dB[step] = mindB+step*stepdB;

    hADC[step] = new TH1F(Form("hADC-%s-%.1fdB-las",date.c_str(),dB[step]),"",100,0,1000);  
    hsumADC[step] = new TH1F(Form("hSumADC%s-%.1fdB-las",date.c_str(),dB[step]),"",100,0,1000);
    
    cADC[step] = new TCanvas(Form("cADC-%s-%.1fdB-las",date.c_str(),dB[step]),"",700,700);
    csumADC[step] = new TCanvas(Form("csumADC-%s-%.1fdB-las",date.c_str(),dB[step]),"",700,700);

    InitHist(hADC[step],"ADC counts on strip hit by the laser","ADC counts","");
    InitHist(hsumADC[step],"ADC counts on strip cluster","ADC counts","");
  }

  for (int step=0;step<steps;step++) {
    dB[step] = mindB+step*stepdB;
    sprintf(inFile,"%s/%s-%.1fdB-las.root",folder.c_str(),date.c_str(),dB[step]);
    std::cout << "Input file: " << inFile << std::endl;

    TFile *input = TFile::Open(inFile);

    //==================================================================
    //
    // Trees.
    //

    TTree *tree = (TTree *)input->Get("tree");
    Int_t evts = (Int_t)tree->GetEntries();
    std::cout << "Number of events: " << evts << std::endl;

    std::vector<float> *vec_adc = 0;
    TBranch *b = 0;
    tree->SetBranchAddress("vec_adc",&vec_adc,&b);

    //==================================================================
    //
    // Save information from trees.
    //

    for (int i=0;i<evts;i++)
    {
      tree->GetEntry(i);
      hADC[step]->Fill(vec_adc->at(strip));
      // std::cout << i << ", " << (vec_adc->at(strip-2*skip))+(vec_adc->at(strip-skip))+(vec_adc->at(strip))+(vec_adc->at(strip+skip)) << std::endl;
      // std::cout << i << ", " << (vec_adc->at(strip-skip))+(vec_adc->at(strip))+(vec_adc->at(strip+skip))+(vec_adc->at(strip+2*skip)) << std::endl;
      if (direction == "left") 
      {
	// std::cout << "left" << std::endl;
	hsumADC[step]->Fill((vec_adc->at(strip-2*skip))+(vec_adc->at(strip-skip))+(vec_adc->at(strip))+(vec_adc->at(strip+skip)));
      }
      else
      {
	// std::cout << "right" << std::endl;
	hsumADC[step]->Fill((vec_adc->at(strip-skip))+(vec_adc->at(strip))+(vec_adc->at(strip+skip))+(vec_adc->at(strip+2*skip)));
      }
    }

    //==================================================================
    //
    // Close input file.
    //

    input->Close();    

    //==================================================================
    //
    // Select output file.
    //

    output->cd();

    //==================================================================
    //
    // Get mean and RMS.
    //

    meanADC[step] = hADC[step]->GetMean();
    rmsADC[step] = hADC[step]->GetRMS();
    std::cout << "Mean of the ADC counts distribution: " << meanADC[step] << std::endl;
    std::cout << "RMS of the ADC counts distribution: " << rmsADC[step] << std::endl;
    
    meanSumADC[step] = hsumADC[step]->GetMean();
    rmsSumADC[step] = hsumADC[step]->GetRMS();
    std::cout << "Mean of the sum of the ADC counts of four adjacent strips distribution: " << meanSumADC[step] << std::endl;
    std::cout << "RMS of the sum of the ADC counts of four adjacent strips distribution: " << rmsSumADC[step] << std::endl;

    //==================================================================
    //
    // Plot useful information.
    //

    DrawHist(cADC[step],hADC[step],"",analysis);
    DrawHist(csumADC[step],hsumADC[step],"",analysis);

    //==================================================================
    //
    // Write output file.
    //
    
    output->Write();
  }

  //==================================================================
  //
  // Select output file.
  //

  output->cd();

  TGraph *gattenuationScan = new TGraph(steps,dB,meanADC);

  InitGraph(gattenuationScan,"Attenuation scan","Attenuation (dB)","ADC counts");

  TCanvas *cattenuationScan = new TCanvas(Form("cattenuationScan-%s",date.c_str()),"",700,700);

  DrawGraph(cattenuationScan,gattenuationScan,"APC",analysis);
  
  //==================================================================
  //
  // Write output file.
  //
  
  output->Write();

  //==================================================================
  //
  // Close output file.
  //

  output->Close();

  return 0;
}

*/