forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrateSimFiles.C
More file actions
93 lines (80 loc) · 3.34 KB
/
migrateSimFiles.C
File metadata and controls
93 lines (80 loc) · 3.34 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#if !defined(__CLING__) || defined(__ROOTCLING__)
#include <TTree.h>
#include <DetectorsCommonDataFormats/DetectorNameConf.h>
#include <DetectorsCommonDataFormats/DetID.h>
#include <DetectorsCommonDataFormats/SimTraits.h>
#endif
// A macro with the purpose to produce
// simulation files in the new layout, where detector hits
// are stored in individual files.
//
// Applicable to the monolithic simulation output produced by o2-sim-serial -- until
// this is moved to the new scheme, too.
// this is a generic code to copy branches from one to another tree
void copyBranch(const char* originfile, const char* targetfile, std::vector<std::string> const& branchnames)
{
const char* TREENAME = "o2sim";
//Get old file, old tree and set top branch address
auto oldfile = TFile::Open(originfile);
TTree* oldtree = nullptr;
oldfile->GetObject(TREENAME, oldtree);
if (oldtree) {
// Deactivate all branches
oldtree->SetBranchStatus("*", 0);
// Activate the branches to be copied (our skim)
for (auto& br : branchnames) {
std::string regex = br + std::string("*");
oldtree->SetBranchStatus(regex.c_str(), 1);
}
//Create a new file + a clone of old tree header.
auto newfile = TFile::Open(targetfile, "RECREATE");
auto newtree = oldtree->CloneTree(0);
// Here we copy the branches
newtree->CopyEntries(oldtree, oldtree->GetEntries());
newtree->SetEntries(oldtree->GetEntries());
// Flush to disk
newtree->Write();
newfile->Close();
// delete newtree;
delete newfile;
}
if (oldfile) {
oldfile->Close();
delete oldfile;
}
}
void migrateSimFiles(const char* filebase = "o2sim")
{
// READ GRP AND ITERATE OVER DETECTED PARTS
auto grp = o2::parameters::GRPObject::loadFrom(filebase);
if (!grp) {
std::cerr << "No GRP found. Exiting\n";
}
// split off the kinematics file
std::string originalfilename = std::string(filebase) + std::string(".root");
auto kinematicsfile = o2::base::NameConf::getMCKinematicsFileName(filebase);
copyBranch(originalfilename.c_str(), kinematicsfile.c_str(), o2::detectors::SimTraits::KINEMATICSBRANCHES);
// split off additional MCHeaders file
std::vector<std::string> headerbranches = {"MCEventHeader"};
auto headersfile = o2::base::NameConf::getMCHeadersFileName(filebase);
copyBranch(originalfilename.c_str(), headersfile.c_str(), headerbranches);
// loop over all possible detectors
for (auto detid = o2::detectors::DetID::First; detid <= o2::detectors::DetID::Last; ++detid) {
if (!grp->isDetReadOut(detid)) {
continue;
}
// fetch possible sim branches for this detector and copy them
auto simbranches = o2::detectors::SimTraits::DETECTORBRANCHNAMES[detid];
copyBranch(originalfilename.c_str(), o2::base::DetectorNameConf::getHitsFileName(detid, filebase).c_str(), simbranches);
}
}