NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
NtpMaker.cpp
Go to the documentation of this file.
1 
2 // dependencies headers
3 #include <docopt.h>
4 #include <fmt/ranges.h>
5 #include <signal.h>
6 #include <spdlog/sinks/stdout_color_sinks.h>
7 #include <spdlog/spdlog.h>
8 
9 // AMS headers
10 #include "TrdKCluster.h"
11 #include "amschain.h"
12 #include "root.h"
13 #include "tkdcards.h"
14 
15 // NAIA headers
16 #include "NAIAVersion.h"
17 #include "NtpMaker/NtpSelector.h"
18 #include "NtpMaker/NtpTools.h"
20 
21 static constexpr auto EXENAME = R"(NtpMaker)";
22 static constexpr auto USAGE = R"(NtpMaker, NAIA ntuple version Usage: NtpMaker (-i FILE...) (-o FILE) [-v | -vv] [options] NtpMaker --version Options: -i, --input-file=FILE Input file -o, --output-file=FILE Output file -w, --overwrite Overwrite results -v... Verbosity level (once for Debug, twice for Trace) -e, --from-event=EVNO Process from this event -E, --single-event=EVNO Process only this event -n, --nevents=NEV Process # events --reprocess_rti Reprocess and replace the RTIInfo tree only -h, --help Give this help list )";
23 
24 bool StopSelector = false;
25 
26 void sig_handler(int signum) {
27  spdlog::warn("Signal Handler called with signal {}", signum);
28  StopSelector = true;
29 }
30 
31 int main(int argc, char **argv) {
32  signal(SIGINT, sig_handler); // Register signal handler
33 
34  auto naiaVersion = NAIA::Version{};
35  auto versionString = fmt::format("{} v{}.{}.{}", EXENAME, naiaVersion.major, naiaVersion.minor, naiaVersion.patch);
36  std::map<std::string, docopt::value> arguments = docopt::docopt(USAGE, {std::next(argv), std::next(argv, argc)},
37  true, // show help if requested
38  versionString); // version string
39 
40  auto logger = NAIA::getLogger(EXENAME);
41  switch (arguments["-v"].asLong()) {
42  case 1:
43  spdlog::set_level(spdlog::level::debug);
44  break;
45  case 2:
46  spdlog::set_level(spdlog::level::trace);
47  break;
48  }
49 
50  // handle input and output arguments
51  auto inputFiles = arguments["--input-file"].asStringList();
52  auto outputFile = arguments["--output-file"].asString();
53 
54  auto fileList = NAIA::NtpTools::LoadInputFiles(inputFiles);
55 
56  logger->info("Input files: {}", inputFiles);
57  logger->info("Output file: {}", outputFile);
58 
59  logger->debug("Creating AMSChain");
60  auto amsChain = std::make_unique<AMSChain>();
61  if (spdlog::get_level() == spdlog::level::info) {
63  }
64 
65  for (const auto &filename : fileList) {
66  amsChain->Add(filename.c_str());
67  }
68 
69  unsigned long long nEvents = amsChain->GetEntries();
70  logger->info("{} events in this file", nEvents);
71 
72  // Get the first event to gather some info, this is needed especially when processing MC
73  AMSEventR *event = amsChain->GetEvent(0);
74  bool isMC = event->nMCEventg() > 0;
75 
76  spdlog::debug("isMC = {}", isMC);
77 
78  if (spdlog::get_level() == spdlog::level::info) {
80  }
81 
82  // check if running on one event or on a smaller subset
83  unsigned long long EventNumber = 0;
84  unsigned long long firstev, lastev;
85  if (arguments["--single-event"]) {
86  logger->debug("Run in single event mode");
87  EventNumber = amsChain->GetEntryNo(event->Run(), arguments["--single-event"].asLong());
88  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
89  firstev = EventNumber;
90  lastev = EventNumber + 1;
91  } else if (arguments["--from_event"]) {
92  logger->debug("Run in from-event mode");
93  EventNumber = amsChain->GetEntryNo(event->Run(), arguments["--from_event"].asLong());
94  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
95  firstev = EventNumber;
96  lastev = arguments["--nevents"] ? EventNumber + arguments["--nevents"].asLong() : nEvents;
97  } else {
98  firstev = 0;
99  lastev = arguments["--nevents"] ? arguments["--nevents"].asLong() : nEvents;
100  }
101 
102  bool reprocess_rti = arguments["--reprocess_rti"].asBool();
103  if (reprocess_rti) {
104  logger->info("Reprocessing RTIInfo tree only");
105  }
106 
107  // ====== setup standard AMS pre-conditions and objects ======
108  // RTI
109  if (!isMC) {
110  AMSSetupR::RTI::UseLatest(8);
111  } else {
112  AMSSetupR::SlowControlR::ReadFromExternalFile = false;
113  }
114  TkDBc::UseFinal();
115  TRMCFFKEY_DEF::ReadFromFile = 0;
116  TRFITFFKEY_DEF::ReadFromFile = 0;
117  TRFITFFKEY.ErcHeY = 0;
118 
119  // Additional (effective) temperature corrections
120  RichRingR::useEffectiveTemperatureCorrection = true;
121  // force load Config & Status from ext. files (needed for pass6 run>=1407139304 && run<=1411991495)
122  RichRingR::reloadRunTag = true;
123 
124  // TrdK stuff
125  if (isMC) {
126  TrdKCluster::ForceReadAlignment = 0;
127  TrdKCluster::ForceReadCalibration = 0;
128  TrdKCluster::ForceReadXePressure = 0;
129  TrdKCluster::SetDefaultMCXePressure(900);
130  }
131 
132  NAIA::NtpSelector analysis{isMC};
133  analysis.SetOutputFilename(outputFile);
134  analysis.ReprocessRTI(reprocess_rti);
135  analysis.firstentry = firstev;
136  analysis.lastentry = lastev;
137 
138  amsChain->Process(&analysis, "", lastev - firstev, firstev);
139 
140  logger->info("...exiting");
141  return 0;
142 }
143 
bool StopSelector
Definition: NtpMaker.cpp:24
int main(int argc, char **argv)
Definition: NtpMaker.cpp:31
std::vector< std::string > LoadInputFiles(std::vector< std::string > &inputFiles)
Definition: NtpTools.cpp:39
void UnmuteOutput()
Definition: Logging.cpp:36
static constexpr auto USAGE
Definition: NtpMaker.cpp:22
static constexpr auto EXENAME
Definition: NtpMaker.cpp:21
void sig_handler(int signum)
Definition: NtpMaker.cpp:26
void SetOutputFilename(std::string outFilename)
Definition: NtpSelector.h:33
void MuteOutput()
Definition: Logging.cpp:22
auto getLogger(const std::string &fnName)
Create a new logger with a given function name.
Definition: Logging.h:18