NAIA
 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 -h, --help Give this help list )";
22 
23 int StopSelector = 0;
24 
25 void sig_handler(int signum) {
26  printf("Singal Handler called with signal %d\n", signum);
27  StopSelector = 1;
28  return;
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  if (spdlog::get_level() == spdlog::level::info) {
78  }
79 
80  // check if running on one event or on a smaller subset
81  unsigned long long EventNumber = 0;
82  unsigned long long firstev, lastev;
83  if (arguments["--single-event"]) {
84  logger->debug("Run in single event mode");
85  EventNumber = amsChain->GetEntryNo(event->Run(), arguments["--single-event"].asLong());
86  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
87  firstev = EventNumber;
88  lastev = EventNumber + 1;
89  } else if (arguments["--from_event"]) {
90  logger->debug("Run in from-event mode");
91  EventNumber = amsChain->GetEntryNo(event->Run(), arguments["--from_event"].asLong());
92  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
93  firstev = EventNumber;
94  lastev = arguments["--nevents"] ? EventNumber + arguments["--nevents"].asLong() : nEvents;
95  } else {
96  firstev = 0;
97  lastev = arguments["--nevents"] ? arguments["--nevents"].asLong() : nEvents;
98  }
99 
100  // ====== setup standard AMS pre-conditions and objects ======
101  // RTI
102  if (!isMC) {
103  AMSSetupR::RTI::UseLatest(7);
104  } else {
105  AMSSetupR::SlowControlR::ReadFromExternalFile = false;
106  }
107  TkDBc::UseFinal();
108  TRMCFFKEY_DEF::ReadFromFile = 0;
109  TRFITFFKEY_DEF::ReadFromFile = 0;
110  TRFITFFKEY.ErcHeY = 0;
111 
112  // Additional (effective) temperature corrections
113  RichRingR::useEffectiveTemperatureCorrection = true;
114  // force load Config & Status from ext. files (needed for pass6 run>=1407139304 && run<=1411991495)
115  RichRingR::reloadRunTag = true;
116 
117  // TrdK stuff
118  if (isMC) {
119  TrdKCluster::ForceReadAlignment = 0;
120  TrdKCluster::ForceReadCalibration = 0;
121  TrdKCluster::ForceReadXePressure = 0;
122  TrdKCluster::SetDefaultMCXePressure(900);
123  }
124 
125  NAIA::NtpSelector analysis(isMC);
126  analysis.SetOutputFilename(outputFile);
127  analysis.firstentry = firstev;
128  analysis.lastentry = lastev;
129 
130  amsChain->Process(&analysis, "", lastev - firstev, firstev);
131 
132  logger->info("...exiting");
133  return 0;
134 }
135 
int StopSelector
Definition: NtpMaker.cpp:23
Container class for versioning info.
Definition: NAIAVersion.h:21
int main(int argc, char **argv)
Definition: NtpMaker.cpp:31
unsigned long long firstentry
first entry to be processed
Definition: NtpSelector.h:28
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:32
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
NAIAVersion container class description.
unsigned long long lastentry
last entry to be processed
Definition: NtpSelector.h:30