CPSimMon.cxx 76.3 KB
Newer Older
1
/*
2
  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3
4
5
6
7
*/

// ********************************************************************
//
// NAME:     CPSimMon.cxx
8
// PACKAGE:  TrigT1CaloMonitoring
9
10
//
// AUTHOR:   Peter Faulkner
11
//
12
13
14
15
//
// ********************************************************************

#include <cmath>
16
#include <utility>
17
18
19
20
21
22
23
24
25
26
27
28

#include "LWHists/LWHist.h"
#include "LWHists/TH1F_LW.h"
#include "LWHists/TH2F_LW.h"
#include "LWHists/TH2I_LW.h"

#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/StatusCode.h"
#include "SGTools/StlVectorClids.h"

#include "AthenaMonitoring/AthenaMonManager.h"

29
#include "TrigT1CaloMonitoringTools/ITrigT1CaloMonErrorTool.h"
30
31
32
33
34
35
36
37
38
39
#include "TrigT1CaloMonitoringTools/TrigT1CaloLWHistogramTool.h"

// =============================================================
// L1 objects
// =============================================================
//#include "TrigT1CaloEvent/TriggerTower.h"
//#include "TrigT1CaloEvent/CMXCPHits.h"
//#include "TrigT1CaloEvent/CMXCPTob.h"
//#include "TrigT1CaloEvent/CPMTower.h"

40
41
#include "xAODTrigL1Calo/CPMTobRoIAuxContainer.h"

42
43
44
#include "TrigT1CaloToolInterfaces/IL1CPCMXTools.h"
#include "TrigT1CaloToolInterfaces/IL1CPMTools.h"
#include "TrigT1CaloToolInterfaces/IL1EmTauTools.h"
45
#include "TrigT1CaloUtils/CPAlgorithm.h"
46
#include "TrigT1CaloUtils/CoordToHardware.h"
47
48
#include "TrigT1CaloUtils/DataError.h"
#include "TrigT1CaloUtils/TriggerTowerKey.h"
49
#include "TrigT1Interfaces/CPRoIDecoder.h"
50
51
52
#include "TrigT1Interfaces/Coordinate.h"
#include "TrigT1Interfaces/CoordinateRange.h"
#include "TrigT1Interfaces/TrigT1CaloDefs.h"
53
54
55
56
57
58

// ============================================================================
// xAOD
// ============================================================================
//#include "xAODTrigL1Calo/TriggerTowerContainer.h"
#include "xAODTrigL1Calo/CMXCPHitsAuxContainer.h"
59
60
#include "xAODTrigL1Calo/CMXCPTobAuxContainer.h"
#include "xAODTrigL1Calo/CPMTowerAuxContainer.h"
61
62
// ============================================================================

