dc740de4 |
1 | /* $Id$ */ |
2 | |
75ec0f41 |
3 | #include "dNdEtaAnalysis.h" |
4 | |
ceb5d1b5 |
5 | #include <TFile.h> |
6 | #include <TH2F.h> |
7 | #include <TH1D.h> |
8 | #include <TMath.h> |
9 | #include <TCanvas.h> |
7029240a |
10 | #include <TCollection.h> |
11 | #include <TIterator.h> |
12 | #include <TList.h> |
fcf2fb36 |
13 | #include <TLegend.h> |
14 | |
15 | #include "dNdEtaCorrection.h" |
ceb5d1b5 |
16 | |
75ec0f41 |
17 | //____________________________________________________________________ |
b7f4a1fd |
18 | ClassImp(dNdEtaAnalysis) |
75ec0f41 |
19 | |
20 | //____________________________________________________________________ |
7029240a |
21 | dNdEtaAnalysis::dNdEtaAnalysis(Char_t* name, Char_t* title) : |
22 | TNamed(name, title) |
23 | { |
24 | hEtaVsVtx = new TH2F(Form("%s_eta_vs_vtx", name),"",80,-20,20,120,-6,6); |
75ec0f41 |
25 | hEtaVsVtx->SetXTitle("vtx z [cm]"); |
26 | hEtaVsVtx->SetYTitle("#eta"); |
4dd2ad81 |
27 | |
7029240a |
28 | hEtaVsVtxUncorrected = dynamic_cast<TH2F*> (hEtaVsVtx->Clone(Form("%s_eta_vs_vtx_uncorrected", name))); |
29 | hVtx = hEtaVsVtx->ProjectionX(Form("%s_vtx", name)); |
30 | for (Int_t i=0; i<kVertexBinning; ++i) |
31 | { |
32 | hdNdEta[i] = hEtaVsVtx->ProjectionY(Form("%s_dNdEta_%d", name, i)); |
33 | hdNdEta[i]->SetYTitle("dN/d#eta"); |
34 | } |
75ec0f41 |
35 | |
36 | hEtaVsVtx->Sumw2(); |
37 | hVtx->Sumw2(); |
75ec0f41 |
38 | } |
39 | |
40 | //____________________________________________________________________ |
41 | void |
5fbd0b17 |
42 | dNdEtaAnalysis::FillTrack(Float_t vtx, Float_t eta) { |
ceb5d1b5 |
43 | hEtaVsVtxUncorrected->Fill(vtx,eta); |
75ec0f41 |
44 | } |
45 | |
46 | //____________________________________________________________________ |
47 | void |
48 | dNdEtaAnalysis::FillEvent(Float_t vtx) { |
49 | hVtx->Fill(vtx); |
50 | } |
51 | |
52 | //____________________________________________________________________ |
fcf2fb36 |
53 | void dNdEtaAnalysis::Finish(dNdEtaCorrection* correction) |
54 | { |
55 | // correct with correction values if available |
56 | // TODO what do we do with the error? |
57 | if (!correction) |
58 | printf("INFO: No correction applied\n"); |
59 | |
60 | // this can be replaced by TH2F::Divide if we agree that the binning will be always the same |
61 | for (Int_t iVtx=0; iVtx<=hEtaVsVtxUncorrected->GetNbinsX(); iVtx++) |
62 | { |
63 | for (Int_t iEta=0; iEta<=hEtaVsVtxUncorrected->GetNbinsY(); iEta++) |
64 | { |
65 | Float_t correctionValue = 1; |
66 | if (correction) |
67 | correctionValue = correction->GetCorrection(hEtaVsVtxUncorrected->GetXaxis()->GetBinCenter(iVtx), hEtaVsVtxUncorrected->GetYaxis()->GetBinCenter(iEta)); |
68 | |
69 | Float_t value = hEtaVsVtxUncorrected->GetBinContent(iVtx, iEta); |
70 | Float_t error = hEtaVsVtxUncorrected->GetBinError(iVtx, iEta); |
71 | |
72 | Float_t correctedValue = value * correctionValue; |
73 | Float_t correctedError = error * correctionValue; |
74 | |
75 | if (correctedValue != 0) |
76 | { |
77 | hEtaVsVtx->SetBinContent(iVtx, iEta, correctedValue); |
78 | hEtaVsVtx->SetBinError(iVtx, iEta, correctedError); |
79 | } |
80 | } |
81 | } |
82 | |
5af55649 |
83 | for (Int_t iEta=0; iEta<=hEtaVsVtx->GetNbinsY(); iEta++) |
84 | { |
7029240a |
85 | // do we have several histograms for different vertex positions? |
fcf2fb36 |
86 | Int_t vertexBinWidth = hVtx->GetNbinsX() / (kVertexBinning-1); |
7029240a |
87 | for (Int_t vertexPos=0; vertexPos<kVertexBinning; ++vertexPos) |
88 | { |
5af55649 |
89 | Int_t vertexBinBegin = 1; |
90 | Int_t vertexBinEnd = hVtx->GetNbinsX() + 1; |
fcf2fb36 |
91 | |
92 | // the first histogram is always for the whole vertex range |
93 | if (vertexPos > 0) |
94 | { |
5af55649 |
95 | vertexBinBegin = 1 + vertexBinWidth * (vertexPos-1); |
fcf2fb36 |
96 | vertexBinEnd = vertexBinBegin + vertexBinWidth; |
97 | } |
7029240a |
98 | |
5af55649 |
99 | Float_t totalEvents = hVtx->Integral(vertexBinBegin, vertexBinEnd - 1); |
100 | if (totalEvents == 0) |
101 | { |
102 | printf("WARNING: No events for hist %d %d %d\n", vertexPos, vertexBinBegin, vertexBinEnd); |
103 | continue; |
7029240a |
104 | } |
7029240a |
105 | |
5af55649 |
106 | Float_t sum = 0; |
107 | Float_t sumError2 = 0; |
108 | for (Int_t iVtx = vertexBinBegin; iVtx < vertexBinEnd; iVtx++) |
109 | { |
110 | if (hEtaVsVtx->GetBinContent(iVtx, iEta) != 0) |
111 | { |
112 | sum = sum + hEtaVsVtx->GetBinContent(iVtx, iEta); |
113 | sumError2 = sumError2 + TMath::Power(hEtaVsVtx->GetBinError(iVtx, iEta),2); |
114 | } |
115 | } |
7029240a |
116 | |
5af55649 |
117 | Float_t dndeta = sum / totalEvents; |
118 | Float_t error = TMath::Sqrt(sumError2) / totalEvents; |
7029240a |
119 | |
5af55649 |
120 | dndeta = dndeta/hdNdEta[vertexPos]->GetBinWidth(iEta); |
121 | error = error/hdNdEta[vertexPos]->GetBinWidth(iEta); |
7029240a |
122 | |
5af55649 |
123 | hdNdEta[vertexPos]->SetBinContent(iEta, dndeta); |
124 | hdNdEta[vertexPos]->SetBinError(iEta, error); |
75ec0f41 |
125 | } |
75ec0f41 |
126 | } |
127 | } |
128 | |
75ec0f41 |
129 | //____________________________________________________________________ |
130 | void |
131 | dNdEtaAnalysis::SaveHistograms() { |
132 | |
7029240a |
133 | gDirectory->mkdir(GetName()); |
134 | gDirectory->cd(GetName()); |
5fbd0b17 |
135 | |
75ec0f41 |
136 | hEtaVsVtx ->Write(); |
fcf2fb36 |
137 | hEtaVsVtxUncorrected->Write(); |
75ec0f41 |
138 | hVtx ->Write(); |
7029240a |
139 | for (Int_t i=0; i<kVertexBinning; ++i) |
140 | hdNdEta[i] ->Write(); |
75ec0f41 |
141 | |
142 | gDirectory->cd("../"); |
143 | } |
144 | |
5fbd0b17 |
145 | void dNdEtaAnalysis::LoadHistograms() |
146 | { |
147 | gDirectory->cd(GetName()); |
148 | |
149 | hEtaVsVtx = dynamic_cast<TH2F*> (gDirectory->Get(hEtaVsVtx->GetName())); |
150 | hEtaVsVtxUncorrected = dynamic_cast<TH2F*> (gDirectory->Get(hEtaVsVtxUncorrected->GetName())); |
151 | |
152 | hVtx = dynamic_cast<TH1D*> (gDirectory->Get(hVtx->GetName())); |
153 | |
154 | for (Int_t i=0; i<kVertexBinning; ++i) |
155 | hdNdEta[i] = dynamic_cast<TH1D*> (gDirectory->Get(hdNdEta[i]->GetName())); |
156 | |
157 | gDirectory->cd("../"); |
158 | } |
159 | |
ceb5d1b5 |
160 | //____________________________________________________________________ |
161 | void dNdEtaAnalysis::DrawHistograms() |
162 | { |
5fbd0b17 |
163 | TCanvas* canvas = new TCanvas("dNdEtaAnalysis", "dNdEtaAnalysis", 800, 800); |
164 | canvas->Divide(2, 2); |
ceb5d1b5 |
165 | |
166 | canvas->cd(1); |
167 | if (hEtaVsVtx) |
168 | hEtaVsVtx->Draw("COLZ"); |
169 | |
170 | canvas->cd(2); |
171 | if (hEtaVsVtxUncorrected) |
172 | hEtaVsVtxUncorrected->Draw("COLZ"); |
173 | |
5fbd0b17 |
174 | canvas->cd(3); |
ceb5d1b5 |
175 | if (hVtx) |
a8d3b360 |
176 | hVtx->Draw(); |
ceb5d1b5 |
177 | |
5fbd0b17 |
178 | canvas->cd(4); |
7029240a |
179 | if (hdNdEta[0]) |
180 | hdNdEta[0]->Draw(); |
fcf2fb36 |
181 | |
182 | // histograms for different vertices? |
183 | if (kVertexBinning > 0) |
184 | { |
185 | // doesnt work, but i dont get it, giving up... |
186 | /*TCanvas* canvas2 =*/ new TCanvas("dNdEtaAnalysisVtx", "dNdEtaAnalysisVtx", 450, 450); |
187 | //Int_t yPads = (Int_t) TMath::Ceil(((Double_t) kVertexBinning - 1) / 2); |
188 | //printf("%d\n", yPads); |
189 | //canvas2->Divide(2, yPads); |
190 | |
191 | TLegend* legend = new TLegend(0.7, 0.7, 0.9, 0.9); |
192 | |
5af55649 |
193 | for (Int_t i=0; i<kVertexBinning; ++i) |
fcf2fb36 |
194 | { |
195 | //canvas2->cd(i-1); |
196 | //printf("%d\n", i); |
197 | if (hdNdEta[i]) |
198 | { |
5af55649 |
199 | hdNdEta[i]->SetLineColor(i+1); |
200 | hdNdEta[i]->Draw((i == 0) ? "" : "SAME"); |
201 | legend->AddEntry(hdNdEta[i], (i == 0) ? "Vtx All" : Form("Vtx Bin %d", i-1)); |
fcf2fb36 |
202 | } |
203 | } |
204 | |
205 | legend->Draw(); |
206 | } |
7029240a |
207 | } |
208 | |
209 | Long64_t dNdEtaAnalysis::Merge(TCollection* list) |
210 | { |
211 | // Merges a list of dNdEtaAnalysis objects with this one. |
212 | // This is needed for PROOF. |
213 | // Returns the number of merged objects (including this) |
214 | |
215 | if (!list) |
216 | return 0; |
217 | |
218 | if (list->IsEmpty()) |
219 | return 1; |
220 | |
221 | TIterator* iter = list->MakeIterator(); |
222 | TObject* obj; |
223 | |
224 | // sub collections |
225 | const Int_t nCollections = kVertexBinning + 3; |
226 | TList* collections[nCollections]; |
227 | for (Int_t i=0; i<nCollections; ++i) |
228 | collections[i] = new TList; |
229 | |
230 | Int_t count = 0; |
231 | while ((obj = iter->Next())) |
232 | { |
233 | dNdEtaAnalysis* entry = dynamic_cast<dNdEtaAnalysis*> (obj); |
234 | if (entry == 0) |
235 | continue; |
236 | |
237 | collections[0]->Add(entry->hEtaVsVtx); |
238 | collections[1]->Add(entry->hEtaVsVtxUncorrected); |
239 | collections[2]->Add(entry->hVtx); |
240 | |
241 | for (Int_t i=0; i<kVertexBinning; ++i) |
242 | collections[3+i]->Add(entry->hdNdEta[i]); |
243 | |
244 | ++count; |
245 | } |
246 | |
247 | hEtaVsVtx->Merge(collections[0]); |
248 | hEtaVsVtxUncorrected->Merge(collections[1]); |
249 | hVtx->Merge(collections[2]); |
250 | for (Int_t i=0; i<kVertexBinning; ++i) |
251 | hdNdEta[i]->Merge(collections[3+i]); |
252 | |
253 | for (Int_t i=0; i<nCollections; ++i) |
254 | delete collections[i]; |
255 | |
256 | return count+1; |
ceb5d1b5 |
257 | } |