ndmspc  0.20250128.0
ndh-import.cxx
1 #include <getopt.h>
2 #include <iostream>
3 #include <TFile.h>
4 #include <TTree.h>
5 #include <fstream>
6 
7 #include <nlohmann/json.hpp>
8 
9 #include "HnSparse.h"
10 #include "ndmspc.h"
11 
12 using json = nlohmann::json;
13 
14 void version()
15 {
16  Printf("%s v%d.%d.%d-%s", NDMSPC_NAME, NDMSPC_VERSION_MAJOR(NDMSPC_VERSION), NDMSPC_VERSION_MINOR(NDMSPC_VERSION),
17  NDMSPC_VERSION_PATCH(NDMSPC_VERSION), NDMSPC_VERSION_RELEASE);
18 }
19 
20 [[noreturn]] void help(int rc = 0)
21 {
22  version();
23  Printf("\nUsage: [OPTION]...");
24  Printf("\nOptions:");
25  Printf(" -c, --config[=VALUE] ndh config file");
26  Printf("\n -h, --help display this help and exit");
27  Printf(" -v, --version output version information and exit");
28  Printf("\nExamples:");
29  Printf(" %s-import -c ndh.yaml", NDMSPC_NAME);
30  Printf(" Using ndh.yaml config file");
31  Printf("\nReport bugs at <https://gitlab.com/ndmspc/ndmspc>");
32  Printf("General help using GNU software: <https://www.gnu.org/gethelp/>");
33 
34  exit(rc);
35 }
36 
37 int main(int argc, char ** argv)
38 {
39 
40  // ***** Default values START *****
42  std::string configFile = "";
43  std::string filename = "";
44  std::string objname = "";
45  std::vector<Int_t> axisSplit = {};
46  std::string outputFilename = "";
47 
48  json config;
49  config["input"]["filename"] =
50  "root://eos.ndmspc.io//eos/ndmspc/scratch/alice.cern.ch/user/a/alitrain/PWGLF/LF_pp_AOD/"
51  "987/phi_leading_3s/AnalysisResults.root";
52  config["input"]["objname"] = "Unlike";
53  config["split"]["axes"] = {1, 2};
54  config["output"]["filename"] = "/tmp/ndh.root";
55 
56  // ***** Default values END *****
57 
58  std::string shortOpts = "hvc:W;";
59  struct option longOpts[] = {{"help", no_argument, nullptr, 'h'},
60  {"version", no_argument, nullptr, 'v'},
61  {"config", required_argument, nullptr, 'c'},
62  {nullptr, 0, nullptr, 0}};
63 
64  int nextOption = 0;
65  do {
66  nextOption = getopt_long(argc, argv, shortOpts.c_str(), longOpts, nullptr);
67  switch (nextOption) {
68  case -1:
69  case 0: break;
70  case 'h': help();
71  case 'v':
72  version();
73  exit(0);
74  break;
75  case 'c': configFile = optarg; break;
76  default: help(1);
77  }
78  } while (nextOption != -1);
79 
80  version();
81 
82  if (!configFile.empty()) {
83  Printf("Loading config file '%s' ...", configFile.data());
84  std::ifstream f(configFile);
85  if (f.fail()) {
86  Printf("Error: Cannot open config file '%s' !!!", configFile.data());
87  }
88  json cfgJson = json::parse(f);
89  config.merge_patch(cfgJson);
90  }
91 
92  std::cout << config << std::endl;
93 
94  filename = config["input"]["filename"].get<std::string>();
95  objname = config["input"]["objname"].get<std::string>();
96  axisSplit = config["split"]["axes"].get<std::vector<int>>();
97  outputFilename = config["output"]["filename"].get<std::string>();
98 
100  h.SetOutputFileName(outputFilename.data());
101  h.Import(axisSplit, filename.data(), objname.data());
102 
103  TFile * f = TFile::Open(outputFilename.data());
104  TTree * t = (TTree *)f->Get("ndh");
105  THnSparse * ss = nullptr;
106  t->SetBranchAddress("h", &ss);
107 
108  for (Int_t i = 0; i < t->GetEntries(); i++) {
109  t->GetEntry(i);
110  ss->Print();
111  }
112 
113  t->GetUserInfo()->Print();
114 
115  return 0;
116 }
void SetOutputFileName(const char *fn)
Setting output file name.
Definition: HnSparse.h:30