ndmspc  0.20240624.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[] = {
60  {"help", no_argument, nullptr, 'h'}, {"version", no_argument, nullptr, 'v'},
61  {"dims", required_argument, nullptr, 'd'}, {"bins", required_argument, nullptr, 'b'},
62  {"fill", required_argument, nullptr, 'f'}, {"start", required_argument, nullptr, 's'},
63  {"fill-random", no_argument, nullptr, 'z'},
64  {"output", required_argument, nullptr, 'o'}, {"debug", required_argument, nullptr, 'x'},
65  {"reserve", required_argument, nullptr, 'r'}, {"print-refresh", required_argument, nullptr, 'p'},
66  {"chunk", required_argument, nullptr, 'c'}, {nullptr, 0, nullptr, 0}};
67 
68  int nextOption = 0;
69  do {
70  nextOption = getopt_long(argc, argv, shortOpts.c_str(), longOpts, nullptr);
71  switch (nextOption) {
72  case -1:
73  case 0: break;
74  case 'h': help();
75  case 'v':
76  version();
77  exit(0);
78  break;
79  case 'd': nDim = atoi(optarg); break;
80  case 'b': nBins = atoi(optarg); break;
81  case 'f': nFill = (Long64_t)atof(optarg); break;
82  case 's': start_str = optarg; break;
83  case 'o': filename = optarg; break;
84  case 'x': debug = atoi(optarg); break;
85  case 'z': fillRandom = true; break;
86  case 'r': nBinsReserved = (Long64_t)atof(optarg); break;
87  case 'p': nPrintRefresh = (Int_t)atof(optarg); break;
88  case 'c': chunkSize = (Int_t)atof(optarg); break;
89  default: help(1);
90  }
91  } while (nextOption != -1);
92 
93  // Handling start (supports 1x, 2x, ...)
94  if (!start_str.empty()) {
95  if (start_str[start_str.length() - 1] == 'x') {
96  start_str.pop_back();
97  startFill = (Long64_t)(atof(start_str.data()) * nFill);
98  }
99  else {
100  startFill = (Long64_t)atof(start_str.data());
101  }
102  }
103 
104  version();
105 
106  double min = -(Double_t)nBins / 2;
107  double max = (Double_t)nBins / 2;
108 
109  Int_t bins[nDim];
110  Double_t mins[nDim];
111  Double_t maxs[nDim];
112  for (Int_t i = 0; i < nDim; i++) {
113  bins[i] = nBins;
114  mins[i] = min;
115  maxs[i] = max;
116  }
117 
118  NDH::HnSparseD * h = new NDH::HnSparseD("hTest", "Testing histogram", nDim, bins, mins, maxs, chunkSize);
119  if (nBinsReserved) h->ReserveBins(nBinsReserved);
120  NDH::HnSparseStress stress;
121  stress.SetDebugLevel(debug);
122  stress.SetPrintRefresh(nPrintRefresh);
123  stress.SetRandomFill(fillRandom);
124  Printf("Starting to fill at %lld random=%d...", startFill, fillRandom);
125  if (!stress.Generate(h, nFill, startFill)) return 1;
126  h->Print();
127  Long64_t nBinsSizeBytes = sizeof(Double_t) * h->GetNbins();
128 
129  if (!filename.empty()) {
130  Printf("Saving output to file '%s' ...", filename.data());
131  TFile * f = TFile::Open(filename.data(), "RECREATE");
132  h->Write();
133  Printf("Memory : %03.2f MB (%lld B) File: %03.2f MB (%lld B)", (double)nBinsSizeBytes / (1024 * 1024),
134  nBinsSizeBytes, (double)f->GetFileBytesWritten() / (1024 * 1024), f->GetFileBytesWritten());
135  f->Close();
136  }
137  else {
138  Printf("Memory : %03.2f MB (%lld B)", (double)nBinsSizeBytes / (1024 * 1024), nBinsSizeBytes);
139  }
140 
141  return 0;
142 }
HnSparseStress object.
void SetRandomFill(bool rf)
Setting fill random flag.
virtual Bool_t Generate(THnSparse *h, Long64_t size=1e3, Long64_t start=1e3)
void SetDebugLevel(Int_t debug)
Setting debug level.
void SetPrintRefresh(Int_t n)
Setting print refresh.
void ReserveBins(Long64_t nBins)
Definition: HnSparse.cxx:145