ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
D3PDTools
SampleHandler
Root
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
11
#include <
SampleHandler/ToolsSplit.h
>
12
13
#include <
RootCoreUtils/Assert.h
>
14
#include <
SampleHandler/MessageCheck.h
>
15
#include <
SampleHandler/MetaFields.h
>
16
#include <
SampleHandler/MetaObject.h
>
17
#include <
SampleHandler/MetaVector.h
>
18
#include <
SampleHandler/SampleHandler.h
>
19
#include <
SampleHandler/SampleLocal.h
>
20
#include <TFile.h>
21
#include <TTree.h>
22
#include <memory>
23
#include <sstream>
24
#include <stdexcept>
25
26
//
27
// method implementations
28
//
29
30
namespace
SH
31
{
32
void
scanNEvents
(
SampleHandler
&
sh
)
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
}
Assert.h
RCU_ASSERT
#define RCU_ASSERT(x)
Definition
Assert.h:210
ANA_MSG_WARNING
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
Definition
Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:293
res
std::pair< std::vector< unsigned int >, bool > res
Definition
JetGroupProductTest.cxx:11
MetaFields.h
MetaObject.h
MetaVector.h
MessageCheck.h
SampleHandler.h
SampleLocal.h
ToolsSplit.h
SH::MetaVector
This class defines a templatized version of the meta-data in vector form.
Definition
MetaVector.h:20
SH::SampleHandler
A class that manages a list of Sample objects.
Definition
SampleHandler.h:53
SH::SampleLocal
A Sample based on a simple file list.
Definition
SampleLocal.h:30
SH::Sample
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition
Sample.h:49
entries
double entries
Definition
listroot.cxx:49
RCU::Shell
Definition
ShellExec.cxx:26
SH
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition
DiskList.cxx:21
SH::scanNEvents
void scanNEvents(SampleHandler &sh)
effects: scan each sample in the sample handler and store the number of entries per file in the meta-...
Definition
ToolsSplit.cxx:32
SH::splitSample
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...
Definition
ToolsSplit.cxx:72
meta
-diff
SH::MetaFields::numEvents
static const std::string numEvents
the number of events
Definition
MetaFields.h:56
SH::MetaFields::treeName_default
static const std::string treeName_default
the default value of treeName
Definition
MetaFields.h:47
SH::MetaFields::numEventsPerFile
static const std::string numEventsPerFile
the number of events in each file
Definition
MetaFields.h:59
SH::MetaFields::treeName
static const std::string treeName
the name of the tree in the sample
Definition
MetaFields.h:44
SH::MetaFields::crossSection
static const std::string crossSection
the cross section field
Definition
MetaFields.h:50
tree
TChain * tree
Definition
tile_monitor.h:30
file
TFile * file
Definition
tile_monitor.h:29
Generated on
for ATLAS Offline Software by
1.17.0