/
tcalibration.C
242 lines (201 loc) · 6.52 KB
/
tcalibration.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// tcalibration - routine for the prtdirc data calibration
// original author: Roman Dzhygadlo - GSI Darmstad
#define TTSelector_cxx
#include "prttools.C"
#include "TStyle.h"
#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TFrame.h>
#include <TRootEmbeddedCanvas.h>
#include <TGStatusBar.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TF1.h>
#include <TRandom.h>
#include <TGraph.h>
#include <TAxis.h>
#include <TGNumberEntry.h>
#include <TGLabel.h>
#include <TGListBox.h>
#include <TGComboBox.h>
#include <TGButtonGroup.h>
#include <TGTextEntry.h>
#include <TGProgressBar.h>
#include <TThread.h>
#include <TProof.h>
#include <TGSplitter.h>
#include <TChainElement.h>
#include <TKey.h>
#include "tcalibration.h"
const Int_t maxfiles(200);
const Int_t maxch =3000;
const Int_t nmcp(15), npix(64);
TString fileList[maxfiles];
TH1F *hCh;
TString ginFile(""), goutFile(""), gcFile(""), gtFile("");
Int_t gTrigger(0), gMode(0);;
const Int_t tdcnum(88);
const Int_t tdcmax(10000);
TString trbsid[tdcnum] =
{"0010","0011","0012","0013","0110","0111","0112","0113","0210","0211","0212","0213","0310","0311","0312","0313","0410","0411","0412","0413"
,"0510","0511","0512","0513","0610","0611","0612","0613","0710","0711","0712","0713","0810","0811","0812","0813","0910","0911","0912","0913"
,"1010","1011","1012","1013","1110","1111","1112","1113","1210","1211","1212","1213","1310","1311","1312","1313","1410","1411","1412","1413"
,"1510","1511","1512","1513","1610","1611","1612","1613","1710","1711","1712","1713","1810","1811","1812","1813","1910","1911","1912","1913"
,"2010","2011","2012","2013","2110","2111","2112","2113"};
Int_t tdcid[tdcnum];
Double_t trbRefTime[tdcnum];
Double_t timeTe0[tdcmax][50];
Int_t mult[tdcmax];
Int_t tdcmap[tdcmax];
Int_t mcpmap[tdcmax];
Int_t pixmap[tdcmax];
Int_t chmap[nmcp][npix];
Int_t gComboId=0;
TGraph *gGr[maxch];
TGraph *gGrDiff[maxch];
//TH1F *hL = new TH1F("hL", "hL" , 500,150,200);
void CreateMap(){
Int_t seqid =0;
for(Int_t i=0; i<tdcmax; i++){
tdcmap[i]=-1;
mcpmap[i]=-1;
for(Int_t j=0; j<tdcnum; j++){
if(i==TString::BaseConvert(trbsid[j],16,10).Atoi()){
tdcmap[i]=seqid++;
mcpmap[i]=j/4;
pixmap[i]=j-j/32;
break;
}
}
}
for(Int_t ch=0; ch<maxch; ch++){
Int_t mcp = ch/128;
Int_t pix = (ch - mcp*128)/2;
Int_t col = 7-(pix/2 - 8*(pix/16));
Int_t row = pix%2 + 2*(pix/16);
pix = col*8+row;
//std::cout<<"ch "<<ch <<" m "<<mcp <<" p "<<pix <<std::endl;
chmap[mcp][pix]=ch;
}
}
void TTSelector::Begin(TTree *){
TString option = GetOption();
TObjArray *strobj = option.Tokenize(" ");
gTrigger = ((TObjString*)strobj->At(0))->GetString().Atoi();
gMode = ((TObjString*)strobj->At(1))->GetString().Atoi();
CreateMap();
TString filedir=ginFile;
filedir.Remove(filedir.Last('.')-4);
fFile = new TFile(goutFile,"RECREATE");
fTree = new TTree("M","Tree for GSI Prt Analysis");
fEvent = new TPrtEvent();
fTree->Branch("TPrtEvent", "TPrtEvent", &fEvent, 64000, 2);
TFile f(gcFile);
TIter nextkey(f.GetListOfKeys());
TKey *key;
while ((key = (TKey*)nextkey())) {
TGraph *gr = (TGraph*)key->ReadObj();
TString name = gr->GetName();
Int_t channel = name.Atoi();
gGr[channel]= new TGraph(*gr);
}
f.Close();
if(gMode == 1){
TFile f2(gtFile);
TIter nextkey2(f2.GetListOfKeys());
TKey *key2;
while ((key2 = (TKey*)nextkey2())) {
TGraph *gr = (TGraph*)key2->ReadObj();
TString name = gr->GetName();
Int_t channel = name.Atoi();
gGrDiff[channel]= new TGraph(*gr);
}
f2.Close();
}
std::cout<<"Initialization successful"<<std::endl;
}
Bool_t TTSelector::Process(Long64_t entry){
Int_t trbSeqId,ch,mcp,pix,col,row;;
Double_t timeTot(0), grTime0(0), grTime1(0),timeLe(0),coarseTime;
Double_t time[50000];
if(entry%1000==0) std::cout<<"event # "<< entry <<std::endl;
GetEntry(entry);
fEvent = new TPrtEvent();
fEvent->SetReferenceChannel(gTrigger);
for(Int_t i=0; i<Hits_; i++){
trbSeqId = tdcmap[Hits_nTrbAddress[i]];
ch = 32*trbSeqId+Hits_nTdcChannel[i];
if(++mult[ch]>50) continue;
coarseTime = 5*(Hits_nEpochCounter[i]*pow(2.0,11) + Hits_nCoarseTime[i]);
time[i] = coarseTime-gGr[ch]->Eval(Hits_nFineTime[i]);
timeTe0[ch][mult[ch]]=time[i];
if(Hits_nTdcChannel[i]==0) { // is ref channel
trbRefTime[trbSeqId] = time[i];
if(gTrigger!=0 && (gTrigger-ch)<=32 && (gTrigger-ch)>0) grTime0 = time[i];
}
if(gTrigger!=0 && ch==gTrigger) grTime1 = time[i];
}
if((grTime0>0 && grTime1>0) || gTrigger==0){
for(Int_t i=0; i<Hits_; i++){
if(Hits_nTrbAddress[i]==0) continue;
trbSeqId = tdcmap[Hits_nTrbAddress[i]];
ch = 32*trbSeqId+Hits_nTdcChannel[i];
mcp = ch/128;
pix = (ch%128)/2;
col = pix/2 - 8*(pix/16);
row = pix%2 + 2*(pix/16);
pix = (7-col)*8+row;
if(ch%2==0) continue; // go away trailing edge
if(ch<3000) {
timeLe = time[i]-trbRefTime[trbSeqId];
timeLe = timeLe - (grTime1-grTime0);
//if(ch == 241) hL->Fill(timeLe);
timeTot = timeTe0[ch+1][1] - timeTe0[ch][1];
if(gMode == 1 && ch<1920) timeLe -= gGrDiff[ch]->Eval(timeTot);
TPrtHit hit(Hits_nTrbAddress[i],Hits_nTdcChannel[i],ch,mcp,pix+1,timeLe,timeTot);
fEvent->AddHit(hit);
}
}
}
for(Int_t i=0; i<Hits_; i++){
trbSeqId = tdcmap[Hits_nTrbAddress[i]];
ch = 32*trbSeqId+Hits_nTdcChannel[i];
mult[ch]=0;
for(Int_t j=0; j<50; j++){
timeTe0[ch][j]=0;
}
}
fTree->Fill();
fEvent->Clear();
delete fEvent;
return kTRUE;
}
void TTSelector::Terminate(){
fFile->Write();
fFile->Close();
// hL->Draw();
// hL->Fit("gaus","V","E1",175,185);
}
void tcalibration(TString inFile= "../../data/cj.hld.root", TString outFile= "outFileC.root", TString cFile= "calib.root", TString tFile= "calibOffsets.root", Int_t trigger=2560, Int_t sEvent =0, Int_t eEvent=0, Int_t build = 0){ //1920
if(build==1) return;
ginFile = inFile;
goutFile = outFile;
gcFile = cFile; // fine time calibration
gtFile = tFile; // pilas offsets + walk corrections
gTrigger = trigger;
if(gtFile=="") gMode = 0;
else gMode = 1;
TChain* ch = new TChain("T");
ch->Add(ginFile);
Int_t entries = ch->GetEntries();
TTSelector *selector = new TTSelector();
TString option = Form("%d %d",gTrigger,gMode);
if(eEvent==0){
std::cout<<"Entries in chain: "<< entries<<std::endl;
ch->Process(selector,option,entries);
}else{
ch->Process(selector,option,eEvent-sEvent,sEvent);
}
}