Project
Loading...
Searching...
No Matches
PrimaryVertexReaderSpec.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13
14#include <vector>
15
18#include "Framework/Logger.h"
21#include "TFile.h"
22#include "TTree.h"
28
29using namespace o2::framework;
30
31namespace o2
32{
33namespace vertexing
34{
35
36// read primary vertices produces by the o2-primary-vertexing-workflow
38{
39 using Label = o2::MCEventLabel;
43
44 public:
45 PrimaryVertexReader(bool useMC) : mUseMC(useMC) {}
46 ~PrimaryVertexReader() override = default;
47 void init(o2::framework::InitContext& ic) final;
49
50 protected:
51 void connectTree();
52
53 int mVerbose = 0;
54 bool mUseMC = false;
55
56 std::vector<PVertex> mVertices, *mVerticesPtr = &mVertices;
57 std::vector<Label> mLabels, *mLabelsPtr = &mLabels;
59 std::vector<GIndex> mPV2MatchIdx, *mPV2MatchIdxPtr = &mPV2MatchIdx;
60
61 std::unique_ptr<TFile> mFile;
62 std::unique_ptr<TTree> mTree;
63 std::string mFileName = "";
64 std::string mFileNameMatches = "";
65 std::string mVertexTreeName = "o2sim";
66 std::string mVertexBranchName = "PrimaryVertex";
67 std::string mVertexTrackIDsBranchName = "PVTrackIndices";
68 std::string mVertex2TrackIDRefsBranchName = "PV2TrackRefs";
69 std::string mVertexLabelsBranchName = "PVMCTruth";
70};
71
73{
75 ic.options().get<std::string>("primary-vertex-infile"));
76 mVerbose = ic.options().get<int>("vertex-verbosity");
78}
79
81{
82 auto ent = mTree->GetReadEntry() + 1;
83 assert(ent < mTree->GetEntries()); // this should not happen
84 mTree->GetEntry(ent);
85 LOG(info) << "Pushing " << mVerticesPtr->size() << " vertices at entry " << ent;
86
87 pc.outputs().snapshot(Output{"GLO", "PVTX", 0}, mVertices);
88 pc.outputs().snapshot(Output{"GLO", "PVTX_TRMTC", 0}, mPV2MatchIdx);
89 pc.outputs().snapshot(Output{"GLO", "PVTX_TRMTCREFS", 0}, mPV2MatchIdxRef);
90
91 if (mUseMC) {
92 pc.outputs().snapshot(Output{"GLO", "PVTX_MCTR", 0}, mLabels);
93 }
94
95 if (mVerbose) {
96 size_t nrec = mPV2MatchIdxRef.size();
97 for (size_t cnt = 0; cnt < nrec; cnt++) {
98 if (cnt < mVertices.size()) {
99 const auto& vtx = mVertices[cnt];
100 Label lb;
101 if (mUseMC) {
102 lb = mLabels[cnt];
103 }
104 LOG(info) << "#" << cnt << " " << mVertices[cnt] << " | MC:" << lb.asString();
105 } else {
106 LOG(info) << "#" << cnt << " this is not a vertex";
107 }
108 LOG(info) << "References: " << mPV2MatchIdxRef[cnt];
109 for (int is = 0; is < GIndex::NSources; is++) {
110 int ncontrib = 0, nambig = 0;
111 int idMin = mPV2MatchIdxRef[cnt].getFirstEntryOfSource(is), idMax = idMin + mPV2MatchIdxRef[cnt].getEntriesOfSource(is);
112 for (int i = idMin; i < idMax; i++) {
113 if (mPV2MatchIdx[i].isPVContributor()) {
114 ncontrib++;
115 } else if (mPV2MatchIdx[i].isAmbiguous()) {
116 nambig++;
117 }
118 }
119 if (mPV2MatchIdxRef[cnt].getEntriesOfSource(is)) {
120 LOGP(info, "{} : total attached: {}, contributors: {}, ambiguous: {}", GIndex::getSourceName(is), mPV2MatchIdxRef[cnt].getEntriesOfSource(is), ncontrib, nambig);
121 }
122 if (mVerbose < 2) {
123 continue;
124 }
125 std::string trIDs;
126 int cntT = 0;
127 for (int i = idMin; i < idMax; i++) {
128 if (mVerbose > 2 || mPV2MatchIdx[i].isPVContributor()) {
129 trIDs += mPV2MatchIdx[i].asString() + " ";
130 if (!((++cntT) % 15)) {
131 LOG(info) << trIDs;
132 trIDs = "";
133 }
134 }
135 }
136 if (!trIDs.empty()) {
137 LOG(info) << trIDs;
138 }
139 }
140 }
141 }
142
143 if (mTree->GetReadEntry() + 1 >= mTree->GetEntries()) {
145 pc.services().get<ControlService>().readyToQuit(QuitRequest::Me);
146 }
147}
148
150{
151 mTree.reset(nullptr); // in case it was already loaded
152 mFile.reset(TFile::Open(mFileName.c_str()));
153 assert(mFile && !mFile->IsZombie());
154 mTree.reset((TTree*)mFile->Get(mVertexTreeName.c_str()));
155 assert(mTree);
156 assert(mTree->GetBranch(mVertexBranchName.c_str()));
157 assert(mTree->GetBranch(mVertexTrackIDsBranchName.c_str()));
158 assert(mTree->GetBranch(mVertex2TrackIDRefsBranchName.c_str()));
159
160 mTree->SetBranchAddress(mVertexBranchName.c_str(), &mVerticesPtr);
161 mTree->SetBranchAddress(mVertexTrackIDsBranchName.c_str(), &mPV2MatchIdxPtr);
162 mTree->SetBranchAddress(mVertex2TrackIDRefsBranchName.c_str(), &mPV2MatchIdxRefPtr);
163
164 if (mUseMC) {
165 assert(mTree->GetBranch(mVertexLabelsBranchName.c_str()));
166 mTree->SetBranchAddress(mVertexLabelsBranchName.c_str(), &mLabelsPtr);
167 }
168
169 LOG(info) << "Loaded " << mVertexTreeName << " tree from " << mFileName << " with " << mTree->GetEntries() << " entries";
170}
171
173{
174 std::vector<OutputSpec> outputs;
175 outputs.emplace_back("GLO", "PVTX", 0, Lifetime::Timeframe);
176 outputs.emplace_back("GLO", "PVTX_TRMTC", 0, Lifetime::Timeframe);
177 outputs.emplace_back("GLO", "PVTX_TRMTCREFS", 0, Lifetime::Timeframe);
178
179 if (useMC) {
180 outputs.emplace_back("GLO", "PVTX_MCTR", 0, Lifetime::Timeframe);
181 }
182
183 return DataProcessorSpec{
184 "primary-vertex-reader",
185 Inputs{},
186 outputs,
187 AlgorithmSpec{adaptFromTask<PrimaryVertexReader>(useMC)},
188 Options{
189 {"primary-vertex-infile", VariantType::String, "o2_primary_vertex.root", {"Name of the input primary vertex file"}},
190 {"vertex-track-matches-infile", VariantType::String, "o2_pvertex_track_matches.root", {"Name of the input file with primary vertex - tracks matches"}},
191 {"input-dir", VariantType::String, "none", {"Input directory"}},
192 {"vertex-verbosity", VariantType::Int, 0, {"Print vertex/tracks info: 1) number of contributor and attached, 2) dump contributors 3) full dump"}}}};
193}
194
195} // namespace vertexing
196} // namespace o2
int32_t i
Definition of the Names Generator class.
Extention of GlobalTrackID by flags relevant for verter-track association.
Referenc on track indices contributing to the vertex, with possibility chose tracks from specific sou...
std::string asString() const
void snapshot(const Output &spec, T const &object)
ConfigParamRegistry const & options()
Definition InitContext.h:33
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
ServiceRegistryRef services()
The services registry associated with this processing context.
virtual void endOfStream(EndOfStreamContext &context)
This is invoked whenever we have an EndOfStream event.
Definition Task.h:43
~PrimaryVertexReader() override=default
void run(o2::framework::ProcessingContext &pc) final
void init(o2::framework::InitContext &ic) final
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
std::vector< ConfigParamSpec > Options
std::vector< InputSpec > Inputs
o2::framework::DataProcessorSpec getPrimaryVertexReaderSpec(bool useMC)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
static std::string rectifyDirectory(const std::string_view p)
static std::string concat_string(Ts const &... ts)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"