ComPWA
Common Partial-Wave-Analysis Framework
SimFit.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 
5 #include <cmath>
6 #include <memory>
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 #include <boost/archive/xml_iarchive.hpp>
12 #include <boost/archive/xml_oarchive.hpp>
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/xml_parser.hpp>
15 
18 #include "Core/Logging.hpp"
19 #include "Core/ProgressBar.hpp"
20 #include "Core/Properties.hpp"
21 #include "Data/DataSet.hpp"
22 #include "Data/Generate.hpp"
24 #include "Physics/BuilderXML.hpp"
27 
31 
32 std::string MyParticleList = R"####(
33 <ParticleList>
34  <Particle Name="f0(980)">
35  <Pid>9000111</Pid>
36  <Parameter Type="Mass" Name="Mass_f0(980)">
37  <Value>0.994</Value>
38  <Error>0.001</Error>
39  <Fix>false</Fix>
40  </Parameter>
41  <QuantumNumber Class="Spin" Type="Spin" Value="0"/>
42  <QuantumNumber Class="Int" Type="Charge" Value="0"/>
43  <QuantumNumber Class="Int" Type="Parity" Value="1"/>
44  <DecayInfo Type="flatte">
45  <FormFactor Type="0" />
46  <Parameter Type="Coupling" Name="gPiPi_f0(980)">
47  <Value>2.66</Value>
48  <Error>0.001</Error>
49  <Fix>true</Fix>
50  <ParticleA>pi+</ParticleA>
51  <ParticleB>pi-</ParticleB>
52  </Parameter>
53  <Parameter Type="Coupling" Name="gKK_f0(980)">
54  <Value>3.121343843602647</Value>
55  <Error>0.001</Error>
56  <Fix>true</Fix>
57  <ParticleA>K+</ParticleA>
58  <ParticleB>K-</ParticleB>
59  </Parameter>
60  <Parameter Type="Coupling" Name="gKK_f0(980)">
61  <Value>3.121343843602647</Value>
62  <Error>0.001</Error>
63  <Fix>true</Fix>
64  <ParticleA>K_S0</ParticleA>
65  <ParticleB>K_S0</ParticleB>
66  </Parameter>
67  <Parameter Type="MesonRadius" Name="Radius_f0(980)">
68  <Value>1.5</Value>
69  <Fix>true</Fix>
70  <Min>1.0</Min>
71  <Max>2.0</Max>
72  </Parameter>
73  </DecayInfo>
74  </Particle>
75  <Particle Name="Zc(3900)">
76  <Pid>999999999</Pid>
77  <Parameter Type="Mass" Name="Mass_Zc(3900)">
78  <Value>3.900</Value>
79  <Error>0.001</Error>
80  <Fix>false</Fix>
81  </Parameter>
82  <QuantumNumber Class="Spin" Type="Spin" Value="0"/>
83  <QuantumNumber Class="Int" Type="Charge" Value="0"/>
84  <QuantumNumber Class="Int" Type="Parity" Value="1"/>
85  <DecayInfo Type="flatte">
86  <FormFactor Type="0" />
87  <Parameter Type="Coupling" Name="gPiJPsi_Zc(3900)">
88  <Value>0.075</Value>
89  <Error>0.001</Error>
90  <Fix>true</Fix>
91  <ParticleA>pi+</ParticleA>
92  <ParticleB>J/psi</ParticleB>
93  </Parameter>
94  <Parameter Type="Coupling" Name="gDD_Zc(3900)">
95  <Value>2.03</Value>
96  <Error>0.001</Error>
97  <Fix>true</Fix>
98  <ParticleA>D+</ParticleA>
99  <ParticleB>D-</ParticleB>
100  </Parameter>
101  <Parameter Type="MesonRadius" Name="Radius_Zc(3900)">
102  <Value>1.5</Value>
103  <Fix>true</Fix>
104  <Min>1.0</Min>
105  <Max>2.0</Max>
106  </Parameter>
107  </DecayInfo>
108  </Particle>
109 </ParticleList>
110 )####";
111 
112 std::string ModelSqrtS4230 = R"####(
113 <Intensity Class="IncoherentIntensity">
114  <Intensity Class="CoherentIntensity">
115  <Amplitude Class="CoefficientAmplitude">
116  <Parameter Type="Magnitude" Name="Magnitude_f0(980)0">
117  <Value>1.</Value>
118  <Fix>true</Fix>
119  </Parameter>
120  <Parameter Type="Phase" Name="Phase_f0(980)0">
121  <Value>0.</Value>
122  <Fix>true</Fix>
123  </Parameter>
124  <Amplitude Class="SequentialAmplitude">
125  <Amplitude Class="HelicityDecay">
126  <DecayParticle Name="f0(980)" Helicity="0"/>
127  <RecoilSystem FinalState="2" />
128  <DecayProducts>
129  <Particle Name="pi+" FinalState="0" Helicity="0"/>
130  <Particle Name="pi-" FinalState="1" Helicity="0"/>
131  </DecayProducts>
132  </Amplitude>
133  </Amplitude>
134  </Amplitude>
135  <Amplitude Class="SequentialAmplitude">
136  <Amplitude Class="HelicityDecay">
137  <DecayParticle Name="Zc(3900)" Helicity="0"/>
138  <RecoilSystem FinalState="0" />
139  <DecayProducts>
140  <Particle Name="pi-" FinalState="1" Helicity="0"/>
141  <Particle Name="J/psi" FinalState="2" Helicity="0"/>
142  </DecayProducts>
143  </Amplitude>
144  </Amplitude>
145  <Amplitude Class="SequentialAmplitude">
146  <Amplitude Class="HelicityDecay">
147  <DecayParticle Name="Zc(3900)" Helicity="0"/>
148  <RecoilSystem FinalState="1" />
149  <DecayProducts>
150  <Particle Name="pi+" FinalState="0" Helicity="0"/>
151  <Particle Name="J/psi" FinalState="2" Helicity="0"/>
152  </DecayProducts>
153  </Amplitude>
154  </Amplitude>
155  </Intensity>
156 </Intensity>
157 )####";
158 
159 std::string ModelSqrtS4260 = R"####(
160 <Intensity Class="IncoherentIntensity">
161  <Intensity Class="CoherentIntensity">
162  <Amplitude Class="CoefficientAmplitude">
163  <Parameter Type="Magnitude" Name="Magnitude_f0(980)0">
164  <Value>1.</Value>
165  <Fix>true</Fix>
166  </Parameter>
167  <Parameter Type="Phase" Name="Phase_f0(980)0">
168  <Value>0.</Value>
169  <Fix>true</Fix>
170  </Parameter>
171  <Amplitude Class="SequentialAmplitude">
172  <Amplitude Class="HelicityDecay">
173  <DecayParticle Name="f0(980)" Helicity="0"/>
174  <RecoilSystem FinalState="2" />
175  <DecayProducts>
176  <Particle Name="pi+" FinalState="0" Helicity="0"/>
177  <Particle Name="pi-" FinalState="1" Helicity="0"/>
178  </DecayProducts>
179  </Amplitude>
180  </Amplitude>
181  </Amplitude>
182  </Intensity>
183 </Intensity>
184 )####";
185 
187 
195 };
196 
197 EnergyParameters createEnergyPar(size_t NumberOfEvents, double SqrtS, int Seed,
199  std::vector<ComPWA::pid> InitialState,
200  std::vector<ComPWA::pid> FinalState,
201  const boost::property_tree::ptree &ModelTree) {
203  ParticleList, InitialState, FinalState,
204  ComPWA::FourMomentum(0.0, 0.0, 0.0, SqrtS));
205 
208 
209  ComPWA::Data::Root::RootUniformRealGenerator RandomGenerator(Seed);
210 
211  auto PhspSample =
212  ComPWA::Data::generatePhsp(100000, Generator, RandomGenerator);
213 
214  // new builder because we need to use a different phsp sample for
215  // normalization
217  ParticleList, Kinematics, ModelTree.get_child("Intensity"), PhspSample);
218  // Construct intensity class from model string
219  auto Intensity = Builder.createIntensity();
220 
222  NumberOfEvents, Kinematics, RandomGenerator, Intensity, PhspSample);
223  auto DataSet = Kinematics.convert(DataSample);
224  return EnergyParameters{NumberOfEvents, std::move(Kinematics),
225  std::move(Intensity), DataSample,
226  PhspSample, DataSet};
227 }
228 
233 int main(int argc, char **argv) {
234  using namespace ComPWA;
235  using namespace boost::property_tree;
236 
237  // initialize logging
238  Logging Log("log.txt", "debug");
239 
240  std::stringstream ModelStream;
241 
242  // List with all particle information needed
243  auto ParticleList = readParticles("particle_list.xml");
244  ModelStream << MyParticleList;
245  insertParticles(ParticleList, ModelStream);
246 
247  std::vector<pid> InitialState({11, -11});
248  std::vector<pid> FinalState({211, -211, 443});
249  FourMomentum CmsP4;
250 
251  //---------------------------------------------------
252  // sqrtS = 4230
253  //---------------------------------------------------
254  ModelStream.clear();
255  ModelStream << ModelSqrtS4230;
256  ptree ModelTr;
257  xml_parser::read_xml(ModelStream, ModelTr);
258 
259  EnergyParameters SqrtS4230 = createEnergyPar(
260  1000, 4.230, 123, ParticleList, InitialState, FinalState, ModelTr);
261 
262  //---------------------------------------------------
263  // sqrtS = 4260
264  //---------------------------------------------------
265  ModelStream.clear();
266  ModelStream << ModelSqrtS4260;
267  ptree ModelTree4260;
268  xml_parser::read_xml(ModelStream, ModelTree4260);
269 
270  EnergyParameters SqrtS4260 = createEnergyPar(
271  1000, 4.260, 456, ParticleList, InitialState, FinalState, ModelTree4260);
272 
273  //---------------------------------------------------
274  // Construct combined likelihood
275  //---------------------------------------------------
276  std::vector<
277  std::pair<ComPWA::FunctionTree::FunctionTreeEstimator, FitParameterList>>
278  Estimators;
280  SqrtS4230.Intensity, SqrtS4230.Points));
282  SqrtS4260.Intensity, SqrtS4260.Points));
283 
284  auto LogLHSumEstimator =
286  std::move(Estimators));
287 
288  //---------------------------------------------------
289  // Run fit
290  //---------------------------------------------------
291 
293 
294  // std::get<0>(LogLHSumEstimator)->print(25);
295  // LOG(INFO) << "Fit parameter list: " <<
296  // std::get<1>(LogLHSumEstimator).to_str();
297  auto Optimizer = Optimizer::Minuit2::MinuitIF();
298  auto FitResult = Optimizer.optimize(std::get<0>(LogLHSumEstimator),
299  std::get<1>(LogLHSumEstimator));
300 
301  LOG(INFO) << FitResult;
302 
303  //---------------------------------------------------
304  // 5.1) Save the fit result
305  //---------------------------------------------------
306  std::ofstream OutputStream("fitResult.xml");
307  boost::archive::xml_oarchive Archive(OutputStream);
308  Archive << BOOST_SERIALIZATION_NVP(FitResult);
309 
310  //---------------------------------------------------
311  // 6) Plot data sample and intensity
312  //---------------------------------------------------
315  "plot.root", "RECREATE");
316  PlotData4230.createDirectory("sqrtS4230");
317  PlotData4230.writeData(SqrtS4230.Kinematics.convert(SqrtS4230.DataSample));
318  PlotData4230.writeIntensityWeightedPhspSample(
319  SqrtS4230.Kinematics.convert(SqrtS4230.PhspSample), SqrtS4230.Intensity);
320 
323  "plot.root", "UPDATE");
324  PlotData4260.createDirectory("sqrtS4230");
325  PlotData4260.writeData(SqrtS4260.Kinematics.convert(SqrtS4260.DataSample));
326  PlotData4260.writeIntensityWeightedPhspSample(
327  SqrtS4260.Kinematics.convert(SqrtS4260.PhspSample), SqrtS4260.Intensity);
328 
329  LOG(INFO) << "Done";
330 
331  return 0;
332 }
ComPWA::Physics::HelicityFormalism::HelicityKinematics Kinematics
Definition: SimFit.cpp:190
ComPWA::Data::DataSet convert(const EventCollection &Events) const final
Creates a DataSet from Events.
ComPWA four momentum class.
EventCollection generatePhsp(unsigned int NumberOfEvents, const ComPWA::PhaseSpaceEventGenerator &Generator, ComPWA::UniformRealNumberGenerator &RandomGenerator)
Definition: Generate.cpp:285
std::string ModelSqrtS4260
Definition: SimFit.cpp:159
Wrapper of the Minuit2 Optimizer library.
Definition: MinuitIF.hpp:23
std::string MyParticleList
Definition: SimFit.cpp:32
Allows output of a data sample and an Intensity (and optionally its components) into a ROOT file via ...
ParticleList readParticles(std::stringstream &Stream)
Read list of particles from a stringstream For some reason the boost xml parser needs a non-const ref...
Definition: Properties.cpp:151
ComPWA::EventCollection PhspSample
Definition: SimFit.cpp:193
size_t NumberOfEvents
Definition: SimFit.cpp:189
Some useful functions for Monte-Carlo event generation.
ComPWA::FunctionTree::FunctionTreeIntensity Intensity
Definition: SimFit.cpp:191
void createDirectory(std::string Name)
Implementation of the ComPWA::Kinematics interface for amplitude models using the helicity formalism...
std::set< ParticleProperties > ParticleList
Definition: Properties.hpp:84
std::pair< ComPWA::FunctionTree::FunctionTreeEstimator, FitParameterList > createMinLogLHFunctionTreeEstimator(ComPWA::FunctionTree::FunctionTreeIntensity &Intensity, const ComPWA::Data::DataSet &DataSample)
Definition: MinLogLH.cpp:64
Data structure which resembles a general fit result.
Definition: FitResult.hpp:19
EnergyParameters createEnergyPar(size_t NumberOfEvents, double SqrtS, int Seed, ComPWA::ParticleList ParticleList, std::vector< ComPWA::pid > InitialState, std::vector< ComPWA::pid > FinalState, const boost::property_tree::ptree &ModelTree)
Definition: SimFit.cpp:197
void insertParticles(ParticleList &list, const boost::property_tree::ptree &pt)
Read list of particles from a boost::property_tree.
Definition: Properties.cpp:95
std::string ModelSqrtS4230
Definition: SimFit.cpp:112
ComPWA::Data::DataSet Points
Definition: SimFit.cpp:194
EventCollection generate(unsigned int NumberOfEvents, const ComPWA::Kinematics &Kinematics, const ComPWA::PhaseSpaceEventGenerator &Generator, ComPWA::Intensity &Intensity, ComPWA::UniformRealNumberGenerator &RandomGenerator)
Definition: Generate.cpp:95
int main(int argc, char **argv)
Simultaneous fit of multiple energy points of the reaction .
Definition: SimFit.cpp:233
Logging class provides an interface for logging all over the framework.
Definition: Logging.hpp:19
ComPWA::EventCollection DataSample
Definition: SimFit.cpp:192
const ParticleStateTransitionKinematicsInfo & getParticleStateTransitionKinematicsInfo() const
std::tuple< FunctionTreeEstimator, FitParameterList > createSumMinLogLHFunctionTreeEstimator(std::vector< std::pair< ComPWA::FunctionTree::FunctionTreeEstimator, FitParameterList >> Estimators)
Definition: SumMinLogLH.cpp:53