63
64
#include "CPSimMon.h"
// ============================================================================
65
namespace LVL1 {
66
// ============================================================================
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
CPSimMon::CPSimMon(const std::string &type, const std::string &name,
                   const IInterface *parent)
    : ManagedMonitorToolBase(type, name, parent),
      m_emTauTool("LVL1::L1EmTauTools/L1EmTauTools"),
      m_cpCmxTool("LVL1::L1CPCMXTools/L1CPCMXTools"),
      m_cpmTool("LVL1::L1CPMTools/L1CPMTools"),
      m_errorTool("LVL1::TrigT1CaloMonErrorTool/TrigT1CaloMonErrorTool"),
      m_histTool("LVL1::TrigT1CaloLWHistogramTool/TrigT1CaloLWHistogramTool"),
      m_debug(false), m_rodTES(0), m_overlapPresent(false), m_limitedRoi(0),
      m_histBooked(false), m_h_cpm_em_2d_etaPhi_tt_PpmEqCore(0),
      m_h_cpm_em_2d_etaPhi_tt_PpmNeCore(0),
      m_h_cpm_em_2d_etaPhi_tt_PpmNoCore(0),
      m_h_cpm_em_2d_etaPhi_tt_CoreNoPpm(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmEqCore(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmNeCore(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmNoCore(0),
      m_h_cpm_had_2d_etaPhi_tt_CoreNoPpm(0),
      m_h_cpm_em_2d_etaPhi_tt_PpmEqOverlap(0),
      m_h_cpm_em_2d_etaPhi_tt_PpmNeOverlap(0),
      m_h_cpm_em_2d_etaPhi_tt_PpmNoOverlap(0),
      m_h_cpm_em_2d_etaPhi_tt_OverlapNoPpm(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmEqOverlap(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmNeOverlap(0),
      m_h_cpm_had_2d_etaPhi_tt_PpmNoOverlap(0),
      m_h_cpm_had_2d_etaPhi_tt_OverlapNoPpm(0), m_h_cpm_2d_tt_PpmEqCpmFpga(0),
      m_h_cpm_2d_tt_PpmNeCpmFpga(0), m_h_cpm_2d_tt_PpmNoCpmFpga(0),
      m_h_cpm_2d_tt_CpmNoPpmFpga(0), m_h_cpm_2d_roi_EmEnergySimEqData(0),
      m_h_cpm_2d_roi_EmEnergySimNeData(0), m_h_cpm_2d_roi_EmEnergySimNoData(0),
      m_h_cpm_2d_roi_EmEnergyDataNoSim(0), m_h_cpm_2d_roi_TauEnergySimEqData(0),
      m_h_cpm_2d_roi_TauEnergySimNeData(0),
      m_h_cpm_2d_roi_TauEnergySimNoData(0),
      m_h_cpm_2d_roi_TauEnergyDataNoSim(0), m_h_cpm_2d_roi_EmIsolSimEqData(0),
      m_h_cpm_2d_roi_EmIsolSimNeData(0), m_h_cpm_2d_roi_EmIsolSimNoData(0),
      m_h_cpm_2d_roi_EmIsolDataNoSim(0), m_h_cpm_2d_roi_TauIsolSimEqData(0),
      m_h_cpm_2d_roi_TauIsolSimNeData(0), m_h_cpm_2d_roi_TauIsolSimNoData(0),
      m_h_cpm_2d_roi_TauIsolDataNoSim(0), m_h_cpm_2d_etaPhi_roi_SimEqData(0),
      m_h_cpm_2d_etaPhi_roi_SimNeData(0), m_h_cpm_2d_etaPhi_roi_SimNoData(0),
      m_h_cpm_2d_etaPhi_roi_DataNoSim(0), m_h_cmx_2d_tob_LeftEnergySimEqData(0),
      m_h_cmx_2d_tob_LeftEnergySimNeData(0),
      m_h_cmx_2d_tob_LeftEnergySimNoData(0),
      m_h_cmx_2d_tob_LeftEnergyDataNoSim(0),
      m_h_cmx_2d_tob_RightEnergySimEqData(0),
      m_h_cmx_2d_tob_RightEnergySimNeData(0),
      m_h_cmx_2d_tob_RightEnergySimNoData(0),
      m_h_cmx_2d_tob_RightEnergyDataNoSim(0),
      m_h_cmx_2d_tob_LeftIsolSimEqData(0), m_h_cmx_2d_tob_LeftIsolSimNeData(0),
      m_h_cmx_2d_tob_LeftIsolSimNoData(0), m_h_cmx_2d_tob_LeftIsolDataNoSim(0),
      m_h_cmx_2d_tob_RightIsolSimEqData(0),
      m_h_cmx_2d_tob_RightIsolSimNeData(0),
      m_h_cmx_2d_tob_RightIsolSimNoData(0),
      m_h_cmx_2d_tob_RightIsolDataNoSim(0), m_h_cmx_2d_tob_OverflowSimEqData(0),
      m_h_cmx_2d_tob_OverflowSimNeData(0), m_h_cmx_2d_etaPhi_tob_SimEqData(0),
      m_h_cmx_2d_etaPhi_tob_SimNeData(0), m_h_cmx_2d_etaPhi_tob_SimNoData(0),
      m_h_cmx_2d_etaPhi_tob_DataNoSim(0), m_h_cmx_1d_thresh_SumsSimEqData(0),
      m_h_cmx_1d_thresh_SumsSimNeData(0), m_h_cmx_1d_thresh_SumsSimNoData(0),
      m_h_cmx_1d_thresh_SumsDataNoSim(0),
      m_h_cmx_2d_thresh_LeftSumsSimEqData(0),
      m_h_cmx_2d_thresh_LeftSumsSimNeData(0),
      m_h_cmx_2d_thresh_RightSumsSimEqData(0),
      m_h_cmx_2d_thresh_RightSumsSimNeData(0), m_h_cmx_2d_topo_SimEqData(0),
      m_h_cmx_2d_topo_SimNeData(0), m_h_cmx_2d_topo_SimNoData(0),
      m_h_cmx_2d_topo_DataNoSim(0), m_h_cpm_2d_SimEqDataOverview(0),
      m_h_cpm_2d_SimNeDataOverview(0), m_h_cpm_1d_SimNeDataSummary(0),
      m_v_2d_MismatchEvents(0)
/*---------------------------------------------------------*/
132
133
134
135
136
137
138
{
  declareProperty("EmTauTool", m_emTauTool);
  declareProperty("CPCMXTool", m_cpCmxTool);
  declareProperty("ErrorTool", m_errorTool);
  declareProperty("HistogramTool", m_histTool);

  declareProperty("CPMTowerLocation",
139
                  m_cpmTowerLocation = LVL1::TrigT1CaloDefs::CPMTowerLocation);
140
  declareProperty("CPMTowerLocationOverlap",
141
142
                  m_cpmTowerLocationOverlap =
                      LVL1::TrigT1CaloDefs::CPMTowerLocation + "Overlap");
143
  declareProperty("CMXCPTobLocation",
144
                  m_cmxCpTobLocation = LVL1::TrigT1CaloDefs::CMXCPTobLocation);
145
  declareProperty("CMXCPHitsLocation",
146
147
                  m_cmxCpHitsLocation =
                      LVL1::TrigT1CaloDefs::CMXCPHitsLocation);
148
  declareProperty("CPMTobRoILocation",
149
150
                  m_cpmTobRoiLocation =
                      LVL1::TrigT1CaloDefs::CPMTobRoILocation);
151
  declareProperty("TriggerTowerLocation",
152
153
154
                  m_triggerTowerLocation =
                      LVL1::TrigT1CaloDefs::xAODTriggerTowerLocation);
  // m_triggerTowerLocation = LVL1::TrigT1CaloDefs::TriggerTowerLocation);
155
  declareProperty("RodHeaderLocation",
156
157
158
                  m_rodHeaderLocation =
                      LVL1::TrigT1CaloDefs::RODHeaderLocation);
  declareProperty("ErrorLocation", m_errorLocation = "L1CaloCPMMismatchVector");
159
160
161
162
163
164
165

  declareProperty("RootDirectory", m_rootDir = "L1Calo/CPM");
}

/*---------------------------------------------------------*/
CPSimMon::~CPSimMon()
/*---------------------------------------------------------*/
166
{}
167
168
169
170
171

/*---------------------------------------------------------*/
StatusCode CPSimMon::initialize()
/*---------------------------------------------------------*/
{
172
  msg(MSG::INFO) << "Initializing " << name() << endmsg;
173
174
175
176
177
178

  m_debug = msgLvl(MSG::DEBUG);

  StatusCode sc;

  sc = ManagedMonitorToolBase::initialize();
179
180
  if (sc.isFailure())
    return sc;
181
182

  sc = m_emTauTool.retrieve();
183
  if (sc.isFailure()) {
184
    msg(MSG::ERROR) << "Unable to locate Tool L1EmTauTools" << endmsg;
185
186
187
188
    return sc;
  }

  sc = m_cpCmxTool.retrieve();
189
  if (sc.isFailure()) {
190
    msg(MSG::ERROR) << "Unable to locate Tool L1CPCMXTools" << endmsg;
191
192
193
    return sc;
  }

194
  sc = m_cpmTool.retrieve();
195
  if (sc.isFailure()) {
196
    msg(MSG::ERROR) << "Unable to locate Tool L1CPMTools" << endmsg;
197
198
199
    return sc;
  }

200
  sc = m_errorTool.retrieve();
201
  if (sc.isFailure()) {
202
    msg(MSG::ERROR) << "Unable to locate Tool TrigT1CaloMonErrorTool" << endmsg;
203
204
205
206
    return sc;
  }

  sc = m_histTool.retrieve();
207
  if (sc.isFailure()) {
208
    msg(MSG::ERROR) << "Unable to locate Tool TrigT1CaloLWHistogramTool"
209
                    << endmsg;
210
211
212
213
214
215
    return sc;
  }

  return StatusCode::SUCCESS;
}

216
217
218
219
/*---------------------------------------------------------*/
StatusCode CPSimMon::finalize()
/*---------------------------------------------------------*/
{
220
  return StatusCode::SUCCESS;
221
222
223
}

StatusCode CPSimMon::bookHistogramsRecurrent() {
224
  msg(MSG::DEBUG) << "bookHistograms entered" << endmsg;
225

226
  if (m_environment == AthenaMonManager::online) {
227
228
    // book histograms that are only made in the online environment...
  }
229

230
  if (m_dataType == AthenaMonManager::cosmics) {
231
232
233
    // book histograms that are only relevant for cosmics data...
  }

234
235
  //if (newEventsBlock || newLumiBlock) {
  //}
236

237
  if (newRunFlag()) {
238
239
240

    MgmtAttr_t attr = ATTRIB_UNMANAGED;
    std::string dir1(m_rootDir + "/Errors/Transmission_Simulation");
241
242
243
244
245
246
    MonGroup monShift(this, dir1, run, attr);
    MonGroup monExpert(this, dir1, run, attr);
    MonGroup monCPMin(this, dir1 + "/PPM2CPMTowers", run, attr);
    MonGroup monRoIs(this, dir1 + "/Towers2RoIs", run, attr);
    MonGroup monEvent1(this, dir1 + "/MismatchEventNumbers", run, attr, "",
                       "eventSample");
247
    std::string dir2(m_rootDir + "_CMX/Errors/Transmission_Simulation");
248
249
250
251
252
    MonGroup monCMXin(this, dir2 + "/RoIs2TOBs", run, attr);
    MonGroup monCMXSums(this, dir2 + "/TOBs2HitSums", run, attr);
    MonGroup monCMXTopo(this, dir2 + "/TOBs2Topo", run, attr);
    MonGroup monEvent2(this, dir2 + "/MismatchEventNumbers", run, attr, "",
                       "eventSample");
253

254
255
256
257
258
    // CPMTowers

    m_histTool->setMonGroup(&monCPMin);

    m_h_cpm_em_2d_etaPhi_tt_PpmEqCore = m_histTool->bookCPMEtaVsPhi(
259
260
        "cpm_em_2d_etaPhi_tt_PpmEqCore",
        "EM Core CPM Tower/PPM Tower Non-zero Matches");
261
    m_h_cpm_em_2d_etaPhi_tt_PpmNeCore = m_histTool->bookCPMEtaVsPhi(
262
263
264
265
266
267
268
269
        "cpm_em_2d_etaPhi_tt_PpmNeCore",
        "EM Core CPM Tower/PPM Tower Non-zero Mismatches");
    m_h_cpm_em_2d_etaPhi_tt_PpmNoCore =
        m_histTool->bookCPMEtaVsPhi("cpm_em_2d_etaPhi_tt_PpmNoCore",
                                    "EM PPM Towers but no Core CPM Towers");
    m_h_cpm_em_2d_etaPhi_tt_CoreNoPpm =
        m_histTool->bookCPMEtaVsPhi("cpm_em_2d_etaPhi_tt_CoreNoPpm",
                                    "EM Core CPM Towers but no PPM Towers");
270
    m_h_cpm_had_2d_etaPhi_tt_PpmEqCore = m_histTool->bookCPMEtaVsPhi(
271
272
        "cpm_had_2d_etaPhi_tt_PpmEqCore",
        "HAD Core CPM Tower/PPM Tower Non-zero Matches");
273
    m_h_cpm_had_2d_etaPhi_tt_PpmNeCore = m_histTool->bookCPMEtaVsPhi(
274
275
276
277
278
279
280
281
        "cpm_had_2d_etaPhi_tt_PpmNeCore",
        "HAD Core CPM Tower/PPM Tower Non-zero Mismatches");
    m_h_cpm_had_2d_etaPhi_tt_PpmNoCore =
        m_histTool->bookCPMEtaVsPhi("cpm_had_2d_etaPhi_tt_PpmNoCore",
                                    "HAD PPM Towers but no Core CPM Towers");
    m_h_cpm_had_2d_etaPhi_tt_CoreNoPpm =
        m_histTool->bookCPMEtaVsPhi("cpm_had_2d_etaPhi_tt_CoreNoPpm",
                                    "HAD Core CPM Towers but no PPM Towers");
282
283
284
285
286
287
    m_h_cpm_em_2d_etaPhi_tt_PpmEqOverlap = m_histTool->bookCPMEtaVsPhi(
        "cpm_em_2d_etaPhi_tt_PpmEqOverlap",
        "EM Overlap CPM Tower/PPM Tower Non-zero Matches");
    m_h_cpm_em_2d_etaPhi_tt_PpmNeOverlap = m_histTool->bookCPMEtaVsPhi(
        "cpm_em_2d_etaPhi_tt_PpmNeOverlap",
        "EM Overlap CPM Tower/PPM Tower Non-zero Mismatches");
288
289
290
291
292
293
    m_h_cpm_em_2d_etaPhi_tt_PpmNoOverlap =
        m_histTool->bookCPMEtaVsPhi("cpm_em_2d_etaPhi_tt_PpmNoOverlap",
                                    "EM PPM Towers but no Overlap CPM Towers");
    m_h_cpm_em_2d_etaPhi_tt_OverlapNoPpm =
        m_histTool->bookCPMEtaVsPhi("cpm_em_2d_etaPhi_tt_OverlapNoPpm",
                                    "EM Overlap CPM Towers but no PPM Towers");
294
295
296
297
298
299
    m_h_cpm_had_2d_etaPhi_tt_PpmEqOverlap = m_histTool->bookCPMEtaVsPhi(
        "cpm_had_2d_etaPhi_tt_PpmEqOverlap",
        "HAD Overlap CPM Tower/PPM Tower Non-zero Matches");
    m_h_cpm_had_2d_etaPhi_tt_PpmNeOverlap = m_histTool->bookCPMEtaVsPhi(
        "cpm_had_2d_etaPhi_tt_PpmNeOverlap",
        "HAD Overlap CPM Tower/PPM Tower Non-zero Mismatches");
300
301
302
303
304
305
    m_h_cpm_had_2d_etaPhi_tt_PpmNoOverlap =
        m_histTool->bookCPMEtaVsPhi("cpm_had_2d_etaPhi_tt_PpmNoOverlap",
                                    "HAD PPM Towers but no Overlap CPM Towers");
    m_h_cpm_had_2d_etaPhi_tt_OverlapNoPpm =
        m_histTool->bookCPMEtaVsPhi("cpm_had_2d_etaPhi_tt_OverlapNoPpm",
                                    "HAD Overlap CPM Towers but no PPM Towers");
306
307

    m_h_cpm_2d_tt_PpmEqCpmFpga = m_histTool->bookCPMCrateModuleVsFPGA(
308
309
        "cpm_2d_tt_PpmEqCpmFpga",
        "CPM Tower/PPM Tower Non-zero Matches by FPGA");
310
    m_h_cpm_2d_tt_PpmNeCpmFpga = m_histTool->bookCPMCrateModuleVsFPGA(
311
312
        "cpm_2d_tt_PpmNeCpmFpga",
        "CPM Tower/PPM Tower Non-zero Mismatches by FPGA");
313
    m_h_cpm_2d_tt_PpmNoCpmFpga = m_histTool->bookCPMCrateModuleVsFPGA(
314
        "cpm_2d_tt_PpmNoCpmFpga", "PPM Towers but no CPM Towers by FPGA");
315
    m_h_cpm_2d_tt_CpmNoPpmFpga = m_histTool->bookCPMCrateModuleVsFPGA(
316
        "cpm_2d_tt_CpmNoPpmFpga", "CPM Towers but no PPM Towers by FPGA");
317
318
319
320
321

    // RoIs

    m_histTool->setMonGroup(&monRoIs);

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    m_h_cpm_2d_roi_EmEnergySimEqData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmEnergySimEqData",
            "CPM RoI EM Energy Data/Simulation Non-zero Matches");
    m_h_cpm_2d_roi_EmEnergySimNeData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmEnergySimNeData",
            "CPM RoI EM Energy Data/Simulation Non-zero Mismatches");
    m_h_cpm_2d_roi_EmEnergySimNoData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmEnergySimNoData",
            "CPM RoI EM Energy Simulation but no Data");
    m_h_cpm_2d_roi_EmEnergyDataNoSim =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmEnergyDataNoSim",
            "CPM RoI EM Energy Data but no Simulation");
    m_h_cpm_2d_roi_TauEnergySimEqData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauEnergySimEqData",
            "CPM RoI Tau Energy Data/Simulation Non-zero Matches");
    m_h_cpm_2d_roi_TauEnergySimNeData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauEnergySimNeData",
            "CPM RoI Tau Energy Data/Simulation Non-zero Mismatches");
    m_h_cpm_2d_roi_TauEnergySimNoData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauEnergySimNoData",
            "CPM RoI Tau Energy Simulation but no Data");
    m_h_cpm_2d_roi_TauEnergyDataNoSim =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauEnergyDataNoSim",
            "CPM RoI Tau Energy Data but no Simulation");

    m_h_cpm_2d_roi_EmIsolSimEqData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmIsolSimEqData",
            "CPM RoI EM Isolation Data/Simulation Non-zero Matches");
    m_h_cpm_2d_roi_EmIsolSimNeData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmIsolSimNeData",
            "CPM RoI EM Isolation Data/Simulation Non-zero Mismatches");
    m_h_cpm_2d_roi_EmIsolSimNoData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmIsolSimNoData",
            "CPM RoI EM Isolation Simulation but no Data");
    m_h_cpm_2d_roi_EmIsolDataNoSim =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_EmIsolDataNoSim",
            "CPM RoI EM Isolation Data but no Simulation");
    m_h_cpm_2d_roi_TauIsolSimEqData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauIsolSimEqData",
            "CPM RoI Tau Isolation Data/Simulation Non-zero Matches");
    m_h_cpm_2d_roi_TauIsolSimNeData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauIsolSimNeData",
            "CPM RoI Tau Isolation Data/Simulation Non-zero Mismatches");
    m_h_cpm_2d_roi_TauIsolSimNoData =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauIsolSimNoData",
            "CPM RoI Tau Isolation Simulation but no Data");
    m_h_cpm_2d_roi_TauIsolDataNoSim =
        m_histTool->bookCPMCrateModuleVsChipLocalCoord(
            "cpm_2d_roi_TauIsolDataNoSim",
            "CPM RoI Tau Isolation Data but no Simulation");
