diff --git a/Software/LaserDelayScan/LaserDelayScan.C b/Software/LaserDelayScan/LaserDelayScan.C index ee020ef..3d03c95 100644 --- a/Software/LaserDelayScan/LaserDelayScan.C +++ b/Software/LaserDelayScan/LaserDelayScan.C @@ -19,6 +19,10 @@ - [input text file] is the complete path, including the folder and the filename, of the text file one wants to process; - [additional folder] is the optional additional folder where the output will be saved. + +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" @@ -88,16 +92,133 @@ // Do not comment this line. gROOT->ProcessLine("#include "); + string filename_as_string = string(filename); + // Check that the filename provided corresponds to a text file. + int found = filename_as_string.find(".dat"); + if (found==string::npos) { + cout << "Error! The filename provided is not associated to a text file." << endl; + return; + } + + // Open input text file. + ifstream datFile; + std::string line; + + // Number of rows to be read from the text file. + int rows = 0; + + // Determine number of rows to be read from the text file. + datFile.open(filename_as_string.c_str()); + if (datFile.is_open()) { + while (getline(datFile,line)) + rows++; + cout << "Number of rows in the input text file: " << rows << endl; + datFile.close(); + } + else + { + cout << "Unable to open file." << endl; + return; + } + + // Read the text file. + // Remember that the first two rows must be skipped. + float delay; + float signal; + + vector vdelay; + vector vsignal; + vector vdelayErr; + vector vsignalErr; + + Int_t i = 0; + + // Save information. + datFile.open(filename_as_string.c_str()); + if (datFile.is_open()) { + i = 0; + while (getline(datFile,line)) { + if (i>1) { + // cout << line << endl; + istringstream iss(line); + iss >> delay >> signal; + vdelay.push_back(delay); + vsignal.push_back(signal); + vdelayErr.push_back(0.); + vsignalErr.push_back(5.); + } + i++; + } + datFile.close(); + } + else + { + cout << "Unable to open file." << endl; + return; + } + + float *adelay = &vdelay[0]; + float *asignal = &vsignal[0]; + float *adelayErr = &vdelayErr[0]; + float *asignalErr = &vsignalErr[0]; + + /* + cout << adelay[0] << endl; + cout << adelay[1] << endl; + cout << asignal[0] << endl; + cout << asignal[1] << endl; + */ + // Do some fanciness to get the directory right. - + string analysis = "/home/hep/flionett/TestStand/AnalysisResults"; + if (externalPath!=0) + analysis = string(analysis+"/"+externalPath); + string filename_as_string_no_path = filename_as_string.substr(filename_as_string.find_last_of('/')+1); + string filename_as_string_no_path_no_extension = filename_as_string_no_path.substr(0,filename_as_string_no_path.length()-4); + // Create a folder named AnalysisResults. Do not worry, nothing bad is going to happen if the folder already exists. + string path_to_make = "mkdir -p "+analysis; + system(path_to_make.c_str()); + cout << "Setting output to: " << path_to_make << endl; + // Create a folder named Figures inside the folder named AnalysisResults. + string path_to_make_figures = "mkdir "+analysis+"/Figures"; + system(path_to_make_figures.c_str()); + path_to_make_figures = "mkdir "+analysis+"/Figures"+"/LaserDelayScan"; + system(path_to_make_figures.c_str()); + path_to_make_figures = "mkdir "+analysis+"/Figures"+"/LaserDelayScan"+"/"+filename_as_string_no_path_no_extension; + system(path_to_make_figures.c_str()); + cout << "Setting figures to: " << path_to_make_figures << endl; - + TString path_to_figures = (string(path_to_make_figures)).substr((string(path_to_make_figures)).find_last_of(' ')+1); + // Open output ROOT file. + TFile *output = TFile::Open(analysis+"/LaserDelayScan-"+TString(filename_as_string_no_path_no_extension)+".root","RECREATE"); + TCanvas *claserDelayScan = new TCanvas("claserDelayScan","",400,300); + int rowsData = rows-2; + + TGraphErrors *glaserDelayScan = new TGraphErrors(rowsData,adelay,asignal,adelayErr,asignalErr); + + // Fit with a polynomial function of degree 2 and get the delay and the signal at the peak. + // gStyle->SetOptFit(0011); + float minFit = 65.; + float maxFit = 95.; + TF1 *fpol2 = new TF1("fpol2","pol2",minFit,maxFit); + glaserDelayScan->Fit("fpol2","R"); + + float delayPeak = -1*(fpol2->GetParameter(1))/(2*fpol2->GetParameter(2)); + float signalPeak = fpol2->Eval(delayPeak); + + cout << "Delay at the peak: " << delayPeak << endl; + cout << "Signal at the peak: " << signalPeak << endl; + + InitGraphErrors(glaserDelayScan,"Laser delay scan","Delay (ns)","Average ADC signal"); + DrawGraphErrors(claserDelayScan,glaserDelayScan,"AP",path_to_figures); + + output->Close(); return; } diff --git a/Software/Tools/Lib.C b/Software/Tools/Lib.C index d926473..6455d48 100644 --- a/Software/Tools/Lib.C +++ b/Software/Tools/Lib.C @@ -48,6 +48,7 @@ // #include #include #include +#include #include #include diff --git a/Software/Tools/Style.C b/Software/Tools/Style.C index 0b767b5..c2a7c2e 100644 --- a/Software/Tools/Style.C +++ b/Software/Tools/Style.C @@ -41,8 +41,12 @@ void InitGraph(TGraph *graph, TString title = "", TString x = "", TString y = ""); +void InitGraphErrors(TGraphErrors *graph, TString title = "", TString x = "", TString y = ""); + void DrawGraph(TCanvas *canvas, TGraph *graph, TString option = "APC", TString folder = ""); +void DrawGraphErrors(TCanvas *canvas, TGraphErrors *graph, TString option = "APC", TString folder = ""); + TLegend *CreateLegend2(TObject *obj1, TString lab1, TObject *obj2, TString lab2, TString option = "lpfw", Double_t x1 = 0.64, Double_t y1 = 0.59, Double_t x2 = 0.94, Double_t y2 = 0.89); TLegend *CreateLegend3(TObject *obj1, TString lab1, TObject *obj2, TString lab2, TObject *obj3, TString lab3, TString option = "lpfw", Double_t x1 = 0.64, Double_t y1 = 0.59, Double_t x2 = 0.94, Double_t y2 = 0.89); @@ -389,6 +393,15 @@ return; } +// InitGraphErrors defines some attributes of the "graph" graph: the title "title" and the x-axis and y-axis labels "x" and "y". +void InitGraphErrors(TGraphErrors *graph, TString title, TString x, TString y) { + graph->SetTitle(title); + graph->GetXaxis()->SetTitle(x); + graph->GetYaxis()->SetTitle(y); + + return; +} + // DrawGraph draws the "graph" graph in the "canvas" canvas, by default with the "APC" option, adds the "leg" legend, and saves the result in the "folder" folder, in a pdf file named "canvas.pdf". // "A" = with axes // "P" = with points @@ -418,6 +431,35 @@ return; } +// DrawGraph draws the "graph" graph in the "canvas" canvas, by default with the "APC" option, adds the "leg" legend, and saves the result in the "folder" folder, in a pdf file named "canvas.pdf". +// "A" = with axes +// "P" = with points +// "C" = with smooth curves +void DrawGraphErrors(TCanvas *canvas, TGraphErrors *graph, TString option, TString folder) { + TString path; + path = folder; + TString name = canvas->GetName(); + canvas->cd(); + canvas->SetTickx(1); + canvas->SetTicky(1); + graph->Draw(option); + graph->SetFillColor(38); + graph->SetMarkerSize(1); + graph->SetMarkerStyle(20); + graph->SetMarkerColor(1); + graph->SetLineWidth(2); + graph->SetLineStyle(1); + graph->SetLineColor(2); + gPad->Modified(); + canvas->Update(); + canvas->Write(); + name = path + "/" + name + ".pdf"; + canvas->SaveAs(name); + canvas->Close(); + + return; +} + // CreateLegend2 creates and returns the legend for "obj1" (with "lab1" label) and "obj2" (with "lab2" label) objects. It allows to specify the drawing options ("lpfw" by default) and the posizion and size inside the canvas. // l = lines // p = points