Skip to content

Commit f0b7134

Browse files
authored
Merge pull request #45378 from bsunanda/Run3-alca247D
Run3-alca247D Avoid potential memory leakage comments in the declaration of new histograms
2 parents 2567f8f + ca1d845 commit f0b7134

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

Calibration/HcalCalibAlgos/macros/CalibFitPlots.C

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ TFitResultPtr functionFit(TH1D* hist, double* fitrange, double* startvalues, dou
386386
delete ffitold;
387387

388388
int npar(6);
389+
TObject* ob = gROOT->FindObject(FunName);
390+
if (ob)
391+
ob->Delete();
389392
TF1* ffit = new TF1(FunName, doubleGauss, fitrange[0], fitrange[1], npar);
390393
ffit->SetParameters(startvalues);
391394
ffit->SetLineColor(kBlue);
@@ -416,6 +419,9 @@ std::pair<double, double> fitLanGau(TH1D* hist, bool debug) {
416419
if (ffitold)
417420
delete ffitold;
418421

422+
TObject* ob = gROOT->FindObject(FunName);
423+
if (ob)
424+
ob->Delete();
419425
TF1* ffit = new TF1(FunName, langaufun, LowEdge, HighEdge, 3);
420426
ffit->SetParameters(startvalues);
421427
ffit->SetParNames("MP", "Area", "GSigma");
@@ -437,6 +443,9 @@ results fitTwoGauss(TH1D* hist, bool debug) {
437443
if (LowEdge < 0.15)
438444
LowEdge = 0.15;
439445
std::string option = (hist->GetEntries() > 100) ? "QRS" : "QRWLS";
446+
TObject* ob = gROOT->FindObject("g1");
447+
if (ob)
448+
ob->Delete();
440449
TF1* g1 = new TF1("g1", "gaus", LowEdge, HighEdge);
441450
g1->SetLineColor(kGreen);
442451
TFitResultPtr Fit = hist->Fit(g1, option.c_str(), "");
@@ -504,6 +513,9 @@ results fitOneGauss(TH1D* hist, bool fitTwice, bool debug) {
504513
std::cout << hist->GetName() << " Mean " << mean << " RMS " << rms << " Range " << LowEdge << ":" << HighEdge
505514
<< "\n";
506515
std::string option = (hist->GetEntries() > 100) ? "QRS" : "QRWLS";
516+
TObject* ob = gROOT->FindObject("g1");
517+
if (ob)
518+
ob->Delete();
507519
TF1* g1 = new TF1("g1", "gaus", LowEdge, HighEdge);
508520
g1->SetLineColor(kGreen);
509521
TFitResultPtr Fit1 = hist->Fit(g1, option.c_str(), "");
@@ -2207,6 +2219,9 @@ void PlotHistCorrFactor(char* infile,
22072219
int fits(0);
22082220
for (int j = 0; j < maxdepth; ++j) {
22092221
sprintf(name, "hd%d", j + 1);
2222+
TObject* ob = gROOT->FindObject(name);
2223+
if (ob)
2224+
ob->Delete();
22102225
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
22112226
int nent(0);
22122227
for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
@@ -2224,6 +2239,9 @@ void PlotHistCorrFactor(char* infile,
22242239
fits++;
22252240
dy += 0.025;
22262241
sprintf(name, "hdf%d", j + 1);
2242+
TObject* ob = gROOT->FindObject(name);
2243+
if (ob)
2244+
ob->Delete();
22272245
TF1* func = new TF1(name, "pol0", etamin, etamax);
22282246
h->Fit(func, "+QWLR", "");
22292247
}
@@ -2338,6 +2356,9 @@ void PlotHistCorrFactor(char* infile,
23382356
double dy(0);
23392357
int fits(0);
23402358
sprintf(name, "hd%d", depth);
2359+
TObject* ob = gROOT->FindObject(name);
2360+
if (ob)
2361+
ob->Delete();
23412362
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
23422363
int nent(0);
23432364
for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
@@ -2355,6 +2376,9 @@ void PlotHistCorrFactor(char* infile,
23552376
fits++;
23562377
dy += 0.025;
23572378
sprintf(name, "hdf%d", depth);
2379+
TObject* ob = gROOT->FindObject(name);
2380+
if (ob)
2381+
ob->Delete();
23582382
TF1* func = new TF1(name, "pol0", etamin, etamax);
23592383
h->Fit(func, "+QWLR", "");
23602384
}
@@ -2458,6 +2482,9 @@ void PlotHistCorrAsymmetry(char* infile, std::string text, std::string prefixF =
24582482
double dy(0);
24592483
for (int j = 0; j < maxdepth; ++j) {
24602484
sprintf(name, "hd%d", j + 1);
2485+
TObject* ob = gROOT->FindObject(name);
2486+
if (ob)
2487+
ob->Delete();
24612488
TH1D* h = new TH1D(name, name, nbin, 0, etamax);
24622489
int nent(0);
24632490
for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
@@ -2614,6 +2641,9 @@ void PlotHistCorrFactors(char* infile1,
26142641
for (int ih = 1; ih < nfile; ++ih) {
26152642
for (int j = 0; j < maxdepth; ++j) {
26162643
sprintf(name, "h%dd%d", ih, j + 1);
2644+
TObject* ob = gROOT->FindObject(name);
2645+
if (ob)
2646+
ob->Delete();
26172647
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
26182648
double sumNum(0), sumDen(0);
26192649
std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin();
@@ -2663,6 +2693,9 @@ void PlotHistCorrFactors(char* infile1,
26632693
for (int k1 = 0; k1 < nfile; ++k1) {
26642694
for (int j = 0; j < maxdepth; ++j) {
26652695
sprintf(name, "h%dd%d", k1, j + 1);
2696+
TObject* ob = gROOT->FindObject(name);
2697+
if (ob)
2698+
ob->Delete();
26662699
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
26672700
int nent(0);
26682701
for (std::map<int, cfactors>::const_iterator itr = cfacs[k1].begin(); itr != cfacs[k1].end(); ++itr) {
@@ -2682,6 +2715,9 @@ void PlotHistCorrFactors(char* infile1,
26822715
if (drawStatBox)
26832716
dy += 0.025;
26842717
sprintf(name, "h%ddf%d", k1, j + 1);
2718+
TObject* ob = gROOT->FindObject(name);
2719+
if (ob)
2720+
ob->Delete();
26852721
TF1* func = new TF1(name, "pol0", etamin, etamax);
26862722
h->Fit(func, "+QWLR", "");
26872723
}
@@ -2833,6 +2869,9 @@ void PlotHistCorr2Factors(char* infile1,
28332869
if (ratio) {
28342870
for (int ih = 1; ih < nfile; ++ih) {
28352871
sprintf(name, "h%dd%d", ih, depth);
2872+
TObject* ob = gROOT->FindObject(name);
2873+
if (ob)
2874+
ob->Delete();
28362875
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
28372876
double sumNum(0), sumDen(0);
28382877
std::map<int, cfactors>::const_iterator ktr = cfacs[ih].begin();
@@ -2872,6 +2911,9 @@ void PlotHistCorr2Factors(char* infile1,
28722911
} else {
28732912
for (int k1 = 0; k1 < nfile; ++k1) {
28742913
sprintf(name, "h%dd%d", k1, depth);
2914+
TObject* ob = gROOT->FindObject(name);
2915+
if (ob)
2916+
ob->Delete();
28752917
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
28762918
int nent(0);
28772919
for (std::map<int, cfactors>::const_iterator itr = cfacs[k1].begin(); itr != cfacs[k1].end(); ++itr) {
@@ -2891,6 +2933,9 @@ void PlotHistCorr2Factors(char* infile1,
28912933
if (drawStatBox)
28922934
dy += 0.025;
28932935
sprintf(name, "h%ddf%d", k1, depth);
2936+
TObject* ob = gROOT->FindObject(name);
2937+
if (ob)
2938+
ob->Delete();
28942939
TF1* func = new TF1(name, "pol0", etamin, etamax);
28952940
h->Fit(func, "+QWLR", "");
28962941
}
@@ -3032,6 +3077,9 @@ void PlotHistCorrSys(std::string infilec, int conds, std::string text, int save
30323077
int nbin = etamax - etamin + 1;
30333078
for (int j = 0; j < maxdepth; ++j) {
30343079
sprintf(name, "hd%d", j + 1);
3080+
TObject* ob = gROOT->FindObject(name);
3081+
if (ob)
3082+
ob->Delete();
30353083
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
30363084
h->SetLineColor(colors[j]);
30373085
h->SetMarkerColor(colors[j]);
@@ -3117,6 +3165,9 @@ void PlotHistCorrLumis(std::string infilec, int conds, double lumi, int save = 0
31173165
int ih = (int)(hists.size());
31183166
for (int j = 0; j < maxdepth; ++j) {
31193167
sprintf(name, "hd%d%d", j + 1, i);
3168+
TObject* ob = gROOT->FindObject(name);
3169+
if (ob)
3170+
ob->Delete();
31203171
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
31213172
h->SetLineColor(colors[j]);
31223173
h->SetMarkerColor(colors[j]);
@@ -3228,6 +3279,9 @@ void PlotHistCorrRel(char* infile1,
32283279
for (int j = 0; j < maxdepth; ++j) {
32293280
int j1 = (i == 0) ? j : maxdepth + j;
32303281
sprintf(name, "hd%d%d", i, j + 1);
3282+
TObject* ob = gROOT->FindObject(name);
3283+
if (ob)
3284+
ob->Delete();
32313285
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
32323286
h->SetLineColor(colors[j1]);
32333287
h->SetMarkerColor(colors[j1]);
@@ -3363,6 +3417,9 @@ void PlotHistCorrDepth(char* infile1,
33633417
char name[100];
33643418
for (int j = 0; j < 2; ++j) {
33653419
sprintf(name, "hd%d", (j + 1));
3420+
TObject* ob = gROOT->FindObject(name);
3421+
if (ob)
3422+
ob->Delete();
33663423
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
33673424
if (j == 0) {
33683425
for (std::map<int, cfactors>::const_iterator itr = cfacs1.begin(); itr != cfacs1.end(); ++itr) {
@@ -4197,6 +4254,9 @@ void PlotDepthCorrFactor(char* infile,
41974254
int fits(0);
41984255
for (int j = 0; j < maxdepth; ++j) {
41994256
sprintf(name, "hd%d", j + 1);
4257+
TObject* ob = gROOT->FindObject(name);
4258+
if (ob)
4259+
ob->Delete();
42004260
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
42014261
int nent(0);
42024262
for (std::map<int, cfactors>::const_iterator itr = cfacs.begin(); itr != cfacs.end(); ++itr) {
@@ -4339,6 +4399,9 @@ void PlotPhiSymmetryResults(
43394399
for (int k = 0; k < maxDepthHB; ++k) {
43404400
sprintf(name, "HB%d", k);
43414401
sprintf(title, "Correction factor for depth %d of HB", k);
4402+
TObject* ob = gROOT->FindObject(name);
4403+
if (ob)
4404+
ob->Delete();
43424405
TH1D* h = new TH1D(name, title, nbin, cfacMin, cfacMax);
43434406
histHB.push_back(h);
43444407
if (debug)
@@ -4348,6 +4411,9 @@ void PlotPhiSymmetryResults(
43484411
for (int k = 0; k < maxDepthHE; ++k) {
43494412
sprintf(name, "HE%d", k);
43504413
sprintf(title, "Correction factor for depth %d of HE", k);
4414+
TObject* ob = gROOT->FindObject(name);
4415+
if (ob)
4416+
ob->Delete();
43514417
TH1D* h = new TH1D(name, title, nbin, cfacMin, cfacMax);
43524418
histHE.push_back(h);
43534419
if (debug)
@@ -4494,6 +4560,9 @@ void PlotHistCorrRatio(char* infile1,
44944560
char name[100];
44954561
for (int ih = 0; ih < nfile; ++ih) {
44964562
sprintf(name, "h%d", ih);
4563+
TObject* ob = gROOT->FindObject(name);
4564+
if (ob)
4565+
ob->Delete();
44974566
TH1D* h = new TH1D(name, name, nbin, etamin, etamax);
44984567
double sumNum(0), sumDen(0);
44994568
int npt(0);
@@ -4534,6 +4603,9 @@ void PlotHistCorrRatio(char* infile1,
45344603
h->GetYaxis()->SetTitleOffset(1.20);
45354604
h->GetYaxis()->SetRangeUser(0.0, 3.0);
45364605
if (doFit) {
4606+
TObject* ob = gROOT->FindObject(name);
4607+
if (ob)
4608+
ob->Delete();
45374609
TF1* func = new TF1(name, "pol0", etamin, etamax);
45384610
func->SetLineColor(colors[ih]);
45394611
func->SetLineStyle(styles[ih]);

0 commit comments

Comments
 (0)