replaced raw pointers by smart pointers for musrfit.cpp.
This commit is contained in:
parent
578dc900c2
commit
c2a2051d29
108
src/musrfit.cpp
108
src/musrfit.cpp
@ -44,6 +44,7 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include <TSAXParser.h>
|
#include <TSAXParser.h>
|
||||||
#include <TString.h>
|
#include <TString.h>
|
||||||
@ -145,7 +146,7 @@ void musrfit_write_ascii(TString fln, PRunData *data, int runCounter)
|
|||||||
// generate dump file name
|
// generate dump file name
|
||||||
TString count("_");
|
TString count("_");
|
||||||
count += runCounter;
|
count += runCounter;
|
||||||
Ssiz_t index = fln.Index(".");
|
Ssiz_t index = fln.Last('.');
|
||||||
fln.Insert(index, count);
|
fln.Insert(index, count);
|
||||||
|
|
||||||
std::ofstream f;
|
std::ofstream f;
|
||||||
@ -288,14 +289,14 @@ void musrfit_write_root(TFile &f, TString fln, PRunData *data, int runCounter)
|
|||||||
|
|
||||||
snprintf(name, sizeof(name),"c%d", runCounter);
|
snprintf(name, sizeof(name),"c%d", runCounter);
|
||||||
|
|
||||||
TCanvas *c = new TCanvas(name, title.Data(), 10, 10, 800, 600);
|
std::unique_ptr<TCanvas> c = std::unique_ptr<TCanvas>(new TCanvas(name, title.Data(), 10, 10, 800, 600));
|
||||||
|
|
||||||
// create histos
|
// create histos
|
||||||
Double_t diff = data->GetDataTimeStep();
|
Double_t diff = data->GetDataTimeStep();
|
||||||
Double_t start = -diff/2.0;
|
Double_t start = -diff/2.0;
|
||||||
Double_t end = data->GetDataTimeStep()*data->GetValue()->size();
|
Double_t end = data->GetDataTimeStep()*data->GetValue()->size();
|
||||||
TH1F *hdata = new TH1F("hdata", "run data", data->GetValue()->size(), start, end);
|
std::unique_ptr<TH1F> hdata = std::unique_ptr<TH1F>(new TH1F("hdata", "run data", data->GetValue()->size(), start, end));
|
||||||
TH1F *htheo = new TH1F("htheo", "run theory", data->GetValue()->size(), start, end);
|
std::unique_ptr<TH1F> htheo = std::unique_ptr<TH1F>(new TH1F("htheo", "run theory", data->GetValue()->size(), start, end));
|
||||||
|
|
||||||
// fill data
|
// fill data
|
||||||
for (unsigned int i=0; i<data->GetValue()->size(); i++) {
|
for (unsigned int i=0; i<data->GetValue()->size(); i++) {
|
||||||
@ -311,21 +312,7 @@ void musrfit_write_root(TFile &f, TString fln, PRunData *data, int runCounter)
|
|||||||
htheo->SetLineWidth(3);
|
htheo->SetLineWidth(3);
|
||||||
htheo->Draw("C SAME");
|
htheo->Draw("C SAME");
|
||||||
|
|
||||||
f.WriteTObject(c);
|
f.WriteTObject(c.get());
|
||||||
|
|
||||||
// clean up
|
|
||||||
if (hdata) {
|
|
||||||
delete hdata;
|
|
||||||
hdata = nullptr;
|
|
||||||
}
|
|
||||||
if (htheo) {
|
|
||||||
delete htheo;
|
|
||||||
htheo = nullptr;
|
|
||||||
}
|
|
||||||
if (c) {
|
|
||||||
delete c;
|
|
||||||
c = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
@ -630,40 +617,22 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
// read startup file
|
// read startup file
|
||||||
char startup_path_name[128];
|
char startup_path_name[128];
|
||||||
TSAXParser *saxParser = new TSAXParser();
|
std::unique_ptr<TSAXParser> saxParser(new TSAXParser());
|
||||||
PStartupHandler *startupHandler = new PStartupHandler();
|
std::unique_ptr<PStartupHandler> startupHandler(new PStartupHandler());
|
||||||
if (!startupHandler->StartupFileFound()) {
|
if (!startupHandler->StartupFileFound()) {
|
||||||
std::cerr << std::endl << ">> musrfit **WARNING** couldn't find " << startupHandler->GetStartupFilePath().Data();
|
std::cerr << std::endl << ">> musrfit **WARNING** couldn't find " << startupHandler->GetStartupFilePath().Data();
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
// clean up
|
|
||||||
if (saxParser) {
|
|
||||||
delete saxParser;
|
|
||||||
saxParser = nullptr;
|
|
||||||
}
|
|
||||||
if (startupHandler) {
|
|
||||||
delete startupHandler;
|
|
||||||
startupHandler = nullptr;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
strcpy(startup_path_name, startupHandler->GetStartupFilePath().Data());
|
strcpy(startup_path_name, startupHandler->GetStartupFilePath().Data());
|
||||||
saxParser->ConnectToHandler("PStartupHandler", startupHandler);
|
saxParser->ConnectToHandler("PStartupHandler", startupHandler.get());
|
||||||
//status = saxParser->ParseFile(startup_path_name);
|
//status = saxParser->ParseFile(startup_path_name);
|
||||||
// parsing the file as above seems to lead to problems in certain environments;
|
// parsing the file as above seems to lead to problems in certain environments;
|
||||||
// use the parseXmlFile function instead (see PStartupHandler.cpp for the definition)
|
// use the parseXmlFile function instead (see PStartupHandler.cpp for the definition)
|
||||||
status = parseXmlFile(saxParser, startup_path_name);
|
status = parseXmlFile(saxParser.get(), startup_path_name);
|
||||||
// check for parse errors
|
// check for parse errors
|
||||||
if (status) { // error
|
if (status) { // error
|
||||||
std::cerr << std::endl << ">> musrfit **WARNING** Reading/parsing musrfit_startup.xml failed.";
|
std::cerr << std::endl << ">> musrfit **WARNING** Reading/parsing musrfit_startup.xml failed.";
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
// clean up
|
|
||||||
if (saxParser) {
|
|
||||||
delete saxParser;
|
|
||||||
saxParser = nullptr;
|
|
||||||
}
|
|
||||||
if (startupHandler) {
|
|
||||||
delete startupHandler;
|
|
||||||
startupHandler = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -673,11 +642,11 @@ int main(int argc, char *argv[])
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// read msr-file
|
// read msr-file
|
||||||
PMsrHandler *msrHandler = nullptr;
|
std::unique_ptr<PMsrHandler> msrHandler;
|
||||||
if (startupHandler)
|
if (startupHandler)
|
||||||
msrHandler = new PMsrHandler(filename, &startup_options);
|
msrHandler = std::unique_ptr<PMsrHandler>(new PMsrHandler(filename, &startup_options));
|
||||||
else
|
else
|
||||||
msrHandler = new PMsrHandler(filename);
|
msrHandler = std::unique_ptr<PMsrHandler>(new PMsrHandler(filename));
|
||||||
status = msrHandler->ReadMsrFile();
|
status = msrHandler->ReadMsrFile();
|
||||||
if (status != PMUSR_SUCCESS) {
|
if (status != PMUSR_SUCCESS) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
@ -695,11 +664,11 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read all the necessary runs (raw data)
|
// read all the necessary runs (raw data)
|
||||||
PRunDataHandler *dataHandler;
|
std::unique_ptr<PRunDataHandler> dataHandler;
|
||||||
if (startupHandler)
|
if (startupHandler)
|
||||||
dataHandler = new PRunDataHandler(msrHandler, startupHandler->GetDataPathList());
|
dataHandler = std::unique_ptr<PRunDataHandler>(new PRunDataHandler(msrHandler.get(), startupHandler->GetDataPathList()));
|
||||||
else
|
else
|
||||||
dataHandler = new PRunDataHandler(msrHandler);
|
dataHandler = std::unique_ptr<PRunDataHandler>(new PRunDataHandler(msrHandler.get()));
|
||||||
|
|
||||||
dataHandler->ReadData();
|
dataHandler->ReadData();
|
||||||
|
|
||||||
@ -717,10 +686,10 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generate the necessary fit histogramms for the fit
|
// generate the necessary fit histogramms for the fit
|
||||||
PRunListCollection *runListCollection = nullptr;
|
std::unique_ptr<PRunListCollection> runListCollection;
|
||||||
if (success) {
|
if (success) {
|
||||||
// feed all the necessary histogramms for the fit
|
// feed all the necessary histogramms for the fit
|
||||||
runListCollection = new PRunListCollection(msrHandler, dataHandler);
|
runListCollection = std::unique_ptr<PRunListCollection>(new PRunListCollection(msrHandler.get(), dataHandler.get()));
|
||||||
for (unsigned int i=0; i < msrHandler->GetMsrRunList()->size(); i++) {
|
for (unsigned int i=0; i < msrHandler->GetMsrRunList()->size(); i++) {
|
||||||
success = runListCollection->Add(i, kFit);
|
success = runListCollection->Add(i, kFit);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
@ -732,19 +701,19 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start timeout thread
|
// start timeout thread
|
||||||
TThread *th = nullptr;
|
std::unique_ptr<TThread> th;
|
||||||
if (timeout_enabled) {
|
if (timeout_enabled) {
|
||||||
pid_t musrfit_pid = getpid();
|
pid_t musrfit_pid = getpid();
|
||||||
th = new TThread(musrfit_timeout, (void*)&musrfit_pid);
|
th = std::unique_ptr<TThread>(new TThread(musrfit_timeout, (void*)&musrfit_pid));
|
||||||
if (th) {
|
if (th) {
|
||||||
th->Run();
|
th->Run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// do fitting
|
// do fitting
|
||||||
PFitter *fitter = nullptr;
|
std::unique_ptr<PFitter> fitter;
|
||||||
if (success) {
|
if (success) {
|
||||||
fitter = new PFitter(msrHandler, runListCollection, chisq_only);
|
fitter = std::unique_ptr<PFitter>(new PFitter(msrHandler.get(), runListCollection.get(), chisq_only));
|
||||||
if (fitter->IsValid()) {
|
if (fitter->IsValid()) {
|
||||||
fitter->DoFit();
|
fitter->DoFit();
|
||||||
if (!fitter->IsScanOnly())
|
if (!fitter->IsScanOnly())
|
||||||
@ -777,9 +746,9 @@ int main(int argc, char *argv[])
|
|||||||
std::cout << std::endl << "will write dump file ..." << std::endl;
|
std::cout << std::endl << "will write dump file ..." << std::endl;
|
||||||
dump.ToLower();
|
dump.ToLower();
|
||||||
if (dump.Contains("ascii"))
|
if (dump.Contains("ascii"))
|
||||||
musrfit_dump_ascii(filename, runListCollection);
|
musrfit_dump_ascii(filename, runListCollection.get());
|
||||||
else if (dump.Contains("root"))
|
else if (dump.Contains("root"))
|
||||||
musrfit_dump_root(filename, runListCollection);
|
musrfit_dump_root(filename, runListCollection.get());
|
||||||
else
|
else
|
||||||
std::cout << std::endl << "do not know format " << dump.Data() << ", sorry :-| " << std::endl;
|
std::cout << std::endl << "do not know format " << dump.Data() << ", sorry :-| " << std::endl;
|
||||||
}
|
}
|
||||||
@ -822,35 +791,6 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean up
|
|
||||||
if (th) {
|
|
||||||
th->Delete();
|
|
||||||
}
|
|
||||||
if (saxParser) {
|
|
||||||
delete saxParser;
|
|
||||||
saxParser = nullptr;
|
|
||||||
}
|
|
||||||
if (startupHandler) {
|
|
||||||
delete startupHandler;
|
|
||||||
startupHandler = nullptr;
|
|
||||||
}
|
|
||||||
if (msrHandler) {
|
|
||||||
delete msrHandler;
|
|
||||||
msrHandler = nullptr;
|
|
||||||
}
|
|
||||||
if (dataHandler) {
|
|
||||||
delete dataHandler;
|
|
||||||
dataHandler = nullptr;
|
|
||||||
}
|
|
||||||
if (runListCollection) {
|
|
||||||
delete runListCollection;
|
|
||||||
runListCollection = nullptr;
|
|
||||||
}
|
|
||||||
if (fitter) {
|
|
||||||
delete fitter;
|
|
||||||
fitter = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << std::endl << "done ..." << std::endl;
|
std::cout << std::endl << "done ..." << std::endl;
|
||||||
|
|
||||||
return PMUSR_SUCCESS;
|
return PMUSR_SUCCESS;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user