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 
13 #include <ios>
14 #include <iostream>
15 #include <stdexcept>
16 
17 #include <TFile.h>
18 #include <TTree.h>
19 
23  : TrackParameterWriter(cfg.collection, "RootTrackParameterWriter", level),
24  m_cfg(cfg),
25  m_outputFile(cfg.rootFile) {
26  // An input collection name and tree name must be specified
27  if (m_cfg.collection.empty()) {
28  throw std::invalid_argument("Missing input collection");
29  } else if (m_cfg.treeName.empty()) {
30  throw std::invalid_argument("Missing tree name");
31  }
32 
33  // Setup ROOT I/O
34  if (m_outputFile == nullptr) {
35  m_outputFile = TFile::Open(m_cfg.filePath.c_str(), m_cfg.fileMode.c_str());
36  if (m_outputFile == nullptr) {
37  throw std::ios_base::failure("Could not open '" + m_cfg.filePath);
38  }
39  }
40  m_outputFile->cd();
41  m_outputTree = new TTree(m_cfg.treeName.c_str(), m_cfg.treeName.c_str());
42  if (m_outputTree == nullptr)
43  throw std::bad_alloc();
44  else {
45  // I/O parameters
46  m_outputTree->Branch("event_nr", &m_eventNr);
47  m_outputTree->Branch("d0", &m_d0);
48  m_outputTree->Branch("z0", &m_z0);
49  m_outputTree->Branch("phi", &m_phi);
50  m_outputTree->Branch("theta", &m_theta);
51  m_outputTree->Branch("qp", &m_qp);
52  // MORE HERE
53  }
54 }
55 
57  if (m_outputFile) {
58  m_outputFile->Close();
59  }
60 }
61 
63  if (m_outputFile) {
64  m_outputFile->cd();
65  m_outputTree->Write();
66  ACTS_INFO("Wrote trackparameters to tree '" << m_cfg.treeName << "' in '"
67  << m_cfg.filePath << "'");
68  }
69  return ProcessCode::SUCCESS;
70 }
71 
74  const std::vector<BoundTrackParameters>& trackParams) {
75  if (m_outputFile == nullptr)
76  return ProcessCode::SUCCESS;
77 
78  // Exclusive access to the tree while writing
79  std::lock_guard<std::mutex> lock(m_writeMutex);
80 
81  // Get the event number
82  m_eventNr = ctx.eventNumber;
83 
84  for (auto& params : trackParams) {
85  m_d0 = params.parameters()[0];
86  m_z0 = params.parameters()[1];
87  m_phi = params.parameters()[2];
88  m_theta = params.parameters()[3];
89  m_qp = params.parameters()[4];
90 
91  m_outputTree->Fill();
92  }
93 
94  return ProcessCode::SUCCESS;
95 }