From 20b09534a648b9d24db94edb35ef70f4177509a6 Mon Sep 17 00:00:00 2001 From: Guanqun Ge Date: Tue, 20 Sep 2022 20:39:18 -0500 Subject: [PATCH 1/5] Allow configuration of the grid in the xml, instead of being hardcoded in the source code --- bin/sbnfit_feldman_cousins.cxx | 10 +++-- bin/sbnfit_singlephoton.cxx | 7 ++-- inc/ngrid.h | 72 +++++++++++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/bin/sbnfit_feldman_cousins.cxx b/bin/sbnfit_feldman_cousins.cxx index 6691d1b5..796b9e46 100644 --- a/bin/sbnfit_feldman_cousins.cxx +++ b/bin/sbnfit_feldman_cousins.cxx @@ -77,7 +77,7 @@ int main(int argc, char* argv[]) std::string tag = "TEST"; std::string mode_option; bool bool_stat_only = false; - int number = 2500; + int number = -1; int grid_pt = 0; double random_number_seed = -1; @@ -158,8 +158,12 @@ int main(int argc, char* argv[]) std::cout<<"Begining FeldmanCousins for tag: "< #include #include +#include +#include #include "TMatrixT.h" +//--- local header ---- +#include "tinyxml.h" + struct SBNfitResult{ size_t bf_pt; @@ -101,14 +106,62 @@ struct NGridDimension{ }; struct NGrid{ - int f_num_dimensions; - int f_num_total_points; + int f_num_dimensions = 0; + int f_num_total_points = 1; std::vector f_dimensions; - NGrid(){ - f_num_dimensions=0; - f_num_total_points=1; + //constructors + NGrid(){} + + // given provided configuration xml, grab the grids in it + NGrid(std::string xml){ + + std::cout << "Load Grid using configuration xml: " << xml << std::endl; + + TiXmlDocument doc(xml.c_str()); + bool loadOkay = doc.LoadFile(); + if(!loadOkay){ + throw std::runtime_error("\tERROR: fail to load configuration XML, generally means broken .xml brackets or attribute syntax."); + } + TiXmlHandle hDoc(&doc); + TiXmlElement *pGrid; + pGrid = doc.FirstChildElement("GridDimension"); + + while(pGrid){ + const char* dim_name = pGrid->Attribute("name"); + const char* dim_fixval = pGrid->Attribute("fix_val"); + const char* dim_lowedge = pGrid->Attribute("min"); + const char* dim_upedge = pGrid->Attribute("max"); + const char* dim_stepsize = pGrid->Attribute("step"); + char* pEnd; + + if(dim_name==NULL){ + std::cerr << "\tERROR! Grid dimension need a name! Please kindly define a name for it"< On Grid dimension " << dim_name; + + if(dim_fixval){ + std::cout << " with fixed value : " << dim_fixval << std::endl; + this->AddFixedDimension(std::string(dim_name), strtod(dim_fixval, &pEnd)); + }else{ + + if(dim_lowedge == NULL || dim_upedge == NULL || dim_stepsize == NULL){ + print_config_rule(); + throw std::runtime_error("Invalid Syntax to setup Grid"); + } + + std::cout << " with min: " << dim_lowedge << " max: " << dim_upedge << " and step size: " << dim_stepsize << std::endl; + this->AddDimension(std::string(dim_name), strtod(dim_lowedge, &pEnd), strtod(dim_upedge, &pEnd), strtod(dim_stepsize, &pEnd)); + } + + pGrid = pGrid->NextSiblingElement("GridDimension"); + } + + std::cout << "Finish reading grids from xml... Total of " << f_num_dimensions << " dimensions." << std::endl; + } void AddDimension(std::string name, double min, double max, double step ){ @@ -225,6 +278,15 @@ struct NGrid{ } + void print_config_rule(){ + std::cerr << "\n"<< std::string(30, '_') << std::endl; + std::cerr << "\tRULEs for xml-based grid setup" << std::endl; + std::cerr << "\tPlease specify the name, lower, upper range for the grid, as well as the stepsize. Please follow style below: " << std::endl; + std::cerr << "\t\tOption 1: name=\"xx\" min=\"xx\" max=\"xx\" step=\"xx\" to setup evenly distributed grid" << std::endl; + std::cerr << "\t\tOption 2: name=\"xx\" fix_val=\"xx\" to setup fixed value for this dimension" << std::endl; + std::cerr << "\n"<< std::string(30, '_') << std::endl; + return; + } }; #endif From 8e91063471ed4584ead66a88f99584b757c03303 Mon Sep 17 00:00:00 2001 From: Guanqun Ge Date: Tue, 27 Sep 2022 11:49:42 -0500 Subject: [PATCH 2/5] commit before merging with branch feature/mark_majormerge --- bin/sbnfit_make_covariance.cxx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/sbnfit_make_covariance.cxx b/bin/sbnfit_make_covariance.cxx index a01d43ab..c7a9ddea 100644 --- a/bin/sbnfit_make_covariance.cxx +++ b/bin/sbnfit_make_covariance.cxx @@ -200,17 +200,19 @@ int main(int argc, char* argv[]) //Will be outputted in the form: SBNfit_covariance_plots_TAG.root example_covar.PrintMatricies(tag,minimal_matrix_plots); - //Constraint will be patched in shortly: mark - std::ofstream ratio_con; - std::vector average_ratio; - ratio_con.open("ratio_list_"+tag+".txt",std::ofstream::trunc); if(constrain_mode){ + //Constraint will be patched in shortly: mark + std::ofstream ratio_con; + std::vector average_ratio; + ratio_con.open("ratio_list_"+tag+".txt",std::ofstream::trunc); + for (int i=0;i Date: Wed, 28 Sep 2022 17:18:12 -0500 Subject: [PATCH 3/5] Generate DeNan dictionary and library locally instead of loading dictionary in /uboone/app/users/mark/ --- bin/CMakeLists.txt | 2 +- bin/sbnfit_make_covariance.cxx | 5 ----- libio/CMakeLists.txt | 6 ++++++ libio/denan.cxx | 10 ++++++++++ libio/denan.h | 9 +++++++++ libio/denan_LinkDef.h | 11 +++++++++++ 6 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 libio/denan.cxx create mode 100644 libio/denan.h create mode 100644 libio/denan_LinkDef.h diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index 894b4e0f..b471db98 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -1,7 +1,7 @@ include_directories(${PROJECT_SOURCE_DIR}/inc) add_executable (sbnfit_make_covariance sbnfit_make_covariance.cxx) -target_link_libraries (sbnfit_make_covariance SBNfitlib sbnfit_tinyxmllib EventWeight) +target_link_libraries (sbnfit_make_covariance SBNfitlib sbnfit_tinyxmllib EventWeight SBNfit_DeNan) add_executable (sbnfit_plot_covariance sbnfit_plot_covariance.cxx) target_link_libraries (sbnfit_plot_covariance SBNfitlib sbnfit_tinyxmllib EventWeight) diff --git a/bin/sbnfit_make_covariance.cxx b/bin/sbnfit_make_covariance.cxx index c7a9ddea..47c6fde1 100644 --- a/bin/sbnfit_make_covariance.cxx +++ b/bin/sbnfit_make_covariance.cxx @@ -144,11 +144,6 @@ int main(int argc, char* argv[]) return 1; } - gSystem->Load("/uboone/app/users/markrl/Hive_v3.0/hellstroms_hive/hive/root_linkdefs/loc/SL7/denan_cxx.so"); - - //std::string dict_location = "../libio/libEventWeight.so"; - //std::cout<<"Trying to load dictionary: "<Load( (dict_location).c_str()); /************************************************************* ************************************************************* diff --git a/libio/CMakeLists.txt b/libio/CMakeLists.txt index 295a5c91..c7040dd5 100644 --- a/libio/CMakeLists.txt +++ b/libio/CMakeLists.txt @@ -6,9 +6,15 @@ unset(CMAKE_CXX_FLAGS) set(CMAKE_CXX_FLAGS " ${CMAKE_SO_CXX_FLAGS}") message("@libio CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") +#--- create eventweight library ROOT_GENERATE_DICTIONARY(G__EventWeight eventweight.h LINKDEF eventweight_LinkDef.h) #---Create a shared library with geneated dictionary add_library(EventWeight SHARED eventweight.cxx G__EventWeight.cxx) target_link_libraries(EventWeight ${ROOT_LIBRARIES}) + +#--- create DeNan library --- +ROOT_GENERATE_DICTIONARY(DeNan_Dict denan.h LINKDEF denan_LinkDef.h) +add_library(SBNfit_DeNan SHARED denan.cxx DeNan_Dict.cxx) +target_link_libraries(SBNfit_DeNan ${ROOT_LIBRARIES}) diff --git a/libio/denan.cxx b/libio/denan.cxx new file mode 100644 index 00000000..32ea7744 --- /dev/null +++ b/libio/denan.cxx @@ -0,0 +1,10 @@ +#include "denan.h" +#include "denan_LinkDef.h" + +double DeNan(double x, double replace){ + if(x!=x || isinf(x))return replace; + return x; +} + + + diff --git a/libio/denan.h b/libio/denan.h new file mode 100644 index 00000000..00f286d1 --- /dev/null +++ b/libio/denan.h @@ -0,0 +1,9 @@ +#ifndef DENAN_H +#define DENAN_H + +#include + +double DeNan(double x, double replace); + + +#endif diff --git a/libio/denan_LinkDef.h b/libio/denan_LinkDef.h new file mode 100644 index 00000000..bcbb8f1c --- /dev/null +++ b/libio/denan_LinkDef.h @@ -0,0 +1,11 @@ +#ifdef __CINT__ + +#pragma link off all globals; +#pragma link off all classes; +#pragma link off all functions; +#pragma link C++ nestedclasses; + +#pragma link C++ function DeNan; + +#endif + From 87807c6380f6ea23df3e2dcd2bdb7aa185809e25 Mon Sep 17 00:00:00 2001 From: Guanqun Ge Date: Thu, 1 Jun 2023 09:20:37 -0500 Subject: [PATCH 4/5] fix most obvious memory leak issue, mostly related to ROOT object --- inc/SBNchi.h | 19 ++++++--- inc/SBNconfig.h | 13 +++++-- inc/SBNcovariance.h | 14 +++---- inc/SBNspec.h | 2 +- inc/branch_variable.h | 14 +++++++ src/SBNchi.cxx | 8 ++-- src/SBNconfig.cxx | 15 ++++++-- src/SBNcovariance.cxx | 89 ++++++++++++++++++++++++++++--------------- src/SBNspec.cxx | 29 +++++++++++--- 9 files changed, 144 insertions(+), 59 deletions(-) diff --git a/inc/SBNchi.h b/inc/SBNchi.h index eec4b6a5..c15a8298 100644 --- a/inc/SBNchi.h +++ b/inc/SBNchi.h @@ -73,6 +73,15 @@ namespace sbn{ //Initialise a stat_only one; SBNchi(SBNspec, bool is_stat_only); SBNchi(std::string); + ~SBNchi(){ + std::cout << "start free SBNchi " << std::endl; + if(rangen_twister) {delete rangen_twister; rangen_twister = nullptr;} + if(rangen_linear) {delete rangen_linear; rangen_linear = nullptr;} + if(rangen_carry) {delete rangen_carry; rangen_carry = nullptr;} + if(rangen) {delete rangen; rangen = nullptr;} + if(m_dist_normal) {delete m_dist_normal; m_dist_normal = nullptr;} + std::cout << "end free SBNchi " << std::endl; + } //This is the core spectra that you are comparing too. This is used to calculate covariance matrix and in a way is on the 'bottom' of the chi^2. SBNspec core_spectrum; @@ -105,13 +114,13 @@ namespace sbn{ /***** Random Number Generation ****/ std::random_device random_device_seed; - std::mt19937 *rangen_twister; //merseinne twister - std::minstd_rand * rangen_linear; - std::ranlux24_base * rangen_carry; + std::mt19937 *rangen_twister = nullptr; //merseinne twister + std::minstd_rand * rangen_linear = nullptr; + std::ranlux24_base * rangen_carry = nullptr; void InitRandomNumberSeeds(); void InitRandomNumberSeeds(double); - TRandom3 * rangen; - std::normal_distribution* m_dist_normal; + TRandom3 * rangen = nullptr; + std::normal_distribution* m_dist_normal = nullptr; /*********************************** Member Functions ********************************/ diff --git a/inc/SBNconfig.h b/inc/SBNconfig.h index fc862e7e..df00f715 100644 --- a/inc/SBNconfig.h +++ b/inc/SBNconfig.h @@ -14,6 +14,7 @@ #include #include #include +#include #include "TH1D.h" #include "TFile.h" @@ -30,7 +31,7 @@ std::string sbnfit_to_string_prec(const T a_value, const int n = 6) out <, std::vector, std::vector, std::vector>, std::vector>); + + //destructor + ~SBNconfig(); + //This is going to be a manual Setup thing int LoadFromXML(const char* filedata,bool,bool); @@ -97,10 +102,10 @@ class SBNconfig { //vectors of length num_channels std::vector num_subchannels; - int* a_num_subchannels; + int* a_num_subchannels = nullptr; std::vector num_subchannels_xml; std::vector num_bins; - int* a_num_bins; + int* a_num_bins = nullptr; std::string xmlname; @@ -200,7 +205,7 @@ class SBNconfig { std::vector pot; std::vector> parameter_names; //obsolete code - std::vector> branch_variables; + std::vector>> branch_variables; /**********************created for single photon****************************/ //systematics root files provided correspond to diff --git a/inc/SBNcovariance.h b/inc/SBNcovariance.h index f86e849c..7c455895 100644 --- a/inc/SBNcovariance.h +++ b/inc/SBNcovariance.h @@ -60,7 +60,7 @@ namespace sbn{ SBNcovariance(std::string xmlname); SBNcovariance(std::string xmlname, bool); SBNcovariance(std::string xmlname, std::string); - + ~SBNcovariance(); void ShapeOnlyProcessing(); int FormCovarianceMatrix(std::string tag); @@ -94,9 +94,9 @@ namespace sbn{ TMatrixD input_frac_covariance; //for plotting, hsa been superseeded by PrintMatricies a bit - TH2D * hist_frac_cov; - TH2D * hist_full_cor; - TH2D * hist_full_cov; + TH2D * hist_frac_cov = nullptr; + TH2D * hist_full_cor = nullptr; + TH2D * hist_full_cov = nullptr; //Some checks on montecarlos double tolerence_positivesemi; @@ -135,9 +135,7 @@ namespace sbn{ std::map m_variations_to_use; // In testing 2D fits with 4D covariance.. - SBNspec template_spec; - SBNspec spec_central_value2; - THnSparseD *frac_covariance_4d; + THnSparseD *frac_covariance_4d = nullptr; TMatrixD full_covariance2D; TMatrixD frac_covariance2D; TMatrixD full_correlation2D; @@ -194,6 +192,8 @@ namespace sbn{ } } } + delete m_check1; m_check1 = nullptr; + delete m_check2; m_check2 = nullptr; return true; } diff --git a/inc/SBNspec.h b/inc/SBNspec.h index 86466f5c..a4f55ea0 100644 --- a/inc/SBNspec.h +++ b/inc/SBNspec.h @@ -71,7 +71,7 @@ namespace sbn{ SBNspec(std::vector input_full_vec, std::vector input_full_err, std::string whichxml); SBNspec(std::vector input_full_vec, std::vector input_full_err, std::string whichxml, bool isverbose); SBNspec(std::vector input_full_vec, std::vector input_full_err, std::string whichxml, int universe, bool isverbose); - + ~SBNspec(){std::cout << "free SBNspec " << std::endl;} std::map> GetCollapsedChannelIndicies(); std::vector GetBlankChannelHists(); diff --git a/inc/branch_variable.h b/inc/branch_variable.h index 4157f4b1..f4efe33d 100644 --- a/inc/branch_variable.h +++ b/inc/branch_variable.h @@ -50,6 +50,20 @@ struct BranchVariable{ int SetOscillate(bool inbool){ oscillate = inbool;}; bool GetOscillate(){ return oscillate;}; + ~BranchVariable(){ + if(branch_formula){ + std::cout << " BranchVariable || delete reco formula " << branch_formula << std::endl; + delete branch_formula; branch_formula = nullptr; + } + if(branch_true_value_formula){ + std::cout << " BranchVariable || delete true value formula " << branch_true_value_formula << std::endl; + delete branch_true_value_formula; branch_true_value_formula = nullptr; + } + if(branch_true_L_formula){ + std::cout << "BranchVariable || Delete true L formula " << branch_true_L_formula << std::endl; + delete branch_true_L_formula; branch_true_L_formula = nullptr; + } + } }; /*struct BranchVariable_i: public BranchVariable{ diff --git a/src/SBNchi.cxx b/src/SBNchi.cxx index 04e03693..29455311 100644 --- a/src/SBNchi.cxx +++ b/src/SBNchi.cxx @@ -1648,12 +1648,12 @@ TMatrixT SBNchi::FillSystematicsFromXML(std::string rootname, std::stri temp2(i,j)=mcont[i][j]; } } - delete temp; + delete temp; temp = nullptr; std::cout<<"SBNchi::FillSystematicsFromXML || loaded with dim : "<Close(); - delete fm; + delete fm; fm = nullptr; if(temp2.IsSymmetric()){ if(is_verbose)std::cout<<"Inputted fracCov covariance matrix is symmetric"<second.at(0),iter->second.at(1),iter->second.at(2)); color_channel_map.insert({iter->first, t_col->GetNumber()}); + delete t_col; t_col = nullptr; } for(int is = 0; is modein, std::vector d }; +SBNconfig::~SBNconfig(){ +std::cout <<"start free SBNconfig" << std::endl; + for(auto& p : montecarlo_additional_weight_formulas) + if(p){ + std::cout << "additional weight : " << p << std::endl; delete p; p = nullptr;} + std::cout << "Finish freeing up the space used by SBNconfig " << std::endl; +} int SBNconfig::CalcTotalBins(){ // These variables are important @@ -509,7 +516,7 @@ int SBNconfig::LoadFromXML(const char * filedata, bool isverbose, bool useuniver TiXmlElement *pBranch; pBranch = pMC->FirstChildElement("branch"); - std::vector TEMP_branch_variables; + std::vector> TEMP_branch_variables; while(pBranch){ const char* bnam = pBranch->Attribute("name"); @@ -576,12 +583,12 @@ int SBNconfig::LoadFromXML(const char * filedata, bool isverbose, bool useuniver if((std::string)btype == "double"){ if(use_universe){ - TEMP_branch_variables.push_back( new BranchVariable_d(bnam, btype, bhist ) ); + TEMP_branch_variables.push_back(std::make_shared(bnam, btype, bhist ) ); } else if((std::string)bcentral == "true"){ - TEMP_branch_variables.push_back( new BranchVariable_d(bnam, btype, bhist,bsyst, true) ); + TEMP_branch_variables.push_back(std::make_shared(bnam, btype, bhist,bsyst, true) ); log(L"%1% || Setting as CV for det sys.") % __func__ ; } else { - TEMP_branch_variables.push_back( new BranchVariable_d(bnam, btype, bhist,bsyst, false) ); + TEMP_branch_variables.push_back(std::make_shared(bnam, btype, bhist,bsyst, false) ); log(L"%1% || Setting as individual (not CV) for det sys.") % __func__ ; } }else{ diff --git a/src/SBNcovariance.cxx b/src/SBNcovariance.cxx index 5f42b25e..a40cb15a 100644 --- a/src/SBNcovariance.cxx +++ b/src/SBNcovariance.cxx @@ -35,6 +35,7 @@ SBNcovariance::SBNcovariance(std::string xmlname, std::string tag): SBNconfig(xm test_file->cd(); test_matrix.Write("frac_covariance"); test_file->Close(); + delete test_file, test_file=nullptr; } } @@ -159,7 +160,6 @@ SBNcovariance::SBNcovariance(std::string xmlname, bool useuniverse) : SBNconfig( std::cout<<"Total Entries: "<GetEntries()<<" good event "<GetEntry(good_event); - std::cout<<__LINE__<<" Got Good"<Close(); - } - std::cout << otag<<" End" << std::endl; - } //end constructor. @@ -619,18 +609,30 @@ SBNcovariance::SBNcovariance(std::string xmlname) : SBNconfig(xmlname) { watch.Stop(); std::cout << otag<<" done CpuTime=" << watch.CpuTime() << " RealTime=" << watch.RealTime() << std::endl; +} - /*************************************************************** - * Now some clean-up and Writing - * ************************************************************/ - - for(auto f: files){ - std::cout << otag<<" TFile::Close() file=" << f->GetName() << " @" << f << std::endl; - f->Close(); +SBNcovariance::~SBNcovariance(){ + std::cout << "start free SBN covariance " << std::endl; + for(auto& evec : m_variation_weight_formulas){ + for(auto& f: evec) + if(f){ + std::cout << " delete variation weights: " << f << std::endl; + delete f; + f=nullptr; + } + } + std::cout << "Close files " << std::endl; + for(auto t: trees) + t->ResetBranchAddresses(); + for(auto f: files){ + if(f && f->IsOpen()){ + std::cout << " TFile::Close() file=" << f->GetName() << " @" << f << std::endl; + f->Close(); } - std::cout << otag<<" End" << std::endl; -} + } + std::cout << "Finish freeing dynamic memory in SBNcovariance" << std::endl; +} void SBNcovariance::ProcessEvent( const std::mapClose(); + delete fout; fout = nullptr; spec_central_value.WriteOut(tag); qualityTesting(); - + + std::cout << "delete double pointer" << std::endl; + delete[] a_multi_vecspec; + delete[] a_vec_full_covariance; + delete[] a_vec_frac_covariance; + delete[] a_vec_full_correlation; std::cout<<"SBNcovariance::FormCovariancematrix\t||\tEnd" << std::endl; return 0; } @@ -1227,7 +1235,7 @@ int SBNcovariance::PrintVariations(std::string tag){ temp_cv_spec->SetTitle((v + " || " +fullnames.at(i)).c_str()); temp_cv_spec->DrawCopy("hist"); - delete temp_cv_spec; + delete temp_cv_spec; temp_cv_spec = nullptr; } vec_canvas.push_back(tmpc); } @@ -1277,8 +1285,8 @@ int SBNcovariance::PrintVariations(std::string tag){ vec_canvas.at(which_matrix).at(i)->Write(); vec_canvas.at(which_matrix).at(i)->SaveAs(("variations/Variation_"+tag+"_"+var+"_"+fullnames[i]+"_1D.pdf").c_str(),"pdf"); ; - delete temp_cv_spec; - delete vec_canvas.at(which_matrix).at(i); + delete temp_cv_spec; temp_cv_spec = nullptr; + delete vec_canvas.at(which_matrix).at(i); vec_canvas.at(which_matrix).at(i) = nullptr; } } @@ -1310,6 +1318,8 @@ int SBNcovariance::PrintVariations(std::string tag){ */ fout->Close(); + delete fout; fout = nullptr; + if(rangen){ delete rangen; rangen = nullptr; } return 0; } int SBNcovariance::PrintVariations_2D(std::string tag){ @@ -1539,6 +1549,8 @@ int SBNcovariance::PrintVariations_2D(std::string tag){ } fout->Close(); + delete fout; fout = nullptr; + if(rangen){ delete rangen; rangen = nullptr;} return 0; } @@ -1774,7 +1786,7 @@ int SBNcovariance::PrintMatricies(std::string tag, bool print_indiv) { fout->cd(); fout->Close(); - + delete fout; fout=nullptr; std::cout << "SBNcovariance::PrintMatricies\t||\tEnd" << std::endl; return 0; } @@ -2175,15 +2187,16 @@ std::vector SBNcovariance::DoConstraint(int which_signal, int which_cons std::cout<<"finished loop"< out_1bin={n_events[0], uncon, con, avg_ratio}; std::cout<<"made_vector"<0){ std::cout< SBNcovariance::DoConstraint(int which_signal, int which_cons std::cout<<"wrote"<Close(); + delete fnew; fnew = nullptr; if(good_bins>0.0){ std::cout<<"good"< SBNcovariance::DoConstraint_test(int which_signal, int which std::cout<<"finished loop"< out_1bin={n_events[0], uncon, con, avg_ratio}; std::cout<<"made_vector"<0){ std::cout< SBNcovariance::DoConstraint_test(int which_signal, int which cov_before.Write("before"); cov_after.Write("after"); std::cout<<"wrote"<Close(); + delete fnew; fnew = nullptr; //constraint_table.close(); std::cout<<"Closed file"<0.0){ @@ -2566,6 +2584,8 @@ void SBNcovariance::WriteOutVariation(std::string signal_tag) { } //variation loop fout->Close(); + delete fout; fout = nullptr; + return; } @@ -2650,6 +2670,7 @@ void SBNcovariance::GrabSubMatrix(std::string filename, std::string matrix_name, output_matrix.Write(matrix_name.c_str()); fout->Close(); fin->Close(); + delete fout; fout = nullptr; } @@ -2848,6 +2869,14 @@ int sbn::analyzeCovariance(std::string xml, std::string signal_file, std::string c->Update(); c->SaveAs(("AnalyzeSys_"+tag+"_"+std::to_string(i)+".pdf").c_str(),"pdf"); } + + for(auto& chi : chis){ + delete chi; + chi = nullptr; + } + for(auto& f : files){ + delete f; f = nullptr; + } return 0; } diff --git a/src/SBNspec.cxx b/src/SBNspec.cxx index f9c67ce3..6629c12f 100644 --- a/src/SBNspec.cxx +++ b/src/SBNspec.cxx @@ -55,7 +55,7 @@ SBNspec::SBNspec(std::string rootfile, std::string whichxml, bool isverbose) : S has_been_scaled=false; m_bool_use_wire_bayes = false; f->Close(); - + delete f; f= nullptr; this->CalcFullVector(); this->CalcErrorVector(); } @@ -162,8 +162,10 @@ int SBNspec::SetAsGaussian(double mean, double sigma, int ngen){ double eve = rangen->Gaus(mean,sigma); h.Fill( eve ); } - } + delete rangen; rangen=nullptr; + } + delete seedGetter;seedGetter = nullptr; return 0; } @@ -187,6 +189,7 @@ int SBNspec::ScalePoisson(){ h.SetBinError(i, sqrt( h.GetBinContent(i))); } } + delete rangen; rangen = nullptr; return 0; } @@ -209,6 +212,7 @@ int SBNspec::ScaleRandom(){ h.Scale(rangen->Uniform(0,2)); } + delete rangen; rangen = nullptr; this->CalcErrorVector(); return 0; } @@ -504,6 +508,7 @@ int SBNspec::WriteOut(std::string tag){ int color_index = TColor::GetFreeColorIndex(); t_col = new TColor(color_index, iter->second.at(0),iter->second.at(1),iter->second.at(2)); color_channel_map.insert({iter->first, t_col->GetNumber()}); + delete t_col; t_col = nullptr; } for(int is = 0; is Close(); + if(f2){ + f2->Close(); + delete f2; f2 = nullptr; + } std::vector temp_hists = hist; @@ -649,12 +657,19 @@ int SBNspec::WriteOut(std::string tag){ Cstack->Write(canvas_name.c_str() ); } + + delete hs; hs = nullptr; + delete legStack; legStack = nullptr; + delete Cstack; Cstack = nullptr; + } } } - f->Close(); - + if(f){ + f->Close(); + delete f; f= nullptr; + } return 0; } @@ -697,6 +712,7 @@ int SBNspec::CompareSBNspecsMark(TMatrixT collapse_covar, SBNspec * comp int color_index = TColor::GetFreeColorIndex(); t_col = new TColor(color_index, iter->second.at(0),iter->second.at(1),iter->second.at(2)); color_channel_map.insert({iter->first, t_col->GetNumber()}); + delete t_col; t_col = nullptr; } for(int is = 0; is collapse_covar, SBNspec * comp } f->Close(); + delete f; f = nullptr; return 0; } @@ -1069,6 +1086,7 @@ int SBNspec::CompareSBNspecs(TMatrixT collapse_covar, SBNspec * compsec, int color_index = TColor::GetFreeColorIndex(); t_col = new TColor(color_index, iter->second.at(0),iter->second.at(1),iter->second.at(2)); color_channel_map.insert({iter->first, t_col->GetNumber()}); + delete t_col; t_col = nullptr; } for(int is = 0; is collapse_covar, SBNspec * compsec, } f->Close(); + delete f; f = nullptr; return 0; } From 86fac3166e4d5509c24af1789582b6f5d6a32203 Mon Sep 17 00:00:00 2001 From: Guanqun Ge Date: Tue, 20 Jun 2023 12:06:13 -0500 Subject: [PATCH 5/5] Modify SBNconfig to avoid friending the same file and tree multiple times --- src/SBNconfig.cxx | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/SBNconfig.cxx b/src/SBNconfig.cxx index ac8d0bef..d50ccb00 100644 --- a/src/SBNconfig.cxx +++ b/src/SBNconfig.cxx @@ -498,18 +498,20 @@ int SBNconfig::LoadFromXML(const char * filedata, bool isverbose, bool useuniver ffname = friend_filename; } - if(montecarlo_file_friend_treename_map.count(montecarlo_file.back())>0){ - (montecarlo_file_friend_treename_map[montecarlo_file.back()]).push_back( pFriend->Attribute("treename") ); - (montecarlo_file_friend_map[montecarlo_file.back()]).push_back(ffname); + bool file_exist = false; + std::string ttname(pFriend->Attribute("treename")); + for(int i = 0 ; i != montecarlo_file_friend_map[montecarlo_file.back()].size(); ++i ){ + if(montecarlo_file_friend_map[montecarlo_file.back()][i] == ffname && montecarlo_file_friend_treename_map[montecarlo_file.back()].at(i) == ttname){ + file_exist = true;break; + } + } + if(!file_exist){ - }else{ - std::vector temp_treename = {pFriend->Attribute("treename")}; - std::vector temp_filename = {ffname}; + montecarlo_file_friend_treename_map[montecarlo_file.back()].push_back( ttname); + montecarlo_file_friend_map[montecarlo_file.back()].push_back(ffname); + } - montecarlo_file_friend_treename_map[montecarlo_file.back()] = temp_treename; - montecarlo_file_friend_map[montecarlo_file.back()] = temp_filename; - } pFriend = pFriend->NextSiblingElement("friend"); }