Skip to content

Commit 3174f79

Browse files
authored
Improve C++ linting (#443)
1 parent db61f84 commit 3174f79

File tree

18 files changed

+276
-141
lines changed

18 files changed

+276
-141
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ UseTab: Never
5454
# Do not format protobuf files
5555
Language: Proto
5656
DisableFormat: true
57+
# ---
58+
# # Since clang-format 13.0.0
59+
# Language: Json
60+
# # O2 dumps JSON files with 4-space indents.
61+
# IndentWidth: 4
Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,70 @@
11
---
22
# https://github.com/marketplace/actions/clang-format-lint
3-
name: Clang Format Linter
3+
name: ClangFormat
44
on: [push, pull_request]
5-
permissions: {}
5+
permissions:
6+
# Give the default GITHUB_TOKEN write permission to commit and push, comment issues & post new PR
7+
# Remove the ones you do not need
8+
contents: write
9+
issues: write
10+
pull-requests: write
11+
concurrency:
12+
group: ${{ github.ref }}-${{ github.workflow }}
13+
cancel-in-progress: true
614
jobs:
7-
build:
8-
name: Clang Format Linter
15+
clang-format:
16+
name: Clang Format
917
runs-on: ubuntu-latest
1018
steps:
1119
- name: Checkout Code
1220
uses: actions/checkout@v3
21+
1322
- name: Clang Format
14-
uses: DoozyX/[email protected]
23+
id: clang
24+
uses: DoozyX/[email protected]
1525
with:
1626
#source: '.'
1727
#exclude: './third_party ./external'
18-
#extensions: 'h,cpp,cxx,C'
19-
clangFormatVersion: 11
28+
extensions: 'C,c,c++,cc,cl,cpp,cu,cuh,cxx,cxx.in,h,h++,hh,h.in,hpp,hxx,inc,inl,macro'
29+
clangFormatVersion: 16
2030
style: file
31+
inplace: true
32+
33+
# Set a results status = 0 if no changes, = 1 if formatted
34+
- name: Set result status
35+
id: clang-result
36+
run: |
37+
status=0
38+
if ! git diff --quiet; then
39+
status=1
40+
fi
41+
echo "status=$status" >> "$GITHUB_OUTPUT"
42+
43+
# Create pull request if applicable (for now works only on PR from same repository, not from forks)
44+
- name: Print PR condition
45+
run: |
46+
# Print the condition
47+
echo "${{ github.event_name }} == 'push'"
48+
- name: Create Pull Request with applied fixes
49+
id: cpr
50+
if: github.event_name == 'push'
51+
uses: peter-evans/create-pull-request@v5
52+
with:
53+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
54+
commit-message: "[Clang format] Apply linters automatic fixes"
55+
title: "[Clang format] Apply linters automatic fixes"
56+
body: "Please merge this pull request to apply automatic fixes by Clang format."
57+
labels: bot
58+
branch: patch-${{ github.workflow }}-${{ github.ref_name }}
59+
delete-branch: true
60+
- name: Create PR output
61+
if: steps.clang-result.outputs.status == 1
62+
run: |
63+
echo "::error::Files need formatting."
64+
if [ ${{ github.event_name }} == 'push' ]; then
65+
echo "::error::Merge pull request ${{ steps.cpr.outputs.pull-request-url }} to apply automatic fixes."
66+
elif [ ${{ github.event_name }} == 'pull_request' ]; then
67+
echo "::error::Check ${{ github.event.pull_request.head.repo.html_url }}/pulls to apply automatic fixes."
68+
echo "::notice::Actions must be allowed in your repository. See ${{ github.event.pull_request.head.repo.html_url }}/settings/actions"
69+
fi
70+
exit 1

.github/workflows/mega-linter.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
steps:
3434
# Git Checkout
3535
- name: Checkout Code
36-
uses: actions/checkout@v3
36+
uses: actions/checkout@v4
3737
with:
3838
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
3939
fetch-depth: 0 # If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to improve performances
@@ -54,8 +54,8 @@ jobs:
5454

5555
# Upload MegaLinter artifacts
5656
- name: Archive production artifacts
57-
if: ${{ success() }} || ${{ failure() }}
58-
uses: actions/upload-artifact@v3
57+
if: success() || failure()
58+
uses: actions/upload-artifact@v4
5959
with:
6060
name: MegaLinter reports
6161
path: |
@@ -66,10 +66,10 @@ jobs:
6666
- name: Print PR condition
6767
run: |
6868
# Print the condition
69-
echo "${{ steps.ml.outputs.has_updated_sources }} == 1 && (${{ env.APPLY_FIXES_EVENT }} == 'all' || ${{ env.APPLY_FIXES_EVENT }} == ${{ github.event_name }}) && ${{ env.APPLY_FIXES_MODE }} == 'pull_request' && (${{ github.event_name }} == 'push' || ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.repository }})"
69+
echo "(${{ env.APPLY_FIXES_EVENT }} == 'all' || ${{ env.APPLY_FIXES_EVENT }} == ${{ github.event_name }}) && ${{ env.APPLY_FIXES_MODE }} == 'pull_request' && (${{ github.event_name }} == 'push' || ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.repository }})"
7070
- name: Create Pull Request with applied fixes
7171
id: cpr
72-
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository)
72+
if: (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository)
7373
uses: peter-evans/create-pull-request@v5
7474
with:
7575
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
@@ -80,9 +80,15 @@ jobs:
8080
branch: patch-${{ github.workflow }}-${{ github.ref_name }}
8181
delete-branch: true
8282
- name: Create PR output
83-
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository)
83+
if: steps.ml.outputs.has_updated_sources == 1
8484
run: |
85-
echo "::error::Merge pull request ${{ steps.cpr.outputs.pull-request-url }} to apply automatic fixes."
85+
echo "::error::MegaLinter has fixed some files."
86+
if [ ${{ github.event_name }} == 'push' ]; then
87+
echo "::error::Merge pull request ${{ steps.cpr.outputs.pull-request-url }} to apply automatic fixes."
88+
elif [ ${{ github.event_name }} == 'pull_request' ]; then
89+
echo "::error::Check ${{ github.event.pull_request.head.repo.html_url }}/pulls to apply automatic fixes."
90+
echo "::notice::Actions must be allowed in your repository. See ${{ github.event.pull_request.head.repo.html_url }}/settings/actions"
91+
fi
8692
exit 1
8793
8894
# Push new commit if applicable (for now works only on PR from same repository, not from forks)

