BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
miniDST.C
Go to the documentation of this file.
1R__ADD_INCLUDE_PATH($VMCWORKDIR)
2#include <services/json.hpp>
3
4using json = nlohmann::json;
5using namespace ROOT;
6
7int mdst_converter(TString inFilePath,
8 TString outFilePath = "",
9 TString configFileName = "mdst_config.json",
10 TString outFormat = "root",
11 Int_t threadsNumber = -1)
12{
13 TStopwatch timer;
14 timer.Start();
15
16 // form output file path if it is empty or a directory
17 gSystem->ExpandPathName(inFilePath);
18 if (outFilePath == "") {
19 outFilePath = inFilePath;
20 outFilePath.ReplaceAll("dst", "mdst");
21 } else
22 gSystem->ExpandPathName(outFilePath);
23 if (BmnFunctionSet::isDirectory(outFilePath))
24 outFilePath = gSystem->ConcatFileName(
25 outFilePath, ((TString)gSystem->BaseName(inFilePath.Data())).ReplaceAll("dst", "mdst"));
26 if (outFilePath == inFilePath) {
27 cout << "[ERROR] Output file path is not set correctly: " << outFilePath << endl;
28 return -1;
29 }
30
31 // read configuration variables
32 configFileName = gSystem->ExpandPathName("$VMCWORKDIR/config/" + configFileName);
33 ifstream configFile(configFileName.Data());
34 if (!configFile.is_open()) {
35 cout << "[ERROR] Cannot open config file " << configFileName << endl;
36 return -2;
37 }
38 json config;
39 try {
40 configFile >> config;
41 } catch (const json::parse_error& ex) {
42 cout << "[ERROR] Wrong config file format: " << ex.what() << endl;
43 return -3;
44 }
45 Int_t minNTracks = config.value("minNTracks", 2);
46 Double_t minZ = config.value("minZ", -TMath::Infinity());
47 Double_t maxZ = config.value("maxZ", TMath::Infinity());
48 vector<string> excludedBranches = config.value("excludedBranches", vector<string>());
49
50 if (outFormat != "root") {
51 cout << "[ERROR] Only \"root\" format is supported at the moment" << endl;
52 return -4;
53 }
54
55 if (threadsNumber >= 0)
56 ROOT::EnableImplicitMT(threadsNumber);
57
58 TFile* inFile = TFile::Open(inFilePath.Data());
59 if (inFile == nullptr) {
60 cout << "[ERROR] Cannot open input DST file " << inFilePath << endl;
61 return -5;
62 }
63 TTree* inTree = (TTree*)inFile->Get("bmndata");
64 if (!inTree) {
65 cout << "[ERROR] Input file " << inFilePath << " does not contain \"bmndata\" tree" << endl;
66 return -6;
67 }
68
69 // exclude branches
70 vector<string> branches;
71 for (TObject* branch : *inTree->GetListOfBranches()) {
72 string branchName = ((TBranch*)branch)->GetName();
73 if (find(excludedBranches.begin(), excludedBranches.end(), branchName) == excludedBranches.end())
74 branches.push_back(branchName);
75 }
76
77 // lamda to filter events
78 auto filter = [minNTracks, minZ, maxZ](const TClonesArray& vertices) {
79 BmnVertex* vtx = static_cast<BmnVertex*>(vertices.UncheckedAt(0));
80 return (vtx->GetZ() > minZ && vtx->GetZ() < maxZ && vtx->GetNTracks() >= minNTracks);
81 };
82
83 RDataFrame data(*inTree);
84 auto eventsCountBeforeFilter = data.Count();
85 auto filteredData = data.Filter(filter, {"BmnVertex"});
86 auto eventsCountAfterFilter = filteredData.Count();
87
88 if (BmnFunctionSet::CreateDirectoryTree(outFilePath, 1) < 0) {
89 cout << "[ERROR] Output file " << outFilePath << " cannot be created" << endl;
90 return -7;
91 }
92 filteredData.Snapshot("bmndata", outFilePath.Data(), branches);
93
94 cout << "Event number before filtering: " << *eventsCountBeforeFilter << endl;
95 cout << "Event number after filtering: " << *eventsCountAfterFilter << endl;
96 delete inFile;
97
98 timer.Stop();
99 Double_t rtime = timer.RealTime(), ctime = timer.CpuTime();
100 printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
101 return 0;
102}
int Int_t
double Double_t
#define R__ADD_INCLUDE_PATH(PATH)
R__EXTERN TSystem * gSystem
static int CreateDirectoryTree(TString &fileName, int iVerbose=0, EAccessMode mode=kWritePermission)
static bool isDirectory(TString path)
Int_t GetNTracks() const
Definition BmnVertex.h:60
Double_t GetZ() const
Definition BmnVertex.h:57
RResultPtr< ULong64_t > Count()
RInterface< RDFDetail::RFilter< F, Proxied >, DS_t > Filter(F f, const ColumnNames_t &columns={}, std::string_view name="")
T * Get(const char *namecycle)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
Double_t CpuTime()
void Stop()
const char * Data() const
TString & ReplaceAll(const char *s1, const char *s2)
virtual TObjArray * GetListOfBranches()
int mdst_converter(TString inFilePath, TString outFilePath="", TString configFileName="mdst_config.json", TString outFormat="root", Int_t threadsNumber=-1)
Definition miniDST.C:7
void EnableImplicitMT(ROOT::EIMTConfig config)
Double_t Infinity()
basic_json<> json
default specialization
Definition json.hpp:3365