How to add multiple files created over multi threads in TThread into one file

I am using TThreads to perform a calibration process over multiple threads. My primary input file is around 5 GB and I am spawning my threads to fill the histograms over 1024 channels quickly and then calibrating them separately. In the end, I am able to write multiple TFiles (depends on number of threads) in output with calibrated values. I want to know if I can merge all these files into one in the same code itself. I tried to use TBufferMerger but it leaves me in an infinite loop every time. Is there any efficient way to do this??
The code is attached below.

Kindly check the function

void* write_outfile(void* ptr){
Long64_t* N = (Long64_t*) ptr;
// std::string fileName = oname;
// ROOT::Experimental::TBufferMerger merger(“testfile.root”, “RECREATE”);
// auto file = merger.GetFile();

for(auto j=N[1]; j<N[2]; j++){
    // auto tree = new TTree("data","data");
    // tree->Branch("q",&qcalib,"qcalib[1024]/F");
    // tree->Branch("t",&tcalib,"tcalib[1024]/l");
        if(CHID >63 && CHID <320){continue;}
        if(q[CHID]>0 ){
            //Float_t S= pow(q[CHID],par[CHID][2]);
            qcalib[CHID] = q[CHID]*S[CHID];
return 0;


void write_outfile_over_threads()
array<TThread*,nth> VT1;
array<Long64_t[3],nth> par1;

for(int i=0; i<nth; i++){
    par1[i][0] = i;
    par1[i][1] = i*(N/nth);
    par1[i][2] = (i+1)*(N/nth);
    VT1[i] = new TThread((string("T")+to_string(i)).data(), write_outfile, (void*) par1[i]);

for(int i=0;i<nth;i++) VT1[i]->Run();
for(int i=0;i<nth;i++) VT1[i]->Join();


ROOT Version: 6.22.08
Platform: Ubuntu 20.04 LTS

This seems a duplicate of How to merge TFiles created over different threads - #4 by Siddharth_Parashari , let’s continue there.