explicityly create a pointer for each thread and added them into TThrededObject

This commit is contained in:
Ryan@Home 2024-08-19 21:36:30 -04:00
parent 83fe8c1498
commit 1aa5f3dbcb

View File

@ -12,6 +12,8 @@
#include "TROOT.h" #include "TROOT.h"
#include "ROOT/TThreadedObject.hxx" #include "ROOT/TThreadedObject.hxx"
#include "ROOT/TTreeProcessorMT.hxx" #include "ROOT/TTreeProcessorMT.hxx"
#include <memory>
#include <vector>
// mod = 0, 1, 2 // mod = 0, 1, 2
// row = 1, 2, 3 // row = 1, 2, 3
@ -25,7 +27,7 @@ const double ExRange[2] = {-0.5, 3.5};
const double ExRel = 0.05; //MeV const double ExRel = 0.05; //MeV
const int nBin = (ExRange[1] - ExRange[0])/ExRel; const int nBin = (ExRange[1] - ExRange[0])/ExRel;
#define SAVETREE false #define SAVETREE true
//^==================================== //^====================================
TH1F * haha[MAXMOD][MAXROW][MAXPStrip]; TH1F * haha[MAXMOD][MAXROW][MAXPStrip];
@ -35,6 +37,8 @@ TH2F * dudu[MAXMOD][MAXROW];
TH1F * hEx; TH1F * hEx;
//^==================================== //^====================================
void parallel(){ void parallel(){
@ -73,6 +77,8 @@ void parallel(){
hEx = new TH1F("hEx", "Ex", 10*nBin, ExRange[0], ExRange[1]); hEx = new TH1F("hEx", "Ex", 10*nBin, ExRange[0], ExRange[1]);
ROOT::TThreadedObject<TH1F> phEx("hEx", "Ex", 10*nBin, ExRange[0], ExRange[1]);
//^=========================== tree //^=========================== tree
int nThreads = 4; int nThreads = 4;
ROOT::EnableImplicitMT(nThreads); ROOT::EnableImplicitMT(nThreads);
@ -87,7 +93,9 @@ void parallel(){
const double y2 = 2.005; const double y2 = 2.005;
TFile * newFile = nullptr; TFile * newFile = nullptr;
TTree * newTree = nullptr;
ROOT::TThreadedObject<TTree> pTree;
std::shared_ptr<TTree> pTree_a[nThreads];
Double_t Ex_org; Double_t Ex_org;
Double_t Ex_C; Double_t Ex_C;
@ -98,16 +106,32 @@ void parallel(){
Double_t thetaCM; Double_t thetaCM;
if( SAVETREE ){ if( SAVETREE ){
newFile = new TFile("Cali_132Sn_pal.root", "recreate"); newFile = new TFile("test.root", "recreate");
newTree = new TTree("tree", "Calibred Ex"); // newTree = new TTree("tree", "Calibred Ex");
newTree->Branch("Ex_org", &Ex_org, "Ex_org/D"); // newTree->Branch("Ex_org", &Ex_org, "Ex_org/D");
newTree->Branch("mod", &mod_C, "mod/I"); // newTree->Branch("mod", &mod_C, "mod/I");
newTree->Branch("row", &row_C, "row/I"); // newTree->Branch("row", &row_C, "row/I");
newTree->Branch("pstrip", &pStrip_C, "pstrip/I"); // newTree->Branch("pstrip", &pStrip_C, "pstrip/I");
newTree->Branch("Ex", &Ex_C, "Ex/D"); // newTree->Branch("Ex", &Ex_C, "Ex/D");
newTree->Branch("z", &z, "z/D"); // newTree->Branch("z", &z, "z/D");
newTree->Branch("thetaCM", &thetaCM, "thetaCM/D"); // newTree->Branch("thetaCM", &thetaCM, "thetaCM/D");
for( int i = 0; i < nThreads; i++ ){
pTree_a[i] = std::make_shared<TTree>("tree", "Calibrated Ex");
pTree_a[i]->Branch("Ex_org", &Ex_org, "Ex_org/D");
pTree_a[i]->Branch("mod", &mod_C, "mod/I");
pTree_a[i]->Branch("row", &row_C, "row/I");
pTree_a[i]->Branch("pstrip", &pStrip_C, "pstrip/I");
pTree_a[i]->Branch("Ex", &Ex_C, "Ex/D");
pTree_a[i]->Branch("z", &z, "z/D");
pTree_a[i]->Branch("thetaCM", &thetaCM, "thetaCM/D");
pTree.SetAtSlot(i, pTree_a[i]);
}
} }
std::mutex treeMuTex; std::mutex treeMuTex;
@ -181,16 +205,22 @@ void parallel(){
dudu[m][r]->Fill(p, Ex_C); dudu[m][r]->Fill(p, Ex_C);
hEx->Fill( *Ex/1000); hEx->Fill( *Ex/1000.);
if( SAVETREE ) { // if( SAVETREE ) {
newFile->cd(); // newFile->cd();
treeMuTex.lock(); // treeMuTex.lock();
newTree->Fill(); // newTree->Fill();
} // }
treeMuTex.unlock(); treeMuTex.unlock();
phEx->Fill(Ex_C);
if( SAVETREE ) {
pTree->Fill();
}
} }
}; };
@ -200,12 +230,28 @@ void parallel(){
if( SAVETREE ) { if( SAVETREE ) {
newFile->cd(); newFile->cd();
newTree->Write();
printf("============== %llu\n", newTree->GetEntries()); TList list;
for (int i = 0; i < pTree.GetNSlots(); ++i) {
if( pTree.GetAtSlot(i)->GetEntries() == 0 ) continue;
list.Add(pTree.GetAtSlot(i).get());
}
TTree* mergedTree = TTree::MergeTrees(&list);
mergedTree->Write("", TObject::kWriteDelete);
printf("============== %llu\n", mergedTree->GetEntries());
newFile->Close(); newFile->Close();
} }
auto kEx = phEx.Merge();
// kEx->SetLineColor(2);
// kEx->DrawCopy();
// hEx->Draw("same");
// gStyle->SetOptStat(""); // gStyle->SetOptStat("");
// TCanvas * canvas = new TCanvas("canvas", "canvas", 2100, 1500); // TCanvas * canvas = new TCanvas("canvas", "canvas", 2100, 1500);
@ -223,9 +269,9 @@ void parallel(){
// canvas->cd(8); dudu[2][1]->Draw("colz"); // canvas->cd(8); dudu[2][1]->Draw("colz");
// canvas->cd(9); dudu[2][2]->Draw("colz"); // canvas->cd(9); dudu[2][2]->Draw("colz");
TCanvas * canvasEx = new TCanvas("canvasEx", "canvasEx", 800, 400); // TCanvas * canvasEx = new TCanvas("canvasEx", "canvasEx", 800, 400);
canvasEx->cd(); // canvasEx->cd();
hEx->Draw(); // hEx->Draw();
// TCanvas * chaha = new TCanvas("chaha", "c haah", 500, 500); // TCanvas * chaha = new TCanvas("chaha", "c haah", 500, 500);
// haha[0][1][100]->Draw(); // haha[0][1][100]->Draw();