ATLAS Offline Software
Loading...
Searching...
No Matches
PixelResidualHistograms.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef PixelResidualHistograms_C
6#define PixelResidualHistograms_C
7
8#include <iostream>
9#include <vector>
10#include <string>
11#include <sstream>
12#include <cmath>
13
14#include <TH1.h>
15#include <TProfile.h>
16#include <TCanvas.h>
17#include <TDirectory.h>
18
22
23namespace PixelCalib{
24
26 const std::string& title,
27 double limits,
28 int nbins,
29 const std::vector< std::vector < float > > &binnage,
30 const std::vector< std::string > &binnames):
35 m_binnames(binnames),
36 m_binnage(binnage),
37 m_axisName(""){
38
39 TH1D *Histomodel = new TH1D(name.c_str(),title.c_str(),nbins,-limits,limits);
41
42 SetAxisTitle(title);
43
44 m_MeanProfilesVector = new std::vector<TProfile*>(m_binnames.size());
45 m_RMSProfilesVector = new std::vector<TProfile*>(m_binnames.size());
46
47 delete Histomodel;
48 Histomodel = 0;
49
50}
51
53
65
67
68void PixelResidualHistograms::Fill(float residual, const std::vector< float >& parameters){
69 m_HistogramsVector->Fill(residual,1,parameters);
70}
71
73
74void PixelResidualHistograms::SetAxisTitle(const std::string& title){
75
76 m_axisName = title;
77 for(unsigned int i = 0 ; i < m_HistogramsVector->GetNhistos() ; i++)
78 m_HistogramsVector->GetHisto(i)->GetXaxis()->SetTitle(title.c_str());
79
80
81}
82
84
86
87 TDirectory *current = gDirectory;
88 current->mkdir( m_HistogramsVector->GetName() )->cd();
89
90 int writtenhistos = 0;
91 m_HistogramsVector->Write();
92 writtenhistos += m_HistogramsVector->GetNhistos();
93
94 TProfile *swap = 0;
95 for(unsigned int i = 0; i < m_MeanProfilesVector->size(); i++){
96 swap = GetProfile(i,true,writebins);
97 if( swap->Write() ) writtenhistos++;
98 swap = GetProfile(i,false,writebins);
99 if( swap->Write() ) writtenhistos++;
100 }
101
102 TH1D *swap2 = GetGlobalHisto();
103 if( swap2->Write() ) writtenhistos++;
104
105 current->cd();
106 swap = 0;
107 swap2 = 0;
108 return writtenhistos;
109}
110
112
114
115
116 TDirectory *current = gDirectory;
117 TDirectory *globaldir = (TDirectory *)current->Get(m_HistogramsVector->GetName());
118 globaldir->cd();
119
120 globaldir = (TDirectory *)gDirectory->Get(m_HistogramsVector->GetName());
121
122 int readhistos = 0;
123 readhistos += m_HistogramsVector->FillFromFile(globaldir);
124
125 current->cd();
126 return readhistos;
127}
128
130
132 return m_HistogramsVector->GetHisto(i);
133}
134
136
137TProfile* PixelResidualHistograms::GetMeanProfile(const std::string& binname){
138
139 for(unsigned int i = 0; i < m_binnames.size(); i++)
140 if(m_binnames[i] == binname) return GetProfile(i,false);
141 return 0;
142
143}
144
146
147TProfile* PixelResidualHistograms::GetRMSProfile(const std::string& binname){
148
149 for(unsigned int i = 0; i < m_binnames.size(); i++)
150 if(m_binnames[i] == binname) return GetProfile(i,true);
151 return 0;
152
153}
154
156
158
159
160 if( m_GlobalHisto != 0) return m_GlobalHisto;
161
162 TH1 *swap = m_HistogramsVector->GetHisto(0);
163 double limits = swap->GetXaxis()->GetXmax();
164 int nbins = swap->GetNbinsX();
165 std::string name = m_HistogramsVector->GetName() + std::string("_global");
166 std::string title = m_HistogramsVector->GetTitle();
167
168 m_GlobalHisto = new TH1D(name.c_str(), title.c_str(),
169 nbins,-limits,limits);
170 m_GlobalHisto->GetXaxis()->SetTitle(title.c_str());
171
172 for(unsigned int i = 0 ; i < m_HistogramsVector->GetNhistos(); i++){
173 swap = GetHisto(i);
174 m_GlobalHisto->Add(swap);
175 }
176
177 return m_GlobalHisto;
178
179}
180
182
183TProfile* PixelResidualHistograms::GetProfile(const int binnumber, bool RMS, bool savebis){
184
185 // check if it has already been calculated!
186 if(RMS && (*m_RMSProfilesVector)[binnumber] != 0)
187 return (*m_RMSProfilesVector)[binnumber];
188 else if( (*m_MeanProfilesVector)[binnumber] != 0)
189 return (*m_MeanProfilesVector)[binnumber];
190
191 // Allocate the new TProfile
192 int nProfileBins = ( m_binnage[binnumber] ).size() - 1;
193 float* xbins = new float[nProfileBins+1];
194 std::string Xvar_name = m_binnames[binnumber];
195 std::string Xvar_name_s = Xvar_name;
196 size_t found0 = Xvar_name_s.find('#');
197 if(found0 != std::string::npos) Xvar_name_s.erase(Xvar_name_s.begin(), Xvar_name_s.begin()+found0+1);
198 size_t found1 = Xvar_name_s.find('_');
199 if(found1 != std::string::npos) Xvar_name_s.erase(Xvar_name_s.begin()+found1,Xvar_name_s.end());
200 for(int i = 0 ; i < nProfileBins+1 ; i++)
201 xbins[i] = (m_binnage[binnumber])[i];
202 std::string name = std::string(m_HistogramsVector->GetName());
203 std::string title = std::string(m_HistogramsVector->GetTitle());
204 std::string RMSname = name + std::string("_RMS_vs_") + Xvar_name_s;
205 std::string RMStitle = std::string("Resolution of ") +
206 title + std::string(" vs ") + Xvar_name;
207 name = name + std::string("_Mean_vs_") + Xvar_name_s;
208 title = std::string("Mean of ") + title + std::string(" vs ") + Xvar_name;
209
210 TProfile *theMeanProfile = new TProfile( name.c_str(), title.c_str(),
211 nProfileBins, xbins);
212 TProfile *theRMSProfile = new TProfile( RMSname.c_str(), RMStitle.c_str(),
213 nProfileBins, xbins);
214
215 theMeanProfile->GetXaxis()->SetTitle( Xvar_name.c_str() );
216 theRMSProfile->GetXaxis()->SetTitle( Xvar_name.c_str() );
217 theMeanProfile->GetYaxis()->SetTitle( (std::string("Mean of ") + m_axisName).c_str() );
218 theRMSProfile->GetYaxis()->SetTitle( (std::string("RMS of ") + m_axisName).c_str() );
219
220 // fill it bin by bin
221 std::vector<int> indexes;
222 for(int ibin = 0; ibin < nProfileBins; ibin++){
223 // creating the histogram for this bin..
224 std::string bintitle = m_HistogramsVector->GetTitle();
225 std::ostringstream binname;
226 binname << m_HistogramsVector->GetName() << "vs" << Xvar_name_s << "_" << ibin;
227 TH1 *swap = m_HistogramsVector->GetHisto(0);
228 double limits = swap->GetXaxis()->GetXmax();
229 int nbins = swap->GetNbinsX();
230 TH1D *bin = new TH1D(binname.str().c_str(), bintitle.c_str(),
231 nbins,-limits,limits);
232 for(unsigned int i = 0 ; i < m_HistogramsVector->GetNhistos(); i++){
233 indexes = m_HistogramsVector->GetDivisionsIndexes(i);
234 if(indexes[binnumber] == ibin){
235 swap = GetHisto(i);
236 bin->Add(swap);
237 }
238 }
239
240 double entries = 0.;
241 double mean = 0.;
242 double mean_error = 0.;
243 double rms = 0.;
244 double rms_error = 0.;
245
246 if(bin->GetEntries() > 50){
247 GetCoreParameters(bin,mean,mean_error,rms,rms_error,entries);
248 }
249
250 if(entries > 1){
251 rms_error = sqrt( fabs(rms_error*rms_error*entries*entries
252 - rms*rms/entries));
253 mean_error = sqrt( fabs(mean_error*mean_error*entries*entries
254 + mean*mean/entries));
255 }
256
257 theMeanProfile->SetBinContent(ibin+1,mean*entries);
258 theMeanProfile->SetBinError(ibin+1,mean_error);
259 theMeanProfile->SetBinEntries(ibin+1,entries);
260 theRMSProfile->SetBinContent(ibin+1,rms*entries);
261 theRMSProfile->SetBinError(ibin+1,rms_error);
262 theRMSProfile->SetBinEntries(ibin+1,entries);
263
264 //std::cout << theMeanProfile->GetBinContent(ibin+1) << " "
265 //<< theMeanProfile->GetBinError(ibin+1) << std::endl;
266
267 if(entries > 0 && savebis) bin->Write();
268 delete bin;
269 bin = 0;
270 swap = 0;
271
272 }
273
274 // put the calcuated profile in he correct place and return it!
275 (*m_MeanProfilesVector)[binnumber] = theMeanProfile;
276 (*m_RMSProfilesVector)[binnumber] = theRMSProfile;
277 if(RMS) return theRMSProfile;
278 else return theMeanProfile;
279
280}
281
283
284std::vector <TCanvas*> *PixelResidualHistograms::DrawProfiles(int color, int marker, float labely,
285 std::vector <TCanvas*> *canvasvector, const std::string& name ){
286
287 SetAtlasStyle();
288 std::string drawoptions = "Psame";
289 std::string legend = name;
290 if(canvasvector == 0){
291 drawoptions = "P";
292 canvasvector = new std::vector<TCanvas*>();
293 for(unsigned int i = 0; i < 2 * m_MeanProfilesVector->size(); i++){
294 TCanvas *c1 = new TCanvas();
295 c1->UseCurrentStyle();
296 canvasvector->push_back(c1);
297 }
298 }else if( canvasvector->size() != 2 * m_MeanProfilesVector->size() ) return 0;
299
300
301 TProfile *swap = 0;
302 for(unsigned int i = 0; i < m_MeanProfilesVector->size(); i++){
303 // mean profile!
304 (*canvasvector)[i]->cd();
305 swap = GetProfile(i,false);
306 swap->UseCurrentStyle();
307 swap->SetLineColor(color);
308 swap->SetMarkerColor(color);
309 swap->SetMarkerStyle(marker);
310 swap->Draw(drawoptions.c_str());
311 swap->GetYaxis()->SetTitleOffset(1.2);
312 double maximum = swap->GetMaximum();
313 double minimum = swap->GetMinimum();
314 swap->SetMaximum(maximum + fabs(minimum) + fabs(maximum));
315 swap->SetMinimum(minimum - fabs(maximum) - fabs(minimum));
316
317
318 if(drawoptions == "P"){
319 size_t found = std::string(swap->GetTitle()).find("p_{T}");
320 if(found != std::string::npos) (*canvasvector)[i]->SetLogx();
321 (*canvasvector)[i]->SetGridy();
322 DrawTitleLatex(swap->GetTitle(), 0.2,0.87);
323 (*canvasvector)[i]->SetName(swap->GetName());
324 }
325 if(labely != 0){
326 if(legend == "") legend = swap->GetTitle();
327 DrawLegendLatex(legend.c_str(),marker,0.7,labely,color,0.04);
328 }
329
330 // rms profile!
331 int i2 = i+m_MeanProfilesVector->size();
332 (*canvasvector)[i2]->cd();
333 swap = GetProfile(i,true);
334 swap->UseCurrentStyle();
335 swap->SetLineColor(color);
336 swap->SetMarkerColor(color);
337 swap->SetMarkerStyle(marker);
338 swap->Draw(drawoptions.c_str());
339 swap->GetYaxis()->SetTitleOffset(1.2);
340 swap->SetMaximum(2*fabs(swap->GetMaximum()));
341
342
343 if(drawoptions == "P"){
344 size_t found = std::string(swap->GetTitle()).find("p_{T}");
345 if(found != std::string::npos) (*canvasvector)[i2]->SetLogx();
346 (*canvasvector)[i2]->SetGridy();
347 DrawTitleLatex(swap->GetTitle(), 0.2,0.87);
348 (*canvasvector)[i2]->SetName(swap->GetName());
349 }
350 if(labely != 0){
351 if(legend == "") legend = swap->GetTitle();
352 DrawLegendLatex(legend.c_str(),marker,0.7,labely,color,0.04);
353 }
354
355 }
356
357 swap = 0;
358 return canvasvector;
359}
360
361
363/*
364const std::vector< std::string > *PixelResidualHistograms::GetBinsNames() const{
365 return &m_binnames;
366}
367*/
369/*
370const std::vector < float > *PixelResidualHistograms::GetBins(std::string binname) const{
371
372 for(unsigned int i = 0; i < m_binnames.size(); i++)
373 if(m_binnames[i] == binname) return &(m_binnage[i]);
374 return 0;
375
376}
377*/
379/*
380PixelResidualHistograms *PixelResidualHistograms::Clone() const{
381
382 const char *title = m_HistogramsVector->GetTitle();
383 TH1 *swap = m_HistogramsVector->GetHisto(0);
384 double limit = swap->GetBinLowEdge(1);
385 PixelResidualHistograms *theclone =
386 new PixelResidualHistograms(m_GlobalName,std::string(title),
387 -limit,m_binnage,m_binnames);
388 return theclone;
389}
390*/
392/*
393int PixelResidualHistograms::Analyze(PixelResidualHistograms *reference,
394 std::string referencename){
395
396 SetAtlasStyle();
397 TCanvas *c1 = new TCanvas();
398 c1->UseCurrentStyle();
399
400 int nhistos = m_HistogramsVector->GetNhistos();
401
402 char *currpath = getcwd(nullptr,0);
403 mkdir(m_GlobalName.c_str(),S_IRWXU | S_IRWXG | S_IRWXO);
404 chdir(m_GlobalName.c_str());
405
406 for(int i = 0 ; i < nhistos ; i++){
407 TH1D *swap = m_HistogramsVector->GetHisto(i);
408 if( swap->GetEntries() < 100) continue;
409 DrawHisto(i,4,27,0.73,0.75,std::string("Analog"),std::string("PE"));
410 if(reference != 0 ) reference->DrawHisto(i,2,22,0.73,0.6,
411 referencename,std::string("samePE"));
412 DrawATLASLabel(0.65,0.87);
413 DrawTitleLatex(swap->GetTitle(), 0.2,0.87);
414 std::string name = std::string(swap->GetName()) + std::string(".pdf");
415 c1->Print(name.c_str());
416 }
417
418 if(GetProfile() != 0){
419 DrawProfile(4,27,0.2,0.75,std::string("Analog"),std::string("PE"));
420 if(reference != 0 )
421 reference->DrawProfile(2,22,0.2,0.6,
422 referencename,std::string("samePE"));
423 DrawTitleLatex(m_HistogramsVector->GetTitle(), 0.2,0.87);
424 std::string name = std::string(m_HistogramsVector->GetName()) +
425 std::string("Profile.pdf");
426 c1->Print(name.c_str());
427 }
428
429 chdir(currpath);
430 delete currpath;
431 delete c1;
432
433 return nhistos;
434
435}
436*/
438/*
439void PixelResidualHistograms::DrawHisto(int iHisto,
440 int color, int marker, float labelx, float labely,
441 std::string name,std::string Options){
442
443 TH1D *swap = m_HistogramsVector->GetHisto(iHisto);
444 swap->UseCurrentStyle();
445 swap->SetLineColor(color);
446 swap->SetMarkerColor(color);
447 swap->SetMarkerStyle(marker);
448 swap->Sumw2();
449 swap->Scale(1/swap->GetEntries());
450 swap->GetXaxis()->SetTitle("Residuals (#mum)");
451 swap->GetYaxis()->SetTitle("Fraction of pixel clusters");
452 swap->GetYaxis()->SetTitleOffset(1.2);
453
454 double entries = 0.;
455 double mean = 0.;
456 double mean_err = 0.;
457 double rms = 0.;
458 double rms_err = 0.;
459 GetCoreParameters(swap,mean,mean_err,rms,rms_err,entries);
460
461 swap->DrawCopy(Options.c_str());
462
463 std::ostringstream LegendString;
464 LegendString.flags(std::ios::fixed);
465 LegendString.precision(2);
466 LegendString << name << " Position: - ";
467 LegendString << "Mean: " << mean << " #pm " << mean_err << " #mum - ";
468 LegendString << "RMS: " << rms << " #pm " << rms_err << " #mum";
469 DrawLegendLatex(LegendString.str().c_str(), marker, labelx, labely, color);
470
471}
472
473*/
475/*
476void PixelResidualHistograms::DrawProfile(int color, int marker, float labelx, float labely,
477 std::string name,std::string Options){
478
479 TProfile *swap = GetProfile( name + std::string("Profile") );
480 if(swap == 0) return;
481 swap->UseCurrentStyle();
482 swap->SetLineColor(color);
483 swap->SetMarkerColor(color);
484 swap->SetMarkerStyle(marker);
485 swap->GetXaxis()->SetTitle((m_binnames[0]).c_str());
486 swap->GetYaxis()->SetTitle("RMS of residuals (#mum)");
487 //swap->GetYaxis()->SetTitleOffset(1.2);
488 swap->DrawCopy(Options.c_str());
489 std:: string NameString = name + std::string(" Position");
490 DrawLegendLatex(NameString.c_str(), marker, labelx, labely, color);
491
492}
493
494*/
495} // end namespace
496
497#endif // #ifdef PixelResidualHistograms_C
void swap(DataVector< T > &a, DataVector< T > &b)
See DataVector<T, BASE>::swap().
void GetCoreParameters(const TH1 *hist, double &mean, double &mean_error, double &rms, double &rms_error, double &nentries)
void DrawLegendLatex(const char *chartitle, int markertype, float x, float y, int color=1, float textsize=0.034)
void DrawTitleLatex(const char *chartitle, float x, float y, int color=1, float textsize=0.04)
TProfile * GetRMSProfile(const std::string &binname)
void SetAxisTitle(const std::string &title)
TProfile * GetProfile(const int binnumber, bool RMS=false, bool savebins=false)
std::vector< TProfile * > * m_MeanProfilesVector
TProfile * GetMeanProfile(const std::string &binname)
void Fill(float residual, const std::vector< float > &parameters)
std::vector< TProfile * > * m_RMSProfilesVector
std::vector< std::vector< float > > m_binnage
std::vector< TCanvas * > * DrawProfiles(int color, int marker, float labely=0, std::vector< TCanvas * > *canvasvector=0, const std::string &name="")
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
double entries
Definition listroot.cxx:49