.mega-linter.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ FLAVOR_SUGGESTIONS: false # Don't show suggestions about different MegaLinter fl
3232
PYTHON_ISORT_CONFIG_FILE: pyproject.toml
3333
PYTHON_PYRIGHT_CONFIG_FILE: pyproject.toml
3434
PYTHON_RUFF_CONFIG_FILE: pyproject.toml
35-
FILTER_REGEX_EXCLUDE: (codeQA/)
35+
CPP_CPPLINT_FILE_EXTENSIONS: [".C", ".c", ".c++", ".cc", ".cl", ".cpp", ".cu", ".cuh", ".cxx", ".cxx.in", ".h", ".h++", ".hh", ".h.in", ".hpp", ".hxx", ".inc", ".inl", ".macro"]
36+
CPP_CLANG_FORMAT_FILE_EXTENSIONS: [".C", ".c", ".c++", ".cc", ".cl", ".cpp", ".cu", ".cuh", ".cxx", ".cxx.in", ".h", ".h++", ".hh", ".h.in", ".hpp", ".hxx", ".inc", ".inl", ".macro"]
37+
FILTER_REGEX_EXCLUDE: (codeQA/|Upgrade/)

CPPLINT.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
linelength=160
2-
filter=-whitespace/braces,-whitespace/comments,-build/namespaces,-runtime/references
1+
filter=-build/c++11,-build/namespaces,-readability/fn_size,-readability/todo,-runtime/references,-whitespace/blank_line,-whitespace/braces,-whitespace/comments,-whitespace/line_length,-whitespace/semicolon,-whitespace/todo

