Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NuclearInteractionOptions.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file NuclearInteractionOptions.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 <string>
14 
15 #include <TFile.h>
16 #include <TH1F.h>
17 #include <TVectorF.h>
18 
19 namespace {
20 std::pair<std::vector<float>, std::vector<uint32_t>> readHistogram(
21  const std::string&& binBordersName, const std::string&& binContentsName) {
22  std::vector<float>* binBorders = nullptr;
23  std::vector<uint32_t>* binContents = nullptr;
24  // Get the decomposed histogram
25  binBorders = (std::vector<float>*)gDirectory->Get(binBordersName.c_str());
26  binContents =
27  (std::vector<uint32_t>*)gDirectory->Get(binContentsName.c_str());
28  // Return the histogram if available
29  if (binBorders != nullptr && binContents != nullptr) {
30  return std::make_pair(*binBorders, *binContents);
31  }
32  return std::make_pair(std::vector<float>(), std::vector<uint32_t>());
33 }
34 
35 Acts::ActsDynamicVector readVector(const std::string&& vectorName) {
36  std::vector<float>* vector = nullptr;
37  // Get the vector
38  vector = (std::vector<float>*)gDirectory->Get(vectorName.c_str());
39  // Return the vector if available
40  if (vector != nullptr) {
42  const unsigned int sizeVec = vector->size();
43  result.resize(sizeVec);
44  for (unsigned int i = 0; i < sizeVec; i++) {
45  result(i) = (*vector)[i];
46  }
47 
48  return result;
49  }
50  return Acts::ActsDynamicVector();
51 }
52 
53 void readKinematicParameters(
55  TObject* folder, bool softInteractionParameters) {
56  if (folder->IsFolder()) {
57  // Find the momentum and invariant mass distributions
58  const char* distributionName = folder->GetName();
59  unsigned int mult = std::stoi(distributionName);
60  gDirectory->cd(distributionName);
61  std::vector<std::pair<std::vector<float>, std::vector<uint32_t>>>
63  momentumDistributions.resize(mult + 1);
64  std::vector<std::pair<std::vector<float>, std::vector<uint32_t>>>
65  invariantMassDistributions;
66  invariantMassDistributions.resize(mult);
67  for (unsigned int i = 0; i < mult; i++) {
68  momentumDistributions[i] = readHistogram(
69  ("MomentumDistributionBinBorders_" + std::to_string(i)).c_str(),
70  ("MomentumDistributionBinContents_" + std::to_string(i)).c_str());
71 
72  invariantMassDistributions[i] = readHistogram(
73  ("InvariantMassDistributionBinBorders_" + std::to_string(i)).c_str(),
74  ("InvariantMassDistributionBinContents_" + std::to_string(i))
75  .c_str());
76  }
77  momentumDistributions.back() = readHistogram(
78  ("MomentumDistributionBinBorders_" + std::to_string(mult)).c_str(),
79  ("MomentumDistributionBinContents_" + std::to_string(mult)).c_str());
80 
81  // Get the eigenspace components for the kinematic parameters
82  Acts::ActsDynamicVector momentumEigenvalues =
83  readVector("MomentumEigenvalues");
84  Acts::ActsDynamicVector momentumEigenvectors =
85  readVector("MomentumEigenvectors");
86  Acts::ActsDynamicVector momentumMean = readVector("MomentumMean");
87  Acts::ActsDynamicVector invariantMassEigenvalues =
88  readVector("InvariantMassEigenvalues");
89  Acts::ActsDynamicVector invariantMassEigenvectors =
90  readVector("InvariantMassEigenvectors");
91  Acts::ActsDynamicVector invariantMassMean = readVector("InvariantMassMean");
92 
93  // Test that a parametrisation is present
94  if (momentumEigenvalues.size() != 0 && momentumEigenvectors.size() != 0 &&
95  momentumMean.size() != 0 && invariantMassEigenvalues.size() != 0 &&
96  invariantMassEigenvectors.size() != 0 &&
97  invariantMassMean.size() != 0) {
98  // Prepare and store the kinematic parameters
100  ParametersWithFixedMultiplicity kinematicParameters(
101  momentumDistributions, momentumEigenvalues, momentumEigenvectors,
102  momentumMean, invariantMassDistributions,
103  invariantMassEigenvalues, invariantMassEigenvectors,
104  invariantMassMean);
105  if (softInteractionParameters) {
106  if (mult >= parameters.softKinematicParameters.size()) {
107  parameters.softKinematicParameters.resize(mult + 1);
108  }
109  parameters.softKinematicParameters[mult] = kinematicParameters;
110  } else {
111  if (mult >= parameters.hardKinematicParameters.size()) {
112  parameters.hardKinematicParameters.resize(mult + 1);
113  }
114  parameters.hardKinematicParameters[mult] = kinematicParameters;
115  }
116  }
117  gDirectory->cd("..");
118  }
119 }
120 } // namespace
121 
125 
126  auto opt = desc.add_options();
127  opt("fatras-nuclear-interaction-parametrisation",
128  value<std::string>()->default_value({}),
129  "File containing parametrisations for nuclear interaction.");
130 }
131 
133  const boost::program_options::variables_map& variables) {
134  return variables["fatras-nuclear-interaction-parametrisation"]
135  .as<std::string>();
136 }
137 
140  // The collection
142 
143  // Now read file
145  TFile tf(fileName.c_str(), "read");
146  gDirectory->cd();
147  auto listOfParticles = gDirectory->GetListOfKeys();
148  auto initialParticle = listOfParticles->First();
149  while (initialParticle != nullptr) {
150  // Get the initial particle
151  char const* particleName = initialParticle->GetName();
152  gDirectory->cd(particleName);
153 
154  // Walk over all initial momenta for a particle
155  auto listOfMomenta = gDirectory->GetListOfKeys();
156  auto initialMomentum = listOfMomenta->First();
157  while (initialMomentum != nullptr) {
158  // Parameters for a fixed initial momentum
160  // Get the initial momentum
161  char const* nameMomentum = initialMomentum->GetName();
162  parameters.momentum = std::stof(nameMomentum);
163  gDirectory->cd(nameMomentum);
164 
165  // Get the nuclear interaction probability
166  parameters.nuclearInteractionProbability = readHistogram(
167  "NuclearInteractionBinBorders", "NuclearInteractionBinContents");
168 
169  // Get the soft interaction probability
170  TVectorF* softInteraction = (TVectorF*)gDirectory->Get("SoftInteraction");
171  parameters.softInteractionProbability = (*softInteraction)[0];
172 
173  // Get the branching probabilities
174  std::vector<int> branchingPdgIds =
175  *((std::vector<int>*)gDirectory->Get("BranchingPdgIds"));
176  std::vector<int> targetPdgIds =
177  *((std::vector<int>*)gDirectory->Get("TargetPdgIds"));
178  std::vector<float> targetPdgProbability =
179  *((std::vector<float>*)gDirectory->Get("TargetPdgProbability"));
180  parameters.pdgMap.reserve(branchingPdgIds.size());
181  for (unsigned int i = 0; i < branchingPdgIds.size(); i++) {
182  auto it = parameters.pdgMap.begin();
183  while (it->first != branchingPdgIds[i] &&
184  it != parameters.pdgMap.end()) {
185  it++;
186  }
187 
188  const auto target =
189  std::make_pair(targetPdgIds[i], targetPdgProbability[i]);
190  if (it != parameters.pdgMap.end()) {
191  it->second.push_back(target);
192  } else {
193  parameters.pdgMap.push_back(std::make_pair(
194  branchingPdgIds[i], std::vector<std::pair<int, float>>{target}));
195  }
196  }
197 
198  // Get the soft distributions
199  gDirectory->cd("soft");
200  // Get the multiplicity distribution
201  parameters.softMultiplicity =
202  readHistogram("MultiplicityBinBorders", "MultiplicityBinContents");
203  // Get the distributions for each final state multiplicity
204  auto softList = gDirectory->GetListOfKeys();
205  auto softElement = softList->First();
206  while (softElement != nullptr) {
207  readKinematicParameters(parameters, softElement, true);
208  softElement = softList->After(softElement);
209  }
210 
211  // Get the hard distributions
212  gDirectory->cd("../hard");
213  // Get the multiplicity distribution
214  parameters.hardMultiplicity =
215  readHistogram("MultiplicityBinBorders", "MultiplicityBinContents");
216 
217  // Get the distributions for each final state multiplicity
218  auto hardList = gDirectory->GetListOfKeys();
219  auto hardElement = hardList->First();
220  while (hardElement != nullptr) {
221  readKinematicParameters(parameters, hardElement, false);
222  hardElement = hardList->After(hardElement);
223  }
224 
225  initialMomentum = listOfMomenta->After(initialMomentum);
226  // Store the parametrisation
227  parametrisation.push_back(
228  std::make_pair(parameters.momentum, parameters));
229  }
230  tf.Close();
231 
232  // Write to the collection to the EventStore
233  mpp.push_back(std::make_pair(std::stof(particleName), parametrisation));
234 
235  initialParticle = listOfParticles->After(initialParticle);
236  }
237  // Return success flag
238  return mpp;
239 }