Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FitCathodeAlignment.C
Go to the documentation of this file.
1 void FitCathodeAlignment(TString inputFile = "hd_root.root"){
2 
3  TFile *file = TFile::Open(inputFile);
4  TFile *outFile = new TFile("result.root","RECREATE");
5 
6  TH2D *resultHists[24];
7  // The old constants are stored in a histogram
8  TH1D * constantsHist = (TH1D *) file->Get("FDC_InternalAlignment/CathodeAlignmentConstants");
9 
10  // Fit Function
11  TF2 *f = new TF2("plane","[0]+[1]*x+[2]*y", -50.,50.,-50.,50.);
12  ofstream constantsFile;
13  constantsFile.open("CathodeAlignment.txt");
14 
15  for(int i=1; i<=24; i++){
16  double i_deltaPhiU = constantsHist->GetBinContent((i-1)*4+1);
17  double i_deltaU = constantsHist->GetBinContent((i-1)*4+2);
18  double i_deltaPhiV = constantsHist->GetBinContent((i-1)*4+3);
19  double i_deltaV = constantsHist->GetBinContent((i-1)*4+4);
20 
21  double phi_u = 75.0*TMath::DegToRad() + i_deltaPhiU;
22  double phi_v = (TMath::Pi() - 75.0*TMath::DegToRad()) + i_deltaPhiV;
23 
24  double sinPhiU = sin(phi_u);
25  double cosPhiU = cos(phi_u);
26  double sinPhiV = sin(phi_v);
27  double cosPhiV = cos(phi_v);
28  double sinPhiUmPhiV = sin(phi_u-phi_v);
29  double sinPhiUmPhiV2 = sinPhiUmPhiV*sinPhiUmPhiV;
30  double cosPhiUmPhiV = cos(phi_u-phi_v);
31 
32  char name[100];
33  sprintf(name,"FDC_InternalAlignment/Plane %.2i Wire Position Vs XY",i);
34 
35  TH3I *h = (TH3I *)file->Get(name);
36 
37  TF1 *g = new TF1("g", "gaus", -0.5, 0.5);
38  TH2D *result = new TH2D(Form("Plane %.2i residual", i), "residual", 100, -50., 50., 100, -50., 50.);
39  resultHists[i-1]=result;
40  for (int iX=2; iX <= h->GetNbinsX(); iX++){
41  for (int iY=2; iY <= h->GetNbinsY(); iY++){
42  // Cut region near beamline since there is some bad behavior
43  if (iX > 45 && iX < 55 && iY > 42 && iY < 58) {
44  result->SetBinContent(iX,iY, -1./0.0);
45  continue;
46  }
47  TString name;
48  if(iY==25 && iX%5 ==0) name = Form("Proj bin (%i,%i)",iY,iX);
49  else name = "_z";
50  TH1D * zProj = h->ProjectionZ(name,iX-1, iX, iY-1, iY);
51  if (zProj->GetEntries() < 5) {
52  result->SetBinContent(iX,iY, -1./0.0);
53  continue;
54  }
55  double max = zProj->GetBinCenter(zProj->GetMaximumBin());
56  g->SetParameter(1, max);
57  TFitResultPtr r = zProj->Fit("g", "Q", "", max - 0.02, max + 0.02 );
58  if ((Int_t) r == 0){
59  double mean = g->GetParameter(1);
60  double err = g->GetParError(1);
61  result->SetBinContent(iX,iY,mean);
62  result->SetBinError(iX,iY,err);
63  }
64  else result->SetBinContent(iX,iY, -1./0.0);
65  }
66  }
67 
68  // Use robust fitting to reject up to 10% outliers.
69  result->Fit("plane", "M+rob=0.90");
70 
71  double c0 = f->GetParameter(0);
72  double c1 = f->GetParameter(1);
73  double c2 = f->GetParameter(2);
74 
75  // Calculate the new offsets from the fit parameters
76  double deltaU = sinPhiUmPhiV*c0/sinPhiV;
77  double deltaV = -sinPhiUmPhiV*c0/sinPhiU;
78  double a = -sinPhiU*sinPhiV/sinPhiUmPhiV;
79  double b = -a;
80  double c = cosPhiU*sinPhiV/sinPhiUmPhiV;
81  double d = -cosPhiV*sinPhiU/sinPhiUmPhiV;
82 
83  double deltaPhiU = (c1/b-c2/d)/(a/b-c/d);
84  double deltaPhiV = (c1/a-c2/c)/(b/a-d/c);
85 
86  cout << " Current Iteration shift plane " << i << " deltaU " << -deltaU << " deltaPhiU " << -deltaPhiU << " deltaPhiV " << -deltaPhiV << endl;
87  constantsFile << i_deltaPhiU - deltaPhiU << " " << i_deltaU - deltaU << " " << i_deltaPhiV - deltaPhiV << " " << i_deltaV << endl;
88  }
89 
90  // Draw the results
91  TCanvas *cResult = new TCanvas("cResult","cResult",1200,900);
92  cResult->Divide(6,4);
93 
94  for (int i=0; i<24; i++){
95  cResult->cd(i+1);
96  resultHists[i]->Draw("colz");
97  resultHists[i]->SetMinimum(-0.025);
98  resultHists[i]->SetMaximum(0.025);
99  }
100 
101  cResult->SaveAs("Result.png");
102  cResult->SaveAs("Result.pdf");
103  cResult->Write();
104 
105  outFile->Write();
106  outFile->Close();
107  constantsFile.close();
108 
109  return;
110 }
Double_t c0[2][NMODULES]
Definition: tw_corr.C:67
TFile * outFile
Definition: FitGains.C:15
sprintf(text,"Post KinFit Cut")
#define c
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
TF1 * f
Definition: FitGains.C:21
Double_t c2[2][NMODULES]
Definition: tw_corr.C:69
double sin(double)
void FitCathodeAlignment(TString inputFile="hd_root.root")