File indexing completed on 2025-08-06 08:12:39
0001 #include <iostream>
0002 #include <fstream>
0003 #include <vector>
0004 #include <string>
0005
0006 std::vector<std::string> readFileToVector(const std::string& filePath) {
0007 std::vector<std::string> lines;
0008 std::ifstream inputFile(filePath);
0009
0010 if (!inputFile) {
0011 std::cerr << "Error: Unable to open file at " << filePath << std::endl;
0012 return lines;
0013 }
0014
0015 std::string line;
0016 while (std::getline(inputFile, line)) {
0017 lines.push_back(line);
0018 }
0019
0020 inputFile.close();
0021
0022 for (const auto& string_line : lines) {
0023 std::cout << string_line << std::endl;
0024 }
0025
0026 return lines;
0027 }
0028
0029
0030
0031 void split_tree_TChain(std::string generator_name, int FileList_index = 1, int File_shift_index = 0) {
0032
0033 std::map<std::string,std::string> sample_directory ={
0034 {"HIJING", "Sim_HIJING_MDC2_ana472_20250307"},
0035 {"AMPT", "Sim_AMPT_MDC2_ana472_20250310"},
0036 {"EPOS", "Sim_EPOS_MDC2_ana472_20250310"},
0037 {"HStrange", "Sim_HIJING_strangeness_MDC2_ana472_20250310"}
0038 };
0039
0040 string input_directory = Form("/sphenix/user/ChengWei/sPH_dNdeta/Run24AuAuMC/%s", sample_directory[generator_name].c_str());
0041 string output_directory = Form("/sphenix/user/ChengWei/sPH_dNdeta/Run24AuAuMC/%s/per5k", sample_directory[generator_name].c_str());
0042
0043 std::cout << "Input directory: " << input_directory << std::endl;
0044 std::cout << "Output directory: " << output_directory << std::endl;
0045
0046 system (Form("mkdir -p %s", output_directory.c_str()));
0047
0048 string input_filelist = Form("FileList_00%d.txt",FileList_index);
0049 string TreeName = "EventTree";
0050 int nEvent_EachFile = 5000;
0051 string input_filename_NoSuffix = "ntuple_per5k";
0052
0053 std::vector<std::string> file_list = readFileToVector(input_directory + "/" + input_filelist);
0054
0055
0056
0057 TChain * inputTree = new TChain(TreeName.c_str());
0058 for (const auto& file : file_list) {
0059 inputTree->Add((file).c_str());
0060 }
0061
0062
0063 Long64_t nTotalEvents = inputTree->GetEntries();
0064
0065
0066 int nFiles = nTotalEvents / nEvent_EachFile;
0067
0068
0069 std::cout << "Total events: " << nTotalEvents << std::endl;
0070 std::cout << "Events per file: " << nEvent_EachFile << std::endl;
0071 std::cout << "n output files: " << nFiles << std::endl;
0072
0073
0074
0075 for (int i = 0; i < nFiles; ++i) {
0076
0077 Long64_t startEvent = i * nEvent_EachFile;
0078 Long64_t endEvent = (i == nFiles - 1) ? nTotalEvents - 1 : startEvent + nEvent_EachFile - 1;
0079
0080 std::cout<<"In core: "<<i<<" startEvent: "<<startEvent<<" endEvent: "<<endEvent<<std::endl;
0081
0082
0083 if (startEvent >= nTotalEvents) break;
0084
0085 std::string job_index = std::to_string( i + File_shift_index );
0086 int job_index_len = 5;
0087 job_index.insert(0, job_index_len - job_index.size(), '0');
0088
0089
0090 string output_filename = input_filename_NoSuffix + "_" + job_index + ".root";
0091 TFile *outputFile = TFile::Open((output_directory+"/"+output_filename).c_str(), "RECREATE");
0092
0093
0094 TTree *outputTree = inputTree->CopyTree(Form("Entry$ >= %lld && Entry$ <= %lld", startEvent, endEvent));
0095
0096
0097 outputTree->Write();
0098 outputFile->Close();
0099 std::cout << "Created file: " << output_filename << " with events [" << startEvent << " - " << endEvent << "]" << std::endl;
0100 }
0101
0102
0103
0104 }