387
388

    m_h_cpm_2d_etaPhi_roi_SimEqData = m_histTool->bookCPMRoIEtaVsPhi(
389
390
        "cpm_2d_etaPhi_roi_SimEqData",
        "CPM RoI Data/Simulation Non-zero Matches");
391
    m_h_cpm_2d_etaPhi_roi_SimNeData = m_histTool->bookCPMRoIEtaVsPhi(
392
393
        "cpm_2d_etaPhi_roi_SimNeData",
        "CPM RoI Data/Simulation Non-zero Mismatches");
394
    m_h_cpm_2d_etaPhi_roi_SimNoData = m_histTool->bookCPMRoIEtaVsPhi(
395
        "cpm_2d_etaPhi_roi_SimNoData", "CPM RoI Simulation but no Data");
396
    m_h_cpm_2d_etaPhi_roi_DataNoSim = m_histTool->bookCPMRoIEtaVsPhi(
397
        "cpm_2d_etaPhi_roi_DataNoSim", "CPM RoI Data but no Simulation");
398
399
400
401
402

    // CMX-CP TOBs

    m_histTool->setMonGroup(&monCMXin);

403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    m_h_cmx_2d_tob_LeftEnergySimEqData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftEnergySimEqData",
            "CMX TOB Left Energy Data/Simulation Non-zero Matches");
    m_h_cmx_2d_tob_LeftEnergySimNeData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftEnergySimNeData",
            "CMX TOB Left Energy Data/Simulation Non-zero Mismatches");
    m_h_cmx_2d_tob_LeftEnergySimNoData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftEnergySimNoData",
            "CMX TOB Left Energy Simulation but no Data");
    m_h_cmx_2d_tob_LeftEnergyDataNoSim =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftEnergyDataNoSim",
            "CMX TOB Left Energy Data but no Simulation");
    m_h_cmx_2d_tob_RightEnergySimEqData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightEnergySimEqData",
            "CMX TOB Right Energy Data/Simulation Non-zero Matches");
    m_h_cmx_2d_tob_RightEnergySimNeData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightEnergySimNeData",
            "CMX TOB Right Energy Data/Simulation Non-zero Mismatches");
    m_h_cmx_2d_tob_RightEnergySimNoData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightEnergySimNoData",
            "CMX TOB Right Energy Simulation but no Data");
    m_h_cmx_2d_tob_RightEnergyDataNoSim =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightEnergyDataNoSim",
            "CMX TOB Right Energy Data but no Simulation");

    m_h_cmx_2d_tob_LeftIsolSimEqData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftIsolSimEqData",
            "CMX TOB Left Isolation Data/Simulation Non-zero Matches");
    m_h_cmx_2d_tob_LeftIsolSimNeData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftIsolSimNeData",
            "CMX TOB Left Isolation Data/Simulation Non-zero Mismatches");
    m_h_cmx_2d_tob_LeftIsolSimNoData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftIsolSimNoData",
            "CMX TOB Left Isolation Simulation but no Data");
    m_h_cmx_2d_tob_LeftIsolDataNoSim =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_LeftIsolDataNoSim",
            "CMX TOB Left Isolation Data but no Simulation");
    m_h_cmx_2d_tob_RightIsolSimEqData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightIsolSimEqData",
            "CMX TOB Right Isolation Data/Simulation Non-zero Matches");
    m_h_cmx_2d_tob_RightIsolSimNeData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightIsolSimNeData",
            "CMX TOB Right Isolation Data/Simulation Non-zero Mismatches");
    m_h_cmx_2d_tob_RightIsolSimNoData =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightIsolSimNoData",
            "CMX TOB Right Isolation Simulation but no Data");
    m_h_cmx_2d_tob_RightIsolDataNoSim =
        m_histTool->bookCPMCrateModuleVsTobChipLocalCoord(
            "cmx_2d_tob_RightIsolDataNoSim",
            "CMX TOB Right Isolation Data but no Simulation");
