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