forked from sPHENIX-Collaboration/coresoftware
/
CMDistortionRecoCart.C
247 lines (188 loc) · 8.67 KB
/
CMDistortionRecoCart.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
243
244
245
246
247
// step 2
#include <iostream>
#include <cmath>
#include <vector>
#include "TMath.h"
#include "TVector3.h"
#include "TTree.h"
#include <TTime.h>
using namespace std;
int CMDistortionRecoCart(int nMaxEvents = -1) {
int nbins = 35;
double low = -80.0;
double high = 80.0;
double deltaX, deltaY, deltaZ, deltaR, deltaPhi;
int nEvents;
//take in events
const char * inputpattern="/sphenix/u/skurdi/CMCalibration/cmDistHitsTree_Event*.root";
//find all files that match the input string (includes wildcards)
TFileCollection *filelist=new TFileCollection();
filelist->Add(inputpattern);
TString sourcefilename;
//how many events
if (nMaxEvents<0){
nEvents=filelist->GetNFiles();
} else if(nMaxEvents<filelist->GetNFiles()){
nEvents=nMaxEvents;
} else {
nEvents= filelist->GetNFiles();
}
//canvas for checking data
//TCanvas *canvas1=new TCanvas("canvas1","CMDistortionRecoCart1",1200,800);
//canvas1->Divide(3,2);
//canvas for time plot
TCanvas *canvas=new TCanvas("canvas","CMDistortionRecoCart2",400,400);
TVector3 *position, *newposition;
position = new TVector3(1.,1.,1.);
newposition = new TVector3(1.,1.,1.);
//histogram to compare times
TH1F *hTimePerEvent = new TH1F("hTimePerEvent","Time Per Event; time (ms)",20,0,10000);
for (int ifile=0;ifile < nEvents;ifile++){
//call to TTime before opening ttree
TTime now;
now=gSystem->Now();
unsigned long before = now;
//get data from ttree
sourcefilename=((TFileInfo*)(filelist->GetList()->At(ifile)))->GetCurrentUrl()->GetFile();
char const *treename="cmDistHitsTree";
TFile *input=TFile::Open(sourcefilename, "READ");
TTree *inTree=(TTree*)input->Get("tree");
inTree->SetBranchAddress("position",&position);
inTree->SetBranchAddress("newposition",&newposition);
//for forward only
TH2F *hStripesPerBin = new TH2F("hStripesPerBin","CM Stripes Per Bin (z in stripes); x (cm); y (cm)",nbins,low,high,nbins,low,high);
TH2F *hCartesianForward[3];
hCartesianForward[0] = new TH2F("hForwardX","X Shift Forward of Stripe Centers (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
hCartesianForward[1] = new TH2F("hForwardY","Y Shift Forward of Stripe Centers (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
hCartesianForward[2] = new TH2F("hForwardZ","Z Shift Forward of Stripe Centers (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
for (int i=0;i<inTree->GetEntries();i++){
inTree->GetEntry(i);
hStripesPerBin->Fill(position->X(),position->Y(),1);
deltaX = (newposition->X() - position->X())*(1e4); //convert from cm to micron
deltaY = (newposition->Y() - position->Y())*(1e4);
deltaZ = (newposition->Z() - position->Z())*(1e4);
deltaR = (newposition->Perp() - position->Perp())*(1e4);
deltaPhi = newposition->DeltaPhi(*position);
hCartesianForward[0]->Fill(position->X(),position->Y(),deltaX);
hCartesianForward[1]->Fill(position->X(),position->Y(),deltaY);
hCartesianForward[2]->Fill(position->X(),position->Y(),deltaZ);
}
TH2F *hCartesianAveShift[3];
hCartesianAveShift[0] = new TH2F("AveShiftX","Average of CM Model X over Stripes per Bin (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
hCartesianAveShift[1] = new TH2F("AveShiftY","Average of CM Model Y over Stripes per Bin (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
hCartesianAveShift[2] = new TH2F("AveShiftZ","Average of CM Model Z over Stripes per Bin (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
TH2F *hCylindricalAveShift[2];
hCylindricalAveShift[0] = new TH2F("AveShiftRCart","Average of CM Model R over Stripes per Bin from Cartesian (#mum); x (cm); y (cm)",nbins,low,high,nbins,low,high);
hCylindricalAveShift[1] = new TH2F("AveShiftPhiCart","Average of CM Model Phi over Stripes per Bin from Cartesian (rad); x (cm); y (cm)",nbins,low,high,nbins,low,high);
for (int i = 0; i < 3; i ++){
hCartesianAveShift[i]->Divide(hCartesianForward[i],hStripesPerBin);
}
//cyl models from cart coords
for(int i = 0; i < nbins; i++){
double x = low + ((high - low)/(1.0*nbins))*(i+0.5); //center of bin
for(int j = 0; j < nbins; j++){
double y = low + ((high - low)/(1.0*nbins))*(j+0.5); //center of bin
int xbin = hCartesianAveShift[0]->FindBin(x,y);
int ybin = hCartesianAveShift[1]->FindBin(x,y);
double xaveshift = (hCartesianAveShift[0]->GetBinContent(xbin))*(1e-4); // converts microns to cm
double yaveshift = (hCartesianAveShift[1]->GetBinContent(ybin))*(1e-4);
TVector3 shifted, original;
original.SetX(x);
original.SetY(y);
shifted.SetX(x+xaveshift);
shifted.SetY(y+yaveshift);
//x n y above for orig
//shifted is orig + ave shift
double raveshift = (shifted.Perp() - original.Perp())*(1e4);
double phiaveshift = shifted.DeltaPhi(original);
//fill with r from x n y
hCylindricalAveShift[0]->Fill(x,y,raveshift);
hCylindricalAveShift[1]->Fill(x,y,phiaveshift);
}
}
//same range and bins for each coordinate, binned in cm
//hardcoded numbers from average distortion file's hIntDistortionPosX
int nphi = 82;
int nr = 54;
int nz = 82;
double minphi = -0.078539819;
double minr = 18.884615;
//double minz = 5.0;
double minz = -1.3187500;
double maxphi = 6.3617253;
double maxr = 79.115387;
double maxz = 106.81875;
TH3F *hCartesianCMModel[3];
hCartesianCMModel[0]=new TH3F("hCMModelX", "CM Model: X Shift Forward of Stripe Centers", nphi,minphi,maxphi, nr,minr,maxr, nz,minz,maxz); //rad, cm, cm
hCartesianCMModel[1]=new TH3F("hCMModelY", "CM Model: Y Shift Forward of Stripe Centers", nphi,minphi,maxphi, nr,minr,maxr, nz,minz,maxz);
hCartesianCMModel[2]=new TH3F("hCMModelZ", "CM Model: Z Shift Forward of Stripe Centers", nphi,minphi,maxphi, nr,minr,maxr, nz,minz,maxz);
TH3F *hCylindricalCMModel[2];
hCylindricalCMModel[0]=new TH3F("hCMModelRCart", "CM Model: Radial Shift Forward of Stripe Centers from Cartesian", nphi,minphi,maxphi, nr,minr,maxr, nz,minz,maxz);
hCylindricalCMModel[1]=new TH3F("hCMModelPhiCart", "CM Model: Phi Shift Forward of Stripe Centers from Cartesian", nphi,minphi,maxphi, nr,minr,maxr, nz,minz,maxz);
double xshift, yshift, zshift, rshiftcart, phishiftcart;
for(int i = 0; i < nphi; i++){
double phi = minphi + ((maxphi - minphi)/(1.0*nphi))*(i+0.5); //center of bin
for(int j = 0; j < nr; j++){
double r = minr + ((maxr - minr)/(1.0*nr))*(j+0.5); //center of bin
double x = r*cos(phi); //cm
double y = r*sin(phi);
for(int k = 0; k < nz; k++){
double z = minz + ((maxz - minz)/(1.0*nz))*(k+0.5); //center of bin
xshift=(hCartesianAveShift[0]->Interpolate(x,y))*(1e-4);//coordinate of your stripe
yshift=(hCartesianAveShift[1]->Interpolate(x,y))*(1e-4);//convert micron to cm
zshift=(hCartesianAveShift[2]->Interpolate(x,y))*(1e-4);
rshiftcart=(hCylindricalAveShift[0]->Interpolate(x,y))*(1e-4);
phishiftcart=hCylindricalAveShift[1]->Interpolate(x,y);
hCartesianCMModel[0]->Fill(phi,r,z,xshift*(1-z/105.5));
hCartesianCMModel[1]->Fill(phi,r,z,yshift*(1-z/105.5));
hCartesianCMModel[2]->Fill(phi,r,z,zshift*(1-z/105.5));
hCylindricalCMModel[0]->Fill(phi,r,z,rshiftcart*(1-z/105.5));
hCylindricalCMModel[1]->Fill(phi,r,z,phishiftcart*(1-z/105.5));
}
}
}
TFile *plots;
plots=TFile::Open(Form("CMModelsCart_Event%d.root",ifile),"RECREATE");
hStripesPerBin->Write();
for(int i = 0; i < 3; i++){
hCartesianForward[i]->Write();
hCartesianAveShift[i]->Write();
hCartesianCMModel[i]->Write();
}
for(int i = 0; i < 2; i++){
hCylindricalAveShift[i]->Write();
hCylindricalCMModel[i]->Write();
}
plots->Close();
//call to TTime after outputting TH3Fs
now=gSystem->Now();
unsigned long after = now;
hTimePerEvent->Fill(after-before);
/*
//to check histograms
for (int i = 0; i < 3; i++){
hCartesianForward[i]->SetStats(0);
}
hStripesPerBin->SetStats(0);
canvas1->cd(1);
hCartesianForward[0]->Draw("colz");
canvas1->cd(2);
hCartesianForward[1]->Draw("colz");
canvas1->cd(3);
hCartesianForward[2]->Draw("colz");
canvas1->cd(4)->Clear();
canvas1->cd(5)->Clear();
canvas1->cd(6)->Clear();
if(ifile == 0){
canvas1->Print("CMDistortionRecoCart1.pdf(","pdf");
} else if (ifile == nEvents - 1){
canvas1->Print("CMDistortionRecoCart1.pdf)","pdf");
} else {
canvas1->Print("CMDistortionRecoCart1.pdf","pdf");
}*/
}
canvas->cd();
hTimePerEvent->Draw();
canvas->Print("CMDistortionRecoCart2.pdf","pdf");
return 0;
}