468
469

    m_h_cmx_2d_tob_OverflowSimEqData = m_histTool->bookCPMCrateModuleVsCMX(
470
471
        "cmx_2d_tob_OverflowSimEqData",
        "CMX TOB RoI Overflow Bit Data/Simulation Matches");
472
    m_h_cmx_2d_tob_OverflowSimNeData = m_histTool->bookCPMCrateModuleVsCMX(
473
474
        "cmx_2d_tob_OverflowSimNeData",
        "CMX TOB RoI Overflow Bit Data/Simulation Mismatches");
475
476

    m_h_cmx_2d_etaPhi_tob_SimEqData = m_histTool->bookCPMRoIEtaVsPhi(
477
478
        "cmx_2d_etaPhi_tob_SimEqData",
        "CMX TOB Data/Simulation Non-zero Matches");
479
    m_h_cmx_2d_etaPhi_tob_SimNeData = m_histTool->bookCPMRoIEtaVsPhi(
480
481
        "cmx_2d_etaPhi_tob_SimNeData",
        "CMX TOB Data/Simulation Non-zero Mismatches");
482
    m_h_cmx_2d_etaPhi_tob_SimNoData = m_histTool->bookCPMRoIEtaVsPhi(
483
        "cmx_2d_etaPhi_tob_SimNoData", "CMX TOB Simulation but no Data");
484
    m_h_cmx_2d_etaPhi_tob_DataNoSim = m_histTool->bookCPMRoIEtaVsPhi(
485
        "cmx_2d_etaPhi_tob_DataNoSim", "CMX TOB Data but no Simulation");
486
487
488
489
490
491

    // Local/Remote/Total sums

    m_histTool->setMonGroup(&monCMXSums);

    m_h_cmx_1d_thresh_SumsSimEqData = m_histTool->bookCPMSumCMX(
492
493
        "cmx_1d_thresh_SumsSimEqData",
        "CMX Hit Sums Data/Simulation Non-zero Matches");
494
    m_h_cmx_1d_thresh_SumsSimNeData = m_histTool->bookCPMSumCMX(
495
496
        "cmx_1d_thresh_SumsSimNeData",
        "CMX Hit Sums Data/Simulation Non-zero Mismatches");
497
    m_h_cmx_1d_thresh_SumsSimNoData = m_histTool->bookCPMSumCMX(
498
        "cmx_1d_thresh_SumsSimNoData", "CMX Hit Sums Simulation but no Data");
499
    m_h_cmx_1d_thresh_SumsDataNoSim = m_histTool->bookCPMSumCMX(
500
        "cmx_1d_thresh_SumsDataNoSim", "CMX Hit Sums Data but no Simulation");
501
502

    m_h_cmx_2d_thresh_LeftSumsSimEqData = m_histTool->bookCPMSumVsThreshold(
503
504
        "cmx_2d_thresh_LeftSumsSimEqData",
        "CMX Hit Sums Left Data/Simulation Threshold Non-zero Matches", 0);
