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