#include <stdio.h>
#include <stdlib.h>
#include <TROOT.h>
#include <TH1D.h>
#include <TH2D.h>
#include <TCanvas.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TFile.h>
#include <TColor.h>
#include <TDirectory.h>
#include <TPaveText.h>
#include <iostream>
#include <string>
#include "base.h"
#include "treshold.h"
#include "savetoroot.h"
int treshold(int runNumber, int save){
int HAPDnumber; //Number of HAPD: 0, 1, 2 or 3!
char name[128];
char pdfname[128];
char buf[256];
const char * hname = "h2d_0";
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;
TH2D * h;
TH1D * p = NULL;
TH2D * h2 = NULL;
TPad * pad1, * pad2, * pad3;
TPaveText * sn, * gainTitle;
/*
TFile * out;
//----------- CREATE OUTPUT FILE
sprintf(buf,"../modules/%04d/%04d.root",runNumber,runNumber);
if (file_exists(buf)){
//out = new TFile(path,"UPDATE");
printf("File %s already exists. To create new, delete old.\n",buf);
return 1;
} else {
out = new TFile(buf,"RECREATE");
printf("Creating file %s\n",buf);
}
*/
/********** 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
/******* PREVERI ČE JE NALOŽEN FILE *******/
if (!strcmp(serialNumber
, "NoFILE")){
printf("Ni naloženege datoteke\n");
return 0;
}
/******* 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);
printf("%s \t %s \t %s \n",FEBserialNumber.
c_str(),HAPDserialNumber.
c_str(),hname
);
sprintf(name
,"%04d_%s_Treshold_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_Treshold_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
,"%s (treshold scan)",HAPDserialNumber.
c_str());
sn->AddText(buf);
//sn->Draw();
/******* DRUGI PAD *******/
c->cd();
pad2 = new TPad("pad2","Date",0.05,0.95,0.95,0.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(.6);
gainTitle->SetTextAlign(22);
sprintf(buf
,"Treshold 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("pad2","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 = (TH2D *) data->Get(name);
TVirtualPad * pad = pad3->cd(m.pozicijaPixlaNaCipu[idx]+1);
pad->SetBorderSize(0);
pad->SetBorderMode(0);
if (h){
sprintf(name
,"HAPD%d ASIC %d ch %d",HAPDnumber
,chip
,idx
);
p = h->ProjectionY(name,idx+HAPDnumber*144+1,idx+HAPDnumber*144+1);
p->SetTitle(name);
p->SetStats(kFALSE);
p->SetFillColor(kBlue);
p->SetMinimum(-0.1);
p->DrawCopy("AH");
if(save==2) {
sprintf(text_for_STR
, "Threshold_1D_%s_ASIC_%d_CH_%d", HAPDserialNumber.
c_str(),chip
,idx
);
SaveToRootAddTH1DCopy(p, text_for_STR);
}
if (idx==0 && h->InheritsFrom("TH2D")) {
sprintf(name
,"%s_2d",p
->GetName
());
TAxis * axis = p->GetXaxis();
h2 = new TH2D(name,p->GetTitle(),144,-0.5,143.5,axis->GetNbins(),
axis->GetXmin(),
axis->GetXmax() );
h2->SetTitle("HAPD Treshold scan");
h2->GetXaxis()->SetTitle("HAPD 2d channels");
h2->GetYaxis()->SetTitle("Treshold");
h2->GetYaxis()->SetTitleOffset(1.4);
}
if (h2){
for (int k=0;k<p->GetNbinsX();k++){
if ((int)p->GetBinContent(k+1)==0) h2->SetBinContent(idx+1,k+1,1);
else h2->SetBinContent(idx+1,k+1,p->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();
//c->Write();
if (save==1){
switch (chip) {
case 0: sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Treshold.pdf(",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
default: sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Treshold.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
}
c->SaveAs(pdfname,"pdf");
} else if (save==3){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c->SaveAs(pdfname,"pdf");
} else if (save==4){
if(chip
==3) sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf)",runNumber
,runNumber
,HAPDserialNumber.
c_str());
else sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c->SaveAs(pdfname,"pdf");
}
}
if (h2) {
sprintf(name
,"%04d_%s_Treshold_c%d",runNumber
,HAPDserialNumber.
c_str(),5);
c = new TCanvas(name,name,600,500);
c->SetRightMargin(0.15);
c->SetLeftMargin(0.15);
c->SetLogz();
sprintf(buf
,"Treshold scan over the center of channels");
h2->SetTitle(buf);
gStyle->SetTitleFontSize(0.05);
h2->SetTitleFont(vrstaPisave);
h2->GetZaxis()->SetRangeUser(1,10000);
h2->GetZaxis()->SetTickLength(.02);
h2->GetZaxis()->SetLabelFont(vrstaPisave);
h2->GetZaxis()->SetLabelSize(0.03);
h2->GetYaxis()->SetRangeUser(500,700);
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("Treshold");
//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->SetStats(kFALSE);
h2->DrawCopy("COLZ");
if(save==2) {
sprintf(text_for_STR
, "Threshold_2D_%s", HAPDserialNumber.
c_str());
SaveToRootAddTH2DCopy(h2, text_for_STR);
}
c->Modified();
c->Update();
//c->Write();
if (save==1){
sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Treshold.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c4->SaveAs(pdfname,"pdf");
sprintf(pdfname
,"../modules/%04d/%04d_%s_1_Treshold.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");
} else if (save==3){
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());
c4->SaveAs(pdfname,"pdf");
sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf)",runNumber
,runNumber
,HAPDserialNumber.
c_str());
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;
//out->Close();
return 0;
}