Belle II Software  release-05-01-25
ROIPayloadAssemblerModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2011 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Giulia Casarosa, Eugenio Paoloni, Bjoern Spruck *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <tracking/modules/pxdDataReduction/ROIPayloadAssemblerModule.h>
12 #include <vxd/dataobjects/VxdID.h>
13 #include <set>
14 #include <hlt/softwaretrigger/core/FinalTriggerDecisionCalculator.h>
15 #include <mdst/dataobjects/SoftwareTriggerResult.h>
16 #include <vxd/geometry/GeoCache.h>
17 #include <vxd/geometry/SensorInfoBase.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 
26 REG_MODULE(ROIPayloadAssembler)
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
33 {
34  //Set module properties
35  setDescription("This module assembles payload for the ROI in the correct format to be sent to the ONSEN");
36  setPropertyFlags(c_ParallelProcessingCertified);
37 
38  addParam("ROIListName", m_ROIListName, "name of the list of ROIs", std::string(""));
39  addParam("ROIpayloadName", m_ROIpayloadName, "name of the payload of ROIs", std::string(""));
40  addParam("SendAllDownscaler", mSendAllDS,
41  "Send all Data (no selection) downscaler; Workaround for missing ONSEN functionality, 0 never set, 1 alway set, 2 set in every second...",
42  9u);
43  addParam("SendROIsDownscaler", mSendROIsDS,
44  "Send ROIs downscaler; Workaround for missing ONSEN functionality, 0 never set, 1 alway set, 2 set in every second...", 3u);
45  addParam("CutNrROIs", mCutNrROIs,
46  "If Nr of ROIs per DHHID reach this, send out only one full sensor ROI", 32u);
47  addParam("AcceptAll", mAcceptAll, "Accept all, Ignore HLT decision", false);
48  addParam("NoRejectFlag", mNoRejectFlag, "Never reject, just send no ROI", true);
49 }
50 
51 void ROIPayloadAssemblerModule::initialize()
52 {
53  m_eventMetaData.isRequired();
54  m_ROIList.isOptional(m_ROIListName);
55 
56  m_roiPayloads.registerInDataStore(
57  m_ROIpayloadName); // DataStore::EStoreFlags::c_ErrorIfAlreadyRegistered will not work with two modules in seperate path branches
58 
59  // in case we don't accept all events, we have to look
60  // up the trigger decision
61  if (!mAcceptAll) {
63  result.isRequired();
64  }
65 }
66 
67 
68 void ROIPayloadAssemblerModule::event()
69 {
70  VXD::GeoCache& aGeometry = VXD::GeoCache::getInstance();
71  unsigned int evtNr = m_eventMetaData->getEvent();
72  unsigned int countROIs = 0;
73  bool accepted = true;
74 
75  if (!mAcceptAll) {
77  if (!result.isValid()) {
79  B2INFO("SoftwareTriggerResult object not available but needed to generate the ROI payload.");
80  accepted = false;
81  } else {
82  accepted = SoftwareTrigger::FinalTriggerDecisionCalculator::getFinalTriggerDecision(*result);
83  }
84  }
85 
86  map<VxdID, set<ROIrawID, ROIrawID>> mapOrderedROIraw;
87 
88  if (accepted) {
89  // skip the preprocessing if the event is not accepted to save CPU time
90 
91  B2DEBUG(1, " number of ROIs in the list = " << m_ROIList.getEntries());
92 
93  for (auto& iROI : m_ROIList) {
94  ROIrawID roiraw;
96  int layer = (iROI.getSensorID()).getLayerNumber() - 1;
97  int ladder = (iROI.getSensorID()).getLadderNumber();
98  int sensor = (iROI.getSensorID()).getSensorNumber() - 1;
99 
100  roiraw.setSystemFlag(0);// System 0 is HLT, 1 would be DATCON
101  roiraw.setDHHID(((layer) << 5) | ((ladder) << 1) | (sensor));
102 
103  roiraw.setMinVid(iROI.getMinVid());
104  roiraw.setMaxVid(iROI.getMaxVid());
105  roiraw.setMinUid(iROI.getMinUid());
106  roiraw.setMaxUid(iROI.getMaxUid());
107 
108  // order set will drop identical ROIs automatically
109  mapOrderedROIraw[iROI.getSensorID()].insert(roiraw);
110  }
111 
112  B2DEBUG(1, " number of original ROIs = " << m_ROIList.getEntries());
113 
114  // The payload is created with a buffer long enough to contains all
115  // the ROIs but the actual payload could be smaller, if the ROIs per
116  // sensor exceed the (ONSEN) limit, we can save bandwidth by just
117  // sending only one full size ROI for that sensor
118  for (auto& it : mapOrderedROIraw) {
119  if (it.second.size() < mCutNrROIs) countROIs += it.second.size();
120  else countROIs++;
121  }
122  }
123 
124  if (m_roiPayloads.isValid()) {
125  B2FATAL("ROIpayload already in datastore, this must not be the case when calling the ROIPayloadAssemblerModule.");
126  }
127 
128  ROIpayload* payload = new ROIpayload(countROIs);// let the ROIpayload compute the size itself
129 
130  m_roiPayloads.assign(payload);
131 
132  // set all the Header flags and event number
133  payload->setHeader(accepted || mNoRejectFlag,
134  mSendAllDS ? (evtNr % mSendAllDS) == 0 : 0, mSendROIsDS ? (evtNr % mSendROIsDS) == 0 : 0);
135  payload->setTriggerNumber(evtNr);
136 
137  // Set run subrun exp number
138  payload->setRunSubrunExpNumber(m_eventMetaData->getRun(), m_eventMetaData->getSubrun(), m_eventMetaData->getExperiment());
139 
140  unsigned int addROI = 0;
141 
142  if (accepted) {
143  // skip this if the event is not accepted (actually mapOrderedROIraw should be empty anyway)
144  // iterate over map
145  for (auto& it : mapOrderedROIraw) {
146  // check size of set
147  if (it.second.size() > 0) {
148  if (it.second.size() < mCutNrROIs) {
149  for (auto& itSet : it.second) {
150  payload->addROIraw(itSet.getBigEndian());
151  addROI++;
152  }
153  } else {
154  ROIrawID roiraw;
155  B2INFO("Nr ROI on DHHID " << it.second.begin()->getDHHID() << endl <<
156  " exceeds limit CutNrROIs, thus full sensor ROI is created.");
157  const VXD::SensorInfoBase& aSensorInfo = aGeometry.getSensorInfo(it.first);
158  const int nPixelsU = aSensorInfo.getUCells();
159  const int nPixelsV = aSensorInfo.getVCells();
160 
161  roiraw.setSystemFlag(0);
162  roiraw.setDHHID(it.second.begin()->getDHHID());
163 
164  roiraw.setMinVid(0);
165  roiraw.setMaxVid(nPixelsV - 1);
166  roiraw.setMinUid(0);
167  roiraw.setMaxUid(nPixelsU - 1);
168 
169  payload->addROIraw(roiraw.getBigEndian());
170  }
171  }
172  }
173  }
174 
175  payload->setPayloadLength();
176  payload->setCRC();
177 
178  B2DEBUG(1, " number of ROIs in payload = " << addROI);
179 }
Belle2::VXD::SensorInfoBase::getUCells
int getUCells() const
Return number of pixel/strips in u direction.
Definition: SensorInfoBase.h:223
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::VXD::SensorInfoBase
Base class to provide Sensor Information for PXD and SVD.
Definition: SensorInfoBase.h:40
Belle2::ROIrawID::getBigEndian
baseType getBigEndian() const
get bigEndian
Definition: ROIrawID.cc:15
Belle2::ROIpayload
ROIpayload @TODO: Better explanation, Is there a reason to inherit from TObject and not Relationsobje...
Definition: ROIpayload.h:45
Belle2::ROIPayloadAssemblerModule
The ROI Payload Assembler Module.
Definition: ROIPayloadAssemblerModule.h:44
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::VXD::SensorInfoBase::getVCells
int getVCells() const
Return number of pixel/strips in v direction.
Definition: SensorInfoBase.h:225
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::VXD::GeoCache
Class to faciliate easy access to sensor information of the VXD like coordinate transformations or pi...
Definition: GeoCache.h:41
Belle2::VXD::GeoCache::getSensorInfo
const SensorInfoBase & getSensorInfo(Belle2::VxdID id) const
Return a referecne to the SensorInfo of a given SensorID.
Definition: GeoCache.cc:68
Belle2::ROIrawID
ROIrawID.
Definition: ROIrawID.h:34