ATLAS Offline Software
LArShapeSubsetCnv_p2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 
8 void
10  LArShapeTransType2* transObj,
11  MsgStream & log) const
12 {
13  // Copy basic metadata
14  transObj->setChannel (persObj->m_subset.m_channel);
15  transObj->setGroupingType (persObj->m_subset.m_groupingType);
16 
17  transObj->initialize (persObj->m_subset.m_febIds, persObj->m_subset.m_gain);
18 
19  // Copy conditions
20  unsigned int nfebids = persObj->m_subset.m_febIds.size();
21  const unsigned int nChannelsPerFeb = persObj->m_subset.subsetSize();
22  unsigned int nPhases = persObj->m_nPhases;
23  unsigned int nSamples = persObj->m_nSamples;
24  unsigned int dataIndex = 0;
25  unsigned int timeIndex = 0;
26 
27  // Loop over febs
28  unsigned int ifebWithData = 0; // counter for febs with data
29 
30  auto subsetIt = transObj->subsetBegin();
31  for (unsigned int i = 0; i < nfebids; ++i, ++subsetIt){
32  // Set febid
33  unsigned int febid = (*subsetIt).first;
34 
35  bool hasSparseData = false;
36  unsigned int chansSet = 0;
37  unsigned int chansOffset = 0;
38  if (ifebWithData < persObj->m_subset.m_febsWithSparseData.size() &&
39  febid == persObj->m_subset.m_febsWithSparseData[ifebWithData])
40  {
41  // Found feb with sparse data
42  hasSparseData = true;
43  ifebWithData++;
44  chansSet = persObj->m_subset.m_febsWithSparseData[ifebWithData];
45  chansOffset = 0;
46  ifebWithData++;
47  }
48 
49  // Loop over channels in feb - only some channels are filled
50  for (unsigned int j = 0; j < nChannelsPerFeb; ++j){
51 
52  bool copyChannel = true;
53  if (hasSparseData) {
54  if (!(chansSet & (1 << (j - chansOffset)))) {
55  // Channel is missing data - skip
56  copyChannel = false;
57  }
58  if (j%32 == 31 && j < nChannelsPerFeb-2) {
59  chansSet = persObj->m_subset.m_febsWithSparseData[ifebWithData];
60  chansOffset += 32;
61  ifebWithData++;
62  }
63  }
64  if (copyChannel) {
65 
66  // Channel has shapes - loop over shapes per channel and
67  // copy to the persistent object
68 
69  // check indexes
70  if (dataIndex + nPhases*nSamples > persObj->m_vShape.size() ||
71  dataIndex + nPhases*nSamples > persObj->m_vShapeDer.size() ||
72  timeIndex >= persObj->m_timeOffset.size() ||
73  timeIndex >= persObj->m_timeBinWidth.size()) {
74  log << MSG::ERROR
75  << "LArShapeSubsetCnv_p2::persToTrans - shape index too large: shapeIndex size shape, size shapeDer, timeIndex timeOffset size, timeBinWidth size "
76  << dataIndex << " " << persObj->m_vShape.size() << " "
77  << persObj->m_vShapeDer.size() << " " << timeIndex << " "
78  << persObj->m_timeOffset.size() << " "
79  << persObj->m_timeBinWidth.size()
80  << endmsg;
81  return;
82  }
83 
85  (*subsetIt).second[j];
86  LArShapeP2 tmp (persObj->m_timeOffset[timeIndex],
87  persObj->m_timeBinWidth[timeIndex],
88  nPhases,
89  nSamples,
90  persObj->m_vShape,
91  persObj->m_vShapeDer,
92  dataIndex);
93  shape.assign (tmp);
94  ++timeIndex;
95  dataIndex += nPhases * nSamples;
96  }
97  else {
98  if (timeIndex > 0) {
99  (*subsetIt).second[j].assign
100  (LArShapeP2(persObj->m_timeOffset[timeIndex-1],
101  persObj->m_timeBinWidth[timeIndex-1],
102  std::vector<std::vector<float> >(),
103  std::vector<std::vector<float> >()));
104  }
105  else {
106  (*subsetIt).second[j].assign (LArShapeP2());
107  }
108  }
109  }
110  }
111 
112  // Copy corrections
113  unsigned int ncorrs = persObj->m_subset.m_corrChannels.size();
115 
116  if (ncorrs) {
117  // corrs exist - resize vector
118  std::vector<float> vSamples(nSamples, 0.0);
119  std::vector<std::vector<float> > vShape(nPhases, vSamples);
120  LArShapeP2 larShapeP2(0.0, 0.0, vShape, vShape);
121 
122  corrs.resize(ncorrs, LArShapeTransType2::CorrectionPair(0, larShapeP2));
123  }
124 
125  // Loop over corrections
126  for (unsigned int i = 0; i < ncorrs; ++i){
127  // check indexes
128  if (dataIndex + nPhases*nSamples > persObj->m_vShape.size() ||
129  dataIndex + nPhases*nSamples > persObj->m_vShapeDer.size() ||
130  timeIndex >= persObj->m_timeOffset.size() ||
131  timeIndex >= persObj->m_timeBinWidth.size()) {
132  log << MSG::ERROR
133  << "LArShapeSubsetCnv_p2::persToTrans - data index too large: dataIndex size shape, size shapeDer, timeIndex timeOffset size, timeBinWidth size "
134  << dataIndex << " " << persObj->m_vShape.size() << " "
135  << nPhases << " " << nSamples << " "
136  << persObj->m_vShapeDer.size() << " " << timeIndex << " "
137  << persObj->m_timeOffset.size() << " "
138  << persObj->m_timeBinWidth.size()
139  << endmsg;
140  return;
141  }
142 
143  // copy channel id
144  corrs[i].first = persObj->m_subset.m_corrChannels[i];
145 
146  LArShapeP2& shape = corrs[i].second;
147  LArShapeP2 tmp (persObj->m_timeOffset[timeIndex],
148  persObj->m_timeBinWidth[timeIndex],
149  nPhases,
150  nSamples,
151  persObj->m_vShape,
152  persObj->m_vShapeDer,
153  dataIndex);
154  shape.setFrom (tmp);
155  ++timeIndex;
156  dataIndex += nPhases * nSamples;
157  }
158  transObj->insertCorrections (std::move (corrs));
159 
160  transObj->shrink_to_fit();
161 }
162 
163 
164 
165 
166 void
168  LArShapePersType2* persObj,
169  MsgStream &log) const
170 {
171  // Copy conditions
172 
173  // We copy all shapes into a few simple vectors.
174  // For the conditions, there are two situations to treat:
175  // 1) dense data: normal conditions where each feb has 128
176  // channels and all channels have data,
177  // 2) sparse data: conditions data where some channels are
178  // missing data. This is true for MC conditions (only some
179  // channels have data, and symmetry is used to obtain
180  // conditions for the rest of the channels), as well for
181  // 'normal' conditions it may happen that some channels may
182  // be missing data.
183  //
184  // Treating 1) is straight-forward. For 2) we need to keep track
185  // of which channels are present. We do so with
186  // m_subset.m_febsWithSparseData where we store the febid followed by
187  // four unsigned ints which contain the full bit pattern of the
188  // channels set (i.e. bits 0-127).
189  //
190  // Note that one may also have a subset with all channels missing
191  // data. In this case, we do not write out the empty subset.
192  //
193  // Finally, for corrections, we save the channel ids in
194  // m_subset.m_corrChannels and the shapes in the same vectors as the
195  // rest of the conditions data.
196  //
197  // For each channel with data, the number of shapes is assumed
198  // constant. This is calculated at the beginning, along with
199  // whether a feb is sparse or not.
200  //
201 
202  // Get the number of channels, corrections and the size of shape vectors
203  unsigned int nsubsetsNotEmpty = 0;
204  unsigned int ncorrs = transObj->correctionVecSize();
205  const unsigned int nChannelsPerFeb = transObj->channelVectorSize();
206  unsigned int nchans = 0;
207  unsigned int nPhases = 0;
208  unsigned int nSamples = 0;
209  bool foundShapes = false;
210  std::vector<unsigned int> febsWithSparseData;
211 
212  // Find the number of shapes and check for sparse conditions,
213  // e.g. MC conditions
214  const auto subsetEnd = transObj->subsetEnd();
215  for (auto subsetIt = transObj->subsetBegin();
216  subsetIt != subsetEnd;
217  ++subsetIt)
218  {
219  unsigned int nfebChans = (*subsetIt).second.size();
220 
221  if (nfebChans != 0 && nfebChans != nChannelsPerFeb) {
222  log << MSG::ERROR
223  << "LArShapeSubsetCnv_p2::transToPers - found incorrect number of channels per feb: " << nfebChans
224  << endmsg;
225  return;
226  }
227  if (nfebChans) ++nsubsetsNotEmpty; // count number of non-empty subsets
228 
229  // Loop over channels and check if this subset has sparse data
230  bool subsetIsSparse = false;
231  for (unsigned int j = 0; j < nfebChans; ++j) {
233  (*subsetIt).second[j];
234  if (shapes.shapeSize() == 0) {
235  if (!subsetIsSparse) {
236  // save febids for sparse subsets
237  subsetIsSparse = true;
238  febsWithSparseData.push_back((*subsetIt).first);
239  }
240  }
241  else {
242  nchans++; // count number of channels
243  if (!foundShapes) {
244  // Save the number of phases and samples for each
245  // shape from first channels present
246  nPhases = shapes.shapeSize();
247  nSamples = shapes.shape(0).size();
248  foundShapes = true;
249  }
250  }
251  }
252  }
253  if (!foundShapes && ncorrs>0) {
254  // Save the number of phases and samples for each shape from
255  // first correction - couldn't find it from channels
256  const LArShapeP2& shapes = transObj->correctionVecBegin()->second;
257  nPhases = shapes.shapeSize();
258  nSamples = nPhases ? shapes.shape(0).size() : 0;
259  }
260 
261  // Save sizes
262  persObj->m_nPhases = nPhases;
263  persObj->m_nSamples = nSamples;
264 
265  // Reserve space in vectors
266  persObj->m_subset.m_febIds.reserve(nsubsetsNotEmpty);
267  persObj->m_subset.m_corrChannels.reserve(ncorrs);
268  unsigned int ndataTot = (nchans + ncorrs)*nPhases*nSamples;
269  unsigned int nTime = (nchans + ncorrs);
270  persObj->m_vShape.reserve(ndataTot);
271  persObj->m_vShapeDer.reserve(ndataTot);
272  persObj->m_timeOffset.reserve(nTime);
273  persObj->m_timeBinWidth.reserve(nTime);
274 
275  // For subsets with sparse data, reserve space for identifying
276  // channels written out:
277  // 1 - febid
278  // 4 - for 128 bits (4*32)
279  if (febsWithSparseData.size())
280  persObj->m_subset.m_febsWithSparseData.reserve(febsWithSparseData.size()*5);
281 
282  // Copy conditions in subset
283  unsigned int isparse = 0;
284  for (auto subsetIt = transObj->subsetBegin();
285  subsetIt != subsetEnd;
286  ++subsetIt)
287  {
288  unsigned int nfebChans = (*subsetIt).second.size();
289 
290  // skip subsets without any channels
291  if (nfebChans == 0) continue;
292 
293  unsigned int febid = (*subsetIt).first;
294  persObj->m_subset.m_febIds.push_back(febid);
295 
296 
297  bool isSparse = false;
298  if (isparse < febsWithSparseData.size() &&
299  febsWithSparseData[isparse] == febid) {
300  // sparse subset, save channels with data
301  isparse++;
302  isSparse = true;
303  // save febid
304  persObj->m_subset.m_febsWithSparseData.push_back(febid);
305  }
306 
307  // Now loop over channels
308  unsigned int chansSet = 0;
309  unsigned int chansOffset = 0;
310  for (unsigned int j = 0; j < nfebChans; ++j){
311 
312  bool saveShapes = true;
313  if (isSparse) {
314  // subset with sparse data
315  if ((*subsetIt).second[j].shapeSize() > 0) {
316  // store the channel number in bit map
317  assert (j >= chansOffset && (j - chansOffset) <= 31);
318  chansSet |= (1 << (j - chansOffset));
319  }
320  else {
321  saveShapes = false;
322  }
323  // Save chansSet
324  if (j == (chansOffset + 31) || j == nfebChans-1 ) {
325  persObj->m_subset.m_febsWithSparseData.push_back(chansSet);
326  chansSet = 0;
327  chansOffset += 32;
328  }
329  }
330  if (saveShapes) {
331  bool tooSmall=false;
332  // Loop over phases and samples per channel
333  for (unsigned int k = 0; k < nPhases; ++k) {
334  for (unsigned int l = 0; l < nSamples; ++l) {
335  //check if data object is big enough
336  if (k>=(*subsetIt).second[j].shapeSize() ||
337  l>=(*subsetIt).second[j].shape(k).size())
338  {
339  tooSmall=true;
340  persObj->m_vShape.push_back(0.);
341  persObj->m_vShapeDer.push_back(0.);
342  }
343  else {
344  persObj->m_vShape.push_back((*subsetIt).second[j].shape(k)[l]);
345  persObj->m_vShapeDer.push_back((*subsetIt).second[j].shapeDer(k)[l]);
346 // std::cout << "WL Data: FEB=" << std::hex << febid << std::dec << " [" << i << "] Channel="
347 // << j << " Phase="<< k<< " Sample " << l << " Shape="
348 // << (*subsetIt).second[j].m_vShape[k][l] << std::endl;
349  }
350  }
351  }
352  // set time offset and binwidth
353  persObj->m_timeOffset.push_back((*subsetIt).second[j].timeOffset());
354  persObj->m_timeBinWidth.push_back((*subsetIt).second[j].timeBinWidth());
355  if (tooSmall)
356  log << MSG::ERROR << "Feb 0x" << std::hex << febid << std::dec << " channel " << j <<": Shape object too small. Expected "
357  << nPhases << " phases and " << nSamples << " samples. Padded with 0.0" << endmsg;
358  }
359 
360 // static unsigned int nch = 0;
361 // ++nch;
362 // std::cout << "transToPers - i, j, save " << i << " " << j << " "
363 // << saveShapes << " " << nch << " febid " << febid
364 // << " chansSet " << std::hex << chansSet << std::dec
365 // << " chansOffset " << chansOffset
366 // << std::endl;
367 
368  }
369  }
370 
371  // Copy corrections
372  const auto corrEnd = transObj->correctionVecEnd();
373  for (auto corrIt = transObj->correctionVecBegin();
374  corrIt != corrEnd;
375  ++corrIt)
376  {
377  // Save channel id in febid vector
378  persObj->m_subset.m_corrChannels.push_back(corrIt->first);
379  // Shapes
380  bool tooSmall=false;
381  // Loop over phases and samples per channel
382  for (unsigned int k = 0; k < nPhases; ++k) {
383  for (unsigned int l = 0; l < nSamples; ++l) {
384  if (k>=corrIt->second.shapeSize() ||
385  l>=corrIt->second.shape(k).size())
386  {
387  tooSmall=true;
388  persObj->m_vShape.push_back(0.);
389  persObj->m_vShapeDer.push_back(0.);
390  }
391  else {
392  persObj->m_vShape.push_back(corrIt->second.shape(k)[l]);
393  persObj->m_vShapeDer.push_back(corrIt->second.shapeDer(k)[l]);
394  }
395  }
396  }
397  // set time offset and binwidth
398  persObj->m_timeOffset.push_back(corrIt->second.timeOffset());
399  persObj->m_timeBinWidth.push_back(corrIt->second.timeBinWidth());
400  if (tooSmall)
401  log << MSG::ERROR << "Correction (channel 0x" << std::hex << corrIt->first << std::dec <<
402  "): Shape object too small. Expected " << nPhases << " phases and " << nSamples << " samples. Padded with 0.0" << endmsg;
403  }
404 
405  // Copy the rest
406  persObj->m_subset.m_gain = transObj->gain();
407  persObj->m_subset.m_channel = transObj->channel();
408  persObj->m_subset.m_groupingType = transObj->groupingType();
409 }
LArConditionsSubset::setGroupingType
void setGroupingType(unsigned int type)
set the type of grouping - defined in LArConditionsContainerBase.h
Definition: LArConditionsSubset.h:575
LArConditionsSubset_p1::m_channel
unsigned int m_channel
Definition: LArConditionsSubset_p1.h:79
LArShapeSubset_p2::m_timeOffset
std::vector< float > m_timeOffset
Definition: LArShapeSubset_p2.h:38
LArConditionsSubset_p1::m_corrChannels
std::vector< unsigned int > m_corrChannels
Definition: LArConditionsSubset_p1.h:76
LArConditionsSubset_p1::m_febsWithSparseData
std::vector< unsigned int > m_febsWithSparseData
Definition: LArConditionsSubset_p1.h:77
LArShapeSubsetCnv_p2::persToTrans
virtual void persToTrans(const LArShapePersType2 *persObj, LArShapeTransType2 *transObj, MsgStream &log) const override
Definition: LArShapeSubsetCnv_p2.cxx:9
LArConditionsSubset::channelVectorSize
unsigned channelVectorSize() const
Definition: LArConditionsSubset.h:608
LAr2DWaveBase::setFrom
void setFrom(LAr2DWaveBase &other)
Assign from another wave object.
Definition: LAr2DWaveBase.cxx:109
LArConditionsSubset_p1::subsetSize
unsigned int subsetSize() const
Definition: LArConditionsSubset_p1.h:82
LArShapeSubsetCnv_p2.h
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
LArConditionsSubset::shrink_to_fit
void shrink_to_fit()
Reallocate to match size actually used.
Definition: LArConditionsSubset.h:550
LArConditionsSubset_p1::m_gain
unsigned int m_gain
Definition: LArConditionsSubset_p1.h:78
LArConditionsSubset::correctionVecEnd
ConstCorrectionVecIt correctionVecEnd() const
Definition: LArConditionsSubset.h:472
LArConditionsSubset_p1::m_febIds
std::vector< unsigned int > m_febIds
Definition: LArConditionsSubset_p1.h:75
LArConditionsSubset::initialize
void initialize(const std::vector< FebId > &ids, unsigned int gain)
Initialize with set of FEB ids.
Definition: LArConditionsSubset.h:529
LArShapeSubset_p2::m_nSamples
unsigned int m_nSamples
Definition: LArShapeSubset_p2.h:41
LArShapeSubset_p2::m_timeBinWidth
std::vector< float > m_timeBinWidth
Definition: LArShapeSubset_p2.h:39
LArConditionsSubset::CorrectionVec
std::vector< CorrectionPair > CorrectionVec
Definition: LArConditionsSubset.h:144
LArShapeSubsetCnv_p2::transToPers
virtual void transToPers(const LArShapeTransType2 *transObj, LArShapePersType2 *persObj, MsgStream &log) const override
Definition: LArShapeSubsetCnv_p2.cxx:167
LArShapeSubset_p2::m_nPhases
unsigned int m_nPhases
Definition: LArShapeSubset_p2.h:40
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
LArConditionsSubset_p1::m_groupingType
unsigned int m_groupingType
Definition: LArConditionsSubset_p1.h:80
LArConditionsSubset::channel
unsigned int channel() const
Access to the COOL channel number.
Definition: LArConditionsSubset.h:498
LArShapeSubset_p2
persistent class container of LArConditionsSubset for LArShape data.
Definition: LArShapeSubset_p2.h:30
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
LArConditionsSubset::setChannel
void setChannel(unsigned int channel)
set the COOL channel number
Definition: LArConditionsSubset.h:567
LArConditionsSubset::ConstReference
Traits::ConstReference ConstReference
Definition: LArConditionsSubset.h:136
LArConditionsSubset
template class for use for I/O of conditions data
Definition: LArConditionsSubset.h:122
LArConditionsSubset.h
This file defines the template class used for I/O of conditions data.
LArConditionsSubset::CorrectionPair
std::pair< ChannelId, T > CorrectionPair
Definition: LArConditionsSubset.h:143
LArShapeP2
c-struct reproducing the structure of the persistent data
Definition: LArShapeP2.h:21
LArShapeSubset_p2::m_vShapeDer
std::vector< float > m_vShapeDer
Definition: LArShapeSubset_p2.h:37
LArShapeP2::shape
ShapeRef_t shape(size_t tbin) const
Definition: LArShapeP2.h:44
LArShapeSubset_p2::m_vShape
std::vector< float > m_vShape
Definition: LArShapeSubset_p2.h:36
LArConditionsSubset::gain
unsigned int gain() const
Access to gain.
Definition: LArConditionsSubset.h:490
LArConditionsSubset::correctionVecSize
size_type correctionVecSize() const
Size of channel set.
Definition: LArConditionsSubset.h:481
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArShapeSubset_p2::m_subset
LArConditionsSubset_p1 m_subset
Definition: LArShapeSubset_p2.h:35
LArShapeP2::shapeSize
size_t shapeSize() const
Definition: LArShapeP2.h:41
LArConditionsSubset::Reference
Traits::Reference Reference
Definition: LArConditionsSubset.h:135
LArDigits2NtupleDumper.nSamples
nSamples
Definition: LArDigits2NtupleDumper.py:70
LArConditionsSubset::subsetBegin
ConstSubsetIt subsetBegin() const
Iterators over subset.
Definition: LArConditionsSubset.h:412
LArConditionsSubset::subsetEnd
ConstSubsetIt subsetEnd() const
Definition: LArConditionsSubset.h:420
LArConditionsSubset::correctionVecBegin
ConstCorrectionVecIt correctionVecBegin() const
Iterators over channel set.
Definition: LArConditionsSubset.h:465
LArConditionsSubset::insertCorrections
void insertCorrections(CorrectionVec &&corrs)
Insert a group of corrections.
Definition: LArConditionsSubset.h:593
fitman.k
k
Definition: fitman.py:528
LArConditionsSubset::groupingType
unsigned int groupingType() const
Type of grouping - defined in LArConditionsContainerBase.h.
Definition: LArConditionsSubset.h:507