BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
copyEvents.C
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2// //
3// copyEvents.C //
4// //
5// An example macro how to copy only a part of events from a source file //
6// //
7////////////////////////////////////////////////////////////////////////////////
8
9int copyEvents(TString inputFilePath, TString outputFilePath, Int_t nStartEvent = 10000, Int_t nEvents = 100)
10{
11 if (inputFilePath == "") {
12 cout << "ERROR: Input file path was not specified" << endl;
13 return -1;
14 }
15 gSystem->ExpandPathName(inputFilePath);
16
17 if (outputFilePath == "") {
18 cout << "ERROR: Output file path was not specified" << endl;
19 return -2;
20 }
21 gSystem->ExpandPathName(outputFilePath);
22
23 TFile* inputFile = new TFile(inputFilePath.Data());
24 if (!inputFile) {
25 cout << "ERROR: Input file was not opened: " << inputFilePath << endl;
26 return -3;
27 }
28
29 TFile* outputFile = new TFile(outputFilePath.Data(), "RECREATE");
30 if (!outputFile) {
31 cout << "ERROR: Output file was not opened: " << outputFilePath << endl;
32 return -4;
33 }
34
35 // loop on all entries to find all trees (or FairRootManager::GetTreeName() can be used)
36 TTree* inputTree = (TTree*)inputFile->Get(FairRootManager::GetTreeName());
37 if (!inputTree) {
38 cout << "ERROR: Input tree was not obtained: " << FairRootManager::GetTreeName() << endl;
39 return -5;
40 }
41 TTree* outputTree = inputTree->CloneTree(0);
42 inputTree->CopyAddresses(outputTree);
43
44 const auto nentries = inputTree->GetEntries();
45 if (nEvents == 0)
46 nEvents = nentries - nStartEvent;
47 else if ((nStartEvent + nEvents) > nentries)
48 nEvents = nentries - nStartEvent;
49 cout << "Tree Name: " << FairRootManager::GetTreeName() << ", event count = " << nEvents << endl;
50
51 for (int i = nStartEvent; i < nStartEvent + nEvents; i++) {
52 inputTree->GetEntry(i);
53 outputTree->Fill();
54 }
55 outputTree->Write();
56
57 delete inputFile;
58 delete outputFile;
59 return 0;
60}
int i
Definition P4_F32vec4.h:22
int Int_t
R__EXTERN TSystem * gSystem
T * Get(const char *namecycle)
const char * Data() const
virtual Int_t Fill()
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
virtual Long64_t GetEntries() const
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const override
virtual TTree * CloneTree(Long64_t nentries=-1, Option_t *option="")
virtual void CopyAddresses(TTree *, bool undo=false)
int copyEvents(TString inputFilePath, TString outputFilePath, Int_t nStartEvent=10000, Int_t nEvents=100)
Definition copyEvents.C:9