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 
0004 void RemoveFirstTwoEvents(const char* inputFile, const char* outputFile, const char* treeName = "EventTree") {
0005     // Open the input ROOT file
0006     TFile* inFile = TFile::Open(inputFile, "READ");
0007     if (!inFile || inFile->IsZombie()) {
0008         std::cerr << "Error: Could not open input file." << std::endl;
0009         return;
0010     }
0011 
0012     // Get the input TTree
0013     TTree* inTree = dynamic_cast<TTree*>(inFile->Get(treeName));
0014     if (!inTree) {
0015         std::cerr << "Error: Could not retrieve input TTree." << std::endl;
0016         inFile->Close();
0017         return;
0018     }
0019 
0020     // Create a new output ROOT file
0021     TFile* outFile = TFile::Open(outputFile, "RECREATE");
0022     if (!outFile || outFile->IsZombie()) {
0023         std::cerr << "Error: Could not create output file." << std::endl;
0024         inFile->Close();
0025         return;
0026     }
0027 
0028     // Clone the input TTree to create a new TTree in the output file
0029     TTree* outTree = inTree->CloneTree(0); // Create new tree with default buffer size
0030 
0031     // Get the total number of events in the input tree
0032     Long64_t totalEvents = inTree->GetEntries();
0033 
0034     // Loop over the events in the input tree, skipping the first two events
0035     for (Long64_t i = 2; i < totalEvents; ++i) {
0036         inTree->GetEntry(i);
0037         outTree->Fill();
0038     }
0039 
0040     // Write the modified tree to the output file
0041     outTree->Write();
0042 
0043     // Close the input and output files
0044     inFile->Close();
0045     outFile->Close();
0046 
0047     // Clean up
0048     delete inFile;
0049     delete outFile;
0050 }
0051 
0052 int event_combiner_test() {
0053     const char*  inputFile = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/centrality_run20869.root";
0054     const char* outputFile = "/sphenix/user/ChengWei/INTT/INTT_commissioning/ZeroField/20869/folder_INTTMBD_EventCombiner/centrality_run20869_out.root";
0055 
0056     RemoveFirstTwoEvents(inputFile, outputFile);
0057 
0058     return 0;
0059 }