ATLAS Offline Software
TestCaloDataAccess.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 #include <iostream>
5 #include <random>
6 #include "tbb/parallel_reduce.h"
7 #include "tbb/blocked_range.h"
8 #include "TestTools/expect.h"
10 #include "Gaudi/Property.h"
15 #include "TestCaloDataAccess.h"
16 #include <sys/time.h>
17 
18 
19 #define DIFF(_name, _a, _b) if ( _a != _b ) \
20  ATH_MSG_WARNING( "Difference in " << _name << " " << _a << " ref " << _b ); \
21  else \
22  ATH_MSG_DEBUG( "Identical " << _name << " " << _a << " ref " << _b );
23 
24 
30 class AskForRoI : public ParallelCallTest, public AthMessaging {
31 public:
32  AskForRoI( const EventContext& context,
34  const TrigRoiDescriptor& roi )
35  : AthMessaging ("TestCaloDataAccess"),
36  m_context( context ),
37  m_svc( svc ),
38  m_roi ( roi ) {
40  }
42  if ( m_colRef ) { m_colRef->clear(); delete m_colRef; }
43  }
44 
46  if ( m_roi.isFullscan() ){
47  std::cout << "wrong RoI descriptor used for RoI" << std::endl;
48  return StatusCode::FAILURE;
49  }
50  else{
51  // keep this for test reasons
52  //usleep (5000);
53  return m_svc->loadCollections( m_context, m_roi, TTEM, 2, sel );
54  }
55  }
56 
58  if ( m_roi.isFullscan() ){
59  return m_svc->loadFullCollections( m_context, c );
60  }
61  else{
62  std::cout << "wrong RoI descriptor used for FS" << std::endl;
63  return StatusCode::FAILURE;
64  }
65  }
66 
67  // calculate reference quantities in the first call
68  void firstCall() override {
69  Gaudi::Hive::setCurrentContext (m_context);
70  if ( m_roi.isFullscan() ) {
71  struct timeval t1{},t2{};
72  gettimeofday(&t1,NULL);
74  m_statusRef.ignore();
75  gettimeofday(&t2,NULL);
76 
77  for ( const auto cell : *m_colRef ) {
78  if ( !cell ) continue;
79  m_etSumRef += cell->et();
80  m_countRef ++;
81  m_minEtaRef = std::min( m_minEtaRef, cell->eta() );
82  m_maxEtaRef = std::max( m_maxEtaRef, cell->eta() );
83  m_minPhiRef = std::min( m_minPhiRef, cell->phi() );
84  m_maxPhiRef = std::max( m_maxPhiRef, cell->phi() );
85  }
86  std::cout << "t lFC : " << m_context << " " << m_etSumRef << " " << t1.tv_sec << " " << t1.tv_usec << " " << t2.tv_sec << " " << t2.tv_usec << " " << ((t2.tv_sec-t1.tv_sec)*1e6+(t2.tv_usec-t1.tv_usec) )*1e-6 << std::endl;
87 
88  } else {
89 
90  struct timeval t1{},t2{};
91  gettimeofday(&t1,NULL);
93  m_statusRef.ignore();
94  gettimeofday(&t2,NULL);
95 
96  for ( const auto cell : m_selRef ) {
97  m_etSumRef += cell->et();
98  m_countRef ++;
99  m_minEtaRef = std::min( m_minEtaRef, cell->eta() );
100  m_maxEtaRef = std::max( m_maxEtaRef, cell->eta() );
101  m_minPhiRef = std::min( m_minPhiRef, cell->phi() );
102  m_maxPhiRef = std::max( m_maxPhiRef, cell->phi() );
103  }
104  std::cout << "t RoI : " << m_context << " " << m_etSumRef << " " << t1.tv_sec << " " << t1.tv_usec << " " << t2.tv_sec << " " << t2.tv_usec << " " << ((t2.tv_sec-t1.tv_sec)*1e6+(t2.tv_usec-t1.tv_usec) )*1e-6 << std::endl;
105  }
106  }
107 
108  bool callAndCompare() const override {
109 
110  Gaudi::Hive::setCurrentContext (m_context);
113  double etSum = 0;
114  size_t count = 0;
115  double minEta = 100;
116  double maxEta = -100;
117  double minPhi = 100;
118  double maxPhi = -100;
120  if ( m_roi.isFullscan() ) {
121  status = request( col );
122  status.ignore();
123 
124  for ( const auto cell : col ) {
125  if ( !cell ) continue;
126  etSum += cell->et();
127  count ++;
128  minEta = std::min( minEta, cell->eta() );
129  maxEta = std::max( maxEta, cell->eta() );
130  minPhi = std::min( minPhi, cell->phi() );
131  maxPhi = std::max( maxPhi, cell->phi() );
132  }
133  } else {
134 
135  status = request( sel );
136  status.ignore();
137 
138  for ( const auto cell : sel ) {
139  etSum += cell->et();
140  count ++;
141  minEta = std::min( minEta, cell->eta() );
142  maxEta = std::max( maxEta, cell->eta() );
143  minPhi = std::min( minPhi, cell->phi() );
144  maxPhi = std::max( maxPhi, cell->phi() );
145  }
146  std::cout << "callAndCompare : " << m_context << " " << count << " " << etSum << " " << minEta << " " << maxEta << " " << minPhi << " " << maxPhi << " " << " " << m_minEtaRef << " " << m_maxEtaRef << " " << m_minPhiRef << " " << m_maxPhiRef << " " << m_etSumRef << " " << m_countRef << std::endl;
147  }
148 
149  DIFF( "RoI mask", status.getCode(), m_statusRef.getCode() );
150  DIFF( "RoI count ", count , m_countRef );
151  DIFF( "RoI etSum ", etSum , m_etSumRef );
152  DIFF( "RoI minEta", minEta, m_minEtaRef );
153  DIFF( "RoI maxEta", maxEta, m_maxEtaRef );
154  DIFF( "RoI minPhi", minPhi, m_minPhiRef );
155  DIFF( "RoI maxPhi", maxPhi, m_maxPhiRef );
156 
157  bool checkStatus = m_statusRef == status
158  and m_countRef == count
159  and m_etSumRef == etSum
160  and m_minEtaRef == minEta
161  and m_maxEtaRef == maxEta
162  and m_minPhiRef == minPhi
163  and m_maxPhiRef == maxPhi;
164 
165  if ( checkStatus == false ) {
166 
167  // iterate over two slectors and compare cell by cell
168  for ( LArTT_Selector<LArCellCont>::const_iterator refIter = m_selRef.begin(), thisIter = sel.begin();
169  refIter != m_selRef.end() and thisIter != sel.end(); ++refIter, ++thisIter ) {
170  const LArCell* refCell = *refIter;
171  const LArCell* thisCell = *thisIter;
172  if ( thisCell->et() != refCell->et() ) {
173  ATH_MSG_WARNING( "eta/phi/et Reference cell " << refCell->eta() << "/" << refCell->phi() << "/" << refCell->et()
174  << " differ from the one in this request " << thisCell->eta() << "/" << thisCell->phi() << "/" << thisCell->et() );
175  }
176  }
177  }
178 
179  return checkStatus;
180  }
181 
182 private:
183  const EventContext& m_context;
186 
190  double m_etSumRef = 0;
191  size_t m_countRef = 0;
192  double m_minEtaRef = 100;
193  double m_maxEtaRef = -100;
194  double m_minPhiRef = 100;
195  double m_maxPhiRef = -100;
196 };
197 
199  ISvcLocator* pSvcLocator ) :
200  ::AthReentrantAlgorithm( name, pSvcLocator ),
201  m_dataAccessSvc( "TrigCaloDataAccessSvc/TrigCaloDataAccessSvc", name ),
202  m_emulateRoIs ( true ),
203  m_emulateFixedRoIs (false)
204 {
205  declareProperty("nFixedRoIs", m_nFixedRoIs = 1);
206  declareProperty("emulateRoIs", m_emulateRoIs);
207  declareProperty("emulateFixedRoIs", m_emulateFixedRoIs);
208 
209 }
210 
212 
214  CHECK( m_dataAccessSvc.retrieve() );
215  return StatusCode::SUCCESS;
216 }
217 
218 void TestCaloDataAccess::emulateRoIs( const EventContext& context, std::vector<ParallelCallTest*>& allRoIs ) const{
219 
220  std::default_random_engine generator;
221  std::normal_distribution<double> N1(0.0, 1.7);
222  std::normal_distribution<double> N2(0.0, 0.2);
223  std::uniform_real_distribution<double> U(0.0, 1.0);
224  std::uniform_real_distribution<double> Uphi(-M_PI, M_PI);
225 
226  double RoI_phi1 = Uphi(generator);
227  double RoI_eta1 = N1(generator);
228  if ( RoI_eta1 < -2.5 ) RoI_eta1 = -2.5;
229  if ( RoI_eta1 > 2.5 ) RoI_eta1 = 2.5;
230  double chance = U(generator);
231  double width = 0.1;
232  TrigRoiDescriptor roi( RoI_eta1, RoI_eta1-width, RoI_eta1+width, // eta
233  RoI_phi1, RoI_phi1-width, RoI_phi1+width, // phi
234  0 );
235  AskForRoI* afr = new AskForRoI( context, m_dataAccessSvc, roi );
236  allRoIs.push_back( afr );
237 
238  chance = U(generator);
239  if ( chance > 0.6 ) {
240  double RoI_eta2 = -RoI_eta1 + N2(generator);
241  double RoI_phi2 = -RoI_phi1 + N2(generator);
242  if ( RoI_eta2 < -2.5 ) RoI_eta2 = -2.5;
243  if ( RoI_eta2 > 2.5 ) RoI_eta2 = 2.5;
244  TrigRoiDescriptor roi( RoI_eta2, RoI_eta2-width, RoI_eta2+width, // eta
245  RoI_phi2, RoI_phi2-width, RoI_phi2+width, // phi
246  0 );
247  AskForRoI* afr = new AskForRoI( context, m_dataAccessSvc, roi );
248  allRoIs.push_back( afr );
249  }
250 
251  for(int i=0;i<10;i++){
252  chance = U(generator);
253  if ( chance > 0.75 ) {
254  double RoI_phi3 = Uphi(generator);
255  double RoI_eta3 = N1(generator);
256  if ( RoI_eta3 < -2.5 ) RoI_eta3 = -2.5;
257  if ( RoI_eta3 > 2.5 ) RoI_eta3 = 2.5;
258  width = 0.1;
259  if ( chance > 0.8 ) width=0.3;
260  TrigRoiDescriptor roi( RoI_eta3, RoI_eta3-width, RoI_eta3+width, // eta
261  RoI_phi3, RoI_phi3-width, RoI_phi3+width, // phi
262  0 );
263  AskForRoI* afr = new AskForRoI( context, m_dataAccessSvc, roi );
264  allRoIs.push_back( afr );
265  }
266  }
267 
268  chance = U(generator);
269  if ( chance > 0.6 ) {
270  TrigRoiDescriptor roi( true );
271  AskForRoI* afr = new AskForRoI( context, m_dataAccessSvc, roi );
272  allRoIs.push_back( afr );
273  }
274 
275 }
276 
277 void TestCaloDataAccess::emulateFixedRoIs( const EventContext& context, std::vector<ParallelCallTest*>& allRoIs ) const{
278 
279  std::vector<TrigRoiDescriptor> rois;
280  TrigRoiDescriptor roi1( 0.7, 0.7-0.1, 0.7+0.1, // eta
281  0.1, 0.1-0.1, 0.1+0.1, // phi
282  0 );
283  TrigRoiDescriptor roi2( 0.8, 0.8-0.2, 0.7+0.2, // eta
284  0.2, 0.2-0.2, 0.2+0.2, // phi
285  0 );
286  TrigRoiDescriptor roi3( -1.7, -1.7-0.4, -1.7+0.4, // eta
287  2.1, 2.1-0.4, 2.1+0.4, // phi
288  0 );
289  TrigRoiDescriptor roi4( -1.0, -1.0-1.4, -1.0+1.4, // eta
290  1.1, 1.1-1.4, 1.1+1.4, // phi
291  0 );
292  rois.push_back(roi1);
293  rois.push_back(roi2);
294  rois.push_back(roi3);
295  rois.push_back(roi4);
296  TrigRoiDescriptor roi5( true );
297  for( int i=0;i<std::min(m_nFixedRoIs,4);++i) {
298  AskForRoI* t1 = new AskForRoI( context, m_dataAccessSvc, rois[i]);
299  allRoIs.push_back(t1);
300  }
301  AskForRoI* t6 = new AskForRoI( context, m_dataAccessSvc, roi5); // FS
302  allRoIs.push_back(t6);
303 
304 
305 }
306 
307 StatusCode TestCaloDataAccess::execute( const EventContext& context ) const {
308  ATH_MSG_DEBUG ( "Executing " << name() << "..." );
309 
310  std::vector<ParallelCallTest*> allRoIs;
311 
312  if ( m_emulateRoIs ) emulateRoIs ( context, allRoIs );
313  if ( m_emulateFixedRoIs ) emulateFixedRoIs ( context, allRoIs );
314 
315 
316  timeval ti1{},ti2{};
317  gettimeofday(&ti1,NULL);
318  bool result = ParallelCallTest::launchTests( 2, allRoIs);
319  gettimeofday(&ti2,NULL);
320  std::cout << ti1.tv_sec << " " << ti1.tv_usec << std::endl;
321  std::cout << ti2.tv_sec << " " << ti2.tv_usec << std::endl;
322  std::cout << name() << "; time : " << 1e-6*( 1e6*(ti2.tv_sec - ti1.tv_sec) + ( ti2.tv_usec - ti1.tv_usec ) ) << std::endl;
323 
324  if ( result == false ) {
325  ATH_MSG_ERROR( "Test of data access failed " );
326  return StatusCode::SUCCESS;
327  }
328 
329  return StatusCode::SUCCESS;
330 }
331 
332 
LUCID_EventTPCnv_Dict::t6
std::vector< LUCID_DigitContainer_p2 > t6
Definition: LUCID_EventTPCnvDict.h:32
AskForRoI::m_context
const EventContext & m_context
Definition: TestCaloDataAccess.cxx:183
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
AskForRoI::m_selRef
LArTT_Selector< LArCellCont > m_selRef
Definition: TestCaloDataAccess.cxx:187
ParallelCallTest
Provides interface and helper functions to perform stress testing of the thread-safe code.
Definition: ParallelCallTest.h:30
expect.h
dqt_zlumi_pandas.N2
N2
Definition: dqt_zlumi_pandas.py:325
TestCaloDataAccess::emulateRoIs
void emulateRoIs(const EventContext &context, std::vector< ParallelCallTest * > &allRoIs) const
Definition: TestCaloDataAccess.cxx:218
CaloCell::phi
virtual double phi() const override final
get phi (through CaloDetDescrElement)
Definition: CaloCell.h:359
get_generator_info.result
result
Definition: get_generator_info.py:21
TestCaloDataAccess::~TestCaloDataAccess
virtual ~TestCaloDataAccess()
Definition: TestCaloDataAccess.cxx:211
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
LArCellCont.h
AskForRoI::AskForRoI
AskForRoI(const EventContext &context, const ServiceHandle< ITrigCaloDataAccessSvc > &svc, const TrigRoiDescriptor &roi)
Definition: TestCaloDataAccess.cxx:32
TestCaloDataAccess::m_emulateFixedRoIs
bool m_emulateFixedRoIs
Definition: TestCaloDataAccess.h:37
AskForRoI::m_svc
const ServiceHandle< ITrigCaloDataAccessSvc > & m_svc
Definition: TestCaloDataAccess.cxx:184
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
LArTT_Selector::end
const_iterator end() const
AskForRoI::m_colRef
CaloConstCellContainer * m_colRef
Definition: TestCaloDataAccess.cxx:188
TestCaloDataAccess.h
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
AskForRoI::m_roi
const TrigRoiDescriptor m_roi
Definition: TestCaloDataAccess.cxx:185
ALFA_EventTPCnv_Dict::t1
std::vector< ALFA_RawDataCollection_p1 > t1
Definition: ALFA_EventTPCnvDict.h:43
TestCaloDataAccess::m_nFixedRoIs
int m_nFixedRoIs
Definition: TestCaloDataAccess.h:35
M_PI
#define M_PI
Definition: ActiveFraction.h:11
AskForRoI::callAndCompare
bool callAndCompare() const override
a function that performs request, and compares the results obtained with the result of the first exec...
Definition: TestCaloDataAccess.cxx:108
AskForRoI::m_minEtaRef
double m_minEtaRef
Definition: TestCaloDataAccess.cxx:192
TrigRoiDescriptor
nope - should be used for standalone also, perhaps need to protect the class def bits #ifndef XAOD_AN...
Definition: TrigRoiDescriptor.h:56
AskForRoI::m_statusRef
StatusCode m_statusRef
Definition: TestCaloDataAccess.cxx:189
TestCaloDataAccess::emulateFixedRoIs
void emulateFixedRoIs(const EventContext &context, std::vector< ParallelCallTest * > &allRoIs) const
Definition: TestCaloDataAccess.cxx:277
ParallelCallTest.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
PUfitVar::maxEta
constexpr float maxEta
Definition: GepMETPufitAlg.cxx:13
TestCaloDataAccess::TestCaloDataAccess
TestCaloDataAccess()
TestCaloDataAccess::m_emulateRoIs
bool m_emulateRoIs
Definition: TestCaloDataAccess.h:36
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
ConstDataVector::clear
void clear()
Erase all the elements in the collection.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TTEM
@ TTEM
Definition: RegSelEnums.h:28
DIFF
#define DIFF(_name, _a, _b)
Definition: TestCaloDataAccess.cxx:19
lumiFormat.i
int i
Definition: lumiFormat.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
sel
sel
Definition: SUSYToolsTester.cxx:97
TestCaloDataAccess::execute
virtual StatusCode execute(const EventContext &context) const override
Definition: TestCaloDataAccess.cxx:307
CaloCell::et
virtual double et() const override final
get et
Definition: CaloCell.h:407
LArTT_Selector::begin
const_iterator begin() const
dqt_zlumi_pandas.N1
int N1
Definition: dqt_zlumi_pandas.py:322
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
AskForRoI::firstCall
void firstCall() override
a method that will be called to obtain first results from the service It should set the reference qua...
Definition: TestCaloDataAccess.cxx:68
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
ParallelCallTest::launchTests
static bool launchTests(size_t nrepeats, const std::vector< ParallelCallTest * > &tests)
Method to run launch number of tests in parallel (increasing the stress of the calle) It has a potent...
Definition: ParallelCallTest.cxx:83
AskForRoI::m_etSumRef
double m_etSumRef
Definition: TestCaloDataAccess.cxx:190
AskForRoI::m_minPhiRef
double m_minPhiRef
Definition: TestCaloDataAccess.cxx:194
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
AskForRoI::request
StatusCode request(CaloConstCellContainer &c) const
Definition: TestCaloDataAccess.cxx:57
LArCell
Data object for LAr calorimeter readout cell.
Definition: LArCell.h:53
LArTT_Selector< LArCellCont >
query_example.col
col
Definition: query_example.py:7
AskForRoI
The test calls for RoI data access for each RoI returned bunch of quantiries are checked,...
Definition: TestCaloDataAccess.cxx:30
ALFA_EventTPCnv_Dict::t2
std::vector< ALFA_RawDataContainer_p1 > t2
Definition: ALFA_EventTPCnvDict.h:44
mc.generator
generator
Configure Herwig7 These are the commands corresponding to what would go into the regular Herwig infil...
Definition: mc.MGH7_FxFx_H71-DEFAULT_test.py:18
AskForRoI::request
StatusCode request(LArTT_Selector< LArCellCont > &sel) const
Definition: TestCaloDataAccess.cxx:45
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
CaloConstCellContainer
CaloCellContainer that can accept const cell pointers.
Definition: CaloConstCellContainer.h:45
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
AthMessaging.h
AskForRoI::~AskForRoI
~AskForRoI()
Definition: TestCaloDataAccess.cxx:41
TriggerTest.rois
rois
Definition: TriggerTest.py:23
RoiDescriptor::isFullscan
virtual bool isFullscan() const override final
is this a full scan RoI?
Definition: RoiDescriptor.h:132
TrigRoiDescriptor.h
merge.status
status
Definition: merge.py:17
TestCaloDataAccess::initialize
virtual StatusCode initialize() override
Definition: TestCaloDataAccess.cxx:213
CaloConstCellContainer.h
CaloCellContainer that can accept const cell pointers.
TestCaloDataAccess::m_dataAccessSvc
ServiceHandle< ITrigCaloDataAccessSvc > m_dataAccessSvc
Definition: TestCaloDataAccess.h:33
python.compressB64.c
def c
Definition: compressB64.py:93
AskForRoI::m_maxPhiRef
double m_maxPhiRef
Definition: TestCaloDataAccess.cxx:195
AskForRoI::m_maxEtaRef
double m_maxEtaRef
Definition: TestCaloDataAccess.cxx:193
AskForRoI::m_countRef
size_t m_countRef
Definition: TestCaloDataAccess.cxx:191
CaloCell::eta
virtual double eta() const override final
get eta (through CaloDetDescrElement)
Definition: CaloCell.h:366
ServiceHandle< ITrigCaloDataAccessSvc >