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 "spdlog/sinks/stdout_color_sinks.h"
3 #include "spdlog/spdlog.h"
4 
5 // AMS headers
6 #include "Tofrec02_ihep.h"
7 #include "TrdKCluster.h"
8 #include "amschain.h"
9 #include "root.h"
10 #include "tkdcards.h"
11 
12 // NAIA headers
13 #include "NtpMaker/NtpSelector.h"
14 #include "NtpMaker/NtpTools.h"
15 #include "NtpMaker/cli_options.h"
17 
18 int main(int argc, char **argv) {
19  auto logger = spdlog::stdout_color_st("NtpMaker");
20 
21  logger->info("Starting up...");
22 
23  struct arguments arguments;
24  arguments.flag_overwrite = false;
25  arguments.flag_input = false;
26  arguments.flag_output = false;
27 
28  int parse_status = argp_parse(&argp, argc, argv, 0, 0, &arguments);
29 
30  if (parse_status)
31  throw std::runtime_error("Failed parsing CLI arguments");
32 
33  if (std::string(arguments.debug_level) == "Debug") {
34  spdlog::set_level(spdlog::level::debug);
35  } else if (std::string(arguments.debug_level) == "Trace") {
36  spdlog::set_level(spdlog::level::trace);
37  }
38 
39  // handle input and output arguments
40  if (!arguments.flag_input) {
41  logger->error("Missing input file");
42  return 1;
43  }
44  std::string inputFileName = arguments.infilename;
45 
46  bool flag_filelist;
47  if (inputFileName.find(".txt") != std::string::npos) {
48  flag_filelist = true;
49  } else if (inputFileName.find(".root") != std::string::npos) {
50  flag_filelist = false;
51  } else {
52  logger->error("{} is not a valid input file", inputFileName);
53  return 1;
54  }
55  std::string outputFileName;
56  if (arguments.flag_output)
57  outputFileName = arguments.outfilename;
58  else {
59  outputFileName = inputFileName.substr(inputFileName.find_last_of('/') + 1,
60  inputFileName.size() - inputFileName.find_last_of('/'));
61  if (flag_filelist)
62  outputFileName = outputFileName.substr(0, outputFileName.find_last_of('.')) + ".root";
63  }
64 
65  logger->info("Running on input file {}", inputFileName);
66  logger->info("Writing output file {}", outputFileName);
67 
68  // logger->debug("Setting up branches");
69  // chain.SetupBranches(NAIAChain::AccessMode::Write);
70 
71  logger->debug("Creating AMSChain");
72  auto amsChain = std::make_unique<AMSChain>();
73  if (std::string(arguments.debug_level) == "Info") {
75  }
76  if (!flag_filelist) {
77  amsChain->Add(inputFileName.c_str());
78  } else {
79  ifstream infilelist(inputFileName.c_str());
80  TString bufname;
81  while (!infilelist.eof()) {
82  infilelist >> bufname;
83  if (bufname.CompareTo("")) {
84  logger->info("Adding file {} to amsChain", bufname);
85  amsChain->Add(bufname);
86  }
87  }
88  }
89  unsigned long long nEvents = amsChain->GetEntries();
90  logger->info("{} events in this file", nEvents);
91 
92  // Get the first event to gather some info, this is needed especially when processing MC
93  AMSEventR *event = amsChain->GetEvent(0);
94  bool isMC = event->nMCEventg() > 0;
95 
96  if (std::string(arguments.debug_level) == "Info") {
98  }
99 
100  // check if running on one event or on a smaller subset
101  unsigned long long EventNumber = 0;
102  unsigned long long firstev, lastev;
103  if (arguments.event > 0) {
104  logger->debug("Run in single event mode");
105  EventNumber = amsChain->GetEntryNo(event->Run(), arguments.event);
106  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
107  firstev = EventNumber;
108  lastev = EventNumber + 1;
109  } else if (arguments.from_event > 0) {
110  logger->debug("Run in from-event mode");
111  EventNumber = amsChain->GetEntryNo(event->Run(), arguments.from_event);
112  logger->debug(" event no. {} - {}", event->Run(), EventNumber);
113  firstev = EventNumber;
114  lastev = arguments.nevents > 0 ? EventNumber + arguments.nevents : nEvents;
115  } else {
116  firstev = 0;
117  lastev = arguments.nevents > 0 ? arguments.nevents : nEvents;
118  }
119 
120  // ====== setup standard AMS pre-conditions and objects ======
121  // RTI
122  if (!isMC) {
123  AMSSetupR::RTI::UseLatest(7);
124  } else {
125  AMSSetupR::SlowControlR::ReadFromExternalFile = false;
126  }
127  TkDBc::UseFinal();
128  TRMCFFKEY_DEF::ReadFromFile = 0;
129  TRFITFFKEY_DEF::ReadFromFile = 0;
130  TRFITFFKEY.ErcHeY = 0;
131 
132  TofRecH::RebuildBetaHInReadHeader = false;
133 
134  // Additional (effective) temperature corrections
135  RichRingR::useEffectiveTemperatureCorrection = true;
136  // force load Config & Status from ext. files (needed for pass6 run>=1407139304 && run<=1411991495)
137  RichRingR::reloadRunTag = true;
138 
139  // TrdK stuff
140  if (isMC) {
141  TrdKCluster::ForceReadAlignment = 0;
142  TrdKCluster::ForceReadCalibration = 0;
143  TrdKCluster::ForceReadXePressure = 0;
144  TrdKCluster::SetDefaultMCXePressure(900);
145  }
146 
147  NAIA::NtpSelector analysis(isMC);
148  analysis.SetOutputFilename(outputFileName);
149  analysis.firstentry = firstev;
150  analysis.lastentry = lastev;
151 
152  amsChain->Process(&analysis, "", lastev - firstev, firstev);
153 
154  logger->info("...exiting");
155  return 0;
156 }
struct argp argp
Definition: cli_options.h:20
int main(int argc, char **argv)
Definition: NtpMaker.cpp:18
unsigned long long firstentry
first entry to be processed
Definition: NtpSelector.h:28
bool flag_overwrite
Definition: cli_options.h:27
void UnmuteOutput()
Definition: Logging.cpp:36
const char * debug_level
Definition: cli_options.h:26
const char * infilename
Definition: cli_options.h:23
int from_event
Definition: cli_options.h:31
void SetOutputFilename(std::string outFilename)
Definition: NtpSelector.h:32
bool flag_output
Definition: cli_options.h:29
void MuteOutput()
Definition: Logging.cpp:22
const char * outfilename
Definition: cli_options.h:24
int nevents
Definition: cli_options.h:32
bool flag_input
Definition: cli_options.h:28
unsigned long long lastentry
last entry to be processed
Definition: NtpSelector.h:30