ATLAS Offline Software
Loading...
Searching...
No Matches
ToolsSplit.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
12
20#include <TFile.h>
21#include <TTree.h>
22#include <memory>
23#include <sstream>
24#include <stdexcept>
25
26//
27// method implementations
28//
29
30namespace SH
31{
33 {
34 for (auto *sample : sh)
35 scanNEvents (*sample);
36 }
37
38
39
40 void scanNEvents (Sample& sample)
41 {
42 SampleLocal *const mysample = dynamic_cast<SampleLocal*>(&sample);
43 if (!mysample)
44 throw std::runtime_error ("sample not of type SampleLocal");
45
46 const std::string tree_name = sample.meta()->castString (MetaFields::treeName, MetaFields::treeName_default);
47 if (tree_name.empty())
48 throw std::runtime_error ("scanNEvents: sample doesn't contain a tree name");
49
50 Long64_t tot_entries = 0;
51 std::vector<Long64_t> entries;
52 const auto fileList = sample.makeFileList ();
53 for (const std::string& file_name : fileList)
54 {
55 std::unique_ptr<TFile> file (TFile::Open (file_name.c_str(), "READ"));
56 if (!file.get())
57 throw std::runtime_error ("failed to open file " + file_name);
58 Long64_t treeEntries = 0;
59 TTree *const tree = dynamic_cast<TTree*>(file->Get (tree_name.c_str()));
60 if (tree != 0)
61 treeEntries = tree->GetEntries ();
62 entries.push_back (treeEntries);
63 tot_entries += treeEntries;
64 }
65 RCU_ASSERT (entries.size() == fileList.size());
66 sample.meta()->addReplace (new MetaVector<Long64_t>(MetaFields::numEventsPerFile, entries));
67 sample.meta()->setDouble (MetaFields::numEvents, tot_entries);
68 }
69
70
71
72 SampleHandler splitSample (Sample& sample, const Long64_t nevt)
73 {
74 using namespace msgSplit;
75
76 if (!dynamic_cast<SampleLocal*>(&sample))
77 throw std::runtime_error ("sample not of type SampleLocal");
78
79 TObject *meta = sample.meta()->get (MetaFields::numEventsPerFile);
80 if (!meta)
81 {
82 ANA_MSG_WARNING ("sample " << sample.name() << " lacks nc_nevtfile, running scanNEvents, please save sample");
83 scanNEvents (sample);
84 meta = sample.meta()->get (MetaFields::numEventsPerFile);
85 }
86 RCU_ASSERT (meta != 0);
87 MetaVector<Long64_t> *const nentries
88 = dynamic_cast<MetaVector<Long64_t> *>(meta);
89 if (nentries == 0)
90 throw std::runtime_error ("nc_nevtfile is of the wrong type");
91 if (nentries->value.size() != sample.numFiles())
92 throw std::runtime_error ("nc_nevtfile has the wrong number of entries");
93
94 SampleHandler result;
95 std::unique_ptr<SampleLocal> res;
96 Long64_t num = 0;
97 const std::string meta_tree = sample.meta()->castString (MetaFields::treeName, MetaFields::treeName_default);
98 const double meta_xs = sample.meta()->castDouble (MetaFields::crossSection, 0);
99 for (unsigned file = 0, end = nentries->value.size(); file != end; ++ file)
100 {
101 if (num > 0 && num + nentries->value[file] > nevt)
102 {
103 result.add (std::move (res));
104 res.reset();
105 num = 0;
106 }
107 if (res.get() == 0)
108 {
109 std::ostringstream name;
110 name << sample.name() << "_" << result.size();
111 res = std::make_unique<SampleLocal> (name.str());
112 res->tags (sample.tags());
113 res->meta()->fetch (*sample.meta());
114 if (!meta_tree.empty())
115 res->meta()->setString (MetaFields::treeName, meta_tree);
116 if (meta_xs != 0)
117 res->meta()->setDouble (MetaFields::crossSection, meta_xs);
118 }
119 res->add (sample.fileName (file));
120 num += nentries->value[file];
121 }
122 if (num > 0)
123 result.add (std::move (res));
124 return result;
125 }
126}
#define RCU_ASSERT(x)
Definition Assert.h:210
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
std::pair< std::vector< unsigned int >, bool > res
This class defines a templatized version of the meta-data in vector form.
Definition MetaVector.h:20
A class that manages a list of Sample objects.
A Sample based on a simple file list.
Definition SampleLocal.h:30
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition Sample.h:49
double entries
Definition listroot.cxx:49
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition DiskList.cxx:21
void scanNEvents(SampleHandler &sh)
effects: scan each sample in the sample handler and store the number of entries per file in the meta-...
SampleHandler splitSample(Sample &sample, const Long64_t nevt)
effects: split the given sample into a set of samples, with each sample containing either exactly one...
-diff
static const std::string numEvents
the number of events
Definition MetaFields.h:56
static const std::string treeName_default
the default value of treeName
Definition MetaFields.h:47
static const std::string numEventsPerFile
the number of events in each file
Definition MetaFields.h:59
static const std::string treeName
the name of the tree in the sample
Definition MetaFields.h:44
static const std::string crossSection
the cross section field
Definition MetaFields.h:50
TChain * tree
TFile * file