Hi there.
I have a root file with a branch named ADC, which consists of std::vector<int>
.
I have successfully written a code using RDataFrame that draws the histogram of the fist element of ADC(that is, ADC[0]
) over all events.
But it failed when I tried to do the same thing to ADC[1]
, ADC[2]
and so on with for-loop.
I would appreciate if you could help me solve the problem.
Cheers
my program
using ints = ROOT::RVec<int>;
void myMacro()
{
auto fileName = "rootfile.root";
auto treeName = "tree";
TString pdfname = "histogram.pdf",
pdf = "pdf";
auto c = new TCanvas();
c->SetLogy();
c->Print(pdfname + "[", pdf);
ROOT::RDataFrame df(treeName, fileName, {"ADC"});
// this works fine :)
{
// Draw the histogram of first element of branch `ADC'
auto first_elm = [](ints v) { return v.at(0); };
auto histo = df.Define("ADC0", first_elm, {"ADC"})
.Histo1D({"histo", "title;ADC(ch);Counts", 2048, 0, 4096}, "ADC0");
histo->Draw();
c->Print(pdfname, pdf);
}
// this doesn't work fine :(
/*
for (int i = 0; i < 30; ++i)
{
// Draw the histogram of i-th element of branch `ADC'
auto ith = [](int i, ints v) { return v.at(i); };
auto histo = df.Define("ADCi", ith, {i, "ADC"})
.Histo1D({"histo", "title;ADC(ch);Counts", 2048, 0, 4096}, "ADCi");
histo->Draw();
c->Print(pdfname, pdf);
}
*/
c->Print(pdfname + "]", pdf);
}
error message
myMacro.cpp:41:46: error: non-constant-expression cannot be narrowed from type 'int' to 'std::__1::vector<std::__1::basic_string<char>,
std::__1::allocator<std::__1::basic_string<char> > >::size_type' (aka 'unsigned long') in initializer list [-Wc++11-narrowing]
auto histo = df.Define("ADCi", ith, {i, "ADC"})
^
myMacro.cpp:41:46: note: insert an explicit cast to silence this issue
auto histo = df.Define("ADCi", ith, {i, "ADC"})
^
static_cast<size_type>( )
environment
❯ uname -a
Darwin ##############.ac.jp 20.4.0 Darwin Kernel Version 20.4.0: Fri Mar 5 01:14:02 PST 2021; root:xnu-7195.101.1~3/RELEASE_ARM64_T8101 arm64
❯ root --version
ROOT Version: 6.22/08
Built for macosxarm64 on Mar 10 2021, 14:20:04
From tags/v6-22-08@v6-22-08