Skip to content

Commit e547ea7

Browse files
committed
cppcoreguidelines-pro-type-reinterpret-cast
1 parent 211aa2b commit e547ea7

1 file changed

Lines changed: 102 additions & 49 deletions

File tree

PWGDQ/Core/HistogramManager.cxx

Lines changed: 102 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
139139
// TODO: replace the cout warning messages with LOG (same for all the other functions)
140140

141141
// get the list to which the histogram should be added
142-
auto* hList = reinterpret_cast<TList*>(fMainList->FindObject(histClass));
142+
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(histClass));
143143
if (!hList) {
144144
LOG(warn) << "HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!";
145145
LOG(warn) << " Histogram not created";
@@ -229,7 +229,9 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
229229
// if requested, build the profile using the profile widths instead of stat errors
230230
// TODO: make this option more transparent to the user ?
231231
if (titleStr.Contains("--s--")) {
232-
(reinterpret_cast<TProfile*>(h))->BuildOptions(0., 0., "s");
232+
if (auto* profile = dynamic_cast<TProfile*>(h)) {
233+
profile->BuildOptions(0., 0., "s");
234+
}
233235
}
234236
} else {
235237
if (!isdouble) {
@@ -276,14 +278,18 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
276278
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
277279
h->Sumw2();
278280
if (titleStr.Contains("--s--")) {
279-
(reinterpret_cast<TProfile3D*>(h))->BuildOptions(0., 0., "s");
281+
if (auto* profile = dynamic_cast<TProfile3D*>(h)) {
282+
profile->BuildOptions(0., 0., "s");
283+
}
280284
}
281285
} else { // TProfile2D
282286
h = new TProfile2D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xmin, xmax, nYbins, ymin, ymax);
283287
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
284288
h->Sumw2();
285289
if (titleStr.Contains("--s--")) {
286-
(reinterpret_cast<TProfile2D*>(h))->BuildOptions(0., 0., "s");
290+
if (auto* profile = dynamic_cast<TProfile2D*>(h)) {
291+
profile->BuildOptions(0., 0., "s");
292+
}
287293
}
288294
}
289295
} else { // TH3F
@@ -348,7 +354,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
348354
//
349355

350356
// get the list to which the histogram should be added
351-
auto* hList = reinterpret_cast<TList*>(fMainList->FindObject(histClass));
357+
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(histClass));
352358
if (!hList) {
353359
LOG(warn) << "HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!";
354360
LOG(warn) << " Histogram not created";
@@ -435,7 +441,9 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
435441
fBinsAllocated += nXbins + 2;
436442
h->Sumw2();
437443
if (titleStr.Contains("--s--")) {
438-
(reinterpret_cast<TProfile*>(h))->BuildOptions(0., 0., "s");
444+
if (auto* profile = dynamic_cast<TProfile*>(h)) {
445+
profile->BuildOptions(0., 0., "s");
446+
}
439447
}
440448
} else {
441449
if (!isdouble) {
@@ -482,14 +490,18 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
482490
fBinsAllocated += (nXbins + 2) * (nYbins + 2) * (nZbins + 2);
483491
h->Sumw2();
484492
if (titleStr.Contains("--s--")) {
485-
(reinterpret_cast<TProfile3D*>(h))->BuildOptions(0., 0., "s");
493+
if (auto* profile = dynamic_cast<TProfile3D*>(h)) {
494+
profile->BuildOptions(0., 0., "s");
495+
}
486496
}
487497
} else {
488498
h = new TProfile2D(hname, (arr->At(0) ? arr->At(0)->GetName() : ""), nXbins, xbins, nYbins, ybins);
489499
fBinsAllocated += (nXbins + 2) * (nYbins + 2);
490500
h->Sumw2();
491501
if (titleStr.Contains("--s--")) {
492-
(reinterpret_cast<TProfile2D*>(h))->BuildOptions(0., 0., "s");
502+
if (auto* profile = dynamic_cast<TProfile2D*>(h)) {
503+
profile->BuildOptions(0., 0., "s");
504+
}
493505
}
494506
}
495507
} else {
@@ -551,7 +563,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
551563
//
552564

553565
// get the list to which the histogram should be added
554-
auto* hList = reinterpret_cast<TList*>(fMainList->FindObject(histClass));
566+
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(histClass));
555567
if (!hList) {
556568
LOG(warn) << "HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!";
557569
LOG(warn) << " Histogram not created";
@@ -626,15 +638,19 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
626638
}
627639
if (!isdouble) {
628640
if (useSparse) {
629-
hList->Add(reinterpret_cast<THnSparseF*>(h));
630-
} else {
631-
hList->Add(reinterpret_cast<THnF*>(h));
641+
if (auto* hn = dynamic_cast<THnSparseF*>(h)) {
642+
hList->Add(hn);
643+
}
644+
} else if (auto* hn = dynamic_cast<THnF*>(h)) {
645+
hList->Add(hn);
632646
}
633647
} else {
634648
if (useSparse) {
635-
hList->Add(reinterpret_cast<THnSparseD*>(h));
636-
} else {
637-
hList->Add(reinterpret_cast<THnD*>(h));
649+
if (auto* hn = dynamic_cast<THnSparseD*>(h)) {
650+
hList->Add(hn);
651+
}
652+
} else if (auto* hn = dynamic_cast<THnD*>(h)) {
653+
hList->Add(hn);
638654
}
639655
}
640656