505
    m_h_cmx_2d_thresh_LeftSumsSimNeData = m_histTool->bookCPMSumVsThreshold(
506
507
        "cmx_2d_thresh_LeftSumsSimNeData",
        "CMX Hit Sums Left Data/Simulation Threshold Non-zero Mismatches", 0);
508
509
510
511
512
513
514
515
516
517
518
519
    m_h_cmx_2d_thresh_RightSumsSimEqData = m_histTool->bookCPMSumVsThreshold(
        "cmx_2d_thresh_RightSumsSimEqData",
        "CMX Hit Sums Right Data/Simulation Threshold Non-zero Matches", 1);
    m_h_cmx_2d_thresh_RightSumsSimNeData = m_histTool->bookCPMSumVsThreshold(
        "cmx_2d_thresh_RightSumsSimNeData",
        "CMX Hit Sums Right Data/Simulation Threshold Non-zero Mismatches", 1);

    // Topo output information

    m_histTool->setMonGroup(&monCMXTopo);

    m_h_cmx_2d_topo_SimEqData = m_histTool->book2F(
520
521
        "cmx_2d_topo_SimEqData",
        "CMX Topo Output Data/Simulation Non-zero Matches", 8, 0, 8, 3, 0, 3);
522
523
    setLabelsTopo(m_h_cmx_2d_topo_SimEqData);
    m_h_cmx_2d_topo_SimNeData = m_histTool->book2F(
524
525
526
        "cmx_2d_topo_SimNeData",
        "CMX Topo Output Data/Simulation Non-zero Mismatches", 8, 0, 8, 3, 0,
        3);
527
528
    setLabelsTopo(m_h_cmx_2d_topo_SimNeData);
    m_h_cmx_2d_topo_SimNoData = m_histTool->book2F(
529
530
        "cmx_2d_topo_SimNoData", "CMX Topo Output Simulation but no Data", 8, 0,
        8, 3, 0, 3);
531
532
    setLabelsTopo(m_h_cmx_2d_topo_SimNoData);
    m_h_cmx_2d_topo_DataNoSim = m_histTool->book2F(
533
534
        "cmx_2d_topo_DataNoSim", "CMX Topo Output Data but no Simulation", 8, 0,
        8, 3, 0, 3);
535
536
537
538
539
540
    setLabelsTopo(m_h_cmx_2d_topo_DataNoSim);

    // Summary

    m_histTool->setMonGroup(&monExpert);

541
542
543
544
    m_h_cpm_2d_SimEqDataOverview = m_histTool->book2F(
        "cpm_2d_SimEqDataOverview", "CP Transmission/Comparison with "
                                    "Simulation Overview - Events with Matches",
        64, 0, 64, NumberOfSummaryBins, 0, NumberOfSummaryBins);
545
546
547
    m_histTool->cpmCMXCrateModule(m_h_cpm_2d_SimEqDataOverview);
    setLabels(m_h_cpm_2d_SimEqDataOverview, false);

548
549
550
551
552
    m_h_cpm_2d_SimNeDataOverview = m_histTool->book2F(
        "cpm_2d_SimNeDataOverview", "CP Transmission/Comparison with "
                                    "Simulation Overview - Events with "
                                    "Mismatches",
        64, 0, 64, NumberOfSummaryBins, 0, NumberOfSummaryBins);
553
554
555
556
557
    m_histTool->cpmCMXCrateModule(m_h_cpm_2d_SimNeDataOverview);
    setLabels(m_h_cpm_2d_SimNeDataOverview, false);

    m_histTool->setMonGroup(&monShift);

558
559
560
561
    m_h_cpm_1d_SimNeDataSummary = m_histTool->book1F(
        "cpm_1d_SimNeDataSummary",
        "CP Transmission/Comparison with Simulation Mismatch Summary;;Events",
        NumberOfSummaryBins, 0, NumberOfSummaryBins);
562
563
564
565
566
567
    setLabels(m_h_cpm_1d_SimNeDataSummary);

    // Mismatch Event Number Samples

    m_histTool->setMonGroup(&monEvent1);

568
    TH2I_LW *hist = 0;
569
570
    m_v_2d_MismatchEvents.clear();
    m_v_2d_MismatchEvents.resize(7, hist);
571
572
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cpm_em_2d_tt_MismatchEvents", "CPM Towers EM Mismatch Event Numbers");
573
    m_v_2d_MismatchEvents[0] = hist;
574
575
576
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cpm_had_2d_tt_MismatchEvents",
        "CPM Towers Had Mismatch Event Numbers");
577
    m_v_2d_MismatchEvents[1] = hist;
578
579
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cpm_2d_roi_EmMismatchEvents", "CPM RoIs EM Mismatch Event Numbers");
580
    m_v_2d_MismatchEvents[2] = hist;
581
582
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cpm_2d_roi_TauMismatchEvents", "CPM RoIs Tau Mismatch Event Numbers");
583
584
585
586
    m_v_2d_MismatchEvents[3] = hist;

    m_histTool->setMonGroup(&monEvent2);

587
588
589
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cmx_2d_tob_LeftMismatchEvents",
        "CMX TOBs Left Mismatch Event Numbers");
590
    m_v_2d_MismatchEvents[4] = hist;
591
592
593
    hist = m_histTool->bookCPMEventVsCrateModule(
        "cmx_2d_tob_RightMismatchEvents",
        "CMX TOBs Right Mismatch Event Numbers");
594
595
596
    m_v_2d_MismatchEvents[5] = hist;

    hist = m_histTool->bookEventNumbers("cmx_2d_thresh_SumsMismatchEvents",
597
598
                                        "CMX Hit Sums Mismatch Event Numbers",
                                        24, 0., 24.);
599
600
601
602
603
604
605
606
607
608
609
    m_histTool->cpmCrateCMX(hist, 0, false);
    m_histTool->cpmCrateCMX(hist, 8, false);
    m_histTool->cpmCrateCMX(hist, 16, false);
    hist->GetYaxis()->SetBinLabel(1, "Local 0/0");
    hist->GetYaxis()->SetBinLabel(9, "Remote 0/0");
    hist->GetYaxis()->SetBinLabel(15, "Total 3/0");
    hist->GetYaxis()->SetBinLabel(17, "Topo 0/0");
    m_v_2d_MismatchEvents[6] = hist;

    m_histTool->unsetMonGroup();
    m_histBooked = true;
610
611
612

  } // end if (isNewRun ...

613
  msg(MSG::DEBUG) << "Leaving bookHistograms" << endmsg;
614

615
616
617
618
619
620
621
  return StatusCode::SUCCESS;
}

/*---------------------------------------------------------*/
StatusCode CPSimMon::fillHistograms()
/*---------------------------------------------------------*/
{
622
  if (m_debug)
623
    msg(MSG::DEBUG) << "fillHistograms entered" << endmsg;
624
625

  if (!m_histBooked) {
626
    if (m_debug)
627
      msg(MSG::DEBUG) << "Histogram(s) not booked" << endmsg;
628
629
    return StatusCode::SUCCESS;
  }
630

631
632
633
  // Skip events believed to be corrupt or with ROB errors

  if (m_errorTool->corrupt() || m_errorTool->robOrUnpackingError()) {
634
    if (m_debug)
635
      msg(MSG::DEBUG) << "Skipping corrupt event" << endmsg;
636
637
638
639
640
641
642
643
    return StatusCode::SUCCESS;
  }

  // NB. Collection retrieves wrapped in m_storeGate->contains<..>(..)
  // are for those not expected to be on ESD. They should be on bytestream.

  StatusCode sc;

644
645
  // Retrieve Trigger Towers from SG
  const xAOD::TriggerTowerContainer *triggerTowerTES = 0;
646
  sc = evtStore()->retrieve(triggerTowerTES, m_triggerTowerLocation);
647
  if (sc.isFailure() || !triggerTowerTES) {
648
    msg(MSG::DEBUG) << "No Trigger Tower container found" << endmsg;
649
650
  }

651
652
653
  // Retrieve Core and Overlap CPM Towers from SG
  const xAOD::CPMTowerContainer *cpmTowerTES = 0;
  const xAOD::CPMTowerContainer *cpmTowerOvTES = 0;
654
  sc = evtStore()->retrieve(cpmTowerTES, m_cpmTowerLocation);
655
  if (sc.isFailure() || !cpmTowerTES) {
656
    msg(MSG::DEBUG) << "No Core CPM Tower container found" << endmsg;
657
  }
658
659
  if (evtStore()->contains<xAOD::CPMTowerContainer>(
          m_cpmTowerLocationOverlap)) {
660
    sc = evtStore()->retrieve(cpmTowerOvTES, m_cpmTowerLocationOverlap);
661
662
663
  } else
    sc = StatusCode::FAILURE;
  if (sc.isFailure() || !cpmTowerOvTES) {
664
    msg(MSG::DEBUG) << "No Overlap CPM Tower container found" << endmsg;
665
666
  }
  m_overlapPresent = cpmTowerOvTES != 0;
667

668
669
670
671
  // Retrieve CPM TOB RoIs from SG
  const xAOD::CPMTobRoIContainer *cpmRoiTES = 0;
  sc = evtStore()->retrieve(cpmRoiTES, m_cpmTobRoiLocation);
  if (sc.isFailure() || !cpmRoiTES) {
672
    msg(MSG::DEBUG) << "No DAQ CPM TOB RoIs container found" << endmsg;
673
674
  }

675
  // Retrieve ROD Headers from SG
676
677
  m_limitedRoi = 0;
  m_rodTES = 0;
678
  if (evtStore()->contains<xAOD::RODHeaderContainer>(m_rodHeaderLocation)) {
679
680
681
682
    sc = evtStore()->retrieve(m_rodTES, m_rodHeaderLocation);
  } else
    sc = StatusCode::FAILURE;
  if (sc.isFailure() || !m_rodTES) {
683
    msg(MSG::DEBUG) << "No ROD Header container found" << endmsg;
684
  }
685

686
687
688
689
  // Retrieve CMX-CP TOBs from SG
  const xAOD::CMXCPTobContainer *cmxCpTobTES = 0;
  sc = evtStore()->retrieve(cmxCpTobTES, m_cmxCpTobLocation);
  if (sc.isFailure() || !cmxCpTobTES) {
690
    msg(MSG::DEBUG) << "No CMX-CP TOB container found" << endmsg;
691
  }
692

693
694
695
696
  // Retrieve CMX-CP Hits from SG
  const xAOD::CMXCPHitsContainer *cmxCpHitsTES = 0;
  sc = evtStore()->retrieve(cmxCpHitsTES, m_cmxCpHitsLocation);
  if (sc.isFailure() || !cmxCpHitsTES) {
697
    msg(MSG::DEBUG) << "No CMX-CP Hits container found" << endmsg;
698
699
700
  }

  // Maps to simplify comparisons
701

702
703
  TriggerTowerMapEm ttMapEm;
  TriggerTowerMapEm ttMapHad;
704
705
706
707
708
  CpmTowerMap cpMap;
  CpmTowerMap ovMap;
  CpmTobRoiMap crMap;
  CmxCpTobMap cbMap;
  CmxCpHitsMap cmMap;
709
  setupMap(triggerTowerTES, ttMapEm, ttMapHad);
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
  setupMap(cpmTowerTES, cpMap);
  setupMap(cpmTowerOvTES, ovMap);
  setupMap(cpmRoiTES, crMap);
  std::vector<int> parityMap(s_crates * s_cmxs * s_modules);
  setupMap(cmxCpTobTES, cbMap, &parityMap);
  setupMap(cmxCpHitsTES, cmMap);

  // Vectors for error overview bits;
  const int vecsizeCpm = 2 * s_crates * s_modules;
  const int vecsizeCmx = 2 * s_crates * s_cmxs;
  ErrorVector errorsCPM(vecsizeCpm);
  ErrorVector errorsCMX(vecsizeCmx);

  // Compare Trigger Towers and CPM Towers from data

  bool overlap = false;
726
727
728
729
  bool mismatchCoreEm = false;
  bool mismatchCoreHad = false;
  bool mismatchOverlapEm = false;
  bool mismatchOverlapHad = false;
730
  mismatchCoreEm = compareEm(ttMapEm, cpMap, errorsCPM, overlap);
731
  mismatchCoreHad = compareHad(ttMapHad, cpMap, errorsCPM, overlap);
732
733
  if (m_overlapPresent) {
    overlap = true;
734
    mismatchOverlapEm = compareEm(ttMapEm, ovMap, errorsCPM, overlap);
735
    mismatchOverlapHad = compareHad(ttMapHad, ovMap, errorsCPM, overlap);
736
737
738
739
  }

  // Compare RoIs simulated from CPM Towers with CPM RoIs from data

740
741
  xAOD::CPMTobRoIContainer *cpmRoiSIM = 0;
  xAOD::CPMTobRoIAuxContainer *cpmRoiSIMAux = 0;
742
  // @TODO(amazurov): aux
743
  if (cpmTowerTES || cpmTowerOvTES) {
744
745
746
    cpmRoiSIM = new xAOD::CPMTobRoIContainer;
    cpmRoiSIMAux = new xAOD::CPMTobRoIAuxContainer;
    cpmRoiSIM->setStore(cpmRoiSIMAux);
747
748
    if (mismatchCoreEm || mismatchCoreHad || mismatchOverlapEm ||
        mismatchOverlapHad) {
749
      simulate(&cpMap, &ovMap, cpmRoiSIM);
750
    } else {
751
      simulate(&cpMap, cpmRoiSIM);
752
753
754
755
756
757
758
    }
  }
  CpmTobRoiMap crSimMap;
  setupMap(cpmRoiSIM, crSimMap);
  compare(crSimMap, crMap, errorsCPM);
  crSimMap.clear();
  delete cpmRoiSIM;
759
  delete cpmRoiSIMAux;
760
761
762

  // Compare CMX-CP TOBs simulated from CPM RoIs with CMX-CP TOBs from data

763
764
  xAOD::CMXCPTobContainer *cmxCpTobSIM = 0;
  xAOD::CMXCPTobAuxContainer *cmxCpTobSIMAux = 0;
765
  if (cpmRoiTES) {
766
767
768
    cmxCpTobSIM = new xAOD::CMXCPTobContainer;
    cmxCpTobSIMAux = new xAOD::CMXCPTobAuxContainer;
    cmxCpTobSIM->setStore(cmxCpTobSIMAux);
769
770
771
772
773
774
775
    simulate(cpmRoiTES, cmxCpTobSIM);
  }
  CmxCpTobMap cbSimMap;
  setupMap(cmxCpTobSIM, cbSimMap);
  compare(cbSimMap, cbMap, parityMap, errorsCPM, errorsCMX);
  cbSimMap.clear();
  delete cmxCpTobSIM;
776
  delete cmxCpTobSIMAux;
777
778
779

  // Compare Local sums simulated from CMX-CP TOBs with Local sums from data

780
781
  xAOD::CMXCPHitsContainer *cmxLocalSIM = 0;
  xAOD::CMXCPHitsAuxContainer *cmxLocalSIMAux = 0;
782
  if (cmxCpTobTES) {
783
784
785
786
    cmxLocalSIM = new xAOD::CMXCPHitsContainer;
    cmxLocalSIMAux = new xAOD::CMXCPHitsAuxContainer;
    cmxLocalSIM->setStore(cmxLocalSIMAux);
    simulate(cmxCpTobTES, cmxLocalSIM, xAOD::CMXCPHits::LOCAL);
787
788
789
  }
  CmxCpHitsMap cmxLocalSimMap;
  setupMap(cmxLocalSIM, cmxLocalSimMap);
790
  compare(cmxLocalSimMap, cmMap, errorsCMX, xAOD::CMXCPHits::LOCAL);
791
792
  cmxLocalSimMap.clear();
  delete cmxLocalSIM;
793
  delete cmxLocalSIMAux;
794
795
796

  // Compare Local sums with Remote sums from data

797
  compare(cmMap, cmMap, errorsCMX, xAOD::CMXCPHits::REMOTE_0);
798
799
800

  // Compare Total sums simulated from Remote sums with Total sums from data

801
802
  xAOD::CMXCPHitsContainer *cmxTotalSIM = 0;
  xAOD::CMXCPHitsAuxContainer *cmxTotalSIMAux = 0;
803
  if (cmxCpHitsTES) {
804
805
806
    cmxTotalSIM = new xAOD::CMXCPHitsContainer;
    cmxTotalSIMAux = new xAOD::CMXCPHitsAuxContainer;
    cmxTotalSIM->setStore(cmxTotalSIMAux);
807
808
809
810
    simulate(cmxCpHitsTES, cmxTotalSIM);
  }
  CmxCpHitsMap cmxTotalSimMap;
  setupMap(cmxTotalSIM, cmxTotalSimMap);
811
  compare(cmxTotalSimMap, cmMap, errorsCMX, xAOD::CMXCPHits::TOTAL);
812
813
  cmxTotalSimMap.clear();
  delete cmxTotalSIM;
814
  delete cmxTotalSIMAux;
815

816
817
  // Compare Topo output information simulated from CMX-CP TOBs with Topo info
  // from data
818

819
820
  xAOD::CMXCPHitsContainer *cmxTopoSIM = 0;
  xAOD::CMXCPHitsAuxContainer *cmxTopoSIMAux = 0;
821
  if (cmxCpTobTES) {
822
823
824
825
    cmxTopoSIM = new xAOD::CMXCPHitsContainer;
    cmxTopoSIMAux = new xAOD::CMXCPHitsAuxContainer;
    cmxTopoSIM->setStore(cmxTopoSIMAux);
    simulate(cmxCpTobTES, cmxTopoSIM, xAOD::CMXCPHits::TOPO_CHECKSUM);
826
827
828
  }
  CmxCpHitsMap cmxTopoSimMap;
  setupMap(cmxTopoSIM, cmxTopoSimMap);
829
  compare(cmxTopoSimMap, cmMap, errorsCMX, xAOD::CMXCPHits::TOPO_CHECKSUM);
830
831
  cmxTopoSimMap.clear();
  delete cmxTopoSIM;
832
  delete cmxTopoSIMAux;
833
834
835
836
837
838
839
840
841
842
843
844
845
846

  // Update error summary plots

  ErrorVector crateErr(s_crates);
  const int cpmBins = s_crates * s_modules;
  const int cmxBins = s_crates * s_cmxs;
  for (int err = 0; err < NumberOfSummaryBins; ++err) {
    int error = 0;
    for (int loc = 0; loc < cpmBins; ++loc) {
      if ((errorsCPM[loc] >> err) & 0x1) {
        m_h_cpm_2d_SimEqDataOverview->Fill(loc, err, 1.);
      }
      if ((errorsCPM[loc + cpmBins] >> err) & 0x1) {
        m_h_cpm_2d_SimNeDataOverview->Fill(loc, err, 1.);
847
848
849
850
        error = 1;
        crateErr[loc / s_modules] |= (1 << err);
        if (m_v_2d_MismatchEvents[err]) {
          m_histTool->fillEventNumber(m_v_2d_MismatchEvents[err], loc);
851
852
853
854
        }
      }
      if (loc < cmxBins) {
        if ((errorsCMX[loc] >> err) & 0x1) {
855
          m_h_cpm_2d_SimEqDataOverview->Fill(loc + cpmBins, err, 1.);
856
857
        }
        if ((errorsCMX[loc + cmxBins] >> err) & 0x1) {
858
859
860
861
862
863
          m_h_cpm_2d_SimNeDataOverview->Fill(loc + cpmBins, err, 1.);
          error = 1;
          crateErr[loc / s_cmxs] |= (1 << err);
          int offset = 0;
          if (err == RemoteSumMismatch || err == TotalSumMismatch)
            offset = 8;
864
865
          else if (err == TopoMismatch)
            offset = 16;
866
867
868
          if (m_v_2d_MismatchEvents[6]) {
            m_histTool->fillEventNumber(m_v_2d_MismatchEvents[6], loc + offset);
          }
869
870
871
        }
      }
    }
872
873
    if (error)
      m_h_cpm_1d_SimNeDataSummary->Fill(err);
874
875
876
877
  }

  // Save error vector for global summary

878
  ErrorVector *save = new ErrorVector(crateErr);
879
880
  sc = evtStore()->record(save, m_errorLocation);
  if (sc != StatusCode::SUCCESS) {
881
    msg(MSG::ERROR) << "Error recording CPM mismatch vector in TES " << endmsg;
882
883
884
    return sc;
  }

885
  if (m_debug)
886
    msg(MSG::DEBUG) << "Leaving fillHistograms" << endmsg;
887
888
889
890

  return StatusCode::SUCCESS;
}

