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