Newer
Older
TestStandRepository / Software / CheckAlignment / CheckAlignment.C
@Federica Lionetto Federica Lionetto on 25 Nov 2014 7 KB Changes in Arduino scripts
//************************************************
// Author: Federica Lionetto
// Created on: 21/11/2014
//************************************************

/*
CheckAlignment reads all the ROOT files of a given alignment data taking, calls FindStrip for each of them to find the Beetle channel corresponding to the strip hit by the laser, calculates the mean of the sum of the ADC counts of four adjacent strips around that hit by the laser as a function of the position along x (horizontal direction), and creates a ROOT file with the following information:
- a graph with the mean of the sum of the ADC counts of four adjacent strips around that hit by the laser as a function of the position along x.

The alignment data taking is identified by the following information:
- <sensor>, that is, the type of sensor (Hans410, ...);
- <filename>, that is, the filename excluding the position value and the run type.
- <firstx>, that is, the first position along x;
- <lastx>, that is, the last position along x;
- <stepx>, that is, the step between two subsequent data acquisitions (1 step = 5 microns).

Compile with:

make

Run with:

./CheckAlignment [sensor] [filename] [firstx] [lastx] [stepx] [additional folder]

For example:

./CheckAlignment Hans410 20141121 0 75 3 Hans410/Alignment

A folder named AnalysisResults will be created in a fixed location and a ROOT file will be saved in there. A folder named Figures will be created inside the folder named AnalysisResults, with some monitoring plots.

If needed, an additional folder can be specified, that will be created inside the folder named AnalysisResults.
*/

#include "../Tools/Lib.C"
#include "../Tools/lhcbStyle.C"
#include "../Tools/Style.C"
#include "../Tools/Useful.C"

#include "../Tools/FindStrip.C"
#include "../Tools/Par.C"

void CheckAlignment(char *sensor, char *filename, const Float_t firstx, const Float_t lastx, const Float_t stepx, char *externalPath=0);

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

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

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

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

    cout << "Error! Arguments missing..." << endl;
    cout << "Please use the following format:" << endl;
    cout << "./CheckAlignment [1] [2] [3] [4] [5] [6]" << endl;
    cout << "with:" << endl;
    cout << "[1] = Type of sensor (Hans410, ...);" << endl;
    cout << "[2] = Filename, excluding the position value and the run type;" << endl;
    cout << "[3] = First position along x;" << endl;
    cout << "[4] = Last position along x" << endl;
    cout << "[5] = Step between two subsequent data acquisitions (1 step = 5 microns);" << endl;
    cout << "[6] = Additional folder, optional." << endl;
    cout << "Type ./CheckAlignment --info for more information." << endl;
    
    cout << "**************************************************" << endl;

    return 0;
  }
  else
  {
    cout << "Type of sensor: " << argv[1] << endl;
    cout << "Filename: " << argv[2] << endl;
    cout << "First position along x: " << argv[3] << endl;
    cout << "Last position along x: " << argv[4] << endl;
    cout << "Step between two subsequent data acquisitions: " << argv[5] << endl;
    if (argc == 6)
      CheckAlignment(argv[1],argv[2],atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
    else if (argc == 7)
      CheckAlignment(argv[1],argv[2],atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),argv[6]);
    else
    {
      cout << "Error! Too many arguments given..." << endl;
      
      return 0;
    }
    
    return 0;  
  }
}

void CheckAlignment(char *sensor, char *filename, const Float_t firstx, const Float_t lastx, const Float_t stepx, char *externalPath)
{
  cout << "**************************************************" << endl;
  cout << "Checking alignment..." << endl;
  cout << "**************************************************" << endl;

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

  // Do some fanciness to get the directory right.
  string inputDirectory = "~/TestStand/AnalysisResults/"+string(sensor)+"/Alignment";  
  string outputDirectory = "~/TestStand/AnalysisResults/"+string(sensor)+"/Alignment";
  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);

  int strip;
  string direction;

  char char_input_ROOT[200];
  string input_ROOT;
  string output_ROOT = outputDirectory+"/Alignment-"+filename+".root";
  string inputFindStrip;
  string outputFindStrip;
  ostringstream convert;
  string temp;

  const Int_t steps = (Int_t)((lastx-firstx)/stepx+1);
  Float_t x[steps];
  Float_t mean4ADC[steps];

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

  for (Int_t step=0;step<steps;step++) {
    x[step] = firstx+step*stepx;

    // Open input data ROOT files.
    // I did not find a better way to create a string containing the bias voltage.
    sprintf(char_input_ROOT,"%s/%s-%dx-0z-las.root",inputDirectory.c_str(),filename,(int)x[step]);
    input_ROOT = string(char_input_ROOT);
    // Check that the filename provided corresponds to a ROOT file.
    int found = input_ROOT.find(".root");
    if (found==string::npos)  {
      cout << "Error! The filename provided is not associated to a ROOT file." << endl;
      return;
    }

    cout << "Open ROOT file #" << step+1 << ": " << input_ROOT << endl;
    TFile *input = TFile::Open(TString(input_ROOT));

    inputFindStrip = input_ROOT;
    convert.str("");
    convert << x[step];
    temp = convert.str();
    outputFindStrip = outputDirectory+"/FindStrip-"+filename+"-"+temp+"x-0z-las.root";
    FindStrip(inputFindStrip,outputFindStrip,path_to_figures,&strip,&direction);  

    cout << "Histograms: " << endl;
    cout << Form("hADCPedSub%d",strip) << endl;
    cout << Form("hADCPedSub%d",strip+NSkip) << endl;
    cout << Form("hADCPedSub%d",strip-NSkip) << endl;
    cout << Form("hADCPedSub%d",strip-2*NSkip) << endl;
    cout << Form("hADCPedSub%d",strip+2*NSkip) << endl;

    // Four adjacent strips.
    TH1D *hist1 = (TH1D *)input->Get(Form("hADCPedSub%d",strip));
    TH1D *hist2 = (TH1D *)input->Get(Form("hADCPedSub%d",strip+NSkip));
    TH1D *hist3 = (TH1D *)input->Get(Form("hADCPedSub%d",strip-NSkip));
    TH1D *hist4;
    if (direction == "left")
      hist4 = (TH1D *)input->Get(Form("hADCPedSub%d",strip-2*NSkip));
    else if (direction == "right")
      hist4 = (TH1D *)input->Get(Form("hADCPedSub%d",strip+2*NSkip));
    mean4ADC[step] = hist1->GetMean()+hist2->GetMean()+hist3->GetMean()+hist4->GetMean();

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

  output->cd();

  for (Int_t step=0;step<steps;step++)
    x[step] = x[step]*5.; // One step corresponds to 5 microns.

  // Four adjacent strips.
  TGraph *gcheckAlignment4 = new TGraph(steps,x,mean4ADC);
  InitGraph(gcheckAlignment4,"Check alignment - 4 adjacent strips","x (#mum)","ADC counts");
  TCanvas *ccheckAlignment4 = new TCanvas(Form("ccheckAlignment4-%s",filename),"",400,300);
  DrawGraph(ccheckAlignment4,gcheckAlignment4,"APC",path_to_figures);

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

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

  return;
}