891
StatusCode CPSimMon::procHistograms() {
892
  msg(MSG::DEBUG) << "procHistograms entered" << endmsg;
893

894
895
  //if (endOfLumiBlock || endOfRun) {
  //}
896
897
898
899
900
901

  return StatusCode::SUCCESS;
}

//  Compare Trigger Towers and CPM Towers

902
903
904
bool CPSimMon::compareEm(const TriggerTowerMapEm &ttMap,
                         const CpmTowerMap &cpMap, ErrorVector &errors,
                         bool overlap) {
905
  if (m_debug) {
906
    msg(MSG::DEBUG) << "Compare Trigger Towers and CPM Towers" << endmsg;
907
  }
908

909
910
  bool mismatch = false;
  LVL1::CoordToHardware converter;
911
  TriggerTowerMapEm::const_iterator ttMapIter = ttMap.begin();
912
  TriggerTowerMapEm::const_iterator ttMapIterEnd = ttMap.end();
913
914
  CpmTowerMap::const_iterator cpMapIter = cpMap.begin();
  CpmTowerMap::const_iterator cpMapIterEnd = cpMap.end();
915
916
917
918
919

  while (ttMapIter != ttMapIterEnd || cpMapIter != cpMapIterEnd) {

    int ttKey = 0;
    int cpKey = 0;
920
921
    int ttEm = 0;
    int cpEm = 0;
922
923
924
925
    double eta = 0.;
    double phi = 0.;
    int key = 0;

926
927
928
929
    if (ttMapIter != ttMapIterEnd)
      ttKey = ttMapIter->first;
    if (cpMapIter != cpMapIterEnd)
      cpKey = cpMapIter->first;
930
931

    if ((cpMapIter == cpMapIterEnd) ||
932
        ((ttMapIter != ttMapIterEnd) && (cpKey > ttKey))) {
933
934
935

      // TriggerTower but no CPMTower

936
      const xAOD::TriggerTower *tt = ttMapIter->second;
937
      ++ttMapIter;
938
      const int layer = tt->layer();
939
940
941
942
      eta = tt->eta();
      phi = tt->phi();
      if (overlap) { // skip non-overlap TTs
        const LVL1::Coordinate coord(phi, eta);
943
        const int crate = converter.cpCrateOverlap(coord);
944
945
        if (crate >= s_crates)
          continue;
946
      }
947
948
949
      // check if the TriggerTower is in EM or HAD layer
      if (layer == 0) { // EM
        ttEm = tt->cpET();
950
      }
951
952
953
      key = ttKey;

    } else if ((ttMapIter == ttMapIterEnd) ||
954
               ((cpMapIter != cpMapIterEnd) && (ttKey > cpKey))) {
955
956
957

      // CPMTower but no TriggerTower

958
      const xAOD::CPMTower *cp = cpMapIter->second;
959
960
961
      ++cpMapIter;
      eta = cp->eta();
      phi = cp->phi();
962
      cpEm = cp->emEnergy();
963
964
965
966
967
968
      key = cpKey;

    } else {

      // Have both

969
970
      const xAOD::TriggerTower *tt = ttMapIter->second;
      const xAOD::CPMTower *cp = cpMapIter->second;
971
      ++ttMapIter;
972
      ++cpMapIter;
973
      const int layer = tt->layer();
974
975
      eta = tt->eta();
      phi = tt->phi();
976
977
978
979
      // check if the TriggerTower is in EM or HAD layer
      if (layer == 0) { // EM
        ttEm = tt->cpET();
        ;
980
      }
981
      cpEm = cp->emEnergy();
982
983
984
      key = ttKey;
    }

985
986
    if (!ttEm && !cpEm)
      continue;
987

988
    //  Fill in error plots
989
    const LVL1::Coordinate coord(phi, eta);
990
991
992
993
994
995
    const int crate =
        (overlap) ? converter.cpCrateOverlap(coord) : converter.cpCrate(coord);
    const int cpm = (overlap) ? converter.cpModuleOverlap(coord)
                              : converter.cpModule(coord);
    if (crate >= s_crates || cpm > s_modules)
      continue;
996
997
    const int loc = crate * s_modules + cpm - 1;
    const int cpmBins = s_crates * s_modules;
998
    const int bitEm = (1 << EMTowerMismatch);
999
1000
    double phiFPGA = phi;
    if (overlap) {
1001
      const double twoPi = 2. * M_PI;
1002
      const double piByFour = M_PI / 4.;
1003
1004
1005
1006
      if (phi > 7. * piByFour)
        phiFPGA -= twoPi;
      else if (phi < piByFour)
        phiFPGA += twoPi;
1007
1008
1009
    }
    const int loc2 = fpga(crate, phiFPGA);

1010
1011
    TH2F_LW *hist1 = 0;
    TH2F_LW *hist2 = 0;
1012
1013
1014
    if (ttEm && ttEm == cpEm) { // non-zero match
      errors[loc] |= bitEm;
      hist1 = (overlap) ? m_h_cpm_em_2d_etaPhi_tt_PpmEqOverlap
1015
                        : m_h_cpm_em_2d_etaPhi_tt_PpmEqCore;
1016
      hist2 = m_h_cpm_2d_tt_PpmEqCpmFpga;
1017
    } else if (ttEm != cpEm) { // mis-match
1018
      mismatch = true;
1019
      errors[loc + cpmBins] |= bitEm;
1020
      if (ttEm && cpEm) { // non-zero mis-match
1021
        hist1 = (overlap) ? m_h_cpm_em_2d_etaPhi_tt_PpmNeOverlap
1022
                          : m_h_cpm_em_2d_etaPhi_tt_PpmNeCore;
1023
        hist2 = m_h_cpm_2d_tt_PpmNeCpmFpga;
1024
      } else if (!cpEm) { // no cp
1025
        hist1 = (overlap) ? m_h_cpm_em_2d_etaPhi_tt_PpmNoOverlap
1026
                          : m_h_cpm_em_2d_etaPhi_tt_PpmNoCore;
1027
        hist2 = m_h_cpm_2d_tt_PpmNoCpmFpga;
1028
      } else { // no tt
1029
        hist1 = (overlap) ? m_h_cpm_em_2d_etaPhi_tt_OverlapNoPpm
1030
                          : m_h_cpm_em_2d_etaPhi_tt_CoreNoPpm;
1031
        hist2 = m_h_cpm_2d_tt_CpmNoPpmFpga;
1032
1033
1034
1035
      }
      if (m_debug) {
        msg(MSG::DEBUG) << " EMTowerMismatch key/eta/phi/crate/cpm/tt/cp: "
                        << key << "/" << eta << "/" << phi << "/" << crate
1036
                        << "/" << cpm << "/" << ttEm << "/" << cpEm << endmsg;
1037
1038
      }
    }
1039
1040
1041
1042
    if (hist1)
      m_histTool->fillCPMEtaVsPhi(hist1, eta, phi);
    if (hist2)
      hist2->Fill(loc, loc2);
1043
1044
1045
1046
1047
  }

  return mismatch;
}

