laura is hosted by Hepforge, IPPP Durham
Laura++  v3r0p1
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauSimFitSlave.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2013 - 2015.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Authors:
7 // Thomas Latham
8 // John Back
9 // Paul Harrison
10 
15 #include <cstdlib>
16 #include <iostream>
17 
18 #include "TMatrixD.h"
19 #include "TMessage.h"
20 #include "TObjArray.h"
21 #include "TObjString.h"
22 #include "TSocket.h"
23 #include "TSystem.h"
24 
25 #include "LauSimFitSlave.hh"
26 
27 
29 
30 
32  sMaster_(0),
33  messageFromMaster_(0),
34  slaveId_(0),
35  nSlaves_(0),
36  nParams_(0),
37  parValues_(0)
38 {
39 }
40 
42 {
43  delete sMaster_; sMaster_ = 0;
45  delete[] parValues_; parValues_ = 0;
46 }
47 
48 void LauSimFitSlave::connectToMaster( const TString& addressMaster, const UInt_t portMaster )
49 {
50  if ( sMaster_ != 0 ) {
51  std::cerr << "ERROR in LauSimFitSlave::connectToMaster : master socket already present" << std::endl;
52  return;
53  }
54 
55  // Open connection to master
56  sMaster_ = new TSocket(addressMaster, portMaster);
58  messageFromMaster_->ReadUInt( slaveId_ );
59  messageFromMaster_->ReadUInt( nSlaves_ );
60 
61  delete messageFromMaster_;
63 
64  std::cout << "INFO in LauSimFitSlave::connectToMaster : Established connection to master on port " << portMaster << std::endl;
65  std::cout << " : We are slave " << slaveId_ << " of " << nSlaves_ << std::endl;
66 }
67 
69 {
70  // Listen for requests from the master and act accordingly
71 
72  TMessage messageToMaster(kMESS_ANY);
73 
74  while ( kTRUE ) {
75 
77 
78  if ( messageFromMaster_->What() == kMESS_STRING ) {
79 
80  TString msgStr;
81  messageFromMaster_->ReadTString( msgStr );
82 
83  std::cout << "INFO in LauSimFitSlave::processMasterRequests : Received message from master: " << msgStr << std::endl;
84 
85  if ( msgStr == "Send Parameters" ) {
86 
87  // Send the fit parameters
88 
89  TObjArray array;
90  this->prepareInitialParArray( array );
91 
92  // Create array to efficiently exchange parameter values with master
93  if ( parValues_ != 0 ) {
94  delete[] parValues_;
95  parValues_ = 0;
96  }
97  nParams_ = array.GetEntries();
98  parValues_ = new Double_t[nParams_];
99 
100  messageToMaster.Reset( kMESS_OBJECT );
101  messageToMaster.WriteObject( &array );
102  sMaster_->Send( messageToMaster );
103 
104  } else if ( msgStr == "Read Expt" ) {
105 
106  // Read the data for this experiment
107  UInt_t iExpt(0);
108  messageFromMaster_->ReadUInt( iExpt );
109 
110  UInt_t nEvents = this->readExperimentData( iExpt );
111  if ( nEvents < 1 ) {
112  std::cerr << "WARNING in LauSimFitSlave::processMasterRequests : Zero events in experiment " << iExpt << ", the master should skip this experiment..." << std::endl;
113  }
114 
115  messageToMaster.Reset( kMESS_ANY );
116  messageToMaster.WriteUInt( slaveId_ );
117  messageToMaster.WriteUInt( nEvents );
118  sMaster_->Send( messageToMaster );
119 
120  } else if ( msgStr == "Cache" ) {
121 
122  // Perform the caching
123 
124  this->cacheInputFitVars();
125 
126  messageToMaster.Reset( kMESS_ANY );
127  messageToMaster.WriteUInt( slaveId_ );
128  messageToMaster.WriteBool( kTRUE );
129  sMaster_->Send( messageToMaster );
130 
131  } else if ( msgStr == "Write Results" ) {
132 
133  this->writeOutAllFitResults();
134 
135  messageToMaster.Reset( kMESS_ANY );
136  messageToMaster.WriteUInt( slaveId_ );
137  messageToMaster.WriteBool( kTRUE );
138  sMaster_->Send( messageToMaster );
139 
140  } else if ( msgStr == "Finish" ) {
141 
142  std::cout << "INFO in LauSimFitSlave::processMasterRequests : Message from master to finish" << std::endl;
143  break;
144 
145  } else {
146 
147  std::cerr << "ERROR in LauSimFitSlave::processMasterRequests : Unexpected message from master" << std::endl;
148  gSystem->Exit( EXIT_FAILURE );
149 
150  }
151 
152  } else if ( messageFromMaster_->What() == kMESS_OBJECT ) {
153 
154  std::cout << "INFO in LauSimFitSlave::processMasterRequests : Received message from master: Finalise" << std::endl;
155 
156  Int_t fitStatus(0);
157  Double_t NLL(0.0);
158  messageFromMaster_->ReadInt( fitStatus );
159  messageFromMaster_->ReadDouble( NLL );
160 
161  TObjArray * objarray = dynamic_cast<TObjArray*>( messageFromMaster_->ReadObject( messageFromMaster_->GetClass() ) );
162  if ( ! objarray ) {
163  std::cerr << "ERROR in LauSimFitSlave::processMasterRequests : Error reading parameters from master" << std::endl;
164  gSystem->Exit( EXIT_FAILURE );
165  }
166 
167  TMatrixD * covMat = dynamic_cast<TMatrixD*>( messageFromMaster_->ReadObject( messageFromMaster_->GetClass() ) );
168  if ( ! covMat ) {
169  std::cerr << "ERROR in LauSimFitSlave::processMasterRequests : Error reading covariance matrix from master" << std::endl;
170  gSystem->Exit( EXIT_FAILURE );
171  }
172 
173  TObjArray array;
174  this->finaliseResults( fitStatus, NLL, objarray, covMat, array );
175 
176  delete objarray; objarray = 0;
177  delete covMat; covMat = 0;
178 
179  // Send the finalised parameters back to the master
180  messageToMaster.Reset( kMESS_ANY );
181  messageToMaster.WriteUInt( slaveId_ );
182  messageToMaster.WriteBool( kTRUE );
183  messageToMaster.WriteObject( &array );
184  sMaster_->Send( messageToMaster );
185 
186  } else if ( messageFromMaster_->What() == kMESS_ANY ) {
187 
188  UInt_t nPars(0);
189  UInt_t nFreePars(0);
190  messageFromMaster_->ReadUInt( nPars );
191  messageFromMaster_->ReadUInt( nFreePars );
192 
193  if ( nPars != nParams_ ) {
194  std::cerr << "ERROR in LauSimFitSlave::processMasterRequests : Unexpected number of parameters received from master" << std::endl;
195  std::cerr << " : Received " << nPars << " when expecting " << nParams_ << std::endl;
196  gSystem->Exit( EXIT_FAILURE );
197  }
198 
199  messageFromMaster_->ReadFastArray( parValues_, nPars );
200 
201  this->setParsFromMinuit( parValues_, nFreePars );
202 
203  Double_t negLogLike = this->getTotNegLogLikelihood();
204 
205  messageToMaster.Reset( kMESS_ANY );
206  messageToMaster.WriteDouble( negLogLike );
207  sMaster_->Send( messageToMaster );
208 
209  } else {
210  std::cerr << "ERROR in LauSimFitSlave::processMasterRequests : Unexpected message type" << std::endl;
211  gSystem->Exit( EXIT_FAILURE );
212  }
213 
214  delete messageFromMaster_;
215  messageFromMaster_ = 0;
216  }
217 }
218 
219 
ClassImp(LauAbsCoeffSet)
UInt_t slaveId_
Slave id number.
void processMasterRequests()
Listen for requests from the master and act accordingly.
virtual ~LauSimFitSlave()
Destructor.
UInt_t nParams_
The total number of fit parameters.
void connectToMaster(const TString &addressMaster, const UInt_t portMaster)
Establish the connection to the master process.
TSocket * sMaster_
A socket to enable parallel setup.
virtual void prepareInitialParArray(TObjArray &array)=0
Package the initial fit parameters for transmission to the master.
TMessage * messageFromMaster_
Message from master to the slaves.
virtual void setParsFromMinuit(Double_t *par, Int_t npar)=0
This function sets the parameter values from Minuit.
File containing declaration of LauSimFitSlave class.
virtual Double_t getTotNegLogLikelihood()=0
Calculates the total negative log-likelihood.
The base class for any slave process for simultaneous/combined fits.
virtual void cacheInputFitVars()=0
Cache the input data values to calculate the likelihood during the fit.
virtual UInt_t readExperimentData(const UInt_t exptIndex)=0
Read in the data for the specified experiment.
UInt_t nSlaves_
The total number of slaves.
Double_t * parValues_
Parameter values array (for reading from the master)
virtual void writeOutAllFitResults()=0
Write out any fit results.
virtual void finaliseResults(const Int_t fitStat, const Double_t NLL, const TObjArray *parsFromMaster, const TMatrixD *covMat, TObjArray &parsToMaster)=0
Perform all finalisation actions.