Skip to content

Commit c63aefe

Browse files
eguirauddpiparo
authored andcommitted
[TDF] Run clang-format on all TDataFrame tutorials
1 parent 0b269b8 commit c63aefe

File tree

7 files changed

+156
-154
lines changed

7 files changed

+156
-154
lines changed

tutorials/dataframe/tdf001_introduction.C

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,37 @@
2222

2323
// A simple helper function to fill a test tree: this makes the example
2424
// stand-alone.
25-
void fill_tree(const char* filename, const char* treeName) {
26-
TFile f(filename,"RECREATE");
27-
TTree t(treeName,treeName);
25+
void fill_tree(const char *filename, const char *treeName)
26+
{
27+
TFile f(filename, "RECREATE");
28+
TTree t(treeName, treeName);
2829
double b1;
2930
int b2;
3031
t.Branch("b1", &b1);
3132
t.Branch("b2", &b2);
32-
for(int i = 0; i < 10; ++i) {
33+
for (int i = 0; i < 10; ++i) {
3334
b1 = i;
34-
b2 = i*i;
35+
b2 = i * i;
3536
t.Fill();
3637
}
3738
t.Write();
3839
f.Close();
3940
return;
4041
}
4142

42-
int tdf001_introduction() {
43+
int tdf001_introduction()
44+
{
4345

4446
// We prepare an input tree to run on
4547
auto fileName = "tdf001_introduction.root";
4648
auto treeName = "myTree";
47-
fill_tree(fileName,treeName);
49+
fill_tree(fileName, treeName);
4850

4951
// We read the tree from the file and create a TDataFrame, a class that
5052
// allows us to interact with the data contained in the tree.
5153
// We select a default column, a *branch* to adopt ROOT jargon, which will
5254
// be looked at if none is specified by the user when dealing with filters
53-
//and actions.
55+
// and actions.
5456
ROOT::Experimental::TDataFrame d(treeName, fileName, {"b1"});
5557

5658
// ## Operations on the dataframe
@@ -68,8 +70,8 @@ int tdf001_introduction() {
6870
// filters. Here we show how the automatic selection of the column kicks
6971
// in in case the user specifies none.
7072
auto entries1 = d.Filter(cutb1) // <- no column name specified here!
71-
.Filter(cutb1b2, {"b2","b1"})
72-
.Count();
73+
.Filter(cutb1b2, {"b2", "b1"})
74+
.Count();
7375

7476
std::cout << *entries1 << " entries passed all filters" << std::endl;
7577

@@ -82,13 +84,13 @@ int tdf001_introduction() {
8284
// ### `Min`, `Max` and `Mean` actions
8385
// These actions allow to retrieve statistical information about the entries
8486
// passing the cuts, if any.
85-
auto b1b2_cut = d.Filter(cutb1b2, {"b2","b1"});
87+
auto b1b2_cut = d.Filter(cutb1b2, {"b2", "b1"});
8688
auto minVal = b1b2_cut.Min();
8789
auto maxVal = b1b2_cut.Max();
8890
auto meanVal = b1b2_cut.Mean();
8991
auto nonDefmeanVal = b1b2_cut.Mean("b2"); // <- Column is not the default
90-
std::cout << "The mean is always included between the min and the max: "
91-
<< *minVal << " <= " << *meanVal << " <= " << *maxVal << std::endl;
92+
std::cout << "The mean is always included between the min and the max: " << *minVal << " <= " << *meanVal
93+
<< " <= " << *maxVal << std::endl;
9294

9395
// ### `Take` action
9496
// The `Take` action allows to retrieve all values of the variable stored in a
@@ -99,8 +101,7 @@ int tdf001_introduction() {
99101
auto b1Vec = b1_cut.Take<double, std::vector<double>>();
100102

101103
std::cout << "Selected b1 entries" << std::endl;
102-
for(auto b1_entry : *b1List)
103-
std::cout << b1_entry << " ";
104+
for (auto b1_entry : *b1List) std::cout << b1_entry << " ";
104105
std::cout << std::endl;
105106
auto b1VecCl = TClass::GetClass(typeid(*b1Vec));
106107
std::cout << "The type of b1Vec is" << b1VecCl->GetName() << std::endl;
@@ -118,8 +119,7 @@ int tdf001_introduction() {
118119
// In this case we fill a histogram. In some sense this is a violation of a
119120
// purely functional paradigm - C++ allows to do that.
120121
TH1F h("h", "h", 12, -1, 11);
121-
d.Filter([](int b2) { return b2 % 2 == 0; }, {"b2"})
122-
.Foreach([&h](double b1) { h.Fill(b1); });
122+
d.Filter([](int b2) { return b2 % 2 == 0; }, {"b2"}).Foreach([&h](double b1) { h.Fill(b1); });
123123

124124
std::cout << "Filled h with " << h.GetEntries() << " entries" << std::endl;
125125

@@ -131,8 +131,8 @@ int tdf001_introduction() {
131131
// writing the entire pipeline on one line. This can be easily achieved.
132132
// We'll show this re-working the `Count` example:
133133
auto cutb1_result = d.Filter(cutb1);
134-
auto cutb1b2_result = d.Filter(cutb1b2, {"b2","b1"});
135-
auto cutb1_cutb1b2_result = cutb1_result.Filter(cutb1b2, {"b2","b1"});
134+
auto cutb1b2_result = d.Filter(cutb1b2, {"b2", "b1"});
135+
auto cutb1_cutb1b2_result = cutb1_result.Filter(cutb1b2, {"b2", "b1"});
136136
// Now we want to count:
137137
auto evts_cutb1_result = cutb1_result.Count();
138138
auto evts_cutb1b2_result = cutb1b2_result.Count();
@@ -156,22 +156,20 @@ int tdf001_introduction() {
156156
// any value of any type.
157157
// Let's dive in an example:
158158
auto entries_sum = d.AddColumn("sum", [](double b1, int b2) { return b2 + b1; }, {"b1", "b2"})
159-
.Filter([](double sum) { return sum > 4.2; }, {"sum"})
160-
.Count();
159+
.Filter([](double sum) { return sum > 4.2; }, {"sum"})
160+
.Count();
161161
std::cout << *entries_sum << std::endl;
162162

163163
// Additional columns can be expressed as strings. The content must be C++
164164
// code. The name of the variables must be the name of the branches. The code
165165
// is just in time compiled.
166-
auto entries_sum2 = d.AddColumn("sum", "b1 + b2")
167-
.Filter("sum > 4.2")
168-
.Count();
166+
auto entries_sum2 = d.AddColumn("sum", "b1 + b2").Filter("sum > 4.2").Count();
169167
std::cout << *entries_sum2 << std::endl;
170168

171169
return 0;
172170
}
173171

174-
int main(){
172+
int main()
173+
{
175174
return tdf001_introduction();
176175
}
177-

tutorials/dataframe/tdf002_dataModel.C

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,30 @@ using CylFourVector = ROOT::Math::RhoEtaPhiVector;
3030

3131
// A simple helper function to fill a test tree: this makes the example
3232
// stand-alone.
33-
void fill_tree(const char* filename, const char* treeName) {
34-
TFile f(filename,"RECREATE");
35-
TTree t(treeName,treeName);
33+
void fill_tree(const char *filename, const char *treeName)
34+
{
35+
TFile f(filename, "RECREATE");
36+
TTree t(treeName, treeName);
3637
FourVectors tracks;
3738
t.Branch("tracks", &tracks);
3839

39-
const double M = 0.13957; // set pi+ mass
40+
const double M = 0.13957; // set pi+ mass
4041
TRandom3 R(1);
4142

4243
for (int i = 0; i < 50; ++i) {
4344
auto nPart = R.Poisson(15);
4445
tracks.clear();
4546
tracks.reserve(nPart);
4647
for (int j = 0; j < nPart; ++j) {
47-
double px = R.Gaus(0,10);
48-
double py = R.Gaus(0,10);
49-
double pt = sqrt(px*px +py*py);
50-
double eta = R.Uniform(-3,3);
51-
double phi = R.Uniform(0.0 , 2*TMath::Pi() );
52-
CylFourVector vcyl( pt, eta, phi);
48+
double px = R.Gaus(0, 10);
49+
double py = R.Gaus(0, 10);
50+
double pt = sqrt(px * px + py * py);
51+
double eta = R.Uniform(-3, 3);
52+
double phi = R.Uniform(0.0, 2 * TMath::Pi());
53+
CylFourVector vcyl(pt, eta, phi);
5354
// set energy
54-
double E = sqrt( vcyl.R()*vcyl.R() + M*M);
55-
FourVector q( vcyl.X(), vcyl.Y(), vcyl.Z(), E);
55+
double E = sqrt(vcyl.R() * vcyl.R() + M * M);
56+
FourVector q(vcyl.X(), vcyl.Y(), vcyl.Z(), E);
5657
// fill track vector
5758
tracks.emplace_back(q);
5859
}
@@ -64,12 +65,13 @@ void fill_tree(const char* filename, const char* treeName) {
6465
return;
6566
}
6667

67-
int tdf002_dataModel() {
68+
int tdf002_dataModel()
69+
{
6870

6971
// We prepare an input tree to run on
7072
auto fileName = "tdf002_dataModel.root";
7173
auto treeName = "myTree";
72-
fill_tree(fileName,treeName);
74+
fill_tree(fileName, treeName);
7375

7476
// We read the tree from the file and create a TDataFrame, a class that
7577
// allows us to interact with the data contained in the tree.
@@ -78,39 +80,36 @@ int tdf002_dataModel() {
7880
// ## Operating on branches which are collection of objects
7981
// Here we deal with the simplest of the cuts: we decide to accept the event
8082
// only if the number of tracks is greater than 5.
81-
auto n_cut = [](const FourVectors & tracks) { return tracks.size() > 8; };
82-
auto nentries = d.Filter(n_cut, {"tracks"})
83-
.Count();
83+
auto n_cut = [](const FourVectors &tracks) { return tracks.size() > 8; };
84+
auto nentries = d.Filter(n_cut, {"tracks"}).Count();
8485

8586
std::cout << *nentries << " passed all filters" << std::endl;
8687

8788
// Another possibility consists in creating a new column containing the
8889
// quantity we are interested in.
8990
// In this example, we will cut on the number of tracks and plot their
9091
// transverse momentum.
91-
auto getPt = [](const FourVectors& tracks) {
92+
auto getPt = [](const FourVectors &tracks) {
9293
std::vector<double> pts;
9394
pts.reserve(tracks.size());
94-
for (auto& t:tracks)
95-
pts.emplace_back(t.Pt());
95+
for (auto &t : tracks) pts.emplace_back(t.Pt());
9696
return pts;
97-
};
97+
};
9898

9999
// We do the same for the weights.
100-
auto getPtWeights = [](const FourVectors& tracks) {
100+
auto getPtWeights = [](const FourVectors &tracks) {
101101
std::vector<double> ptsw;
102102
ptsw.reserve(tracks.size());
103-
for (auto& t:tracks)
104-
ptsw.emplace_back(1./t.Pt());
103+
for (auto &t : tracks) ptsw.emplace_back(1. / t.Pt());
105104
return ptsw;
106-
};
105+
};
107106

108-
auto augmented_d = d.AddColumn("tracks_n", [](const FourVectors& tracks){return (int)tracks.size();})
109-
.Filter([](int tracks_n){return tracks_n > 2;}, {"tracks_n"})
110-
.AddColumn("tracks_pts", getPt)
111-
.AddColumn("tracks_pts_weights", getPtWeights);
107+
auto augmented_d = d.AddColumn("tracks_n", [](const FourVectors &tracks) { return (int)tracks.size(); })
108+
.Filter([](int tracks_n) { return tracks_n > 2; }, {"tracks_n"})
109+
.AddColumn("tracks_pts", getPt)
110+
.AddColumn("tracks_pts_weights", getPtWeights);
112111

113-
auto trN = augmented_d.Histo1D("tracks_n",40,-.5,39.5);
112+
auto trN = augmented_d.Histo1D("tracks_n", 40, -.5, 39.5);
114113
auto trPts = augmented_d.Histo1D("tracks_pts");
115114
auto trWPts = augmented_d.Histo1D<std::vector<double>, std::vector<double>>("tracks_pts", "tracks_pts_weights");
116115

@@ -129,7 +128,7 @@ int tdf002_dataModel() {
129128
return 0;
130129
}
131130

132-
int main(){
131+
int main()
132+
{
133133
return tdf002_dataModel();
134134
}
135-

tutorials/dataframe/tdf003_profiles.C

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919

2020
// A simple helper function to fill a test tree: this makes the example
2121
// stand-alone.
22-
void fill_tree(const char* filename, const char* treeName)
22+
void fill_tree(const char *filename, const char *treeName)
2323
{
24-
TFile f(filename,"RECREATE");
25-
TTree t(treeName,treeName);
24+
TFile f(filename, "RECREATE");
25+
TTree t(treeName, treeName);
2626
float px, py, pz;
2727
t.Branch("px", &px);
2828
t.Branch("py", &py);
2929
t.Branch("pz", &pz);
30-
for (int i=0; i<25000; i++) {
31-
gRandom->Rannor(px,py);
32-
pz = px*px + py*py;
30+
for (int i = 0; i < 25000; i++) {
31+
gRandom->Rannor(px, py);
32+
pz = px * px + py * py;
3333
t.Fill();
34-
}
34+
}
3535
t.Write();
3636
f.Close();
3737
return;
@@ -42,20 +42,20 @@ void tdf003_profiles()
4242
// We prepare an input tree to run on
4343
auto fileName = "tdf003_profiles.root";
4444
auto treeName = "myTree";
45-
fill_tree(fileName,treeName);
45+
fill_tree(fileName, treeName);
4646

4747
// We read the tree from the file and create a TDataFrame.
48-
ROOT::Experimental::TDataFrame d(treeName, fileName, {"px","py","pz"});
48+
ROOT::Experimental::TDataFrame d(treeName, fileName, {"px", "py", "pz"});
4949

5050
// Create the profiles
51-
auto hprof1d = d.Profile1D<float, float>(TProfile("hprof1d","Profile of pz versus px",64,-4,4));
52-
auto hprof2d = d.Profile2D<float, float, float>(TProfile2D("hprof2d","Profile of pz versus px and py",40,-4,4,40,-4,4,0,20));
51+
auto hprof1d = d.Profile1D<float, float>(TProfile("hprof1d", "Profile of pz versus px", 64, -4, 4));
52+
auto hprof2d = d.Profile2D<float, float, float>(
53+
TProfile2D("hprof2d", "Profile of pz versus px and py", 40, -4, 4, 40, -4, 4, 0, 20));
5354

5455
// And Draw
55-
auto c1 = new TCanvas("c1","Profile histogram example",200,10,700,500);
56+
auto c1 = new TCanvas("c1", "Profile histogram example", 200, 10, 700, 500);
5657
hprof1d->DrawClone();
57-
auto c2 = new TCanvas("c2","Profile2D histogram example",200,10,700,500);
58+
auto c2 = new TCanvas("c2", "Profile2D histogram example", 200, 10, 700, 500);
5859
c2->cd();
5960
hprof2d->DrawClone();
60-
6161
}

tutorials/dataframe/tdf004_cutFlowReport.C

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,31 @@ using CylFourVector = ROOT::Math::RhoEtaPhiVector;
2525

2626
// A simple helper function to fill a test tree: this makes the example
2727
// stand-alone.
28-
void fill_tree(const char* filename, const char* treeName) {
29-
TFile f(filename,"RECREATE");
30-
TTree t(treeName,treeName);
28+
void fill_tree(const char *filename, const char *treeName)
29+
{
30+
TFile f(filename, "RECREATE");
31+
TTree t(treeName, treeName);
3132
double b1;
3233
int b2;
3334
t.Branch("b1", &b1);
3435
t.Branch("b2", &b2);
35-
for(int i = 0; i < 50; ++i) {
36+
for (int i = 0; i < 50; ++i) {
3637
b1 = i;
37-
b2 = i*i;
38+
b2 = i * i;
3839
t.Fill();
3940
}
4041
t.Write();
4142
f.Close();
4243
return;
4344
}
4445

45-
void tdf004_cutFlowReport() {
46+
void tdf004_cutFlowReport()
47+
{
4648

4749
// We prepare an input tree to run on
4850
auto fileName = "tdf004_cutFlowReport.root";
4951
auto treeName = "myTree";
50-
fill_tree(fileName,treeName);
52+
fill_tree(fileName, treeName);
5153

5254
// We read the tree from the file and create a TDataFrame
5355
ROOT::Experimental::TDataFrame d(treeName, fileName, {"b1", "b2"});
@@ -62,22 +64,26 @@ void tdf004_cutFlowReport() {
6264
auto filtered1 = d.Filter(cut1, {"b1"}, "Cut1");
6365
auto filtered2 = d.Filter(cut2, {"b2"}, "Cut2");
6466

65-
auto augmented1 = filtered2.AddColumn("b3", [](double b1, int b2){ return b1/b2;});
67+
auto augmented1 = filtered2.AddColumn("b3", [](double b1, int b2) { return b1 / b2; });
6668
auto cut3 = [](double x) { return x < .5; };
6769
auto filtered3 = augmented1.Filter(cut3, {"b3"}, "Cut3");
6870

6971
// Statistics are retrieved through a call to the Report method:
70-
// when Report is called on the main TDataFrame object, it prints stats for all named filters declared up to that point
71-
// when called on a stored chain state (i.e. a chain/graph node), it prints stats for all named filters in the section
72+
// when Report is called on the main TDataFrame object, it prints stats for all named filters declared up to that
73+
// point
74+
// when called on a stored chain state (i.e. a chain/graph node), it prints stats for all named filters in the
75+
// section
7276
// of the chain between the main TDataFrame and that node (included).
73-
// Stats are printed in the same order as named filters have been added to the graph, and refer to the latest event-loop that has been run using the relevant TDataFrame.
77+
// Stats are printed in the same order as named filters have been added to the graph, and refer to the latest
78+
// event-loop that has been run using the relevant TDataFrame.
7479
std::cout << "Cut3 stats:" << std::endl;
7580
filtered3.Report();
7681
std::cout << "All stats:" << std::endl;
7782
d.Report();
7883
}
7984

80-
int main(){
85+
int main()
86+
{
8187
tdf004_cutFlowReport();
8288
return 0;
8389
}

0 commit comments

Comments
 (0)