ATLAS Offline Software
LArAutoCorrSubsetCnv_p1.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  LArAutoCorrTransType* 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  transObj->initialize (persObj->m_subset.m_febIds, persObj->m_subset.m_gain);
17 
18  unsigned int nfebids = persObj->m_subset.m_febIds.size();
19  const unsigned int nChannelsPerFeb = persObj->m_subset.subsetSize();
20  unsigned int nAutoCorrs = persObj->m_vAutoCorrSize;
21  unsigned int autocorrIndex = 0;
22 
23  unsigned int ifebWithData = 0; // counter for febs with data
24  auto subsetIt = transObj->subsetBegin();
25  for (unsigned int i = 0; i < nfebids; ++i, ++subsetIt){
26  unsigned int febid = subsetIt->first;
27  bool hasSparseData = false;
28  unsigned int chansSet = 0;
29  unsigned int chansOffset = 0;
30  if (ifebWithData < persObj->m_subset.m_febsWithSparseData.size() &&
31  febid == persObj->m_subset.m_febsWithSparseData[ifebWithData] ) {
32  // Found feb with sparse data
33  hasSparseData = true;
34  ifebWithData++;
35  chansSet = persObj->m_subset.m_febsWithSparseData[ifebWithData];
36  chansOffset = 0;
37  ifebWithData++;
38  }
39 
40  // Loop over channels in feb - only some channels are filled
41  for (unsigned int j = 0; j < nChannelsPerFeb; ++j){
42 
43  bool copyChannel = true;
44  if (hasSparseData) {
45  if (!(chansSet & (1 << (j - chansOffset)))) {
46  // Channel is missing data - skip
47  copyChannel = false;
48  }
49  if (j%32 == 31 && j < nChannelsPerFeb-2) {
50  chansSet = persObj->m_subset.m_febsWithSparseData[ifebWithData];
51  chansOffset += 32;
52  ifebWithData++;
53  }
54  }
55  if (copyChannel) {
56 
57  // Channel has autocorrs - loop over autocorrs per channel
58  // and copy to the persistent object
59 
60  // check indexes
61  if (autocorrIndex + nAutoCorrs > persObj->m_vAutoCorr.size()) {
62  log << MSG::ERROR
63  << "LArAutoCorrSubsetCnv_p1::persToTrans - autocorr index too large: autocorr/size "
64  << autocorrIndex << " " << persObj->m_vAutoCorr.size() << " "
65  << endmsg;
66  return;
67  }
68 
69  subsetIt->second[j].m_vAutoCorr.assign
70  (persObj->m_vAutoCorr.begin() + autocorrIndex,
71  persObj->m_vAutoCorr.begin() + autocorrIndex + nAutoCorrs);
72  autocorrIndex += nAutoCorrs;
73  }
74 
75  // static unsigned int nch1 = 0;
76  // ++nch1;
77  // std::cout << "persToTrans - i, j, copy " << i << " " << j << " "
78  // << copyChannel << " " << nch1
79  // << " hasSparseData " << hasSparseData
80  // << " chansSet, chansOffset, ifebWithData " << std::hex
81  // << chansSet << std::dec << " " << chansOffset << " " << ifebWithData
82  // << " febids " << febid << " "
83  // << sparseFebid
84  // << std::endl;
85 
86  }
87  }
88 
89  unsigned int ncorrs = persObj->m_subset.m_corrChannels.size();
91  corrs.resize(ncorrs);
92 
93  // Loop over corrections
94  for (unsigned int i = 0; i < ncorrs; ++i){
95  // check indexes
96  if (autocorrIndex + nAutoCorrs > persObj->m_vAutoCorr.size()) {
97  log << MSG::ERROR
98  << "LArAutoCorrSubsetCnv_p1::persToTrans - autocorr index too large: autocorr/size "
99  << autocorrIndex << " " << persObj->m_vAutoCorr.size() << " "
100  << endmsg;
101  return;
102  }
103 
104  corrs[i].first = persObj->m_subset.m_corrChannels[i];
105 
106  corrs[i].second.m_vAutoCorr.assign
107  (persObj->m_vAutoCorr.begin() + autocorrIndex,
108  persObj->m_vAutoCorr.begin() + autocorrIndex + nAutoCorrs);
109  autocorrIndex += nAutoCorrs;
110  }
111  transObj->insertCorrections (std::move (corrs));
112 
113 }
114 
115 
116 
117 
118 void
120  LArAutoCorrPersType* persObj,
121  MsgStream &log) const
122 {
123  // Copy conditions
124 
125  // We copy all autocorrs into a single vector.
126  // For the conditions, there are two situations to treat:
127  // 1) dense data: normal conditions where each feb has 128
128  // channels and all channels have data,
129  // 2) sparse data: conditions data where some channels are
130  // missing data. This is true for MC conditions (only some
131  // channels have data, and symmetry is used to obtain
132  // conditions for the rest of the channels), as well for
133  // 'normal' conditions it may happen that some channels may
134  // be missing data.
135  //
136  // Treating 1) is straight-forward. For 2) we need to keep track
137  // of which channels are present. We do so with
138  // m_subset.m_febsWithSparseData where we store the febid followed by
139  // four unsigned ints which contain the full bit pattern of the
140  // channels set (i.e. bits 0-127).
141  //
142  // Note that one may also have a subset with all channels missing
143  // data. In this case, we do not write out the empty subset.
144  //
145  // Finally, for corrections, we save the channel ids in
146  // m_subset.m_corrChannels and the autocorrs in the same vectors as
147  // the rest of the conditions data.
148  //
149  // For each channel with data, the number of autocorrs is assumed
150  // constant. This is calculated at the beginning, along with
151  // whether a feb is sparse or not.
152  //
153 
154  // Get the number of channels, corrections and the size of autocorr
155  unsigned int nsubsetsNotEmpty = 0;
156  unsigned int ncorrs = transObj->correctionVecSize();
157  unsigned int nchans = 0;
158  unsigned int nAutoCorrs = 0;
159  const unsigned int nChannelsPerFeb = transObj->channelVectorSize();
160  bool foundNAutoCorrs = false;
161  std::vector<unsigned int> febsWithSparseData;
162 
163  // Find the number of autocorrs and check for sparse conditions,
164  // e.g. MC conditions
165  const auto subsetEnd = transObj->subsetEnd();
166  for (auto subsetIt = transObj->subsetBegin();
167  subsetIt != subsetEnd;
168  ++subsetIt)
169  {
170  unsigned int nfebChans = subsetIt->second.size();
171 
172  if (nfebChans != 0 && nfebChans != nChannelsPerFeb) {
173  log << MSG::ERROR
174  << "LArAutoCorrSubsetCnv_p1::transToPers - found incorrect number of channels per feb: " << nfebChans
175  << endmsg;
176  return;
177  }
178  if (nfebChans) ++nsubsetsNotEmpty; // count number of non-empty subsets
179 
180  // Loop over channels and check if this subset has sparse data
181  bool subsetIsSparse = false;
182  for (unsigned int j = 0; j < nfebChans; ++j) {
183  const LArAutoCorrP1& autocorr = subsetIt->second[j];
184  if (autocorr.m_vAutoCorr.size() == 0) {
185  if (!subsetIsSparse) {
186  // save febids for sparse subsets
187  subsetIsSparse = true;
188  febsWithSparseData.push_back(subsetIt->first);
189  }
190  }
191  else {
192  nchans++; // count number of channels
193  if (!foundNAutoCorrs) {
194  // Save the number of autocorrs and derivatives from first channels present
195  nAutoCorrs = autocorr.m_vAutoCorr.size();
196  foundNAutoCorrs = true;
197  }
198  }
199  }
200  }
201  if (!foundNAutoCorrs && ncorrs>0) {
202  // Save the number of autocorrs and derivatives from first
203  // correct - couldn't find it from channels
204  const LArAutoCorrP1& autocorr = transObj->correctionVecBegin()->second;
205  nAutoCorrs = autocorr.m_vAutoCorr.size();
206  }
207  // Save sizes
208  persObj->m_vAutoCorrSize = nAutoCorrs;
209 
210  // Reserve space in vectors
211  persObj->m_subset.m_febIds.reserve(nsubsetsNotEmpty);
212  persObj->m_subset.m_corrChannels.reserve(ncorrs);
213  unsigned int nAutoCorrsTot = (nchans + ncorrs)*nAutoCorrs;
214  persObj->m_vAutoCorr.reserve(nAutoCorrsTot);
215 
216  // For subsets with sparse data, reserve space for identifying
217  // channels written out:
218  // 1 - febid
219  // 4 - for 128 bits (4*32)
220  if (febsWithSparseData.size())
221  persObj->m_subset.m_febsWithSparseData.reserve(febsWithSparseData.size()*5);
222 
223  // Copy conditions in subset
224  unsigned int isparse = 0;
225  for (auto subsetIt = transObj->subsetBegin();
226  subsetIt != subsetEnd;
227  ++subsetIt)
228  {
229  unsigned int nfebChans = subsetIt->second.size();
230 
231  // skip subsets without any channels
232  if (nfebChans == 0) continue;
233 
234  unsigned int febid = subsetIt->first;
235  persObj->m_subset.m_febIds.push_back(febid);
236 
237 
238  bool isSparse = false;
239  if (isparse < febsWithSparseData.size() &&
240  febsWithSparseData[isparse] == febid) {
241  // sparse subset, save channels with data
242  isparse++;
243  isSparse = true;
244  // save febid
245  persObj->m_subset.m_febsWithSparseData.push_back(febid);
246  }
247 
248  // Now loop over channels and save autocorrs and channel number
249  unsigned int chansSet = 0;
250  unsigned int chansOffset = 0;
251  for (unsigned int j = 0; j < nfebChans; ++j){
252 
253  bool saveAutoCorrs = true;
254  if (isSparse) {
255  // subset with sparse data
256 
257  if (subsetIt->second[j].m_vAutoCorr.size() > 0) {
258  // store the channel number in bit map
259  assert (j >= chansOffset && (j - chansOffset) <= 31);
260  chansSet |= (1 << (j - chansOffset));
261  }
262  else {
263  saveAutoCorrs = false;
264  }
265  // Save chansSet
266  if (j == (chansOffset + 31) || j == nfebChans - 1) {
267  persObj->m_subset.m_febsWithSparseData.push_back(chansSet);
268  chansSet = 0;
269  chansOffset += 32;
270  }
271  }
272  if (saveAutoCorrs) {
273  // save autocorrs
274  bool tooSmall=false;
275  for (unsigned int k = 0; k < nAutoCorrs; ++k){
276  if (k>=subsetIt->second[j].m_vAutoCorr.size()) {
277  persObj->m_vAutoCorr.push_back(0.);
278  tooSmall=true;
279  }
280  else
281  persObj->m_vAutoCorr.push_back(subsetIt->second[j].m_vAutoCorr[k]);
282  }
283  if (tooSmall)
284  log << MSG::ERROR << "Feb 0x" << std::hex << febid << std::dec << " channel " << j <<": AutoCorr object too small. Expected "
285  << nAutoCorrs << " entries. Padded with 0.0" << endmsg;
286  }
287 // static unsigned int nch = 0;
288 // ++nch;
289 // std::cout << "transToPers - i, j, save " << i << " " << j << " "
290 // << saveAutoCorrs << " " << nch << " febid " << febid
291 // << " chansSet " << std::hex << chansSet << std::dec
292 // << " chansOffset " << chansOffset
293 // << std::endl;
294 
295  }
296  }
297 
298  const auto corrEnd = transObj->correctionVecEnd();
299  for (auto corrIt = transObj->correctionVecBegin();
300  corrIt != corrEnd;
301  ++corrIt)
302  {
303  // Save channel id in febid vector
304  persObj->m_subset.m_corrChannels.push_back(corrIt->first);
305  // AutoCorrs
306  bool tooSmall=false;
307  for (unsigned int k = 0; k < nAutoCorrs; ++k){
308  if (k>=corrIt->second.m_vAutoCorr.size()) {
309  persObj->m_vAutoCorr.push_back(0.);
310  tooSmall=true;
311  }
312  else
313  persObj->m_vAutoCorr.push_back(corrIt->second.m_vAutoCorr[k]);
314  }
315  if (tooSmall)
316  log << MSG::ERROR << "Correction (channel 0x" << std::hex << corrIt->first << std::dec <<
317  "): AutoCorr object too small. Expected " << nAutoCorrs << " entries. Padded with 0.0" << endmsg;
318  }
319 
320  // Copy the rest
321  persObj->m_subset.m_gain = transObj->gain();
322  persObj->m_subset.m_channel = transObj->channel();
323  persObj->m_subset.m_groupingType = transObj->groupingType();
324 
325 }
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
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
LArConditionsSubset::channelVectorSize
unsigned channelVectorSize() const
Definition: LArConditionsSubset.h:608
LArAutoCorrP1
c-struct reproducing the structure of the persistent data
Definition: LArAutoCorrP1.h:25
LArConditionsSubset_p1::subsetSize
unsigned int subsetSize() const
Definition: LArConditionsSubset_p1.h:82
LArConditionsSubset_p1::m_gain
unsigned int m_gain
Definition: LArConditionsSubset_p1.h:78
LArConditionsSubset::correctionVecEnd
ConstCorrectionVecIt correctionVecEnd() const
Definition: LArConditionsSubset.h:472
LArAutoCorrSubsetCnv_p1::persToTrans
virtual void persToTrans(const LArAutoCorrPersType *persObj, LArAutoCorrTransType *transObj, MsgStream &log) const override
Definition: LArAutoCorrSubsetCnv_p1.cxx:9
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
LArConditionsSubset::CorrectionVec
std::vector< CorrectionPair > CorrectionVec
Definition: LArConditionsSubset.h:144
lumiFormat.i
int i
Definition: lumiFormat.py:92
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
LArConditionsSubset::setChannel
void setChannel(unsigned int channel)
set the COOL channel number
Definition: LArConditionsSubset.h:567
LArAutoCorrSubsetCnv_p1::transToPers
virtual void transToPers(const LArAutoCorrTransType *transObj, LArAutoCorrPersType *persObj, MsgStream &log) const override
Definition: LArAutoCorrSubsetCnv_p1.cxx:119
LArAutoCorrSubset_p1::m_vAutoCorrSize
unsigned int m_vAutoCorrSize
Definition: LArAutoCorrSubset_p1.h:37
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.
LArAutoCorrSubset_p1::m_subset
LArConditionsSubset_p1 m_subset
Definition: LArAutoCorrSubset_p1.h:35
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
LArAutoCorrSubsetCnv_p1.h
LArAutoCorrSubset_p1
persistent class container of LArConditionsSubset for LArAutoCorr data.
Definition: LArAutoCorrSubset_p1.h:30
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArConditionsSubset::subsetBegin
ConstSubsetIt subsetBegin() const
Iterators over subset.
Definition: LArConditionsSubset.h:412
LArAutoCorrP1::m_vAutoCorr
std::vector< float > m_vAutoCorr
Definition: LArAutoCorrP1.h:31
LArAutoCorrSubset_p1::m_vAutoCorr
std::vector< float > m_vAutoCorr
Definition: LArAutoCorrSubset_p1.h:36
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