#include <stdio.h>
#include <stdlib.h>
#include <TROOT.h>
#include <TH1D.h>
#include <TH1F.h>
#include <TH2D.h>
#include <TH2F.h>
#include <TH2I.h>
#include <TNtuple.h>
#include <TTree.h>
#include <TGraph.h>
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TFile.h>
#include <TDirectory.h>
#include <TPaveText.h>
#include <TSpectrum.h>
#include <TF1.h>
#include <TColor.h>
#include <algorithm>
using namespace std;
#include <iostream>
#include <string>
#include "base.h"
#include "gain.h"
#include "savetoroot.h"
int charge(int runNumber, int save){
int HAPDnumber; //Number of HAPD: 0, 1, 2 or 3!
double xmin = -50;
double xmax = 300;
char name[64];
char pdfname[128];
char buf[256];
char hname[0xF];
sprintf(buf
,"../modules/%04d/%04d_1_Treshold_Waveform.root",runNumber
,runNumber
);
TFile * data = new TFile(buf);
const char * serialNumber = getSN(runNumber);
printf("base.c return: %s\n", serialNumber
);
/******* PREVERI ČE JE NALOŽEN FILE *******/
if (!strcmp(serialNumber
, "NoFILE")){
printf("Ni naloženege datoteke\n");
return -1;
}
std::string serialNumberTemp = serialNumber;
std::cout << serialNumberTemp << std::endl;
std::string HAPDserialNumber,FEBserialNumber;
GRID m = mapping();
int vrstaPisave = 82;
TCanvas * c, * c4;
TH1D * h;
TH2D * h2 = NULL;
TPad * pad1, * pad2, * pad3;
TPaveText * sn, * gainTitle;
/********** GLOBALNE **********/
gStyle->SetOptStat(0);
//gStyle->SetOptTitle(1);
gStyle->SetTextFont(vrstaPisave);
gStyle->SetTitleFont(vrstaPisave);
gStyle->SetTitleFontSize(0.1);
gStyle->SetNumberContours(100);
gStyle->SetPalette(55);
//gROOT->SetBatch(kTRUE); //Does not show canvases
/******* ZRIHTA GRAFE *******/
for(HAPDnumber=0;HAPDnumber<4;HAPDnumber++){
if(save==2) {
SaveToRootInit();
}
//std::cout << serialNumberTemp << std::endl;
FEBserialNumber = serialNumberTemp.substr(0,serialNumberTemp.find(","));
serialNumberTemp.erase(0,FEBserialNumber.length()+1);
if(!FEBserialNumber.compare("noserial")) continue;
HAPDserialNumber = FEBserialNumber.substr(0,FEBserialNumber.find("/"));
FEBserialNumber.erase(0,FEBserialNumber.find("/")+1);
sprintf(hname
,"hcharge%d_0",HAPDnumber
);
printf("%s \t %s \t %s \n",FEBserialNumber.
c_str(),HAPDserialNumber.
c_str(),hname
);
sprintf(name
,"%04d_%s_Charge_c4",runNumber
,HAPDserialNumber.
c_str());
c4 = new TCanvas(name,name,0,0,1200,1200);
c4->Divide(2,2);
for (int chip=0;chip<4; chip++){
sprintf(name
,"%04d_%s_c%d",runNumber
,HAPDserialNumber.
c_str(),chip
);
c = new TCanvas(name,name,0,0,1200,1200);
c->cd();
pad1 = new TPad("pad11","SN",0,0.975,1,1,0,0);
pad1->SetFillStyle(4000);
pad1->Draw();
pad1->cd();
sn = new TPaveText(0.2,0,0.8,1,"ndc");
sn->SetBorderSize(0);
sn->SetFillColor(4000);
sn->SetTextSize(.8);
sn->SetTextAlign(22);
sprintf(buf
,"Charge %s",HAPDserialNumber.
c_str());
sn->AddText(buf);
//sn->Draw();
/******* DRUGI PAD *******/
c->cd();
pad2 = new TPad("pad2","Date",0.05,0.95,0.95,.99,0,0);
pad2->SetFillStyle(4000);
pad2->Draw();
pad2->cd();
gainTitle = new TPaveText(0,0,1,1,"ndc");
gainTitle->SetBorderSize(0);
gainTitle->SetFillColor(kSpring+1);
gainTitle->SetTextSize(.8);
gainTitle->SetTextAlign(22);
sprintf(buf
,"Charge scan over center of channels:");
switch(chip){
case 0: sprintf(buf
,"%s Chip A",buf
);break;
case 1: sprintf(buf
,"%s Chip B",buf
);break;
case 2: sprintf(buf
,"%s Chip D",buf
);break;
case 3: sprintf(buf
,"%s Chip C",buf
);break;
}
gainTitle->AddText(buf);
gainTitle->Draw();
//----------- PAD 2 (FIGURES)
c->cd();
pad3 = new TPad("pad3","Figures",0,0,1,0.96,0,0);
pad3->SetFillStyle(4000);
pad3->Draw();
pad3->cd();
pad3->Divide(6,6,0,0,0);
for (int i=0;i<36;i++) {
int idx = i+chip*36;
sprintf(name
,"%s;%d",hname
,m.
pozicijaPixla[idx
]+1);
h = ((TH1D * ) data->Get(name));
TVirtualPad *pad = pad3->cd(m.pozicijaPixlaNaCipu[idx]+1);
pad->SetBorderSize(0);
pad->SetBorderMode(0);
if (idx==0 && h->InheritsFrom("TH1D") ) {
sprintf(name
,"%s_2d",h
->GetName
());
TAxis *axis = h->GetXaxis();
h2 = new TH2D(name,h->GetTitle(),144,-0.5,143.5,axis->GetNbins(),
axis->GetXmin(),
axis->GetXmax());
h2->GetXaxis()->SetTitle("HAPD 2d channels");
h2->GetYaxis()->SetTitle(axis->GetTitle());
h2->GetYaxis()->SetTitleOffset(1.4);
}
if (h) {
//printf("TH2D %s\n",name);
sprintf(name
,"ASIC %d ch %d",chip
,idx
);
h->SetTitle(name);
h->GetXaxis()->SetRangeUser(xmin,xmax);
h->SetMinimum(-0.1);
h->SetStats(kFALSE);
h->SetFillColor(kBlue);
h->DrawCopy("AH");
if(save==2) {
sprintf(text_for_STR
, "Charge_1D_%s_ASIC_%d_CH_%d", HAPDserialNumber.
c_str(),chip
,idx
);
SaveToRootAddTH1DCopy(h, text_for_STR);
}
if (h2){
for(int k=xmin;k<h->GetNbinsX(); k++){
//if (idx==133) printf("x = %d \t y = %d \t value = %d\n", idx+1, k,(int)h->GetBinContent(k+1));
if ((int)h->GetBinContent(k+1)==0) h2->SetBinContent(idx+1,k+1,1);
else h2->SetBinContent(idx+1,k+1,h->GetBinContent(k+1));
}
}
} else printf("TH2D %s does not exist !!!!\n",name
);
}
c->Modified();
c->Update();
if(chip%2==0) c4->cd(chip+2);
else c4->cd(chip);
c->DrawClonePad();
if (save==1){
switch (chip) {
case 0: sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Charge.pdf(",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
default: sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Charge.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
}
c->SaveAs(pdfname,"pdf");
} else if (save==4){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c->SaveAs(pdfname,"pdf");
}
}
if (h2) {
sprintf(name
,"%04d_%s_c%d",runNumber
,HAPDserialNumber.
c_str(),5);
c = new TCanvas(name,name,600,500);
c->SetLeftMargin(0.15);
c->SetRightMargin(0.15);
c->SetLogz(1);
sprintf(buf
,"Charge scan over the center of channels");
h2->SetTitle(buf);
gStyle->SetTitleFontSize(0.05);
h2->SetTitleFont(vrstaPisave);
h2->SetTitleOffset(0);
h2->GetZaxis()->SetRangeUser(1,10000);
h2->GetZaxis()->SetTickLength(.02);
h2->GetZaxis()->SetLabelFont(vrstaPisave);
h2->GetZaxis()->SetLabelSize(0.03);
h2->GetYaxis()->SetRangeUser(xmin,xmax);
h2->GetYaxis()->SetTitleFont(vrstaPisave);
h2->GetYaxis()->SetLabelFont(vrstaPisave);
//h2->GetYaxis()->SetLabelSize(0.04);
h2->GetYaxis()->SetLabelColor(kBlack);
//h2->GetYaxis()->SetNdivisions(12);
h2->GetYaxis()->CenterTitle();
h2->GetYaxis()->SetTitle("ADC channel (a.u.)");
//h2->GetYaxis()->SetTitleSize(0.05);
h2->GetYaxis()->SetTitleOffset(1.5);
h2->GetYaxis()->SetTitleColor(kBlack);
//h2->GetXaxis()->SetTickLength(0.05);
h2->GetXaxis()->SetTitleFont(vrstaPisave);
h2->GetXaxis()->SetLabelFont(vrstaPisave);
//h2->GetXaxis()->SetLabelSize(0.04);
h2->GetXaxis()->SetLabelColor(kBlack);
//h2->GetXaxis()->SetLabelOffset(.05);
//h2->GetXaxis()->SetNdivisions(0);
//h2->GetXaxis()->CenterTitle();
h2->GetXaxis()->SetTitle("Channel");
//h2->GetXaxis()->SetTitleSize(0.05);
h2->GetXaxis()->SetTitleColor(kBlack); //zato ker ga ne moreš pozicionirat
h2->GetXaxis()->SetTitleOffset(1.2);
h2->DrawCopy("COLZ");
if(save==2) {
sprintf(text_for_STR
, "Charge_2D_%s", HAPDserialNumber.
c_str());
SaveToRootAddTH2DCopy(h2, text_for_STR);
}
c->Modified();
c->Update();
if (save==1){
sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Charge.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c4->SaveAs(pdfname,"pdf");
sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Charge.pdf)",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c->SaveAs(pdfname,"pdf");
} else if (save==2){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c4->SaveAs(pdfname,"pdf");
c->SaveAs(pdfname,"pdf");
}
if(save==2) {
sprintf(text_for_STR
,"../modules/%04d/%04d_%s_out.root",runNumber
,runNumber
,HAPDserialNumber.
c_str());
SaveToRootWrite(text_for_STR, HAPDserialNumber.c_str());
}
}
}
delete data;
return 0;
}
float getGain(TH1D * h, int deadChannelSigma) {
TF1 * g0, * g1, * g2, * g3;
float gain;
float mean1, sigma1, mean2;
//Double_t * xpeaks;
Float_t * xpeaks;
TSpectrum * s = new TSpectrum(3);
int nfound = s->Search(h); //TALE FUNKICJA LAHKO POVZROČA TEŽAVE, KER NI NUJNO DA VRNE VRHOVE V PRAVEM ZAPOREDJU
//printf("Found %d candidate peaks to fit\n",nfound);
xpeaks = s->GetPositionX();
Float_t * first(&xpeaks[0]);
Float_t * last(first + nfound);
std::sort(first, last);
float xp;
for (int p=0;p<nfound;p++) {
xp = xpeaks[p];
//printf("Vrh številka %d je na poziciji %f\n",p,xp);
if(p==0) {
g0 = new TF1("0ph","gaus",xp-14,xp+14);
h->Fit(g0,"QR");
} else if(p==1) {
g1 = new TF1("1ph","gaus",xp-14,xp+14);
h->Fit(g1,"QR+");
} else if(p==2) {
g2 = new TF1("2ph","gaus",xp-14,xp+14);
h->Fit(g2,"QR+");
} else {
g3 = new TF1("3ph","gaus",xp-14,xp+14);
h->Fit(g3,"QR+");
}
}
if(nfound >= 1){
mean1 = g0->GetParameter(1);
sigma1 = g0->GetParameter(2);
if (sigma1<deadChannelSigma) gain = 0;
else gain = 30;
} else {
gain = 0;
}
if(nfound > 1){
mean2 = g1->GetParameter(1);
printf("%f \t %f\n", mean1
, mean2
);
if (mean1<mean2) gain = mean2 - mean1;
else gain = mean1 - mean2;
//printf("nfound: %d \t gain: %.1f \t mean1: %.1f \t sigma1: %.1f \t mean2: %.1f\n", nfound, gain, mean1, sigma1, mean2);
}
delete s;
return gain;
}
int gain(int runNumber, int save){
int HAPDnumber = 0; //Number of HAPD: 0, 1, 2 or 3!
char name[128];
char pdfname[128];
char buf[0xFF];
float ojacanje;
char hname[0xF];
sprintf(buf
,"../modules/%04d/%04d_1_Treshold_Waveform.root",runNumber
,runNumber
);
TFile * data = new TFile(buf);
const char * serialNumber = getSN(runNumber);
printf("base.c return: %s\n", serialNumber
);
/******* PREVERI ČE JE NALOŽEN FILE *******/
if (!strcmp(serialNumber
, "NoFILE")){
printf("Ni naloženege datoteke\n");
return -1;
}
std::string serialNumberTemp = serialNumber;
std::cout << serialNumberTemp << std::endl;
std::string HAPDserialNumber,FEBserialNumber;
GRID m = mapping();
int vrstaPisave = 82;
int blackWhite = 40;
int max = 140;
TCanvas * c1, * c2, * c3;
TPad * pad1, * pad2;
TPaveText * title;
TH1D * h;
TH1F * Gain1D;
TH2F * Gain2D;
TH2I * Gain2D_black, * Gain2D_white;
gStyle->SetOptStat(0);
gStyle->SetPalette(52);
gStyle->SetNumberContours(100);
for(HAPDnumber=0;HAPDnumber<4;HAPDnumber++){
SaveToRootInit();
//std::cout << serialNumberTemp << std::endl;
FEBserialNumber = serialNumberTemp.substr(0,serialNumberTemp.find(","));
serialNumberTemp.erase(0,FEBserialNumber.length()+1);
if(!FEBserialNumber.compare("noserial")) continue;
HAPDserialNumber = FEBserialNumber.substr(0,FEBserialNumber.find("/"));
FEBserialNumber.erase(0,FEBserialNumber.find("/")+1);
sprintf(hname
,"hcharge%d_0",HAPDnumber
);
printf("%s \t %s \t %s \n",FEBserialNumber.
c_str(),HAPDserialNumber.
c_str(),hname
);
/******* 1D *******/
sprintf(buf
,"%04d_%s_Gain1D",runNumber
,HAPDserialNumber.
c_str());
c1 = new TCanvas(buf,buf,0,0,600,300);
sprintf(name
,"1D Gain of HAPD %s (%04d);Channel number;Gain",HAPDserialNumber.
c_str(),runNumber
);
Gain1D = new TH1F("Gain1D",name,144,0,144);
/******* 2D *******/
sprintf(buf
,"%04d_%s_Gain2D",runNumber
,HAPDserialNumber.
c_str());
c2 = new TCanvas(buf,buf,0,0,600,600);
c2->SetRightMargin(0.15);
sprintf(name
,"Backside view: Gain of HAPD %s (%04d)",HAPDserialNumber.
c_str(),runNumber
);
Gain2D = new TH2F("Gain2D",name,12,-0.5,11.5,12,-0.5,11.5);
Gain2D_black = new TH2I("Gain2D_black","Gain2D_black",12,-0.5,11.5,12,-0.5,11.5);
Gain2D_white = new TH2I("Gain2D_white","Gain2D_white",12,-0.5,11.5,12,-0.5,11.5);
Gain2D->SetMinimum(-0.1);
Gain2D->SetMaximum(max);
Gain2D_black->SetMinimum(blackWhite);
/******* NAREDI HISTOGRAME *******/
for (int i=0;i<12;i++) {
for (int j=0;j<12;j++) {
sprintf(name
,"%s;%d",hname
,m.
pozicijaPixla[m.
koordinatniSistem[i
][j
]]+1);
//printf("%s\n",name);
h = (TH1D *) data->Get(name);
h->GetXaxis()->SetRangeUser(-50,300);
ojacanje = getGain(h);
//printf("channel %d \t gain %f\n", m.koordinatniSistem[i][j], ojacanje);
Gain1D->Fill(m.koordinatniSistem[i][j],ojacanje);
Gain2D->Fill(i,j,ojacanje);
if(ojacanje<=blackWhite) Gain2D_white->Fill(i,j,ojacanje);
Gain2D_black->Fill(i,j,ojacanje);
}
}
c1->cd();
c1->SetGrid(1);
Gain1D->SetTitle("");
//Gain1D->SetLineWidth(2);
Gain1D->SetFillColor(kRed-10);
Gain1D->GetYaxis()->SetRangeUser(0,140);
Gain1D->GetYaxis()->SetTickLength(0.01);
Gain1D->GetYaxis()->SetTitleFont(vrstaPisave);
Gain1D->GetYaxis()->SetLabelFont(vrstaPisave);
Gain1D->GetYaxis()->SetLabelSize(0.04);
Gain1D->GetYaxis()->SetLabelColor(kBlack);
//Gain1D->GetYaxis()->SetNdivisions(0);
Gain1D->GetYaxis()->CenterTitle();
Gain1D->GetYaxis()->SetTitle("Gain");
Gain1D->GetYaxis()->SetTitleSize(0.05);
//Gain1D->GetYaxis()->SetTitleOffset(.5);
Gain1D->GetYaxis()->SetTitleColor(kBlack);
//Gain1D->GetXaxis()->SetRangeUser(0,80);
//Gain1D->GetXaxis()->SetTickLength(0.05);
Gain1D->GetXaxis()->SetTitleFont(vrstaPisave);
Gain1D->GetXaxis()->SetLabelFont(vrstaPisave);
Gain1D->GetXaxis()->SetLabelSize(0.04);
Gain1D->GetXaxis()->SetLabelColor(kBlack);
//Gain1D->GetXaxis()->SetLabelOffset(.05);
//Gain1D->GetXaxis()->SetNdivisions(0);
//Gain1D->GetXaxis()->CenterTitle();
Gain1D->GetXaxis()->SetTitle("Channel");
Gain1D->GetXaxis()->SetTitleSize(0.05);
Gain1D->GetXaxis()->SetTitleColor(kBlack); //zato ker ga ne moreš pozicionirat
Gain1D->GetXaxis()->SetTitleOffset(1);
Gain1D->DrawCopy();
c2->cd();
Gain2D->SetTitle("");
Gain2D->GetZaxis()->SetTickLength(.02);
Gain2D->GetZaxis()->SetTitleFont(vrstaPisave);
Gain2D->GetZaxis()->SetLabelFont(vrstaPisave);
Gain2D->GetZaxis()->SetLabelSize(0.03);
Gain2D->GetZaxis()->SetTitle("G_{a}");
Gain2D->GetZaxis()->CenterTitle();
//Gain2D->GetYaxis()->SetTickLength(0.03);
Gain2D->GetYaxis()->SetTitleFont(vrstaPisave);
Gain2D->GetYaxis()->SetLabelFont(vrstaPisave);
Gain2D->GetYaxis()->SetLabelSize(0.03);
Gain2D->GetYaxis()->SetLabelColor(kBlack);
Gain2D->GetYaxis()->SetLabelOffset(.01);
//Gain2D->GetYaxis()->SetNdivisions(12);
Gain2D->GetYaxis()->CenterTitle();
Gain2D->GetYaxis()->SetTitle("Rows");
Gain2D->GetYaxis()->SetTitleSize(0.035);
Gain2D->GetYaxis()->SetTitleOffset(1.2);
Gain2D->GetYaxis()->SetTitleColor(kBlack);
//Gain2D->GetXaxis()->SetTickLength(0.03);
Gain2D->GetXaxis()->SetTitleFont(vrstaPisave);
Gain2D->GetXaxis()->SetLabelFont(vrstaPisave);
Gain2D->GetXaxis()->SetLabelSize(0.03);
Gain2D->GetXaxis()->SetLabelColor(kBlack);
//Gain2D->GetXaxis()->SetLabelOffset(.05);
//Gain2D->GetXaxis()->SetNdivisions(0);
//Gain2D->GetXaxis()->CenterTitle();
Gain2D->GetXaxis()->SetTitle("Columns");
Gain2D->GetXaxis()->SetTitleSize(0.03);
Gain2D->GetXaxis()->SetTitleColor(kBlack); //zato ker ga ne moreš pozicionirat
Gain2D->GetXaxis()->SetTitleOffset(1.2);
Gain2D->DrawCopy("COLZ");
Gain2D_black->DrawCopy("TEXT,SAME");
Gain2D_white->SetMarkerColor(kWhite);
Gain2D_white->DrawCopy("TEXT,SAME");
c1->Modified();
c1->Update();
c2->Modified();
c2->Update();
sprintf(buf
,"%04d_%s_Gain",runNumber
,HAPDserialNumber.
c_str());
c3 = new TCanvas(buf,buf,0,0,1000,1000);
c3->cd();
/******* 1D *******/
sprintf(buf
,"%04d_%s_gain1d_pad1",runNumber
,HAPDserialNumber.
c_str());
pad1 = new TPad(buf,"SN",.15,0.7,.85,1,0,0);
pad1->SetFillStyle(4000);
pad1->Draw();
//pad1->SetRightMargin(0.15);
/******* 2D *******/
sprintf(buf
,"%04d_%s_gain2d_pad2",runNumber
,HAPDserialNumber.
c_str());
pad2 = new TPad(buf,"Date",.15,0,.85,0.7,0,0);
pad2->SetFillStyle(4000);
pad2->Draw();
pad2->SetRightMargin(0.15);
pad1->cd();
pad1->SetGrid(1);
Gain1D->DrawCopy();
sprintf(text_for_STR
, "Gain_1D_%s", HAPDserialNumber.
c_str());
SaveToRootAddTH1F(Gain1D, text_for_STR);
title = new TPaveText(0.2,.93,0.8,.94,"ndc");
title->SetBorderSize(0);
title->SetFillColor(4000);
title->SetTextSize(.06);
title->SetTextAlign(22);
title->SetTextFont(vrstaPisave);
title->AddText("Gain");
title->Draw();
pad2->cd();
Gain2D->DrawCopy("COLZ");
Gain2D_black->DrawCopy("TEXT,SAME");
Gain2D_white->SetMarkerColor(kWhite);
Gain2D_white->DrawCopy("TEXT,SAME");
sprintf(text_for_STR
, "Gain_2D_%s", HAPDserialNumber.
c_str());
SaveToRootAddTH2F(Gain2D, text_for_STR);
title = new TPaveText(0.2,.91,0.8,.93,"ndc");
title->SetBorderSize(0);
title->SetFillColor(4000);
title->SetTextSize(.035);
title->SetTextAlign(22);
title->SetTextFont(vrstaPisave);
title->AddText("Backside view gain");
title->Draw();
delete c1;
delete c2;
if (save==1) {
sprintf(pdfname
,"../modules/%04d/%04d_%s_Gain.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c3->SaveAs(pdfname,"pdf");
} else if (save==2){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c3->SaveAs(pdfname,"pdf");
} else if (save==3){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf(",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c3->SaveAs(pdfname,"pdf");
} else if (save==5){
sprintf(pdfname
,"../modules/%04d/%04d_%s.root",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c3->SaveAs(pdfname,"root");
}
sprintf(text_for_STR
,"../modules/%04d/%04d_%s_out.root",runNumber
,runNumber
,HAPDserialNumber.
c_str());
SaveToRootWrite(text_for_STR, HAPDserialNumber.c_str());
delete Gain1D;
delete Gain2D;
delete Gain2D_white;
delete Gain2D_black;
}
delete data;
return 0;
}