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
25//
26// method implementations
27//
28
29namespace SH
30{
32 {
33 for (SampleHandler::iterator sample = sh.begin(),
34 end = sh.end(); sample != end; ++ sample)
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 RCU_THROW_MSG ("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 RCU_THROW_MSG ("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 RCU_THROW_MSG ("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 if (!dynamic_cast<SampleLocal*>(&sample))
75 RCU_THROW_MSG ("sample not of type SampleLocal");
76
77 TObject *meta = sample.meta()->get (MetaFields::numEventsPerFile);
78 if (!meta)
79 {
80 RCU_WARN_MSG ("sample " + sample.name() + " lacks nc_nevtfile, running scanNEvents, please save sample");
81 scanNEvents (sample);
82 meta = sample.meta()->get (MetaFields::numEventsPerFile);
83 }
84 RCU_ASSERT (meta != 0);
85 MetaVector<Long64_t> *const nentries
86 = dynamic_cast<MetaVector<Long64_t> *>(meta);
87 if (nentries == 0)
88 RCU_THROW_MSG ("nc_nevtfile is of the wrong type");
89 if (nentries->value.size() != sample.numFiles())
90 RCU_THROW_MSG ("nc_nevtfile has the wrong number of entries");
91
92 SampleHandler result;
93 std::unique_ptr<SampleLocal> res;
94 Long64_t num = 0;
95 const std::string meta_tree = sample.meta()->castString (MetaFields::treeName, MetaFields::treeName_default);
96 const double meta_xs = sample.meta()->castDouble (MetaFields::crossSection, 0);
97 for (unsigned file = 0, end = nentries->value.size(); file != end; ++ file)
98 {
99 if (num > 0 && num + nentries->value[file] > nevt)
100 {
101 result.add (std::move (res));
102 res.reset();
103 num = 0;
104 }
105 if (res.get() == 0)
106 {
107 std::ostringstream name;
108 name << sample.name() << "_" << result.size();
109 res.reset (new SampleLocal (name.str()));
110 res->tags (sample.tags());
111 res->meta()->fetch (*sample.meta());
112 if (!meta_tree.empty())
113 res->meta()->setString (MetaFields::treeName, meta_tree);
114 if (meta_xs != 0)
115 res->meta()->setDouble (MetaFields::crossSection, meta_xs);
116 }
117 res->add (sample.fileName (file));
118 num += nentries->value[file];
119 }
120 if (num > 0)
121 result.add (std::move (res));
122 return result;
123 }
124}
#define RCU_ASSERT(x)
Definition Assert.h:217
std::pair< std::vector< unsigned int >, bool > res
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:53
#define RCU_WARN_MSG(message)
Definition PrintMsg.h:47
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.
boost::transform_iterator< SamplePtrToRawSample, std::vector< std::shared_ptr< Sample > >::const_iterator > iterator
the iterator to use
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 PrunDriver.h:15
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