-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathMCEventHeader.cxx
More file actions
134 lines (114 loc) · 4.19 KB
/
MCEventHeader.cxx
File metadata and controls
134 lines (114 loc) · 4.19 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
// 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.
/// \author R+Preghenella - August 2017
#include "SimulationDataFormat/MCEventHeader.h"
#include "FairRootManager.h"
#include <TFile.h>
#include <TTree.h>
#include <iostream>
namespace o2
{
namespace dataformats
{
/*****************************************************************/
/*****************************************************************/
void MCEventHeader::Reset()
{
/** reset **/
FairMCEventHeader::Reset();
clearInfo();
mEmbeddingFileName.clear();
mEmbeddingEventIndex = 0;
}
void MCEventHeader::extractFileFromKinematics(std::string_view kinefilename, std::string_view targetfilename)
{
auto oldfile = TFile::Open(kinefilename.data());
auto kinetree = (TTree*)oldfile->Get("o2sim");
// deactivate all branches
kinetree->SetBranchStatus("*", 0);
// activate the header branch
kinetree->SetBranchStatus("MCEventHeader*", 1);
// create a new file + a clone of old tree header. Do not copy events
auto newfile = TFile::Open(targetfilename.data(), "RECREATE");
auto newtree = kinetree->CloneTree(0);
// here we copy the branches
newtree->CopyEntries(kinetree, kinetree->GetEntries());
newtree->SetEntries(kinetree->GetEntries());
// flush to disk
newtree->Write();
newfile->Close();
delete newfile;
// clean
if (oldfile) {
oldfile->Close();
delete oldfile;
}
}
void MCEventHeader::print() const
{
// print some used parts from FairMCEventHeader
std::cout << "RunID " << fRunId << "\n";
std::cout << "EventID " << fEventId << "\n";
std::cout << "Vertex-X " << fX << "\n";
std::cout << "Vertex-Y " << fY << "\n";
std::cout << "Vertex-Z " << fZ << "\n";
std::cout << "Vertex-T " << fT << "\n";
std::cout << "Impact-B " << fB << "\n";
std::cout << "NPrim " << fNPrim << "\n";
// print meta-fields
printInfo();
}
/** alternative implementations below
void MCEventHeader::extractFileFromKinematics(std::string_view kinefilename, std::string_view targetfilename) {
TFile kinefile(kinefilename.data(), "OPEN");
auto kinetree = static_cast<TTree*>(kinefile.Get("o2sim")); // belongs to "kinefile"
kinetree->SetBranchStatus("*", 0);
// activate header branch
kinetree->SetBranchStatus("MCEventHeader*", 1);
std::unique_ptr<TFile> headeronlyfile{TFile::Open(targetfilename.data(), "RECREATE")};
auto headeronlytree = kinetree->CloneTree(0);
// copy the branches
headeronlytree->CopyEntries(kinetree, kinetree->GetEntries());
headeronlytree->SetEntries(kinetree->GetEntries());
// flush to disc
headeronlytree->Write();
}
void MCEventHeader::extractFileFromKinematics(std::string_view kinefilename, std::string_view targetfilename)
{
TFile kinefile(kinefilename.data(), "OPEN");
auto kinetree = static_cast<TTree*>(kinefile.Get("o2sim")); // owned by "kinefile"
auto headerbranchorig = kinetree->GetBranch("MCEventHeader.");
if (!headerbranchorig) {
return;
}
std::unique_ptr<TFile> headeronlyfile{TFile::Open(targetfilename.data(), "RECREATE")};
auto headeronlytree = new TTree("o2sim", "o2sim"); // owned by headeronlyfile
MCEventHeader* header = nullptr;
auto targetBranch = headeronlytree->Branch("MCEventHeader.", &header);
headerbranchorig->SetAddress(&header);
for (auto entry = 0; entry < kinetree->GetEntries(); ++entry) {
headerbranchorig->GetEntry(entry);
targetBranch->Fill();
if (header) {
delete header;
header = nullptr;
}
}
headeronlytree->SetEntries(kinetree->GetEntries());
// flush to disc
headeronlytree->Write();
}
*/
/*****************************************************************/
/*****************************************************************/
} /* namespace dataformats */
} /* namespace o2 */
ClassImp(o2::dataformats::MCEventHeader);