FirstAnalysis/Correlations/doPhiProjections.C

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
112
//////////////////////////////////////////////////////////////
213
// Macro to obtain projections of two-particle correlations for flow studies
314
//
@@ -50,7 +61,7 @@ void doPhiProjections(
5061
static Double_t Nch[] = {0, 10, 20, 30, 40, 50, 60, 80, 100, 200};
5162
// Nbins is the number of multiplicity bins
5263
static const uint Nbins = 9;
53-
//static const uint Nbins = sizeof(Nch) / sizeof(Nch[0]);
64+
// static const uint Nbins = sizeof(Nch) / sizeof(Nch[0]);
5465
const double absDeltaPhi = 1.3; // y-projection range (it is only needed for jet yield calculation from delta eta projection)
5566

5667
TFile* infile = new TFile(inFileName, "read");
@@ -64,7 +75,7 @@ void doPhiProjections(
6475
// do reference flow
6576

6677
// 2D histogram of two-particle correlation: same/mixed event ratio (normalised as it should be: Ntrig, B(0,0))
67-
TH2D* hdphidetaRidge_ref = (TH2D*)infile->Get(Form("dphi_ref_%u", imult));
78+
TH2D* hdphidetaRidge_ref = reinterpret_cast<TH2D*>(infile->Get(Form("dphi_ref_%u", imult)));
6879
if (!hdphidetaRidge_ref) {
6980
printf("No histograms corresponding mult bin %u \n", imult);
7081
continue;
@@ -85,7 +96,7 @@ void doPhiProjections(
8596
hdphiRidgeN_ref->Write();
8697

8798
// add the projections positive + negative
88-
TH1D* hdphiRidge_ref = (TH1D*)hdphiRidgeP_ref->Clone(Form("proj_dphi_ref_%u", imult));
99+
TH1D* hdphiRidge_ref = reinterpret_cast<TH1D*>(hdphiRidgeP_ref->Clone(Form("proj_dphi_ref_%u", imult)));
89100
hdphiRidge_ref->Add(hdphiRidgeP_ref, hdphiRidgeN_ref, 0.5, 0.5);
90101

91102
outfile->cd();
@@ -127,14 +138,14 @@ void doPhiProjections(
127138
for (uint iassoc = 0; iassoc < assocCount; ++iassoc) {
128139

129140
// 2D histogram of two-particle correlation: same/mixed event ratio (normalised as it should be: Ntrig, B(0,0))
130-
TH2D* hdphidetaRidge = (TH2D*)infile->Get(Form("dphi_%u_%u_%u", itrig, iassoc, imult));
141+
TH2D* hdphidetaRidge = reinterpret_cast<TH2D*>(infile->Get(Form("dphi_%u_%u_%u", itrig, iassoc, imult)));
131142
if (!hdphidetaRidge) {
132143
printf("No histograms corresponding mult bin %u. (itrig=%u, iassoc=%u)\n", imult, itrig, iassoc);
133144
continue;
134145
} // if histogram not existing
135146

136147
// Clone hdphidetaJet: hdphidetaRidge will be used for phi projection; hdphidetaJet for eta projection
137-
TH2D* hdphidetaJet = (TH2D*)hdphidetaRidge->Clone("hdphidetaJet");
148+
TH2D* hdphidetaJet = reinterpret_cast<TH2D*>(hdphidetaRidge->Clone("hdphidetaJet"));
138149

139150
// Normalise hdphidetaRidge used for delta phi projection with the width of the long-range region
140151
double norm = 2.0 * (absDeltaEtaMax - absDeltaEtaMin);
@@ -188,7 +199,7 @@ void doPhiProjections(
188199
hdphiRidgeN->Write();
189200

190201
// add the projections positive + negative
191-
TH1D* hdphiRidge = (TH1D*)hdphiRidgeP->Clone(Form("proj_dphi_%u_%u_%u", itrig, iassoc, imult));
202+
TH1D* hdphiRidge = reinterpret_cast<TH1D*>(hdphiRidgeP->Clone(Form("proj_dphi_%u_%u_%u", itrig, iassoc, imult)));
192203
hdphiRidge->Add(hdphiRidgeP, hdphiRidgeN, 0.5, 0.5);
193204

194205
outfile->cd();

FirstAnalysis/Correlations/doTemplate.C

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
112
///////////////////////////////////////////////////////////////////////////
213
// Macro to perform a template fit on 1D histograms of projected correlations on delta phi
314
//
@@ -65,8 +76,8 @@ void doTemplate(
6576
// get the histograms projected to delta phi,
6677
// we need to distinguish histogram with desired (high) multiplicity
6778
// from a histogram with low multiplicity used as the peripheral baseline
68-
hminuit = (TH1D*)inFile->Get(Form("proj_dphi_ref_%d", iMult))->Clone("hminuit");
69-
hminuit_periph = (TH1D*)inFile->Get("proj_dphi_ref_0")->Clone("hminuit_periph");
79+
hminuit = reinterpret_cast<TH1D*>(inFile->Get(Form("proj_dphi_ref_%d", iMult))->Clone("hminuit"));
80+
hminuit_periph = reinterpret_cast<TH1D*>(inFile->Get("proj_dphi_ref_0")->Clone("hminuit_periph"));
7081

7182
// do the template fit
7283
double par[4], parerr[4];
@@ -82,10 +93,10 @@ void doTemplate(
8293
// F*Y_peripheral(deltaphi) + Y_ridge = template fit
8394
TF1* fTemplate = new TF1("fTemplate", templateFitFunction, -0.5 * TMath::Pi() + 1e-6, 1.5 * TMath::Pi() - 1e-6, 4);
8495
fTemplate->SetParameters(par); // set the parameters obtained from template fit above using tempMinuit()
85-
//fTemplate->SetLineStyle(kSolid);
86-
//fTemplate->SetLineColor(kBlue+1);
96+
// fTemplate->SetLineStyle(kSolid);
97+
// fTemplate->SetLineColor(kBlue+1);
8798

88-
TH1F* hTemplate = (TH1F*)hminuit->Clone();
99+
TH1F* hTemplate = reinterpret_cast<TH1F*>(hminuit->Clone());
89100
hTemplate->Reset();
90101
for (int iBin = 1; iBin < hTemplate->GetNbinsX() + 1; iBin++) {
91102
hTemplate->SetBinContent(iBin, fTemplate->Eval(hTemplate->GetBinCenter(iBin)));
@@ -131,7 +142,7 @@ void doTemplate(
131142
fPeripheral->SetLineWidth(3);
132143
fPeripheral->SetLineColor(kGreen + 2);
133144

134-
TH1F* hPeripheral = (TH1F*)hminuit->Clone();
145+
TH1F* hPeripheral = reinterpret_cast<TH1F*>(hminuit->Clone());
135146
hPeripheral->Reset();
136147
for (int iBin = 1; iBin < hPeripheral->GetNbinsX() + 1; iBin++) {
137148
hPeripheral->SetBinContent(iBin, fPeripheral->Eval(hPeripheral->GetBinCenter(iBin)));
@@ -184,7 +195,7 @@ void doTemplate(
184195
latex->DrawLatex(0.3, 0.93, "pp #sqrt{s} = 13 TeV");
185196
latex->DrawLatex(0.3, 0.86, Form("%.1f < p_{T, trig, assoc} < %.1f", binspTref[0], binspTref[1]));
186197
latex->DrawLatex(0.3, 0.79, Form("%.1f < N_{ch} < %.1f", binsMult[iMult], binsMult[iMult + 1]));
187-
//latex->DrawLatex(0.3,0.72,Form("%.1f < #Delta#eta < %.1f",etaMin,etaMax));
198+
// latex->DrawLatex(0.3,0.72,Form("%.1f < #Delta#eta < %.1f",etaMin,etaMax));
188199

189200
if (savePlots)
190201
cTemplate->SaveAs(Form("%s/template_ref_%d.png", outputPlotsName, iMult));
@@ -199,8 +210,8 @@ void doTemplate(
199210
// get the histograms projected to delta phi,
200211
// we need to distinguish histogram with desired (high) multiplicity
201212
// from a histogram with low multiplicity used as the peripheral baseline
202-
hminuit = (TH1D*)inFile->Get(Form("proj_dphi_%d_0_%d", ipTtrig, iMult))->Clone("hminuit");
203-
hminuit_periph = (TH1D*)inFile->Get(Form("proj_dphi_%d_0_0", ipTtrig))->Clone("hminuit_periph");
213+
hminuit = reinterpret_cast<TH1D*>(inFile->Get(Form("proj_dphi_%d_0_%d", ipTtrig, iMult))->Clone("hminuit"));
214+
hminuit_periph = reinterpret_cast<TH1D*>(inFile->Get(Form("proj_dphi_%d_0_0", ipTtrig))->Clone("hminuit_periph"));
204215

205216
// do the template fit
206217
double par[4], parerr[4];
@@ -219,7 +230,7 @@ void doTemplate(
219230
fTemplate->SetLineStyle(kSolid);
220231
fTemplate->SetLineColor(kRed);
221232

222-
TH1F* hTemplate = (TH1F*)hminuit->Clone();
233+
TH1F* hTemplate = reinterpret_cast<TH1F*>(hminuit->Clone());
223234
hTemplate->Reset();
224235
for (int iBin = 1; iBin < hTemplate->GetNbinsX() + 1; iBin++) {
225236
hTemplate->SetBinContent(iBin, fTemplate->Eval(hTemplate->GetBinCenter(iBin)));
@@ -247,7 +258,7 @@ void doTemplate(
247258
fPeripheral->SetLineStyle(kSolid);
248259
fPeripheral->SetLineColor(kMagenta);
249260

250-
TH1F* hPeripheral = (TH1F*)hminuit->Clone();
261+
TH1F* hPeripheral = reinterpret_cast<TH1F*>(hminuit->Clone());
251262
hPeripheral->Reset();
252263
for (int iBin = 1; iBin < hPeripheral->GetNbinsX() + 1; iBin++) {
253264
hPeripheral->SetBinContent(iBin, fPeripheral->Eval(hPeripheral->GetBinCenter(iBin)));
@@ -297,7 +308,7 @@ void doTemplate(
297308
latex->DrawLatex(0.3, 0.93, "pp #sqrt{s} = 13 TeV");
298309
latex->DrawLatex(0.3, 0.86, Form("%.1f < p_{T, trig} < %.1f", binspTtrig[ipTtrig], binspTtrig[ipTtrig + 1]));
299310
latex->DrawLatex(0.3, 0.79, Form("%.1f < N_{ch} < %.1f", binsMult[iMult], binsMult[iMult + 1]));
300-
//latex->DrawLatex(0.3,0.72,Form("%.1f < #Delta#eta < %.1f",etaMin,etaMax));
311+
// latex->DrawLatex(0.3,0.72,Form("%.1f < #Delta#eta < %.1f",etaMin,etaMax));
301312

302313
if (savePlots)
303314
cTemplate->SaveAs(Form("%s/template_%d_%d.png", outputPlotsName, ipTtrig, iMult));
@@ -340,8 +351,8 @@ double templateFitFunction(double* x, double* par)
340351
///////////////////////////////////////////////////////////////////////////
341352
void minuitFunction(int& npar, double* gin, double& ff, double* par, int iflag)
342353
{
343-
TH1D* h = (TH1D*)hminuit->Clone("h");
344-
TH1D* hperi = (TH1D*)hminuit_periph->Clone("hperi");
354+
TH1D* h = reinterpret_cast<TH1D*>(hminuit->Clone("h"));
355+
TH1D* hperi = reinterpret_cast<TH1D*>(hminuit_periph->Clone("hperi"));
345356

346357
double f = par[0];
347358
double gv = par[1];

0 commit comments

Comments
 (0)