Skip to content
Snippets Groups Projects
Commit 8580255d authored by Theodore Efremov's avatar Theodore Efremov :hibiscus:
Browse files

Added some data manipulation macro

parent 08210ec2
No related branches found
No related tags found
1 merge request!27Draft: [Epic] Preparation of the environement for the new GaseousDetectorScorers...
Pipeline #358339 passed
void Merger(int N, const char* outputDir, const char* outputFileName, const char* inputBaseName) {
TString outputPath = Form("%s/%s", outputDir, outputFileName);
TFile *outputFile = new TFile(outputPath, "RECREATE");
TList *fileList = new TList();
for (int i = 0; i < N; i++) {
TString fileName = Form("%s%d.root", inputBaseName, i); // Base name + index + .root
TFile *inputFile = TFile::Open(fileName);
if (!inputFile || inputFile->IsZombie()) {
std::cerr << "Error opening file: " << fileName << std::endl;
continue;
}
fileList->Add(inputFile);
// Display progress
int progress = ((i + 1) * 100) / N;
std::cout << "\rMerging files: " << progress << "% complete." << std::flush;
}
TFileMerger merger;
merger.OutputFile(outputFile, "RECREATE");
merger.Merge(fileList);
std::cout << std::endl << "Merging completed! Output saved at: " << outputPath << std::endl;
outputFile->Close();
}
# Macro to handle data analysis
## Secator
This macro subdivide a root file into N subfile with the same number of
entries.
This is useful if you want to parallelize your analysis.
## Merger
Do the opposite of secator.
# DONT FORGET TO CREATE AN OUTPUT DIR IN THIS DIRECTORY
#include <iostream>
void Secator(const char* inputFileName, const char* outputDir, int N) {
// Open the original ROOT file
TFile* inputFile = TFile::Open(inputFileName, "READ");
if (!inputFile || inputFile->IsZombie()) {
printf("Error: Cannot open file %s\n", inputFileName);
return;
}
// Extract the base name of the input file (without path and extension)
TString baseName = gSystem->BaseName(inputFileName);
baseName.ReplaceAll(".root", ""); // Remove the .root extension
// Ensure the output directory ends with a '/'
TString outputDirStr = outputDir;
if (!outputDirStr.EndsWith("/")) {
outputDirStr.Append("/");
}
// Get the tree from the file
TTree* inputTree = (TTree*)inputFile->Get("RawTree"); // replace with actual tree name
Long64_t totalEntries = inputTree->GetEntries();
// Calculate entries per subfile
Long64_t entriesPerFile = totalEntries / N;
// Loop to create N subfiles
for (int i = 0; i < N; ++i) {
// Create output file name by appending a number to the base name
TString outputFileName = TString::Format("%s%s_%d.root", outputDirStr.Data(), baseName.Data(), i);
TFile* outputFile = TFile::Open(outputFileName, "RECREATE");
// Create a clone of the tree in the new file
TTree* outputTree = inputTree->CloneTree(0);
// Write the appropriate entries to the subfile
Long64_t start = i * entriesPerFile;
Long64_t end = (i == N - 1) ? totalEntries : start + entriesPerFile;
for (Long64_t entry = start; entry < end; ++entry) {
inputTree->GetEntry(entry);
outputTree->Fill();
// Progress bar update
float progress = (float)(entry - start + 1) / (end - start) * 100;
printf("\rProcessing subfile %d: [%.2f%%]", i + 1, progress);
fflush(stdout); // Ensure immediate print
}
printf("\n"); // Newline after progress bar completion for this subfile
// Write and close the subfile
outputTree->Write();
outputFile->Close();
delete outputFile;
}
inputFile->Close();
delete inputFile;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment