Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExtractCDCDeformation.C
Go to the documentation of this file.
1 // Script to extract time-walk constants for the BCAL
2 
3 //Leave this global so the accesors don't need the pointer as an argument
4 TFile *thisFile;
5 
6 // Accessor functions to grab histograms from our file
7 // (Makes things easy with the HistogramTools fills)
8 TH1I * Get1DHistogram(const char * plugin, const char * directoryName, const char * name, bool print = true){
9  TH1I * histogram;
10  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
11  thisFile->GetObject(fullName, histogram);
12  if (histogram == 0){
13  if (print) cout << "Unable to find histogram " << fullName.Data() << endl;
14  return NULL;
15  }
16  return histogram;
17 }
18 
19 TH2I * Get2DHistogram(const char * plugin, const char * directoryName, const char * name){
20  TH2I * histogram;
21  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
22  thisFile->GetObject(fullName, histogram);
23  if (histogram == 0){
24  cout << "Unable to find histogram " << fullName.Data() << endl;
25  return NULL;
26  }
27  return histogram;
28 }
29 
30 TCanvas * Plot2DCDC(TH2D **histograms, TString name, TString title, float minScale, float maxScale){
31  TCanvas *canvas = new TCanvas(name,name, 800, 800);
32  TH2D *axes = new TH2D(TString("axes") + name, title, 1, -65.0, 65.0, 1, -65.0, 65.0);
33  axes->SetBinContent(1,1,-1.0/0.0); // Don't draw background color
34  axes->SetStats(0);
35  axes->Fill(100,100); // without this, the color ramp is not drawn
36  axes->GetZaxis()->SetRangeUser(minScale, maxScale);
37  axes->Draw("colz");
38  for(unsigned int iring=1; iring<=28; iring++){
39  histograms[iring]->GetZaxis()->SetRangeUser(minScale, maxScale);
40  histograms[iring]->SetStats(0);
41  histograms[iring]->Draw("same col pol"); // draw remaining histos without overwriting color palette
42  }
43  return canvas;
44 }
45 
46 // Do the extraction of the actual constants
47 void ExtractCDCDeformation(TString filename = "hd_root.root"){
48 
49  // Open our input and output file
50  thisFile = TFile::Open(filename);
51  TFile *outputFile = TFile::Open("CDCDeformation_Results.root", "RECREATE");
52 
53  // Check to make sure it is open
54  if (thisFile == 0) {
55  cout << "Unable to open file " << filename.Data() << "...Exiting" << endl;
56  return;
57  }
58 
59  // This stream will be for outputting the results in a format suitable for the CCDB
60  // Will wait to open until needed
61  ofstream textFile;
62  textFile.open("CDC_Deformation.txt");
63 
64  // We want to display the direction of the shift as well as the magnitude in the "CDC view"
65  // Let's make it happen
66  int straw_offset[29] = {0,0,42,84,138,192,258,324,404,484,577,670,776,882,1005,1128,1263,1398,1544,1690,1848,2006,2176,2346,2528,2710,2907,3104,3313};
67  int Nstraws[28] = {42, 42, 54, 54, 66, 66, 80, 80, 93, 93, 106, 106, 123, 123, 135, 135, 146, 146, 158, 158, 170, 170, 182, 182, 197, 197, 209, 209};
68  double radius[28] = {10.72134, 12.08024, 13.7795, 15.14602, 18.71726, 20.2438, 22.01672, 23.50008, 25.15616, 26.61158, 28.33624, 29.77388, 31.3817, 32.75838, 34.43478, 35.81146, 38.28542, 39.7002, 41.31564, 42.73042, 44.34078, 45.75302, 47.36084, 48.77054, 50.37582, 51.76012, 53.36286, 54.74716};
69  double phi[28] = {0, 0.074707844, 0.038166294, 0.096247609, 0.05966371, 0.012001551, 0.040721951, 0.001334527, 0.014963808, 0.048683644, 0.002092645, 0.031681749, 0.040719354, 0.015197341, 0.006786058, 0.030005892, 0.019704045, -0.001782064, -0.001306618, 0.018592421, 0.003686784, 0.022132975, 0.019600866, 0.002343723, 0.021301449, 0.005348855, 0.005997358, 0.021018761};
70 
71  TH2D * Amplitude_view[29];
72  TH2D * Direction_view[29];
73  TH2D * Vertical_view[29];
74  TH2D * Horizontal_view[29];
75 
76  outputFile->mkdir("PerRing");
77  outputFile->cd("PerRing");
78  for(unsigned int iring=0; iring<28; iring++){
79  double r_start = radius[iring] - 0.8;
80  double r_end = radius[iring] + 0.8;
81  double phi_start = phi[iring];
82  double phi_end = phi_start + TMath::TwoPi();
83 
84  char hname[256];
85  sprintf(hname, "Amplitude_view_ring[%d]", iring+1);
86  Amplitude_view[iring+1] = new TH2D(hname, "", Nstraws[iring], phi_start, phi_end, 1, r_start, r_end);
87  sprintf(hname, "Direction_view_ring[%d]", iring+1);
88  Direction_view[iring+1] = new TH2D(hname, "", Nstraws[iring], phi_start, phi_end, 1, r_start, r_end);
89  sprintf(hname, "Vertical_view_ring[%d]", iring+1);
90  Vertical_view[iring+1] = new TH2D(hname, "", Nstraws[iring], phi_start, phi_end, 1, r_start, r_end);
91  sprintf(hname, "Horizontal_view_ring[%d]", iring+1);
92  Horizontal_view[iring+1] = new TH2D(hname, "", Nstraws[iring], phi_start, phi_end, 1, r_start, r_end);
93  }
94 
95  //Fit function for
96  TF1 *f1 = new TF1("f1", "[0] + [1] * TMath::Cos(x + [2])", -3.14, 3.14);
97  f1->SetParLimits(0, 0.5, 1.0);
98  f1->SetParLimits(1, 0.0, 0.35);
99  //f1->SetParLimits(2, -3.14, 3.14);
100  f1->SetParameters(0.78, 0.0, 0.0);
101 
102  outputFile->cd();
103  outputFile->mkdir("FitParameters");
104  outputFile->cd("FitParameters");
105 
106  // Make some histograms to get the distributions of the fit parameters
107  TH1I *h1_c0 = new TH1I("h1_c0", "Distribution of Constant", 100, 0.5, 1.0);
108  TH1I *h1_c1 = new TH1I("h1_c1", "Distribution of Amplitude", 100, 0.0, 0.35);
109  TH1I *h1_c2 = new TH1I("h1_c2", "Direction of Longest Drift Time", 100, -3.14, 3.14);
110  TH1F *h1_c2_weighted = new TH1F("h1_c2_weighted", "Distribution of Direction weighted by amplitude", 100, -3.14, 3.14);
111  TH2I *h2_c0_c1 = new TH2I("h2_c0_c1", "c_{1} Vs. c_{0}; c_{0}; c_{1}", 100, 0.5, 1.0, 100, 0, 0.35);
112  TH2I *h2_c0_c2 = new TH2I("h2_c0_c2", "c_{2} Vs. c_{0}; c_{0}; c_{2}", 100, 0.5, 1.0, 100, -10, 10);
113  TH2I *h2_c1_c2 = new TH2I("h2_c1_c2", "c_{2} Vs. c_{1}; c_{1}; c_{2}", 100, 0.0, 0.35, 100, -10, 10);
114 
115  outputFile->cd();
116  outputFile->mkdir("Fits");
117  outputFile->cd("Fits");
118 
119  // Now we want to loop through all available module/layer/sector and try to make a fit of each one
120  int ring = 1, straw = 1;
121  while (ring <= 28){
122  cout << "Entering Fit " << endl;
123  char folder[100];
124  sprintf(folder, "Ring %.2i", ring);
125  char strawname[100];
126  sprintf(strawname,"Straw %.3i Predicted Drift Distance Vs phi_DOCA", straw);
127  TH2I *thisStrawHistogram = Get2DHistogram("CDCPerStrawReco_Middle",folder,strawname);
128 
129  if (thisStrawHistogram != NULL) {
130 
131  // Now to do our fits. This time we know there are 16 bins.
132  double percentile95[16], percentile97[16], percentile99[16]; // Location of 95, 97,and 99th percentile bins
133  double binCenter[16];
134  char name[100];
135  sprintf(name,"Ring %.2i Straw %.3i", ring, straw);
136  TH1D *extractedPoints = new TH1D(name, name, 16, -3.14, 3.14);
137  for (int i = 1; i <= thisStrawHistogram->GetNbinsX() ; i++){
138  TH1D *projY = thisStrawHistogram->ProjectionY(" ", i, i);
139  binCenter[i-1] = thisStrawHistogram->GetXaxis()->GetBinCenter(i);
140  int nbins = projY->GetNbinsX();
141  //Get the total nubmer of entries
142  int nEntries = projY->GetEntries();
143  if (nEntries == 0) continue;
144  double errorFraction = TMath::Sqrt(nEntries) / nEntries;
145  double perc95 = 0.95*nEntries, perc97 = 0.97 * nEntries, perc99 = 0.99 * nEntries;
146  //Accumulate from the beginning to get total, mark 95, 97, 99% location
147  int total = 0;
148  for (int j = 0; j <= nbins; j++){
149  total += projY->GetBinContent(j);
150  if (total > perc99) percentile99[i-1] = projY->GetBinCenter(j);
151  else if (total > perc97) {
152  percentile97[i-1] = projY->GetBinCenter(j);
153  extractedPoints->SetBinContent(i, projY->GetBinCenter(j));
154  extractedPoints->SetBinError(i, errorFraction * projY->GetBinCenter(j));
155  }
156  else if (total > perc95) percentile95[i-1] = projY->GetBinCenter(j);
157  }
158  }
159  f1->SetParameters(0.78, 0.0, 0.0);
160  TFitResultPtr fr = extractedPoints->Fit(f1, "SR");
161  Int_t fitStatus = fr;
162  if (fitStatus == 0){
163  double c0 = fr->Parameter(0);
164  double c1 = fr->Parameter(1);
165  double c2 = fr->Parameter(2);
166  // Move c2 to fit on our range
167  while (c2 > TMath::Pi()) c2 -= 2 * TMath::Pi();
168  while (c2 < -1* TMath::Pi()) c2 += 2 * TMath::Pi();
169  h1_c0->Fill(c0); h1_c1->Fill(c1); h1_c2->Fill(-1*c2); h1_c2_weighted->Fill(-1*c2,c1);
170  h2_c0_c1->Fill(c0,c1); h2_c0_c2->Fill(c0,c2); h2_c1_c2->Fill(c1,c2);
171  Amplitude_view[ring]->SetBinContent(straw,1,c1);
172  Direction_view[ring]->SetBinContent(straw,1,-1*c2);
173  Vertical_view[ring]->SetBinContent(straw,1,c1*TMath::Sin(-1*c2));
174  Horizontal_view[ring]->SetBinContent(straw,1,c1*TMath::Cos(-1*c2));
175  textFile << c1 << " " << c2 << endl;
176  }
177  else {
178  cout << "WARNING: Fit Status "<< fitStatus << " for ring " << ring << " straw " << straw << endl;
179  textFile << "0.0 0.0" << endl;
180  }
181 
182  }
183  else{
184  textFile << "0.0 0.0" << endl;
185  }
186 
187  // On to the next one
188  straw++;
189  if(straw > Nstraws[ring-1]){
190  straw = 1;
191  ring++;
192  }
193  }
194 
195  outputFile->cd();
196  outputFile->mkdir("2D");
197  outputFile->cd("2D");
198 
199  TCanvas *c_Amplitude = Plot2DCDC(Amplitude_view,"c_Amplitude", "Amplitude of Sinusoid", 0.0, 0.3);
200  TCanvas *c_Direction = Plot2DCDC(Direction_view,"c_Direction", "Direction of #delta", -3.14, 3.14);
201  TCanvas *c_Vertical = Plot2DCDC(Vertical_view,"c_Vertical", "Vertical Projection of Delta", -0.3, 0.3);
202  TCanvas *c_Horizontal = Plot2DCDC(Horizontal_view,"c_Horizontal", "Horizontal Projection of Delta", -0.3, 0.3);
203 
204  c_Amplitude->Write();
205  c_Direction->Write();
206  c_Horizontal->Write();
207  c_Vertical->Write();
208 
209  cout << "Closing Files..." << endl;
210  outputFile->Write();
211  thisFile->Close();
212  textFile.close();
213  return;
214 }
215 
216 
217 
Double_t c0[2][NMODULES]
Definition: tw_corr.C:67
TH2D * axes
double total
Definition: FitGains.C:151
Float_t maxScale
sprintf(text,"Post KinFit Cut")
TString filename
TH1I * Get1DHistogram(const char *plugin, const char *directoryName, const char *name, bool print=true)
TFile * thisFile
TH2I * Get2DHistogram(const char *plugin, const char *directoryName, const char *name)
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
TFile f1(fnam)
Double_t c2[2][NMODULES]
Definition: tw_corr.C:69
TCanvas * Plot2DCDC(TH2D **histograms, TString name, TString title, float minScale, float maxScale)
void ExtractCDCDeformation(TString filename="hd_root.root")
Float_t minScale