-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathextractCTF.C
More file actions
181 lines (149 loc) · 4.96 KB
/
extractCTF.C
File metadata and controls
181 lines (149 loc) · 4.96 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#if !defined(__CLING__) || defined(__ROOTCLING__)
#include "DetectorsCommonDataFormats/EncodedBlocks.h"
#include "CommonUtils/NameConf.h"
#include "DetectorsCommonDataFormats/CTFHeader.h"
#include "DataFormatsITSMFT/CTF.h"
#include "DataFormatsTPC/CTF.h"
#include "DataFormatsTRD/CTF.h"
#include "DataFormatsFT0/CTF.h"
#include "DataFormatsFV0/CTF.h"
#include "DataFormatsFDD/CTF.h"
#include "DataFormatsTOF/CTF.h"
#include "DataFormatsMID/CTF.h"
#include "DataFormatsMCH/CTF.h"
#include "DataFormatsEMCAL/CTF.h"
#include "DataFormatsPHOS/CTF.h"
#include "DataFormatsCPV/CTF.h"
#include "DataFormatsZDC/CTF.h"
#include "DataFormatsHMP/CTF.h"
#include "DataFormatsCTP/CTF.h"
#endif
using DetID = o2::detectors::DetID;
template <typename T>
bool readFromTree(TTree& tree, const std::string brname, T& dest, int ev = 0)
{
auto* br = tree.GetBranch(brname.c_str());
if (br && br->GetEntries() > ev) {
auto* ptr = &dest;
br->SetAddress(&ptr);
br->GetEntry(ev);
br->ResetAddress();
return true;
}
return false;
}
template <typename C>
void writeToTree(TTree& tree, const std::vector<o2::ctf::BufferType>& buff, DetID det)
{
const auto ctfImage = C::getImage(buff.data());
ctfImage.appendToTree(tree, det.getName());
}
template <typename T>
size_t appendToTree(TTree& tree, const std::string brname, T& ptr)
{
size_t s = 0;
auto* br = tree.GetBranch(brname.c_str());
auto* pptr = &ptr;
if (br) {
br->SetAddress(&pptr);
} else {
br = tree.Branch(brname.c_str(), &pptr);
}
int res = br->Fill();
if (res < 0) {
throw std::runtime_error(fmt::format("Failed to fill CTF branch {}", brname));
}
s += res;
br->ResetAddress();
return s;
}
template <typename C>
void extractDetCTF(int ctfID, DetID det, TTree& treeIn, TTree& treeOut)
{
std::vector<o2::ctf::BufferType> buff;
buff.resize(sizeof(C));
C::readFromTree(buff, treeIn, det.getName(), ctfID);
writeToTree<C>(treeOut, buff, det);
}
void extractCTF(int ctfID,
const std::string& fnameIn,
const std::string& fnameOut,
const std::string selDet = "all")
{
std::unique_ptr<TFile> flIn(TFile::Open(fnameIn.c_str()));
std::unique_ptr<TTree> treeIn((TTree*)flIn->Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()));
if (treeIn->GetEntries() <= ctfID) {
LOG(error) << "File " << fnameIn << " has only " << treeIn->GetEntries() << " entries, requested : " << ctfID;
treeIn.reset();
return;
}
o2::ctf::CTFHeader ctfHeader;
if (!readFromTree(*treeIn, "CTFHeader", ctfHeader, ctfID)) {
throw std::runtime_error("did not find CTFHeader");
}
LOG(info) << ctfHeader;
DetID::mask_t detsTF = ctfHeader.detectors & DetID::getMask(selDet);
if (detsTF.none()) {
LOG(error) << "Nothing is selected with mask " << selDet << " CTF constains " << DetID::getNames(ctfHeader.detectors);
treeIn.reset();
return;
}
ctfHeader.detectors = detsTF;
TFile flOut(fnameOut.c_str(), "recreate");
std::unique_ptr<TTree> treeOut = std::make_unique<TTree>(std::string(o2::base::NameConf::CTFTREENAME).c_str(), "O2 CTF tree");
if (detsTF[DetID::ITS]) {
extractDetCTF<o2::itsmft::CTF>(ctfID, DetID::ITS, *treeIn, *treeOut);
}
if (detsTF[DetID::MFT]) {
extractDetCTF<o2::itsmft::CTF>(ctfID, DetID::MFT, *treeIn, *treeOut);
}
if (detsTF[DetID::TPC]) {
extractDetCTF<o2::tpc::CTF>(ctfID, DetID::TPC, *treeIn, *treeOut);
}
if (detsTF[DetID::TRD]) {
extractDetCTF<o2::trd::CTF>(ctfID, DetID::TRD, *treeIn, *treeOut);
}
if (detsTF[DetID::TOF]) {
extractDetCTF<o2::tof::CTF>(ctfID, DetID::TOF, *treeIn, *treeOut);
}
if (detsTF[DetID::FT0]) {
extractDetCTF<o2::ft0::CTF>(ctfID, DetID::FT0, *treeIn, *treeOut);
}
if (detsTF[DetID::FV0]) {
extractDetCTF<o2::fv0::CTF>(ctfID, DetID::FV0, *treeIn, *treeOut);
}
if (detsTF[DetID::FDD]) {
extractDetCTF<o2::fdd::CTF>(ctfID, DetID::FDD, *treeIn, *treeOut);
}
if (detsTF[DetID::MCH]) {
extractDetCTF<o2::mch::CTF>(ctfID, DetID::MCH, *treeIn, *treeOut);
}
if (detsTF[DetID::MID]) {
extractDetCTF<o2::mid::CTF>(ctfID, DetID::MID, *treeIn, *treeOut);
}
if (detsTF[DetID::ZDC]) {
extractDetCTF<o2::zdc::CTF>(ctfID, DetID::ZDC, *treeIn, *treeOut);
}
if (detsTF[DetID::EMC]) {
extractDetCTF<o2::emcal::CTF>(ctfID, DetID::EMC, *treeIn, *treeOut);
}
if (detsTF[DetID::PHS]) {
extractDetCTF<o2::phos::CTF>(ctfID, DetID::PHS, *treeIn, *treeOut);
}
if (detsTF[DetID::CPV]) {
extractDetCTF<o2::cpv::CTF>(ctfID, DetID::CPV, *treeIn, *treeOut);
}
if (detsTF[DetID::HMP]) {
extractDetCTF<o2::hmpid::CTF>(ctfID, DetID::HMP, *treeIn, *treeOut);
}
if (detsTF[DetID::CTP]) {
extractDetCTF<o2::ctp::CTF>(ctfID, DetID::CTP, *treeIn, *treeOut);
}
appendToTree(*treeOut, "CTFHeader", ctfHeader);
treeOut->SetEntries(1);
LOG(info) << "Wrote CTFs of entry " << ctfID << " for " << DetID::getNames(ctfHeader.detectors) << " to " << fnameOut;
treeOut->Write();
treeOut.reset();
flOut.Close();
treeIn.reset();
}