ATLAS Offline Software
Loading...
Searching...
No Matches
ToolsDuplicates.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
12
13#include <memory>
14#include <set>
15#include <sstream>
16#include <stdexcept>
17#include <utility>
18#include <TChain.h>
19#include <TFile.h>
20#include <TLeaf.h>
21#include <TTree.h>
27
28//
29// method implementations
30//
31
32namespace SH
33{
34 namespace
35 {
41 using RunEvent = std::pair<ULong64_t,ULong64_t>;
42
44 using RunEventList = std::set<RunEvent>;
45
46
47
55 struct NumberBranch
56 {
57 ULong64_t m_value64 = 0;
58 UInt_t m_value32 = 0;
59 bool m_is64 = false;
60
61 void connect (TTree& tree, const std::string& name)
62 {
63 TLeaf *leaf = tree.GetLeaf (name.c_str());
64 if (leaf == nullptr)
65 throw std::runtime_error ("failed to find leaf: " + name);
66 const std::string type = leaf->GetTypeName();
67 m_is64 = (type == "ULong64_t" || type == "Long64_t");
68 tree.SetBranchStatus (name.c_str(), 1);
69 const Int_t rc = m_is64
70 ? tree.SetBranchAddress (name.c_str(), &m_value64)
71 : tree.SetBranchAddress (name.c_str(), &m_value32);
72 if (rc < 0)
73 throw std::runtime_error ("failed to set branch address for: " + name);
74 }
75
76 ULong64_t value () const
77 {
78 return m_is64 ? m_value64 : ULong64_t (m_value32);
79 }
80 };
81
82
83
87 const std::set<std::string>& runNames ()
88 {
89 static const std::set<std::string> result =
90 { "RunNumber", "runNumber" };
91 return result;
92 }
93
94
95
99 const std::set<std::string>& eventNames ()
100 {
101 static const std::set<std::string> result =
102 { "EventNumber", "eventNumber" };
103 return result;
104 }
105
106
107
112 std::string
113 findBranch (const TTree& tree, const std::set<std::string>& names)
114 {
115 TTree& tree_nc ATLAS_THREAD_SAFE = const_cast<TTree&>(tree);
116 TObjArray *branches = tree_nc.GetListOfBranches();
117
118 for (const auto& name : names)
119 {
120 if (branches->FindObject (name.c_str()) != 0)
121 return name;
122 }
123 throw std::runtime_error ("failed to find branch of valid name");
124 }
125
126
127
133 void printDuplicateEvents (TTree& tree, RunEventList& list)
134 {
135 using namespace msgDuplicates;
136
137 const Long64_t nentries = tree.GetEntries();
138 if (nentries < 0)
139 throw std::runtime_error ("failed to read number of events from n-tuple");
140 if (nentries == 0)
141 return;
142
143 const std::string runName = findBranch (tree, runNames());
144 const std::string eventName = findBranch (tree, eventNames());
145
146 tree.SetBranchStatus ("*", 0);
147 NumberBranch run;
148 run.connect (tree, runName);
149 NumberBranch event;
150 event.connect (tree, eventName);
151
152 tree.SetCacheSize (10 * 1024 * 1024);
153 for (Long64_t entry = 0;
154 entry < nentries; ++ entry)
155 {
156 if (tree.GetEntry (entry) < 0)
157 throw std::runtime_error ("failed to read event");
158 RunEvent runEvent (run.value(), event.value());
159 if (list.find (runEvent) == list.end())
160 {
161 list.insert (runEvent);
162 } else
163 {
164 ANA_MSG_WARNING ("duplicate event run=" << run.value() << " event=" << event.value() << " file=" << tree.GetCurrentFile()->GetName());
165 }
166 }
167 }
168 }
169
170
171
172 void printDuplicateEvents (const Sample& sample)
173 {
174 RunEventList list;
175 std::unique_ptr<TChain> chain (sample.makeTChain ());
176 printDuplicateEvents (*chain, list);
177 }
178
179
180
182 {
183 for (auto *sample : sh)
184 {
185 printDuplicateEvents (*sample);
186 }
187 }
188
189
190
192 {
193 RunEventList list;
194 for (auto *sample : sh)
195 {
196 std::unique_ptr<TChain> chain (sample->makeTChain ());
197 printDuplicateEvents (*chain, list);
198 }
199 }
200}
#define ANA_MSG_WARNING(xmsg,...)
Macro printing warning messages.
static Double_t rc
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
A class that manages a list of Sample objects.
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition Sample.h:49
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition DiskList.cxx:21
void printDuplicateEventsSplit(const SampleHandler &sh)
effects: check each sample for duplicate events and then print them out guarantee: basic,...
void printDuplicateEvents(const Sample &sample)
effects: check the given sample for duplicate events and then print them out guarantee: basic,...
void printDuplicateEventsJoint(const SampleHandler &sh)
effects: check for duplicate events between all the samples and then print them out guarantee: basic,...
list(name, path='/')
Definition histSizes.py:38
TChain * tree
int run(int argc, char *argv[])