11 #include "boost/program_options.hpp" 23 namespace bpo = boost::program_options;
30 pluralize(std::size_t count,
32 std::string singular =
""s,
33 std::string plural =
"s"s)
35 ((count == 1) ? singular : plural))
48 count_events(std::string
const& fileName, std::ostream& os, std::ostream& err)
51 auto savedErrorLevel = gErrorIgnoreLevel;
52 gErrorIgnoreLevel = kBreak;
53 TFile*
tf = TFile::Open(fileName.c_str());
54 gErrorIgnoreLevel = savedErrorLevel;
56 err << fileName <<
"\tCould not be opened by ROOT: skipped.\n";
59 std::array<std::size_t, art::NumBranchTypes> counters{{0ull}};
62 std::string treeName =
64 TTree* tree =
static_cast<TTree*
>(tf->Get(treeName.c_str()));
66 err << fileName <<
"\tNot a valid art ROOT-format file: skipped.\n";
69 counters[i] = tree->GetEntries();
72 auto tree =
static_cast<TTree*
>(
74 if (tree && (tree->GetNbranches() > 1)) {
79 os << fileName <<
"\t" << pluralize(counters[
art::InRun],
"run") <<
", " 81 << pluralize(counters[
art::InEvent],
"event") <<
", and " 84 os << fileName <<
'\t' << counters[
art::InRun] <<
'\t' 93 main(
int argc,
char** argv)
95 using stringvec = std::vector<std::string>;
97 std::ostringstream descstr;
98 descstr << argv[0] <<
"Usage: count_events [<options>] <filename>+\n";
99 bpo::options_description desc(descstr.str());
100 desc.add_options()(
"hr",
"Human-readable output")(
101 "help,h",
"this help message.")(
"source,s",
102 bpo::value<stringvec>()->composing(),
103 "source data file (multiple OK).");
104 bpo::options_description all_opts(
"All Options.");
108 bpo::positional_options_description pd;
109 pd.add(
"source", -1);
111 bpo::variables_map vm;
113 bpo::store(bpo::command_line_parser(argc, argv)
121 std::cerr <<
"Exception from command line processing in " << argv[0] <<
": " 125 if (vm.count(
"help")) {
126 std::cerr << desc << std::endl;
128 }
else if (vm.count(
"hr")) {
131 if (vm.count(
"source") == 0) {
132 std::cerr <<
"Require at least one source file.\n";
133 std::cerr << desc <<
"\n";
136 auto const& sources = vm[
"source"].as<
stringvec>();
137 auto const expected = sources.size();
138 auto succeeded = std::count_if(sources.cbegin(),
140 std::bind(&count_events,
141 std::placeholders::_1,
143 std::ref(std::cerr)));
144 if (expected == static_cast<size_t>(succeeded)) {
145 std::cout <<
"Counted events successfully for " << expected
146 <<
" specified files." << std::endl;
150 std::cout <<
"Failed to count events for " << expected - succeeded <<
" of " 151 << expected <<
" specified files." << std::endl;
153 return result & 0xff;
std::string const & BranchTypeToProductTreeName(BranchType const bt)
Stream & operator<<(Stream &&out, CallInfo_t const &info)
Helper operator to insert a call information in a stream with default options.
std::vector< std::string > stringvec
int main(int argc, char **argv)
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).