Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:12:31

0001 #include <TFile.h>
0002 #include <TTree.h>
0003 #include <TBranch.h>
0004 
0005 void AddBranchFromFileAToFileB(const char* fileAName, const char* fileBName) {
0006     // Open fileB.root
0007     TFile* fileB = TFile::Open(fileBName, "UPDATE");
0008     if (!fileB || fileB->IsZombie()) {
0009         std::cerr << "Error: Could not open fileB.root" << std::endl;
0010         return;
0011     }
0012 
0013     // Open fileA.root
0014     TFile* fileA = TFile::Open(fileAName, "READ");
0015     if (!fileA || fileA->IsZombie()) {
0016         std::cerr << "Error: Could not open fileA.root" << std::endl;
0017         fileB->Close();
0018         return;
0019     }
0020 
0021     // Get the TTree from fileB.root
0022     TTree* treeB = dynamic_cast<TTree*>(fileB->Get("EventTree"));
0023     if (!treeB) {
0024         std::cerr << "Error: Could not retrieve TTree from fileB.root" << std::endl;
0025         fileA->Close();
0026         fileB->Close();
0027         return;
0028     }
0029 
0030     // Get the TBranch from fileA.root
0031     TTree* treeA = dynamic_cast<TTree*>(fileA->Get("EventTree"));
0032     if (!treeA) {
0033         std::cerr << "Error: Could not retrieve TTree from fileA.root" << std::endl;
0034         fileA->Close();
0035         fileB->Close();
0036         return;
0037     }
0038 
0039     TBranch* branchA = treeA->GetBranch("clk");
0040     if (!branchA) {
0041         std::cerr << "Error: Could not retrieve TBranch from fileA.root" << std::endl;
0042         fileA->Close();
0043         fileB->Close();
0044         return;
0045     }
0046 
0047     // Clone the TBranch from fileA.root and add it to treeB
0048     // TBranch* newBranch = treeA->CloneTree()->GetBranch("clk");
0049     treeB -> Branch(branchA)
0050     // TBranch* newBranch = branchA->Branch("new_branch_name", 0);
0051 
0052     // Loop over entries in fileA.root, read the value of the branch,
0053     // and fill it into the corresponding branch in treeB
0054     Long64_t numEntries = treeA->GetEntries();
0055     for (Long64_t i = 0; i < 2; ++i) {
0056         treeA->GetEntry(i);
0057         newBranch->Fill();
0058     }
0059 
0060     // Write the changes to fileB.root
0061     fileB->Write("", TObject::kOverwrite);
0062 
0063     // Clean up
0064     fileA->Close();
0065     fileB->Close();
0066 }
0067 
0068 
0069 int event_combiner_test3() {
0070     const char* file1Name = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/intt_run20869_test.root";
0071     const char* file2Name = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/centrality_run20869_test.root";
0072 
0073     AddBranchFromFileAToFileB(file2Name, file1Name);
0074 
0075     return 0;
0076 }
0077 
0078 
0079 
0080 
0081 // int event_combiner_test3() {
0082 //     // const char* inputFile1 = "input1.root";
0083 //     // const char* inputFile2 = "input2.root";
0084 //     // const char* outputFile = "output.root";
0085 
0086 //     const char* inputFile1 = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/intt_run20869.root";
0087 //     const char* inputFile2  = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/centrality_run20869.root";
0088 //     const char* outputFile      = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/Final_combine_merge_test.root";
0089 
0090 //     MergeTTrees(inputFile1, inputFile2, outputFile);
0091 
0092 //     return 0;
0093 // }