ndmspc  0.20250128.0
ndh-gen.cxx
1 #include <getopt.h>
2 #include <TFile.h>
3 #include <TH2.h>
4 #include "HnSparse.h"
5 #include "HnSparseStress.h"
6 #include "ndmspc.h"
7 
8 void version()
9 {
10  Printf("%s v%d.%d.%d-%s", NDMSPC_NAME, NDMSPC_VERSION_MAJOR(NDMSPC_VERSION), NDMSPC_VERSION_MINOR(NDMSPC_VERSION),
11  NDMSPC_VERSION_PATCH(NDMSPC_VERSION), NDMSPC_VERSION_RELEASE);
12 }
13 
14 [[noreturn]] void help(int rc = 0)
15 {
16  version();
17  Printf("\nUsage: [OPTION]...");
18  Printf("\nOptions:");
19  Printf(" -d, --dimentsions[=VALUE] number of dimensions (default: 2)");
20  Printf(" -b, --bins[=VALUE] number of bins per axis (default: 5)");
21  Printf(" -f, --fill[=VALUE] fill size (default : 1e5)");
22  Printf(" -s, --start[=VALUE] start (default : 0)");
23  Printf(" -r, --reserve[=VALUE] reserve bins (default : 0 - nothing is reserved)");
24  Printf(" -p, --print-refresh[=VALUE] print refresh (default : 1)");
25  Printf(" -c, --chunk[=VALUE] chunk size (default : 1024*16)");
26  Printf(" -o, --output[=VALUE] output filename (default: \"\")");
27  Printf(" -z, --fill-random fill random");
28  Printf("\n -h, --help display this help and exit");
29  Printf(" -v, --version output version information and exit");
30  Printf(" -x, --debug[=VALUE] debug level");
31  Printf("\nExamples:");
32  Printf(" %s-gen -s 1e5", NDMSPC_NAME);
33  Printf(" Generate default histogram with 1e5 entries");
34  Printf("\nReport bugs at <https://gitlab.com/ndmspc/ndmspc>");
35  Printf("General help using GNU software: <https://www.gnu.org/gethelp/>");
36 
37  exit(rc);
38 }
39 int main(int argc, char ** argv)
40 {
41 
42  // ***** Default values START *****
44  Int_t debug = 0;
45  Int_t nPrintRefresh = 1;
46  std::string filename = "";
47 
48  int nDim = 2;
49  int nBins = 5;
50  Long64_t nFill = 1e5;
51  Long64_t startFill = 0;
52  std::string start_str;
53  Int_t chunkSize = 1024 * 16;
54  Long64_t nBinsReserved = 0;
55  bool fillRandom = false;
56  // ***** Default values END *****
57 
58  std::string shortOpts = "hvzd:b:f:s:o:x:r:p:c:W;";
59  struct option longOpts[] = {{"help", no_argument, nullptr, 'h'},
60  {"version", no_argument, nullptr, 'v'},
61  {"dims", required_argument, nullptr, 'd'},
62  {"bins", required_argument, nullptr, 'b'},
63  {"fill", required_argument, nullptr, 'f'},
64  {"start", required_argument, nullptr, 's'},
65  {"fill-random", no_argument, nullptr, 'z'},
66  {"output", required_argument, nullptr, 'o'},
67  {"debug", required_argument, nullptr, 'x'},
68  {"reserve", required_argument, nullptr, 'r'},
69  {"print-refresh", required_argument, nullptr, 'p'},
70  {"chunk", required_argument, nullptr, 'c'},
71  {nullptr, 0, nullptr, 0}};
72 
73  int nextOption = 0;
74  do {
75  nextOption = getopt_long(argc, argv, shortOpts.c_str(), longOpts, nullptr);
76  switch (nextOption) {
77  case -1:
78  case 0: break;
79  case 'h': help();
80  case 'v':
81  version();
82  exit(0);
83  break;
84  case 'd': nDim = atoi(optarg); break;
85  case 'b': nBins = atoi(optarg); break;
86  case 'f': nFill = (Long64_t)atof(optarg); break;
87  case 's': start_str = optarg; break;
88  case 'o': filename = optarg; break;
89  case 'x': debug = atoi(optarg); break;
90  case 'z': fillRandom = true; break;
91  case 'r': nBinsReserved = (Long64_t)atof(optarg); break;
92  case 'p': nPrintRefresh = (Int_t)atof(optarg); break;
93  case 'c': chunkSize = (Int_t)atof(optarg); break;
94  default: help(1);
95  }
96  } while (nextOption != -1);
97 
98  // Handling start (supports 1x, 2x, ...)
99  if (!start_str.empty()) {
100  if (start_str[start_str.length() - 1] == 'x') {
101  start_str.pop_back();
102  startFill = (Long64_t)(atof(start_str.data()) * nFill);
103  }
104  else {
105  startFill = (Long64_t)atof(start_str.data());
106  }
107  }
108 
109  version();
110 
111  double min = -(Double_t)nBins / 2;
112  double max = (Double_t)nBins / 2;
113 
114  Int_t bins[nDim];
115  Double_t mins[nDim];
116  Double_t maxs[nDim];
117  for (Int_t i = 0; i < nDim; i++) {
118  bins[i] = nBins;
119  mins[i] = min;
120  maxs[i] = max;
121  }
122 
124  new Ndmspc::Ndh::HnSparseD("hTest", "Testing histogram", nDim, bins, mins, maxs, chunkSize);
125  if (nBinsReserved) h->ReserveBins(nBinsReserved);
127  stress.SetDebugLevel(debug);
128  stress.SetPrintRefresh(nPrintRefresh);
129  stress.SetRandomFill(fillRandom);
130  Printf("Starting to fill at %lld random=%d...", startFill, fillRandom);
131  if (!stress.Generate(h, nFill, startFill)) return 1;
132  h->Print();
133  Long64_t nBinsSizeBytes = sizeof(Double_t) * h->GetNbins();
134 
135  if (!filename.empty()) {
136  Printf("Saving output to file '%s' ...", filename.data());
137  TFile * f = TFile::Open(filename.data(), "RECREATE");
138  h->Write();
139  Printf("Memory : %03.2f MB (%lld B) File: %03.2f MB (%lld B)", (double)nBinsSizeBytes / (1024 * 1024),
140  nBinsSizeBytes, (double)f->GetFileBytesWritten() / (1024 * 1024), f->GetFileBytesWritten());
141  f->Close();
142  }
143  else {
144  Printf("Memory : %03.2f MB (%lld B)", (double)nBinsSizeBytes / (1024 * 1024), nBinsSizeBytes);
145  }
146 
147  return 0;
148 }
HnSparseStress object.
void SetPrintRefresh(Int_t n)
Setting print refresh.
virtual Bool_t Generate(THnSparse *h, Long64_t size=1e3, Long64_t start=1e3)
void SetRandomFill(bool rf)
Setting fill random flag.
void SetDebugLevel(Int_t debug)
Setting debug level.
void ReserveBins(Long64_t nBins)
Definition: HnSparse.cxx:145