1 // ------------------------------------------------------
3 // Class to handle 2d-corrections.
5 // ------------------------------------------------------
15 #include "CorrectionMatrix2D.h"
17 //____________________________________________________________________
18 ClassImp(CorrectionMatrix2D)
20 //____________________________________________________________________
21 CorrectionMatrix2D::CorrectionMatrix2D(const CorrectionMatrix2D& c)
25 ((CorrectionMatrix2D &)c).Copy(*this);
28 //____________________________________________________________________
29 CorrectionMatrix2D::CorrectionMatrix2D(Char_t* name, Char_t* title,
30 Int_t nBinX, Float_t Xmin, Float_t Xmax,
31 Int_t nBinY, Float_t Ymin, Float_t Ymax)
39 fhMeas = new TH2F(Form("meas_%s",name), Form("meas_%s",title), nBinX, Xmin, Xmax, nBinY, Ymin, Ymax);
40 fhGene = new TH2F(Form("gene_%s",name), Form("gene_%s",title), nBinX, Xmin, Xmax, nBinY, Ymin, Ymax);
41 fhCorr = new TH2F(Form("corr_%s",name), Form("corr_%s",title), nBinX, Xmin, Xmax, nBinY, Ymin, Ymax);
48 //____________________________________________________________________
49 CorrectionMatrix2D::CorrectionMatrix2D(Char_t* name,Char_t* title,
50 Int_t nBinX, Float_t *X, Int_t nBinY, Float_t *Y)
57 fhMeas = new TH2F(Form("meas_%s",name), Form("meas_%s",title), nBinX, X, nBinY, Y);
58 fhGene = new TH2F(Form("gene_%s",name), Form("gene_%s",title), nBinX, X, nBinY, Y);
59 fhCorr = new TH2F(Form("corr_%s",name), Form("corr_%s",title), nBinX, X, nBinY, Y);
67 //____________________________________________________________________
68 CorrectionMatrix2D::~CorrectionMatrix2D() {
72 if (fhMeas) delete fhMeas;
73 if (fhGene) delete fhGene;
74 if (fhCorr) delete fhCorr;
77 //____________________________________________________________________
78 CorrectionMatrix2D &CorrectionMatrix2D::operator=(const CorrectionMatrix2D &c)
83 ((CorrectionMatrix2D &) c).Copy(*this);
88 //____________________________________________________________________
89 TH1F* CorrectionMatrix2D::Get1DCorrection(Char_t* opt) {
91 // integrate the correction over one variable
97 if (strcmp(opt,"x")==0) {
98 meas1D = fhMeas->ProjectionX();
99 gene1D = fhGene->ProjectionX();
101 if (strcmp(opt,"y")==0) {
102 meas1D = fhMeas->ProjectionY();
103 gene1D = fhGene->ProjectionY();
107 gene1D->SetName(Form("corr_1D_%s",fName.Data()));
108 gene1D->SetTitle(Form("corr_1D_%s",fName.Data()));
110 gene1D->Divide(gene1D, meas1D, 1, 1, "B");
112 return (TH1F*)gene1D;
116 //____________________________________________________________________
118 CorrectionMatrix2D::Copy(TObject& c) const
122 CorrectionMatrix2D& target = (CorrectionMatrix2D &) c;
124 target.fhMeas = fhMeas;
125 target.fhGene = fhGene;
126 target.fhCorr = fhCorr;
130 //________________________________________________________________________
131 void CorrectionMatrix2D::SetAxisTitles(Char_t* titleX, Char_t* titleY)
134 // method for setting the axis titles of the histograms
137 fhMeas ->SetXTitle(titleX); fhMeas ->SetYTitle(titleY);
138 fhGene ->SetXTitle(titleX); fhGene ->SetYTitle(titleY);
139 fhCorr ->SetXTitle(titleX); fhCorr ->SetYTitle(titleY);
142 //____________________________________________________________________
143 Long64_t CorrectionMatrix2D::Merge(TCollection* list) {
144 // Merge a list of CorrectionMatrix2D objects with this (needed for
146 // Returns the number of merged objects (including this).
154 TIterator* iter = list->MakeIterator();
157 // collections of measured and generated histograms
158 TList* collectionMeas = new TList;
159 TList* collectionGene = new TList;
162 while ((obj = iter->Next())) {
164 CorrectionMatrix2D* entry = dynamic_cast<CorrectionMatrix2D*> (obj);
168 collectionMeas->Add(entry->GetMeasuredHistogram());
169 collectionGene->Add(entry->GetGeneratedHistogram());
173 fhMeas->Merge(collectionMeas);
174 fhGene->Merge(collectionGene);
176 // is this really faster than just adding the histograms in the list???
177 delete collectionMeas;
178 delete collectionGene;
185 //____________________________________________________________________
186 void CorrectionMatrix2D::Divide() {
188 // divide the histograms to get the correction
191 if (!fhMeas || !fhGene) return;
193 fhCorr->Divide(fhGene, fhMeas, 1,1,"B");
197 //____________________________________________________________________
199 CorrectionMatrix2D::RemoveEdges(Float_t cut, Int_t nBinsXedge, Int_t nBinsYedge)
201 // remove edges of correction histogram by removing
202 // - bins with content less than cut
203 // - bins next to bins with zero bin content
205 Int_t nBinsX = fhCorr->GetNbinsX();
206 Int_t nBinsY = fhCorr->GetNbinsY();
208 // set bin content to zero for bins with content smaller cut
209 for (Int_t bx=0; bx<=nBinsX; bx++) {
210 for (Int_t by=0; by<=nBinsY; by++) {
211 if (fhCorr->GetBinContent(bx,by)>cut) {
212 fhCorr->SetBinContent(bx,by,0);
213 fhCorr->SetBinError(bx,by,0);
218 // set bin content to zero for bins next to bins with zero
219 TH2F* tmp = (TH2F*)fhCorr->Clone("tmp");
222 Bool_t done = kFALSE;
223 Int_t nBinsXCount = 0;
224 Int_t nBinsYCount = 0;
226 if (nBinsXCount<nBinsXedge)
227 for (Int_t bx=0; bx<=nBinsX; bx++) {
228 for (Int_t by=0; by<=nBinsY; by++) {
229 if ((fhCorr->GetBinContent(bx+1,by)==0)||
230 (fhCorr->GetBinContent(bx-1,by)==0))
231 tmp->SetBinContent(bx,by,1);
235 if (nBinsYCount<nBinsYedge)
236 for (Int_t bx=0; bx<=nBinsX; bx++) {
237 for (Int_t by=0; by<=nBinsY; by++) {
238 if ((fhCorr->GetBinContent(bx,by+1)==0)||
239 (fhCorr->GetBinContent(bx,by-1)==0))
240 tmp->SetBinContent(bx,by,1);
243 for (Int_t bx=0; bx<=nBinsX; bx++) {
244 for (Int_t by=0; by<=nBinsY; by++) {
245 if (tmp->GetBinContent(bx,by)==1) {
246 fhCorr->SetBinContent(bx,by,0);
247 fhCorr->SetBinError(bx,by,0);
253 if ((nBinsXCount>=nBinsXedge)&&(nBinsYCount>=nBinsYedge)) done=kTRUE;
259 //____________________________________________________________________
260 Bool_t CorrectionMatrix2D::LoadHistograms(Char_t* fileName, Char_t* dir) {
262 // loads the histograms from a file
265 TFile* fin = TFile::Open(fileName);
268 //Info("LoadHistograms",Form(" %s file does not exist",fileName));
272 if(fhGene) {delete fhGene; fhGene=0;}
273 if(fhCorr) {delete fhCorr; fhCorr=0;}
274 if(fhMeas) {delete fhMeas; fhMeas=0;}
276 fhMeas = (TH2F*)fin->Get(Form("%s/meas_%s", dir,GetName()));
277 if(!fhMeas) Info("LoadHistograms","No meas hist available");
279 fhGene = (TH2F*)fin->Get(Form("%s/gene_%s",dir, GetName()));
280 if(!fhGene) Info("LoadHistograms","No gene hist available");
282 fhCorr = (TH2F*)fin->Get(Form("%s/corr_%s",dir, GetName()));
285 Info("LoadHistograms","No corr hist available");
293 //____________________________________________________________________
295 CorrectionMatrix2D::SaveHistograms() {
297 // saves the histograms
307 //____________________________________________________________________
308 void CorrectionMatrix2D::DrawHistograms()
311 // draws all the four histograms on one TCanvas
314 TCanvas* canvas = new TCanvas(Form("correction_%s",fName.Data()),
315 Form("correction_%s",fName.Data()), 800, 800);
316 canvas->Divide(2, 2);
320 fhMeas->Draw("COLZ");
324 fhGene->Draw("COLZ");
328 fhCorr->Draw("COLZ");
332 // add: draw here the stat. errors of the correction histogram