-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgen_cmsraw.cxx
97 lines (84 loc) · 2.66 KB
/
gen_cmsraw.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <ROOT/RField.hxx>
#include <ROOT/RNTuple.hxx>
#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleOptions.hxx>
#include <TBranch.h>
#include <TCanvas.h>
#include <TChain.h>
#include <TFile.h>
#include <TH1F.h>
#include <TLeaf.h>
#include <TTree.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>
#include <TSystem.h>
#include <cassert>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <unistd.h>
#include "util.h"
// Import classes from experimental namespace for the time being
using RNTupleModel = ROOT::Experimental::RNTupleModel;
using RFieldBase = ROOT::Experimental::Detail::RFieldBase;
using RNTupleWriter = ROOT::Experimental::RNTupleWriter;
using RNTupleWriteOptions = ROOT::Experimental::RNTupleWriteOptions;
void Usage(char *progname) {
std::cout << "Usage: " << progname << " -o <ntuple output dir> -c <compression> -o <tree input>"
<< std::endl;
}
int main(int argc, char **argv) {
std::string inputPath;
std::string outputDir;
int compressionSettings = 0;
std::string compressionShorthand = "none";
int c;
while ((c = getopt(argc, argv, "hvo:c:i:")) != -1) {
switch (c) {
case 'h':
case 'v':
Usage(argv[0]);
return 0;
case 'o':
outputDir = optarg;
break;
case 'c':
compressionSettings = GetCompressionSettings(optarg);
compressionShorthand = optarg;
break;
case 'i':
inputPath = optarg;
break;
default:
fprintf(stderr, "Unknown option: -%c\n", c);
Usage(argv[0]);
return 1;
}
}
if (inputPath.empty() || outputDir.empty()) {
Usage(argv[0]);
return 1;
}
std::string outputFile = outputDir + "/cmsraw~" + compressionShorthand + ".ntuple";
std::cout << "Converting " << inputPath << " --> " << outputFile << std::endl;
auto file = TFile::Open(inputPath.c_str());
auto tree = file->Get<TTree>("Events");
auto model = RNTupleModel::Create();
auto vNtuple = model->MakeField<std::vector<std::vector<unsigned char>>>("v");
RNTupleWriteOptions options;
options.SetCompression(compressionSettings);
options.SetNumElementsPerPage(100000);
auto ntuple = RNTupleWriter::Recreate(std::move(model), "Events", outputFile, options);
TTreeReader reader(tree);
TTreeReaderValue<std::vector<std::vector<unsigned char>>> vTree(reader, "v");
// Fills the ntuple with entries from the TTree.
int count = 0;
while(reader.Next()) {
*vNtuple = *vTree;
ntuple->Fill();
if (++count % 1000 == 0)
std::cout << "Wrote " << count << " events" << std::endl;
}
}