1048
1049
1050
bool CPSimMon::compareHad(const TriggerTowerMapHad &ttMap,
                          const CpmTowerMap &cpMap, ErrorVector &errors,
                          bool overlap) {
1051
  if (m_debug) {
1052
    msg(MSG::DEBUG) << "Compare Trigger Towers and CPM Towers" << endmsg;
1053
  }
1054

1055
1056
  bool mismatch = false;
  LVL1::CoordToHardware converter;
1057
  TriggerTowerMapHad::const_iterator ttMapIter = ttMap.begin();
1058
  TriggerTowerMapHad::const_iterator ttMapIterEnd = ttMap.end();
1059
1060
  CpmTowerMap::const_iterator cpMapIter = cpMap.begin();
  CpmTowerMap::const_iterator cpMapIterEnd = cpMap.end();
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071

  while (ttMapIter != ttMapIterEnd || cpMapIter != cpMapIterEnd) {

    int ttKey = 0;
    int cpKey = 0;
    int ttHad = 0;
    int cpHad = 0;
    double eta = 0.;
    double phi = 0.;
    int key = 0;

1072
1073
1074
1075
    if (ttMapIter != ttMapIterEnd)
      ttKey = ttMapIter->first;
    if (cpMapIter != cpMapIterEnd)
      cpKey = cpMapIter->first;
1076
1077

    if ((cpMapIter == cpMapIterEnd) ||
1078
        ((ttMapIter != ttMapIterEnd) && (cpKey > ttKey))) {
1079
1080

      // TriggerTower but no CPMTower
1081

1082
      const xAOD::TriggerTower *tt = ttMapIter->second;
1083
1084
1085
1086
1087
1088
      ++ttMapIter;
      const int layer = tt->layer();
      eta = tt->eta();
      phi = tt->phi();
      if (overlap) { // skip non-overlap TTs
        const LVL1::Coordinate coord(phi, eta);
1089
        const int crate = converter.cpCrateOverlap(coord);
1090
1091
        if (crate >= s_crates)
          continue;