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