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