Belle II Software development
numberOfEventsInBelleMdst.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/core/Environment.h>
10#include <framework/datastore/DataStore.h>
11#include <framework/datastore/StoreObjPtr.h>
12#include <framework/utilities/FileSystem.h>
13
14#include <framework/logging/Logger.h>
15
16#define BELLE_TARGET_H "belle-x86_64-unknown-linux-gnu-g++.h"
17#include "belle_legacy/panther/panther.h"
18#include "belle_legacy/panther/panther_group.h"
19#include "belle_legacy/tables/belletdf.h"
20
21#include <boost/program_options.hpp>
22#include <iostream>
23#include <cstdlib>
24#include <algorithm>
25#include <filesystem>
26
27using namespace std;
28using namespace Belle2;
29namespace po = boost::program_options;
30
31
32int main(int argc, char* argv[])
33{
34
35 std::string filename;
36 po::options_description description("Options");
37 description.add_options()
38 ("filename", po::value<std::string>(&filename), "belle 1 mdst file");
39 po::variables_map vm;
40 po::positional_options_description p;
41 p.add("filename", -1);
42
43 try {
44 po::parsed_options parsed = po::command_line_parser(argc, argv).options(description).positional(p).run();
45 po::store(parsed, vm);
46
47 if (vm.count("help")) {
48 std::cout << description << std::endl;
49 return 1;
50 }
51 po::notify(vm);
52 } catch (po::error& err) {
53 std::cerr << "Error: " << err.what() << "\n";
54 return 1;
55 }
56
57 // check environment
58 const char* table_dir = getenv("PANTHER_TABLE_DIR");
59 if (!table_dir or !FileSystem::isDir(table_dir)) {
60 string fixed_table_dir = Environment::Instance().getExternalsPath() + "/share/belle_legacy/panther";
61 B2WARNING("PANTHER_TABLE_DIR environment variable not set correctly. This is a known problem with externals v00-05-09, using " <<
62 fixed_table_dir << " instead.");
63 if (!FileSystem::isDir(fixed_table_dir))
64 B2FATAL("Path " << fixed_table_dir << " does not exist, your externals setup seems broken.");
65 setenv("PANTHER_TABLE_DIR", fixed_table_dir.c_str(), 1); //overwrite existing value
66 }
67
68 // Initialize Panther
69 BsInit(0);
70 // delete existing FileIO
71 BsClrTab(BBS_CLEAR_ALL);
72 // Check if file exists
73 if (!std::filesystem::exists(filename)) {
74 std::cout << "Couldn't find file!" << std::endl;
75 return 1;
76 }
77
78 // Open data file
79 Belle::Panther_FileIO* fd = new Belle::Panther_FileIO(filename.c_str(), BBS_READ);
80
81 // Read first record (does not contain event data)
82 if (fd->read() == -1) {
83 B2FATAL("Couldn't read file '" << filename << "'!");
84 }
85
86 unsigned int nevt = 0;
87
88 // read event
89 int rectype = -1;
90 while (rectype > -2) {
91 rectype = -1;
92 while (rectype < 0 && rectype != -2) {
93 //clear all previous event data before reading!
94 BsClrTab(BBS_CLEAR);
95 rectype = fd->read();
96 if (rectype == -1) {
97 B2ERROR("Error while reading panther tables! Record skipped.");
98 }
99 nevt++;
100 }
101 }
102
103 delete fd;
104
105 std::cout << static_cast<int>(nevt) - 2 << std::endl;
106 if (rectype == -2) { // EoF detected
107 return 0;
108 }
109
110}
const std::string & getExternalsPath() const
Returns the path which points to the externals directory of the framework.
Definition: Environment.h:61
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
static bool isDir(const std::string &filename)
Check if filename points to an existing directory.
Definition: FileSystem.cc:51
Abstract base class for different kinds of events.
STL namespace.