ComPWA
Common Partial-Wave-Analysis Framework
DalitzFitApp.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 
9 
10 #include <cmath>
11 #include <iostream>
12 #include <memory>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 #include <boost/archive/xml_iarchive.hpp>
18 #include <boost/archive/xml_oarchive.hpp>
19 #include <boost/property_tree/ptree.hpp>
20 #include <boost/property_tree/xml_parser.hpp>
21 
23 #include "Core/Logging.hpp"
24 #include "Core/Properties.hpp"
25 #include "Data/DataSet.hpp"
26 #include "Data/Generate.hpp"
28 #include "Physics/BuilderXML.hpp"
30 #include "Tools/FitFractions.hpp"
32 
35 
36 using namespace ComPWA;
39 
40 // Enable serialization of MinuitResult. For some reason has to be outside
41 // any namespaces.
42 // BOOST_CLASS_EXPORT(ComPWA::Optimizer::Minuit2::MinuitResult)
43 
44 // We define an intensity model using a raw string literal. Currently, this is
45 // just a toy model without any physical meaning.
46 // (comments within the string are ignored!). This is convenient since we
47 // do not have to configure the build system to copy input files somewhere.
48 // In practise you may want to use a normal XML input file instead.
49 std::string AmplitudeModel = R"####(
50 <Intensity Class="NormalizedIntensity">
51  <IntegrationStrategy Class="MCIntegrationStrategy"/>
52  <Intensity Class="CoherentIntensity" Component="jpsiGammaPiPi">
53  <Amplitude Class="CoefficientAmplitude" Component="f2(1270)">
54  <Parameter Class='Double' Type="Magnitude" Name="Magnitude_f2">
55  <Value>1.0</Value>
56  <Min>-1.0</Min>
57  <Max>2.0</Max>
58  <Fix>false</Fix>
59  </Parameter>
60  <Parameter Class='Double' Type="Phase" Name="Phase_f2">
61  <Value>0.0</Value>
62  <Min>-100</Min>
63  <Max>100</Max>
64  <Fix>false</Fix>
65  </Parameter>
66  <Amplitude Class="NormalizedAmplitude">
67  <IntegrationStrategy Class="MCIntegrationStrategy"/>
68  <Amplitude Class="HelicityDecay">
69  <DecayParticle Name="f2(1270)" Helicity="0"/>
70  <RecoilSystem FinalState="0" />
71  <DecayProducts>
72  <Particle Name="pi0" FinalState="1" Helicity="0"/>
73  <Particle Name="pi0" FinalState="2" Helicity="0"/>
74  </DecayProducts>
75  </Amplitude>
76  </Amplitude>
77  </Amplitude>
78  <Amplitude Class="CoefficientAmplitude" Component="myAmp">
79  <Parameter Class='Double' Type="Magnitude" Name="Magnitude_my">
80  <Value>1.0</Value>
81  <Min>-1.0</Min>
82  <Max>2.0</Max>
83  <Fix>true</Fix>
84  </Parameter>
85  <Parameter Class='Double' Type="Phase" Name="Phase_my`">
86  <Value>0.0</Value>
87  <Min>-100</Min>
88  <Max>100</Max>
89  <Fix>true</Fix>
90  </Parameter>
91  <Amplitude Class="NormalizedAmplitude">
92  <IntegrationStrategy Class="MCIntegrationStrategy"/>
93  <Amplitude Class="HelicityDecay" >
94  <DecayParticle Name="myRes" Helicity="0"/>
95  <RecoilSystem FinalState="0" />
96  <DecayProducts>
97  <Particle Name="pi0" FinalState="1" Helicity="0"/>
98  <Particle Name="pi0" FinalState="2" Helicity="0"/>
99  </DecayProducts>
100  </Amplitude>
101  </Amplitude>
102  </Amplitude>
103  </Intensity>
104 </Intensity>
105 )####";
106 
107 std::string MyParticleList = R"####(
108 <ParticleList>
109  <Particle Name="J/psi">
110  <Pid>443</Pid>
111  <Parameter Type="Mass" Name="Mass_jpsi">
112  <Value>3.096900</Value>
113  <Fix>true</Fix>
114  </Parameter>
115  <QuantumNumber Class="Spin" Type="Spin" Value="1" />
116  <QuantumNumber Class="Int" Type="Charge" Value="0" />
117  <QuantumNumber Class="Int" Type="Parity" Value="-1" />
118  <QuantumNumber Class="Int" Type="Cparity" Value="-1" />
119  <QuantumNumber Class="Int" Type="Gparity" Value="-1" />
120  <QuantumNumber Class="Spin" Type="IsoSpin" Value="0" Projection="0" />
121  <QuantumNumber Class="Int" Type="BaryonNumber" Value="0" />
122  <QuantumNumber Class="Int" Type="Charm" Value="0" />
123  <QuantumNumber Class="Int" Type="Strangeness" Value="0" />
124  <DecayInfo Type="relativisticBreitWigner">
125  <FormFactor Type="0" />
126  <Parameter Type="Width" Name="Width_jpsi">
127  <Value>9.29E-05</Value>
128  <Fix>true</Fix>
129  </Parameter>
130  <Parameter Type="MesonRadius" Name="Radius_jpsi">
131  <Value>2.5</Value>
132  <Fix>true</Fix>
133  <Min>2.0</Min>
134  <Max>3.0</Max>
135  </Parameter>
136  </DecayInfo>
137  </Particle>
138  <Particle Name="pi0">
139  <Pid>111</Pid>
140  <Parameter Type="Mass" Name="Mass_neutralPion">
141  <Value>0.1349766</Value>
142  <Error>0.000006</Error>
143  </Parameter>
144  <QuantumNumber Class="Spin" Type="Spin" Value="0" />
145  <QuantumNumber Class="Int" Type="Charge" Value="0" />
146  <QuantumNumber Class="Int" Type="Parity" Value="-1" />
147  <QuantumNumber Class="Int" Type="Cparity" Value="1" />
148  <QuantumNumber Class="Int" Type="Gparity" Value="-1" />
149  <QuantumNumber Class="Spin" Type="IsoSpin" Value="1" Projection="0" />
150  <QuantumNumber Class="Int" Type="BaryonNumber" Value="0" />
151  <QuantumNumber Class="Int" Type="Charm" Value="0" />
152  <QuantumNumber Class="Int" Type="Strangeness" Value="0" />
153  </Particle>
154  <Particle Name="gamma">
155  <Pid>22</Pid>
156  <Parameter Type="Mass" Name="Mass_gamma">
157  <Value>0.0</Value>
158  </Parameter>
159  <QuantumNumber Class="Spin" Type="Spin" Value="1" />
160  <QuantumNumber Class="Int" Type="Charge" Value="0" />
161  <QuantumNumber Class="Int" Type="Parity" Value="-1" />
162  <QuantumNumber Class="Int" Type="Cparity" Value="-1" />
163  <QuantumNumber Class="Spin" Type="IsoSpin" Value="0" Projection="0" />
164  <QuantumNumber Class="Int" Type="BaryonNumber" Value="0" />
165  <QuantumNumber Class="Int" Type="Charm" Value="0" />
166  <QuantumNumber Class="Int" Type="Strangeness" Value="0" />
167  </Particle>
168  <Particle Name="f2(1270)">
169  <Pid>225</Pid>
170  <Parameter Class='Double' Type="Mass" Name="Mass_f2(1270)">
171  <Value>1.2755</Value>
172  <Error>8.0E-04</Error>
173  <Min>0.1</Min>
174  <Max>2.0</Max>
175  <Fix>false</Fix>
176  </Parameter>
177  <QuantumNumber Class="Spin" Type="Spin" Value="2"/>
178  <QuantumNumber Class="Int" Type="Charge" Value="0"/>
179  <QuantumNumber Class="Int" Type="Parity" Value="+1"/>
180  <QuantumNumber Class="Int" Type="Cparity" Value="+1"/>
181  <DecayInfo Type="relativisticBreitWigner">
182  <FormFactor Type="0" />
183  <Parameter Class='Double' Type="Width" Name="Width_f2(1270)">
184  <Value>0.1867</Value>
185  </Parameter>
186  <Parameter Class='Double' Type="MesonRadius" Name="Radius_rho">
187  <Value>2.5</Value>
188  <Fix>true</Fix>
189  </Parameter>
190  </DecayInfo>
191  </Particle>
192  <Particle Name="myRes">
193  <Pid>999999</Pid>
194  <Parameter Class='Double' Type="Mass" Name="Mass_myRes">
195  <Value>2.0</Value>
196  <Error>8.0E-04</Error>
197  <Min>1.1</Min>
198  <Max>4.0</Max>
199  <Fix>true</Fix>
200  </Parameter>
201  <QuantumNumber Class="Spin" Type="Spin" Value="1"/>
202  <QuantumNumber Class="Int" Type="Charge" Value="0"/>
203  <QuantumNumber Class="Int" Type="Parity" Value="+1"/>
204  <QuantumNumber Class="Int" Type="Cparity" Value="+1"/>
205  <DecayInfo Type="relativisticBreitWigner">
206  <FormFactor Type="0" />
207  <Parameter Class='Double' Type="Width" Name="Width_myRes">
208  <Value>1.0</Value>
209  <Min>0.1</Min>
210  <Max>1.5</Max>
211  <Fix>false</Fix>
212  </Parameter>
213  <Parameter Class='Double' Type="MesonRadius" Name="Radius_myRes">
214  <Value>2.5</Value>
215  <Fix>true</Fix>
216  </Parameter>
217  </DecayInfo>
218  </Particle>
219 </ParticleList>
220 )####";
221 
233 int main(int argc, char **argv) {
234  // initialize logging
235  Logging Log("debug", "DalitzFit-log.txt");
236 
237  // List with all particle information needed
238  std::stringstream ParticlesStream(MyParticleList);
239  ParticleList Particles = readParticles(ParticlesStream);
240 
241  //---------------------------------------------------
242  // 1) Create Kinematics object
243  //---------------------------------------------------
244  std::vector<pid> InitialState = {443};
245  std::vector<pid> FinalState = {22, 111, 111};
246  HelicityKinematics Kinematics(Particles, InitialState, FinalState);
247 
248  //---------------------------------------------------
249  // 2) Generate a large phase space sample
250  //---------------------------------------------------
253 
255 
256  auto PhspSample(Data::generatePhsp(100000, Generator, RandomGenerator));
257 
258  //---------------------------------------------------
259  // 3) Create intensity from pre-defined model
260  //---------------------------------------------------
261  // Read in model property_tree
262  std::stringstream ModelStream;
263  ModelStream << AmplitudeModel;
264  boost::property_tree::ptree ModelTree;
265  boost::property_tree::xml_parser::read_xml(ModelStream, ModelTree);
266 
267  // Construct intensity class from model string
269  Particles, Kinematics, ModelTree.get_child("Intensity"), PhspSample);
270  auto Intensity = Builder.createIntensity();
271 
272  //---------------------------------------------------
273  // 4) Generate a data sample given intensity and kinematics
274  //---------------------------------------------------
275  auto DataSample = ComPWA::Data::generate(1000, Kinematics, RandomGenerator,
276  Intensity, PhspSample);
277 
278  auto SampleDataSet = Kinematics.convert(DataSample);
279  //---------------------------------------------------
280  // 5) Fit the model to the data and print the result
281  //---------------------------------------------------
283  Intensity, SampleDataSet);
284 
285  LOG(DEBUG) << Estimator.first.print(25);
286 
287  auto Optimizer = Optimizer::Minuit2::MinuitIF();
288 
289  // STARTING MINIMIZATION
290  auto FitResult =
291  Optimizer.optimize(std::get<0>(Estimator), std::get<1>(Estimator));
292 
293  LOG(INFO) << FitResult;
294 
295  // calculate fit fractions and errors
296  auto Components = Builder.createIntensityComponents(
297  {{"f2(1270)"}, {"myAmp"}, {"jpsiGammaPiPi"}});
298 
299  auto MyFractions = {std::make_pair(Components[0], Components[2]),
300  std::make_pair(Components[1], Components[2])};
301 
302  ComPWA::Tools::FitFractions FitFraction;
303  auto FitFractionList =
305  MyFractions, Kinematics.convert(PhspSample), FitResult);
306 
307  LOG(INFO) << FitFractionList;
308 
309  //---------------------------------------------------
310  // 5.1) Save the fit result
311  //---------------------------------------------------
312  std::ofstream FileStream("DalitzFit-fitResult.xml");
313  boost::archive::xml_oarchive Archive(FileStream);
314  Archive << BOOST_SERIALIZATION_NVP(FitResult);
315 
316  //---------------------------------------------------
317  // 6) Plot data sample and intensity
318  //---------------------------------------------------
319  ComPWA::Tools::Plotting::DalitzPlot Plot(Kinematics, "DalitzFit", 100);
320  Plot.fill(DataSample, true, "data", "Data sample", kBlack);
321  Plot.fill(PhspSample, false, "phsp", "Phsp sample", kGreen);
322  Plot.fill(PhspSample, Intensity, false, "fit", "jpsiGammaPiPi model", kBlue);
323  Plot.plot();
324  LOG(INFO) << "Done";
325 
326  return 0;
327 }
ComPWA::Data::DataSet convert(const EventCollection &Events) const final
Creates a DataSet from Events.
void fill(const ComPWA::EventCollection &Data, bool Normalize=false, const std::string &Name="", const std::string &Title="", Color_t Color=kBlack)
Definition: DalitzPlot.cpp:53
EventCollection generatePhsp(unsigned int NumberOfEvents, const ComPWA::PhaseSpaceEventGenerator &Generator, ComPWA::UniformRealNumberGenerator &RandomGenerator)
Definition: Generate.cpp:285
std::string MyParticleList
std::string AmplitudeModel
Wrapper of the Minuit2 Optimizer library.
Definition: MinuitIF.hpp:23
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
int main(int argc, char **argv)
Simple Dalitz plot fit of the channel J/psi -> gamma pi0 pi0.
Some useful functions for Monte-Carlo event generation.
Implementation of the ComPWA::Kinematics interface for amplitude models using the helicity formalism...
std::vector< FitFraction > FitFractionList
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
FitFractionList calculateFitFractionsWithCovarianceErrorPropagation(const std::vector< std::pair< IntensityComponent, IntensityComponent >> &Components, const ComPWA::Data::DataSet &PhspSample, const ComPWA::FitResult &Result)
Calculates the fit fractions with errors via error propagation from the covariance matrix...
Data structure which resembles a general fit result.
Definition: FitResult.hpp:19
The Kinematics interface defines the conversion of Events to a DataSet.
Definition: Kinematics.hpp:19
EventCollection generate(unsigned int NumberOfEvents, const ComPWA::Kinematics &Kinematics, const ComPWA::PhaseSpaceEventGenerator &Generator, ComPWA::Intensity &Intensity, ComPWA::UniformRealNumberGenerator &RandomGenerator)
Definition: Generate.cpp:95
Logging class provides an interface for logging all over the framework.
Definition: Logging.hpp:19
const ParticleStateTransitionKinematicsInfo & getParticleStateTransitionKinematicsInfo() const
Interface template for a general Function of the form OutputType Function(InputTypes) The concept clo...
Definition: Function.hpp:24