@@ -651,7 +667,7 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
651667
//
652668

653669
// get the list to which the histogram should be added
654-
auto* hList = reinterpret_cast<TList*>(fMainList->FindObject(histClass));
670+
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(histClass));
655671
if (!hList) {
656672
LOG(warn) << "HistogramManager::AddHistogram(): Histogram list " << histClass << " not found!";
657673
LOG(warn) << " Histogram not created";
@@ -733,15 +749,19 @@ void HistogramManager::AddHistogram(const char* histClass, const char* hname, co
733749
}
734750
if (!isdouble) {
735751
if (useSparse) {
736-
hList->Add(reinterpret_cast<THnSparseF*>(h));
737-
} else {
738-
hList->Add(reinterpret_cast<THnF*>(h));
752+
if (auto* hn = dynamic_cast<THnSparseF*>(h)) {
753+
hList->Add(hn);
754+
}
755+
} else if (auto* hn = dynamic_cast<THnF*>(h)) {
756+
hList->Add(hn);
739757
}
740758
} else {
741759
if (useSparse) {
742-
hList->Add(reinterpret_cast<THnSparseD*>(h));
743-
} else {
744-
hList->Add(reinterpret_cast<THnD*>(h));
760+
if (auto* hn = dynamic_cast<THnSparseD*>(h)) {
761+
hList->Add(hn);
762+
}
763+
} else if (auto* hn = dynamic_cast<THnD*>(h)) {
764+
hList->Add(hn);
745765
}
746766
}
747767
fBinsAllocated += bins;
@@ -755,7 +775,7 @@ void HistogramManager::FillHistClass(const char* className, Float_t* values)
755775
//
756776

757777
// get the needed histogram list
758-
auto* hList = reinterpret_cast<TList*>(fMainList->FindObject(className));
778+
auto* hList = dynamic_cast<TList*>(fMainList->FindObject(className));
759779
if (!hList) {
760780
// TODO: add some meaningfull error message
761781
/*LOG(warn) << "HistogramManager::FillHistClass(): Histogram list " << className << " not found!";
@@ -788,8 +808,10 @@ void HistogramManager::FillHistClass(const char* className, Float_t* values)
788808
isTHn = ((*varIter)[1] > 0);
789809
if (isTHn) {
790810
dimension = (*varIter)[1];
811+
} else if (auto* h1 = dynamic_cast<TH1*>(h)) {
812+
dimension = h1->GetDimension();
791813
} else {
792-
dimension = (reinterpret_cast<TH1*>(h))->GetDimension();
814+
continue;
793815
}
794816

795817
// get the various variable indices
@@ -810,70 +832,94 @@ void HistogramManager::FillHistClass(const char* className, Float_t* values)
810832
switch (dimension) {
811833
case 1:
812834
if (isProfile) {
835+
auto* profile = dynamic_cast<TProfile*>(h);
836+
if (!profile) {
837+
break;
838+
}
813839
if (varW > kNothing) {
814840
if (isFillLabelx) {
815-
(reinterpret_cast<TProfile*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], values[varW]);
841+
profile->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], values[varW]);
816842
} else {
817-
(reinterpret_cast<TProfile*>(h))->Fill(values[varX], values[varY], values[varW]);
843+
profile->Fill(values[varX], values[varY], values[varW]);
818844
}
819845
} else {
820846
if (isFillLabelx) {
821-
(reinterpret_cast<TProfile*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), values[varY]);
847+
profile->Fill(Form("%d", static_cast<int>(values[varX])), values[varY]);
822848
} else {
823-
(reinterpret_cast<TProfile*>(h))->Fill(values[varX], values[varY]);
849+
profile->Fill(values[varX], values[varY]);
824850
}
825851
}
826852
} else {
853+
auto* h1 = dynamic_cast<TH1*>(h);
854+
if (!h1) {
855+
break;
856+
}
827857
if (varW > kNothing) {
828858
if (isFillLabelx) {
829-
(reinterpret_cast<TH1*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), values[varW]);
859+
h1->Fill(Form("%d", static_cast<int>(values[varX])), values[varW]);
830860
} else {
831-
(reinterpret_cast<TH1*>(h))->Fill(values[varX], values[varW]);
861+
h1->Fill(values[varX], values[varW]);
832862
}
833863
} else {
834864
if (isFillLabelx) {
835-
(reinterpret_cast<TH1*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), 1.);
865+
h1->Fill(Form("%d", static_cast<int>(values[varX])), 1.);
836866
} else {
837-
(reinterpret_cast<TH1*>(h))->Fill(values[varX]);
867+
h1->Fill(values[varX]);
838868
}
839869
}
840870
}
841871
break;
842872
case 2:
843873
if (isProfile) {
874+
auto* profile = dynamic_cast<TProfile2D*>(h);
875+
if (!profile) {
876+
break;
877+
}
844878
if (varW > kNothing) {
845-
(reinterpret_cast<TProfile2D*>(h))->Fill(values[varX], values[varY], values[varZ], values[varW]);
879+
profile->Fill(values[varX], values[varY], values[varZ], values[varW]);
846880
} else {
847-
(reinterpret_cast<TProfile2D*>(h))->Fill(values[varX], values[varY], values[varZ]);
881+
profile->Fill(values[varX], values[varY], values[varZ]);
848882
}
849883
} else {
884+
auto* h2 = dynamic_cast<TH2*>(h);
885+
if (!h2) {
886+
break;
887+
}
850888
if (varW > kNothing) {
851889
if (isFillLabelx) {
852-
(reinterpret_cast<TH2*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], values[varW]);
890+
h2->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], values[varW]);
853891
} else {
854-
(reinterpret_cast<TH2*>(h))->Fill(values[varX], values[varY], values[varW]);
892+
h2->Fill(values[varX], values[varY], values[varW]);
855893
}
856894
} else {
857895
if (isFillLabelx) {
858-
(reinterpret_cast<TH2*>(h))->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], 1.);
896+
h2->Fill(Form("%d", static_cast<int>(values[varX])), values[varY], 1.);
859897
} else {
860-
(reinterpret_cast<TH2*>(h))->Fill(values[varX], values[varY]);
898+
h2->Fill(values[varX], values[varY]);
861899
}
862900
}
863901
}
864902
break;
865903
case 3:
866904
if (isProfile) {
905+
auto* profile = dynamic_cast<TProfile3D*>(h);
906+
if (!profile) {
907+
break;
908+
}
867909
if (varW > kNothing) {
868-
(reinterpret_cast<TProfile3D*>(h))->Fill(values[varX], values[varY], values[varZ], values[varT], values[varW]);
910+
profile->Fill(values[varX], values[varY], values[varZ], values[varT], values[varW]);
869911
} else {
870-
(reinterpret_cast<TProfile3D*>(h))->Fill(values[varX], values[varY], values[varZ], values[varT]);
912+
profile->Fill(values[varX], values[varY], values[varZ], values[varT]);
871913
}
872914
} else {
915+
auto* h3 = dynamic_cast<TH3*>(h);
916+
if (!h3) {
917+
break;
918+
}
873919
if (varW > kNothing) {
874-
(reinterpret_cast<TH3*>(h))->Fill(values[varX], values[varY], values[varZ], values[varW]);
920+
h3->Fill(values[varX], values[varY], values[varZ], values[varW]);
875921
} else {
876-
(reinterpret_cast<TH3*>(h))->Fill(values[varX], values[varY], values[varZ]);
922+
h3->Fill(values[varX], values[varY], values[varZ]);
877923
}
878924
}
879925
break;
@@ -885,15 +931,19 @@ void HistogramManager::FillHistClass(const char* className, Float_t* values)
885931
} else {
886932
if (varW > kNothing) {
887933
if (isSparse) {
888-
(reinterpret_cast<THnSparse*>(h))->Fill(fillValues, values[varW]);
889-
} else {
890-
(reinterpret_cast<THn*>(h))->Fill(fillValues, values[varW]);
934+
if (auto* hn = dynamic_cast<THnSparse*>(h)) {
935+
hn->Fill(fillValues, values[varW]);
936+
}
937+
} else if (auto* hn = dynamic_cast<THn*>(h)) {
938+
hn->Fill(fillValues, values[varW]);
891939
}
892940
} else {
893941
if (isSparse) {
894-
(reinterpret_cast<THnSparse*>(h))->Fill(fillValues);
895-
} else {
896-
(reinterpret_cast<THn*>(h))->Fill(fillValues);
942+
if (auto* hn = dynamic_cast<THnSparse*>(h)) {
943+
hn->Fill(fillValues);
944+
}
945+
} else if (auto* hn = dynamic_cast<THn*>(h)) {
946+
hn->Fill(fillValues);
897947
}
898948
}
899949
} // end else
@@ -925,7 +975,10 @@ void HistogramManager::Print(Option_t*) const
925975
cout << "###################################################################" << endl;
926976
cout << "HistogramManager:: " << fMainList->GetName() << endl;
927977
for (int i = 0; i < fMainList->GetEntries(); ++i) {
928-
auto* list = reinterpret_cast<TList*>(fMainList->At(i));
978+
auto* list = dynamic_cast<TList*>(fMainList->At(i));
979+
if (!list) {
980+
continue;
981+
}
929982
cout << "************** List " << list->GetName() << endl;
930983
for (int j = 0; j < list->GetEntries(); ++j) {
931984
TObject* obj = list->At(j);

0 commit comments

Comments
 (0)