ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RootTrackParameterWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RootTrackParameterWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 #include <TFile.h>
13 #include <TTree.h>
14 #include <ios>
15 #include <iostream>
16 #include <stdexcept>
17 
20  : TrackParameterWriter(cfg.collection, "RootTrackParameterWriter", level),
21  m_cfg(cfg),
22  m_outputFile(cfg.rootFile) {
23  // An input collection name and tree name must be specified
24  if (m_cfg.collection.empty()) {
25  throw std::invalid_argument("Missing input collection");
26  } else if (m_cfg.treeName.empty()) {
27  throw std::invalid_argument("Missing tree name");
28  }
29 
30  // Setup ROOT I/O
31  if (m_outputFile == nullptr) {
32  m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
33  if (m_outputFile == nullptr) {
34  throw std::ios_base::failure("Could not open '" + m_cfg.filePath);
35  }
36  }
37  m_outputFile->cd();
38  m_outputTree = new TTree(m_cfg.treeName.c_str(), m_cfg.treeName.c_str());
39  if (m_outputTree == nullptr)
40  throw std::bad_alloc();
41  else {
42  // I/O parameters
43  m_outputTree->Branch("event_nr", &m_eventNr);
44  m_outputTree->Branch("d0", &m_d0);
45  m_outputTree->Branch("z0", &m_z0);
46  m_outputTree->Branch("phi", &m_phi);
47  m_outputTree->Branch("theta", &m_theta);
48  m_outputTree->Branch("qp", &m_qp);
49  // MORE HERE
50  }
51 }
52 
54  if (m_outputFile) {
55  m_outputFile->Close();
56  }
57 }
58 
60  if (m_outputFile) {
61  m_outputFile->cd();
62  m_outputTree->Write();
63  ACTS_INFO("Wrote trackparameters to tree '" << m_cfg.treeName << "' in '"
64  << m_cfg.filePath << "'");
65  }
66  return ProcessCode::SUCCESS;
67 }
68 
70  const FW::AlgorithmContext& ctx,
71  const std::vector<BoundTrackParameters>& trackParams) {
72  if (m_outputFile == nullptr)
73  return ProcessCode::SUCCESS;
74 
75  // Exclusive access to the tree while writing
76  std::lock_guard<std::mutex> lock(m_writeMutex);
77 
78  // Get the event number
79  m_eventNr = ctx.eventNumber;
80 
81  for (auto& params : trackParams) {
82  m_d0 = params.parameters()[0];
83  m_z0 = params.parameters()[1];
84  m_phi = params.parameters()[2];
85  m_theta = params.parameters()[3];
86  m_qp = params.parameters()[4];
87 
88  m_outputTree->Fill();
89  }
90 
91  return ProcessCode::SUCCESS;
92 }