#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <TROOT.h>
#include <TLatex.h>
#include <TH1F.h>
#include <TH2D.h>
#include <TH3D.h>
#include <TGraph.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TStyle.h>
#include <TSystem.h>
#include <TFile.h>
#include <TTree.h>
#include <TDirectory.h>
#include <TPaveText.h>
#include "base.h"
#include "info.h"
#include "savetoroot.h"
int info(int runNumber, int save) {
int HAPDnumber; //Number of HAPD: 0, 1, 2 or 3!
char text[256];
char pdfname[256];
char buf[256];
char path[128];
int vrstaPisave = 82;
const char * serialNumber;
std::string serialNumberTemp;
std::string HAPDserialNumber,FEBserialNumber;
unsigned int id, len, cas;
int vmon[24], vset[24];
int imon[24];
//int status[6];
//int min_imon[6], max_imon[6], min_vmon[6], max_vmon[6], min_status[6], max_status[6];
int min_chipBias = 0, max_chipBias = 0;
int sizeOfTree;
time_t itime;
TDatime dh(1995,01,01,00,00,00); // to je za SetTimeOffset
char * viri[6] = {"Guard", "Chip A", "Chip B", "Chip C", "Chip D", "HV"};
TFile * data[3];
TCanvas * c;
TPad * pad1, * pad2, * pad3, * pad4, * pad5;
TPaveText * sn, * timeStamp, * imeMeritve, * zacetekMeritve;
TGraph * gr[10];
TLegend * leg[2];
TTree * tree;
/********** GLOBALNE **********/
gStyle->SetOptStat(0);
//gStyle->SetOptTitle(1);
gStyle->SetTextFont(vrstaPisave);
gStyle->SetTitleFont(vrstaPisave);
gStyle->SetTitleFontSize(0.08);
gStyle->SetPalette(55);
//gROOT->SetBatch(kTRUE); //Does not show canvases
sprintf(path
,"../modules/%04d/%04d_1_Treshold_Waveform.root",runNumber
,runNumber
);
data[0] = new TFile(path);
if (!file_exists(path)){
printf("File '%s' does not exist.\n",path
);
return -1;
}
serialNumber = getSN(runNumber);
printf("base.c return: %s\n", serialNumber
);
serialNumberTemp = serialNumber;
sprintf(path
,"../modules/%04d/%04d_2_2DX.root",runNumber
,runNumber
);
data[1] = new TFile(path);
if (!file_exists(path)){
printf("File '%s' does not exist.\n",path
);
return -1;
}
sprintf(path
,"../modules/%04d/%04d_3_2DY.root",runNumber
,runNumber
);
data[2] = new TFile(path);
if (!file_exists(path)){
printf("File '%s' does not exist.\n",path
);
return -1;
}
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);
printf("%s \t %s \n",FEBserialNumber.
c_str(),HAPDserialNumber.
c_str());
sprintf(buf
,"%04d_%s_Monitor",runNumber
,HAPDserialNumber.
c_str());
c = new TCanvas(buf,buf,0,0,1500,1200);
/******* PRVI PAD *******/
sprintf(buf
,"%04d_%s_Monitor_pad1",runNumber
,HAPDserialNumber.
c_str());
pad1 = new TPad(buf,"SN",0,0.95,1,1,0,0);
pad1->SetFillStyle(4000);
pad1->Draw();
sn = new TPaveText(0.2,0,0.8,1,"ndc");
sn->SetBorderSize(0);
sn->SetFillColor(4000);
sn->SetTextSize(.8);
sn->SetTextAlign(22);
sn->SetTextFont(vrstaPisave);
/******* DRUGI PAD *******/
sprintf(buf
,"%04d_%s_Monitor_pad2",runNumber
,HAPDserialNumber.
c_str());
pad2 = new TPad(buf,"Date",0.05,0.9,0.95,0.95,0,0);
pad2->SetFillStyle(4000);
pad2->Draw();
timeStamp = new TPaveText(0.2,0,0.8,1,"ndc");
timeStamp->SetBorderSize(0);
timeStamp->SetFillColor(4000);
timeStamp->SetTextSize(.4);
timeStamp->SetTextAlign(22);
timeStamp->SetTextFont(vrstaPisave);
/******* TRETJI PAD *******/
sprintf(buf
,"%04d_%s_Monitor_pad3",runNumber
,HAPDserialNumber.
c_str());
pad3 = new TPad(buf,"Napetosti",0,0,1,0.9,0,0);
pad3->SetFillStyle(4000);
pad3->Draw();
pad3->cd();
pad3->Divide(0,3);
for (int k=0;k<3;k++){
pad3->cd(k+1);
sprintf(buf
,"%04d_%s_Monitor_pad4",runNumber
,HAPDserialNumber.
c_str());
pad4 = new TPad(buf,"Naslov",0.05,0.9,0.95,1,0,0);
pad4->SetFillStyle(4000);
pad4->Draw();
imeMeritve = new TPaveText(0,0,1,1,"ndc");
imeMeritve->SetBorderSize(0);
imeMeritve->SetFillColor(kSpring+1);
imeMeritve->SetTextSize(.6);
imeMeritve->SetTextAlign(22);
imeMeritve->SetTextFont(vrstaPisave);
zacetekMeritve = new TPaveText(0.8,0,1,1,"ndc");
zacetekMeritve->SetBorderSize(0);
zacetekMeritve->SetFillColor(kSpring+1);
zacetekMeritve->SetTextSize(.4);
zacetekMeritve->SetTextAlign(22);
zacetekMeritve->SetTextFont(vrstaPisave);
switch (k){
case 0: sprintf(text
, "Treshold and Waveform");break;
case 1: sprintf(text
, "2D in X");break;
case 2: sprintf(text
, "2D in Y");break;
}
pad4->cd();
imeMeritve->AddText(text);
imeMeritve->Draw();
pad3->cd(k+1);
sprintf(buf
,"%04d_%s_Monitor_pad5",runNumber
,HAPDserialNumber.
c_str());
pad5 = new TPad(buf,"Grafi",0,0,1,.9,0,0);
pad5->SetFillStyle(4000);
pad5->Draw();
pad5->cd();
pad5->Divide(4,0);
tree = (TTree*) data[k]->Get("Monitor");
tree->SetBranchAddress("id",&id);
tree->SetBranchAddress("len",&len);
tree->SetBranchAddress("time",&cas);
tree->SetBranchAddress("imon",&imon);
tree->SetBranchAddress("vmon",&vmon);
tree->SetBranchAddress("vset",&vset);
//tree->SetBranchAddress("status",&status);
sizeOfTree = tree->GetEntries();
gr[0] = new TGraph(sizeOfTree);
gr[1] = new TGraph(sizeOfTree);
gr[2] = new TGraph(sizeOfTree);
gr[3] = new TGraph(sizeOfTree);
gr[4] = new TGraph(sizeOfTree);
gr[5] = new TGraph(sizeOfTree);
gr[6] = new TGraph(sizeOfTree);
gr[7] = new TGraph(sizeOfTree);
gr[8] = new TGraph(sizeOfTree);
gr[9] = new TGraph(sizeOfTree);
for (int i=0;i<sizeOfTree;i++){
tree->GetEntry(i);
cas = cas-2208988800; //(2208988800)CVI zapiše čas v sekundah od 00:00:00 1.1.1900 naprej, UNIX čas je od 00:00:00 1.1.1970
// cas-u morš še nekaj odštet, kr kasneje ko delaš TimeOffset, le ta vodi čas od 00:00:00 1.1.1995 naprej.
gr[0]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+0]);
gr[1]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+1]-vset[HAPDnumber*6+1]);
gr[2]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+2]-vset[HAPDnumber*6+2]);
gr[3]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+3]-vset[HAPDnumber*6+3]);
gr[4]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+4]-vset[HAPDnumber*6+4]);
gr[5]->SetPoint(i,cas-788918400,vmon[HAPDnumber*6+5]);
gr[6]->SetPoint(i,cas-788918400,imon[HAPDnumber*6+1]/1000.);
gr[7]->SetPoint(i,cas-788918400,imon[HAPDnumber*6+2]/1000.);
gr[8]->SetPoint(i,cas-788918400,imon[HAPDnumber*6+3]/1000.);
gr[9]->SetPoint(i,cas-788918400,imon[HAPDnumber*6+4]/1000.);
if (!i){
itime = cas;
zacetekMeritve
->AddText
(ctime(&itime
));
} else if (k==2 && i==sizeOfTree-1){
itime = cas;
timeStamp->AddText(buf);
}
for (int j=1;j<5;j++){
if (!i && j==1) {
min_chipBias = max_chipBias = vmon[HAPDnumber*6+j]-vset[HAPDnumber*6+j];
} else {
if (min_chipBias > vmon[HAPDnumber*6+j]-vset[HAPDnumber*6+j]) min_chipBias = vmon[HAPDnumber*6+j]-vset[HAPDnumber*6+j];
else if (max_chipBias < vmon[HAPDnumber*6+j]-vset[HAPDnumber*6+j]) max_chipBias = vmon[HAPDnumber*6+j]-vset[HAPDnumber*6+j];
}
}
}
leg[0] = new TLegend(0.55,0.1,.9,0.35);
leg[1] = new TLegend(0.70,0.70,.9,0.9);
leg[0]->SetFillColor(0);
leg[1]->SetFillColor(0);
for (int i=0;i<10;i++){
gr[i]->GetYaxis()->CenterTitle();
gr[i]->GetYaxis()->SetTitleFont(vrstaPisave);
gr[i]->GetYaxis()->SetLabelFont(vrstaPisave);
gr[i]->GetYaxis()->SetTitleOffset(1.6);
gr[i]->GetXaxis()->SetTitleFont(vrstaPisave);
gr[i]->GetXaxis()->SetLabelFont(vrstaPisave);
//gr[i]->GetXaxis()->CenterTitle();
gr[i]->GetXaxis()->SetTimeDisplay(1);
gr[i]->GetXaxis()->SetTimeOffset(dh.Convert());
gr[i]->GetXaxis()->SetTimeFormat("#splitline{%H:%M}{%d-%m}");
gr[i]->GetXaxis()->SetLabelOffset(0.02);
gr[i]->GetXaxis()->SetTitleOffset(1.5);
if(!i){
pad5->cd(1);
sprintf(text
, "Guard bias (RMS = %.0f mV);Time;Bias [mV]", gr
[i
]->GetRMS
(2));
gr[i]->SetTitle(text);
gr[i]->GetYaxis()->SetRangeUser(170000,180000);
gr[i]->Draw("AL");
sprintf(text
, "Guard_%s_T_%d", HAPDserialNumber.
c_str(), k
);
SaveToRootAddGraph(gr[i], text);
} else if (i<5 && i!=0){
pad5->cd(2);
sprintf(text
, "%s (RMS = %.0f mV)", viri
[i
], gr
[i
]->GetRMS
(2));
leg[0]->AddEntry(gr[i],text,"L");
//gr[i]->GetYaxis()->SetRangeUser(min_chipBias-50,max_chipBias+50);
gr[i]->GetYaxis()->SetRangeUser(-1000,1000);
gr[i]->SetTitle("Chip (vmon - vset) difference;Time;Bias difference [mV]");
if (i==1) gr[i]->Draw("AL");
gr[i]->SetLineColor(i);
gr[i]->SetLineWidth(1);
gr[i]->Draw("LSAME");
sprintf(text
, "BiasDifference_%s_CHIP_%d_T_%d", HAPDserialNumber.
c_str(), i
-1, k
);
SaveToRootAddGraph(gr[i], text);
} else if (i>5){
pad5->cd(3);
leg[1]->AddEntry(gr[i],text,"L");
gr[i]->GetYaxis()->SetRangeUser(-1,11);
gr[i]->SetTitle("Chip leakage current;Time;Leakage current [mA]");
if (i==6) gr[i]->Draw("AL");
gr[i]->SetLineColor(i);
gr[i]->SetLineWidth(1);
gr[i]->Draw("LSAME");
sprintf(text
, "LeakageCurrent_%s_CHIP_%d_T_%d", HAPDserialNumber.
c_str(), i
-6, k
);
SaveToRootAddGraph(gr[i], text);
} else if (i==5){
pad5->cd(4);
sprintf(text
, "HV (RMS = %.0f mV);Time;Bias [mV]", gr
[i
]->GetRMS
(2));
gr[i]->GetYaxis()->SetRangeUser(6900000,7100000);
gr[i]->SetTitle(text);
gr[i]->Draw("AL");
sprintf(text
, "HV_%s_T_%d", HAPDserialNumber.
c_str(), k
);
SaveToRootAddGraph(gr[i], text);
}
}
pad5->cd(2);
leg[0]->Draw();
pad5->cd(3);
leg[1]->Draw();
pad4->cd();
zacetekMeritve->Draw();
}
pad1->cd();
sprintf(buf
,"RUN#: %04d (pos %d) FEB#: %s HAPD#: %s",runNumber
, HAPDnumber
, FEBserialNumber.
c_str(),HAPDserialNumber.
c_str());
sn->AddText(buf);
sn->Draw();
pad2->cd();
timeStamp->Draw();
c->Modified();
c->Update();
if (save) {
switch (save) {
case 1: sprintf(pdfname
,"../modules/%04d/%04d_%s_0_Info.pdf",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
case 2: sprintf(pdfname
,"../modules/%04d/%04d_%s.pdf(",runNumber
,runNumber
,HAPDserialNumber.
c_str());break;
}
c->SaveAs(pdfname,"pdf");
}
sprintf(pdfname
,"../modules/%04d/%04d_%s_out.root",runNumber
,runNumber
,HAPDserialNumber.
c_str());
SaveToRootWrite(pdfname, HAPDserialNumber.c_str());
}
for(int k=0;k<3;k++) delete data[k];
return 0;
}