ComPWA
Common Partial-Wave-Analysis Framework
RootPlotData.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013, 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
6 #include "Core/Logging.hpp"
7 #include "Core/ProgressBar.hpp"
8 #include "Core/Properties.hpp"
9 #include "Data/DataSet.hpp"
11 
12 #include "TFile.h"
13 #include "TParameter.h"
14 #include "TTree.h"
15 
16 namespace ComPWA {
17 namespace Tools {
18 namespace Plotting {
19 
22  const std::string &filename, const std::string &option)
23  : RootFile(filename.c_str(), option.c_str()) {
24 
25  // write final state id to name mapping to file
26 
27  RootFile.cd();
28  RootFile.mkdir("final_state_id_to_name_mapping")->cd();
29  for (auto const x : KinematicsInfo.getFinalStateIDToNameMapping()) {
30  TParameter<int> TempPar(x.second.c_str(), x.first);
31  TempPar.Write();
32  }
33  RootFile.cd();
34 }
35 
36 void RootPlotData::createDirectory(std::string Name) {
37  RootFile.cd();
38  RootFile.mkdir(Name.c_str());
39  RootFile.cd(Name.c_str());
40 }
41 
42 void writeDataSimple(const Data::DataSet &DataSample, std::string TreeName) {
43  double EventWeight(1.0);
44 
45  double DataIntegral(0.0);
46 
47  TTree tree(TreeName.c_str(), TreeName.c_str());
48  std::vector<double> DataPointValues(DataSample.Data.size(), 0.0);
49  auto iter = DataSample.Data.begin();
50  for (size_t i = 0; i < DataSample.Data.size(); ++i) {
51  tree.Branch(iter->first.c_str(), &DataPointValues.at(i),
52  (iter->first + "/D").c_str());
53  ++iter;
54  }
55  tree.Branch("weight", &EventWeight, "event_weight/D");
56 
57  ComPWA::ProgressBar bar(DataSample.Weights.size());
58  for (size_t i = 0; i < DataSample.Weights.size(); ++i) {
59  EventWeight = DataSample.Weights[i];
60 
61  iter = DataSample.Data.begin();
62  for (size_t j = 0; j < DataPointValues.size(); ++j) {
63  DataPointValues[j] = iter->second[i];
64  ++iter;
65  }
66 
67  DataIntegral += DataSample.Weights[i];
68  tree.Fill();
69  bar.next();
70  }
71  tree.Write();
72 }
73 
74 void RootPlotData::writeData(const Data::DataSet &DataSample,
75  std::string TreeName) {
76  writeDataSimple(DataSample, TreeName + "_data");
77 }
78 
80  const Data::DataSet &PhspSample, ComPWA::Intensity &Intensity,
81  std::string TreeName,
82  std::map<std::string, std::shared_ptr<ComPWA::Intensity>>
83  IntensityComponents) {
84 
85  LOG(INFO) << "RootPlotData::write | calculating total intensity integral"
86  " using phase space sample...";
87 
88  // double PhspIntensityIntegral(0.0);
89  // for (auto const &dp : PhspSample.DataPoints) { // loop over data
90  // PhspIntensityIntegral += dp.Weight * Intensity->evaluate(dp);
91  //}
92 
93  // double ScalingFactor(DataIntegral / PhspIntensityIntegral);
94 
95  TTree tree((TreeName + "_WeightedPhspMCSample").c_str(),
96  (TreeName + "_WeightedPhspMCSample").c_str());
97  double EventWeight(1.0);
98 
99  std::vector<double> DataPointValues(PhspSample.Data.size(), 0.0);
100  auto iter = PhspSample.Data.begin();
101  for (size_t i = 0; i < PhspSample.Data.size(); ++i) {
102  tree.Branch(iter->first.c_str(), &DataPointValues.at(i),
103  (iter->first + "/D").c_str());
104  ++iter;
105  }
106  tree.Branch("weight", &EventWeight, "event_weight/D");
107 
108  double IntensityWeight(0.0);
109  tree.Branch("intensity", &IntensityWeight, "intensity/D");
110  std::vector<double> AmplitudeComponentWeights =
111  std::vector<double>(IntensityComponents.size(), 0.0);
112  unsigned int counter(0);
113  for (auto const &amp : IntensityComponents) {
114  tree.Branch(amp.first.c_str(), &AmplitudeComponentWeights.at(counter),
115  (amp.first + "/D").c_str());
116  ++counter;
117  }
118 
119  auto Intensities = Intensity.evaluate(PhspSample.Data);
120  std::vector<std::vector<double>> Components;
121  for (auto amp : IntensityComponents) {
122  Components.push_back(amp.second->evaluate(PhspSample.Data));
123  }
124 
125  ComPWA::ProgressBar bar(PhspSample.Weights.size());
126  for (size_t i = 0; i < PhspSample.Weights.size(); ++i) {
127  EventWeight = PhspSample.Weights[i];
128 
129  iter = PhspSample.Data.begin();
130  for (size_t j = 0; j < DataPointValues.size(); ++j) {
131  DataPointValues[j] = iter->second[i];
132  ++iter;
133  }
134  IntensityWeight = Intensities[i];
135  for (size_t j = 0; j < Components.size(); ++j) {
136  AmplitudeComponentWeights[j] = Components[j][i];
137  }
138 
139  tree.Fill();
140  bar.next();
141  }
142  tree.Write();
143 }
144 
146  std::string TreeName) {
147  writeDataSimple(HitMissSample, TreeName + "_Hit&MissMCSample");
148 }
149 
150 } // namespace Plotting
151 } // namespace Tools
152 } // namespace ComPWA
void writeIntensityWeightedPhspSample(const Data::DataSet &PhspSample, ComPWA::Intensity &Intensity, std::string TreeName="intensity_weighted_phspdata", std::map< std::string, std::shared_ptr< ComPWA::Intensity >> IntensityComponents={})
void writeHitMissSample(const Data::DataSet &HitMissSample, std::string TreeName="hitmiss_data")
ComPWA::DataMap Data
Definition: DataSet.hpp:18
std::vector< double > Weights
Definition: DataSet.hpp:19
std::map< unsigned int, std::string > getFinalStateIDToNameMapping() const
void createDirectory(std::string Name)
virtual OutputType evaluate(const InputTypes &... args) noexcept=0
RootPlotData(const Physics::ParticleStateTransitionKinematicsInfo &KinematicsInfo, const std::string &filename, const std::string &option="RECREATE")
void writeDataSimple(const Data::DataSet &DataSample, std::string TreeName)
void writeData(const Data::DataSet &DataSample, std::string TreeName="data")
Interface template for a general Function of the form OutputType Function(InputTypes) The concept clo...
Definition: Function.hpp:24