Belle II Software  release-05-01-25
Convert2RawDet.cc
1 //+
2 // File : Convert2RawDet.cc
3 // Description : Module to convert from RawCOPPER or RawDataBlock to RawDetector objects
4 //
5 // Author : Satoru Yamada Itoh, IPNS, KEK
6 // Date : 24 - Oct - 2014
7 //-
8 #include <rawdata/modules/Convert2RawDet.h>
9 #include <TSystem.h>
10 #include <stdlib.h>
11 
12 // #define DESY
13 
14 using namespace std;
15 using namespace Belle2;
16 
17 //-----------------------------------------------------------------
18 // Register the Module
19 //-----------------------------------------------------------------
20 REG_MODULE(Convert2RawDet)
21 
22 //-----------------------------------------------------------------
23 // Implementation
24 //-----------------------------------------------------------------
25 
27 {
28  //Set module properties
29  setDescription("convert from RawCOPPER or RawDataBlock to RawDetector objects");
30  setPropertyFlags(c_Input);
31  m_nevt = 0;
32 }
33 
34 
35 Convert2RawDetModule::~Convert2RawDetModule()
36 {
37 }
38 
39 void Convert2RawDetModule::initialize()
40 {
41  gSystem->Load("libdataobjects");
42 
43  // Initialize EvtMetaData
44 
45  // Initialize Array of RawCOPPER
46  m_rawDataBlock.registerInDataStore();
47  m_rawCOPPER.registerInDataStore();
48  m_rawSVD.registerInDataStore();
49  m_rawCDC.registerInDataStore();
50  m_rawTOP.registerInDataStore();
51  m_rawARICH.registerInDataStore();
52  m_rawECL.registerInDataStore();
53  m_rawKLM.registerInDataStore();
54  m_rawTRG.registerInDataStore();
55  m_rawFTSW.registerInDataStore();
56 
57 
58  // Read the first event in RingBuffer and restore in DataStore.
59  // This is necessary to create object tables before TTree initialization
60  // if used together with SimpleOutput.
61  // ---- Prefetch the first event
62  // registerRawCOPPERs();
63 }
64 
65 
66 void Convert2RawDetModule::beginRun()
67 {
68  B2INFO("beginRun called.");
69 }
70 
71 
72 void Convert2RawDetModule::event()
73 {
74  // B2INFO("Event " << m_nevt);
75 
76  vector<unsigned int> cpr_id;
77 
78  //
79  // Check if COPPER ID is duplicated.
80  //
81  StoreArray<RawSVD> raw_svdarray;
82  for (int i = 0; i < raw_svdarray.getEntries(); i++) {
83  for (int j = 0; j < raw_svdarray[ i ]->GetNumEntries(); j++) {
84  cpr_id.push_back(raw_svdarray[ i ]->GetNodeID(j));
85  }
86  }
87 
88  StoreArray<RawCDC> raw_cdcarray;
89  for (int i = 0; i < raw_cdcarray.getEntries(); i++) {
90  for (int j = 0; j < raw_cdcarray[ i ]->GetNumEntries(); j++) {
91  cpr_id.push_back(raw_cdcarray[ i ]->GetNodeID(j));
92  }
93  }
94 
95  StoreArray<RawTOP> raw_toparray;
96  for (int i = 0; i < raw_toparray.getEntries(); i++) {
97  for (int j = 0; j < raw_toparray[ i ]->GetNumEntries(); j++) {
98  cpr_id.push_back(raw_toparray[ i ]->GetNodeID(j));
99  }
100  }
101 
102  StoreArray<RawARICH> raw_aricharray;
103  for (int i = 0; i < raw_aricharray.getEntries(); i++) {
104  for (int j = 0; j < raw_aricharray[ i ]->GetNumEntries(); j++) {
105  cpr_id.push_back(raw_aricharray[ i ]->GetNodeID(j));
106  }
107  }
108 
109  StoreArray<RawECL> raw_eclarray;
110  for (int i = 0; i < raw_eclarray.getEntries(); i++) {
111  for (int j = 0; j < raw_eclarray[ i ]->GetNumEntries(); j++) {
112  cpr_id.push_back(raw_eclarray[ i ]->GetNodeID(j));
113  }
114  }
115 
116  StoreArray<RawKLM> raw_klmarray;
117  for (int i = 0; i < raw_klmarray.getEntries(); i++) {
118  for (int j = 0; j < raw_klmarray[ i ]->GetNumEntries(); j++) {
119  cpr_id.push_back(raw_klmarray[ i ]->GetNodeID(j));
120  }
121  }
122 
123  StoreArray<RawTRG> raw_trgarray;
124  for (int i = 0; i < raw_trgarray.getEntries(); i++) {
125  for (int j = 0; j < raw_trgarray[ i ]->GetNumEntries(); j++) {
126  cpr_id.push_back(raw_trgarray[ i ]->GetNodeID(j));
127  }
128  }
129 
130 
131  StoreArray<RawDataBlock> raw_datablkarray;
132  for (int i = 0; i < raw_datablkarray.getEntries(); i++) {
133  convertDataObject(raw_datablkarray[ i ], cpr_id);
134  }
135  raw_datablkarray.clear();
136 
137  StoreArray<RawCOPPER> raw_cprarray;
138  for (int i = 0; i < raw_cprarray.getEntries(); i++) {
139  convertDataObject((RawDataBlock*)(raw_cprarray[ i ]), cpr_id);
140  }
141  raw_cprarray.clear();
142 
143 
144  for (unsigned int i = 0; i < cpr_id.size(); i++) {
145  for (unsigned int j = i + 1; j < cpr_id.size(); j++) {
146  // printf("[DEBUG] eve %d i %d 0x%.8x j %d 0x%.8x\n", m_nevt, i, cpr_id[i], j, cpr_id[j] );
147  if (cpr_id[ i ] == cpr_id[ j ]) {
148  B2FATAL("Duplicated COPPER object is found. ID=0x" << hex << cpr_id[ i ] << " Exiting...");
149  }
150  }
151  }
152  m_nevt++;
153 
154 }
155 
156 
157 void Convert2RawDetModule::convertDataObject(RawDataBlock* raw_dblk, std::vector<unsigned int>& cpr_id)
158 {
159  //
160  // Convert RawDataBlock to Raw***
161  //
162 
163  int num_eve = raw_dblk->GetNumEvents();
164  if (num_eve != 1) {
165  printf("[FATAL] %s : This module should be used for data with num_eve = 1\n", __PRETTY_FUNCTION__);
166  exit(1);
167  }
168 
169  int num_nodes = raw_dblk->GetNumNodes();
170 
171 
172  // {
173  // int nwords = raw_dblk->TotalBufNwords();
174  // int* temp_buf = new int[ nwords ];
175  // memcpy( temp_buf, raw_dblk->GetWholeBuffer(), nwords*sizeof(int) );
176  // StoreArray<RawCOPPER> ary;
177  // (ary.appendNew())->SetBuffer(temp_buf, nwords, 1, num_eve, num_nodes);
178  // }
179  // return;
180 
181  for (int j = 0; j < num_eve; j++) {
182  for (int k = 0; k < num_nodes; k++) {
183  // index of blkidries
184  int blkid = j * num_nodes + k;
185 
186  // allocate a new buffer
187  int nwords = raw_dblk->GetBlockNwords(blkid);
188  int* temp_buf = new int[ nwords ];
189  memcpy(temp_buf, raw_dblk->GetBuffer(blkid), nwords * sizeof(int));
190 
191 
192  // Set buffer to RawCOPPER class to access detector ID
193  RawCOPPER tempcpr;
194  const int temp_num_eve = 1;
195  const int temp_num_nodes = 1;
196  int delete_flag;
197  delete_flag = 0; // this buffer will not be deleted in RawCOPPER destructor.
198  tempcpr.SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
199  int subsys_id = tempcpr.GetNodeID(0);
200 
201  // store COPPER ID for check
202  cpr_id.push_back((unsigned int)subsys_id);
203 
204  delete_flag = 1; // this buffer will be deleted in Raw*** destructor.
205 
206 
207 
208  if ((subsys_id & DETECTOR_MASK) == SVD_ID) {
209  StoreArray<RawSVD> ary;
210  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
211  } else if ((subsys_id & DETECTOR_MASK) == CDC_ID) {
212  StoreArray<RawCDC> ary;
213  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
214  } else if ((subsys_id & DETECTOR_MASK) == TOP_ID) {
215  StoreArray<RawTOP> ary;
216  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
217  } else if ((subsys_id & DETECTOR_MASK) == ARICH_ID) {
219  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
220  } else if ((subsys_id & DETECTOR_MASK) == BECL_ID) {
221  StoreArray<RawECL> ary;
222  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
223  } else if ((subsys_id & DETECTOR_MASK) == EECL_ID) {
224  StoreArray<RawECL> ary;
225  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
226  } else if ((subsys_id & DETECTOR_MASK) == BKLM_ID) {
227  StoreArray<RawKLM> ary;
228  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
229  } else if ((subsys_id & DETECTOR_MASK) == EKLM_ID) {
230  StoreArray<RawKLM> ary;
231  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
232  } else if (((subsys_id & DETECTOR_MASK) & 0xF0000000) == TRGDATA_ID) {
233  StoreArray<RawTRG> ary;
234  (ary.appendNew())->SetBuffer(temp_buf, nwords, delete_flag, temp_num_eve, temp_num_nodes);
235  } else {
236  printf("[FATAL] Undefined detector ID(0x%.8x). Exiting...\n", subsys_id);
237  exit(1);
238  }
239 
240 
241 
242  }
243  }
244 
245 
246  return;
247 }
248 
249 
250 void Convert2RawDetModule::endRun()
251 {
252  //fill Run data
253 
254  B2INFO("Convert2RawDet: endRun done.");
255 }
256 
257 
258 void Convert2RawDetModule::terminate()
259 {
260  B2INFO("Convert2RawDet: terminate called");
261 }
262 
Belle2::RawCOPPER
The Raw COPPER class This class stores data received by COPPER via belle2linkt Data from all detector...
Definition: RawCOPPER.h:50
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::Convert2RawDetModule
A class definition of a module to convert from RawCOPPER or RawDataBlock to RawDetector objects.
Definition: Convert2RawDet.h:35
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::ProcType::c_Input
@ c_Input
Input Process.
Belle2::RawDataBlock::GetBlockNwords
virtual int GetBlockNwords(int n)
get size of a data block
Definition: RawDataBlock.h:94
Belle2::StoreArray::clear
void clear() override
Delete all entries in this array.
Definition: StoreArray.h:217
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RawDataBlock::GetNumEvents
virtual int GetNumEvents()
get # of events in m_buffer
Definition: RawDataBlock.h:81
Belle2::RawCOPPER::GetNodeID
unsigned int GetNodeID(int n)
get node-ID from data
Definition: RawCOPPER.h:383
Belle2::RawCOPPER::SetBuffer
void SetBuffer(int *bufin, int nwords, int delete_flag, int num_events, int num_nodes) OVERRIDE_CPP17
set buffer ( delete_flag : m_buffer is freeed( = 0 )/ not freeed( = 1 ) in Destructer )
Definition: RawCOPPER.cc:132
Belle2::RawDataBlock::GetNumNodes
virtual int GetNumNodes()
get # of data sources(e.g. # of COPPER boards) in m_buffer
Definition: RawDataBlock.h:74
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::RawDataBlock
The RawDataBlock class Base class for rawdata handling.
Definition: RawDataBlock.h:27
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::RawDataBlock::GetBuffer
virtual int * GetBuffer(int n)
get nth buffer pointer
Definition: RawDataBlock.h:53