6 #include "TGraphErrors.h"
19 std::cout <<
"processing " << tracklets.size() <<
" tracklets..." << std::endl;
21 for(
auto& tracklet : tracklets)
23 double x1 = tracklet.first->posX();
24 double y1 = tracklet.first->posY();
25 double z1 = tracklet.first->posZ();
26 double x2 = tracklet.second->posX();
27 double y2 = tracklet.second->posY();
28 double z2 = tracklet.second->posZ();
30 int size1 = tracklet.first->PhiSize();
31 int size2 = tracklet.second->PhiSize();
36 double stripwidth = 0.0078;
37 double rphierr1 = stripwidth*size1;
38 double rphierr2 = stripwidth*size2;
39 double r1 = sqrt(x1*x1+y1*y1);
40 double r2 = sqrt(x2*x2+y2*y2);
41 double phi1 = atan2(y1,x1);
42 double phi2 = atan2(y2,x2);
43 double sigma2_x1 = pow(rphierr1*cos(phi1),2.);
44 double sigma2_y1 = pow(rphierr1*sin(phi1),2.);
45 double sigma2_x2 = pow(rphierr2*cos(phi2),2.);
46 double sigma2_y2 = pow(rphierr2*sin(phi2),2.);
51 double sigma2_px = sigma2_x2+sigma2_x1;
52 double sigma2_py = sigma2_y2+sigma2_y1;
53 double phi0 = atan2(py,px);
54 double sigma2_phi0 = (sigma2_px*py*py+sigma2_py*px*px)/pow(px*px+py*py,2.);
58 double pca_x = x1 + ((BS_init.
x-x1)*cos(phi0)+(BS_init.
y-y1)*sin(phi0))*cos(phi0);
59 double pca_y = y1 + ((BS_init.
x-x1)*cos(phi0)+(BS_init.
y-y1)*sin(phi0))*sin(phi0);
62 double dca_origin = sqrt(pca_x*pca_x+pca_y*pca_y);
63 double dzdr = (z2-z1)/(r2-r1);
64 double z0 = z1-dzdr*(r1-dca_origin);
66 double d0 = sqrt(pow(pca_x-BS_init.
x,2.)+pow(pca_y-BS_init.
y,2.));
67 if(d0>d0_cut)
continue;
68 double phi_pca = atan2(pca_y-BS_init.
y,pca_x-BS_init.
x);
69 double oppositephi_pca = phi_pca+M_PI;
70 if(oppositephi_pca>M_PI) oppositephi_pca -= 2*M_PI;
75 reco_BS.
pcadist->Fill(pca_x,pca_y);
81 TF1 z_f(
"gaus",
"gaus",-60.,60.);
85 TGraphErrors*
g =
new TGraphErrors();
89 TH1D* slice = reco_BS.
d0phi0dist->ProjectionY(
"slice",
i,
i+1);
90 int maxbin = slice->GetMaximumBin();
91 double best_d0 = slice->GetBinCenter(maxbin);
92 double low_d0 = slice->GetBinCenter(slice->FindFirstBinAbove(0.5*slice->GetMaximum(),1,maxbin-10,maxbin));
93 double high_d0 = slice->GetBinCenter(slice->FindLastBinAbove(0.5*slice->GetMaximum(),1,maxbin,maxbin+10));
95 if(best_d0-low_d0>high_d0-best_d0) d0_error = best_d0-low_d0;
96 else d0_error = high_d0-best_d0;
97 std::cout <<
"d0 error: " << d0_error << std::endl;
98 double slice_phi = -M_PI+
i*(2.*M_PI)/200.;
99 g->AddPoint(slice_phi,best_d0);
100 g->SetPointError(
i,M_PI/100.,d0_error);
103 TF1 d0cos(
"d0cos",
"[0]*TMath::Cos(x-[1])",-M_PI,M_PI);
105 g->Write(
"maxgraph");
116 reco_BS.
z0dist->Fit(&z_f);
121 double BS_d0 = d0cos.GetParameter(0);
122 double BS_phi0 = d0cos.GetParameter(1);
123 double BS_sigmad0 = d0cos.GetParError(0);
124 double BS_sigmaphi0 = d0cos.GetParError(1);
126 reco_BS.
x = BS_d0*cos(BS_phi0)+BS_init.
x;
127 reco_BS.
y = BS_d0*sin(BS_phi0)+BS_init.
y;
128 reco_BS.
sigma_x = sqrt(pow(cos(BS_phi0),2.)*pow(BS_sigmad0,2.)+pow(BS_d0*sin(BS_phi0),2.)*pow(BS_sigmaphi0,2.));
129 reco_BS.
sigma_y = sqrt(pow(sin(BS_phi0),2.)*pow(BS_sigmad0,2.)+pow(BS_d0*cos(BS_phi0),2.)*pow(BS_sigmaphi0,2.));
133 reco_BS.
z = z_f.GetParameter(
"Mean");
134 reco_BS.
sigma_z = z_f.GetParameter(
"Sigma");
139 int main(
int argc,
char** argv)
143 std::cout <<
"Usage: ./BeamspotReco [infile] [outfile] [dphi_cut]" << std::endl;
149 double dphi_cut = atof(argv[3]);
151 TFile*
inf = TFile::Open(infile.c_str());
152 TTree*
events = (TTree*)inf->Get(
"EventTree");
154 TFile* outf =
new TFile(outfile.c_str(),
"RECREATE");
155 TTree*
t =
new TTree(
"reco_beamspot",
"reco_beamspot");
158 std::vector<double>* clusters_x = 0;
159 std::vector<double>* clusters_y = 0;
160 std::vector<double>* clusters_z = 0;
161 std::vector<int>* clusters_layer = 0;
162 std::vector<int>* clusters_phisize = 0;
164 events->SetBranchAddress(
"NClus",&Nclusters);
165 events->SetBranchAddress(
"ClusX",&clusters_x);
166 events->SetBranchAddress(
"ClusY",&clusters_y);
167 events->SetBranchAddress(
"ClusZ",&clusters_z);
168 events->SetBranchAddress(
"ClusLayer",&clusters_layer);
169 events->SetBranchAddress(
"ClusPhiSize",&clusters_phisize);
171 std::vector<std::pair<std::shared_ptr<Hit>,std::shared_ptr<Hit>>> tracklets;
174 for(
int i=0;
i<events->GetEntries();
i++)
176 std::vector<std::shared_ptr<Hit>> layer1_clusters;
177 std::vector<std::shared_ptr<Hit>> layer2_clusters;
181 std::cout <<
"event " <<
i << std::endl;
183 for(
int j=0;
j<Nclusters;
j++)
185 if(clusters_layer->at(
j)==3 || clusters_layer->at(
j)==4)
187 layer1_clusters.push_back(std::make_shared<Hit>(clusters_x->at(
j),clusters_y->at(
j),clusters_z->at(
j),0.,0.,0.,0,clusters_phisize->at(
j)));
189 if(clusters_layer->at(
j)==5 || clusters_layer->at(
j)==6)
191 layer2_clusters.push_back(std::make_shared<Hit>(clusters_x->at(
j),clusters_y->at(
j),clusters_z->at(
j),0.,0.,0.,0,clusters_phisize->at(
j)));
194 for(
auto& cluster1 : layer1_clusters)
196 for(
auto& cluster2: layer2_clusters)
198 if(fabs(cluster1->Phi()-cluster2->Phi())<dphi_cut && fabs(cluster1->posZ()-cluster2->posZ())<5.)
200 tracklets.push_back(std::make_pair(cluster1,cluster2));
212 t->Branch(
"x",&BS.
x);
213 t->Branch(
"y",&BS.
y);
214 t->Branch(
"z",&BS.
z);
215 t->Branch(
"sigma_x",&BS.
sigma_x);
216 t->Branch(
"sigma_y",&BS.
sigma_y);
217 t->Branch(
"sigma_z",&BS.
sigma_z);
224 BS.
d0phi0dist->Write(
"d0phi0dist",TObject::kOverwrite);
225 BS.
z0dist->Write(
"z0dist",TObject::kOverwrite);
226 BS.
pcadist->Write(
"pcadist",TObject::kOverwrite);