Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:34

0001 #include "CondFormats/SiPixelObjects/interface/SiPixelDbItem.h"
0002 
0003 #include <iostream>
0004 #include <algorithm>
0005 
0006 //! The logic of individual setters:
0007 //! First, we put the new value into newXXX, which is a 32-bit word
0008 //! which has the new value at the bits where it should go, and 0's
0009 //! everywhere else.  To make it, we:
0010 //! - assign the value
0011 //! - AND with a mask -- so all others are zeroes
0012 //! - shift up by noise_shift bits
0013 //!
0014 //! Next, we prepare the oldValue with a whole where newXXX needs to go.
0015 //!  ~(mask << shift) has 1's everywhere except where the new value will go,
0016 //! so AND-ing packedVal_ with it creates a `whole' for the new value.
0017 //!
0018 //! Finally, the new value is an OR of the two (since the wholes have 0's)
0019 //!
0020 //! Example:
0021 //!   00 03 c3 02 -- old, new one must blow c3 away, so we AND with ff ff 00 ff,
0022 //!   and get 00 03 00 02.
0023 //!   The new number is now positioned to be ff ff a2 ff, and then we AND these
0024 //!   two and get 00 03 a2 02.  We have replaced c3 with a2.
0025 
0026 void SiPixelDbItem::setNoise(short n) {
0027   PackedPixDbType newNoise = (n & packing_.noise_mask) << packing_.noise_shift;
0028   PackedPixDbType oldValue = packedVal_ & (~(packing_.noise_mask << packing_.noise_shift));
0029   packedVal_ = oldValue | newNoise;
0030 }
0031 
0032 void SiPixelDbItem::setPedestal(short p) {
0033   PackedPixDbType newPedestal = (p & packing_.pedestal_mask) << packing_.pedestal_shift;
0034   PackedPixDbType oldValue = packedVal_ & (~(packing_.pedestal_mask << packing_.pedestal_shift));
0035   packedVal_ = oldValue | newPedestal;
0036 }
0037 
0038 void SiPixelDbItem::setGain(float g) {
0039   // Convert gain into a shifted-integer
0040   int mult_factor = 1 << packing_.gain_shift;  // TO DO: check
0041   unsigned short gInIntRep = int(g * mult_factor);
0042 
0043   PackedPixDbType newGain = (gInIntRep & packing_.gain_mask) << packing_.gain_shift;
0044   PackedPixDbType oldValue = packedVal_ & (~(packing_.gain_mask << packing_.gain_shift));
0045   packedVal_ = oldValue | newGain;
0046 }
0047 
0048 void SiPixelDbItem::setStatus(char s) {
0049   PackedPixDbType newStatus = (s & packing_.status_mask) << packing_.status_shift;
0050   PackedPixDbType oldValue = packedVal_ & (~(packing_.status_mask << packing_.status_shift));
0051   packedVal_ = oldValue | newStatus;
0052 }
0053 
0054 //! A fast version which sets all in one go.
0055 void SiPixelDbItem::set(short noise, short pedestal, float gain, char status) {
0056   // Convert gain into a shifted-integer
0057   int mult_factor = 1 << packing_.gain_shift;  // TO DO: check usage here
0058   unsigned short gInIntRep = int(gain * mult_factor);
0059 
0060   packedVal_ = (noise << packing_.noise_shift) | (pedestal << packing_.pedestal_shift) |
0061                (gInIntRep << packing_.gain_shift) | (status << packing_.status_shift);
0062 }
0063 
0064 SiPixelDbItem::Packing::Packing(int noise_w, int pedestal_w, int gain_w, int status_w)
0065     : noise_width(noise_w), pedestal_width(pedestal_w), status_width(status_w) {
0066   // Constructor: pre-computes masks and shifts from field widths
0067   // Order of fields (from right to left) is
0068   // noise, pedestal, gain, status count.
0069 
0070   if (noise_w + pedestal_w + gain_w + status_w != 32) {
0071     std::cout << std::endl
0072               << "Error in SiPixelDbItem::Packing constructor:"
0073               << "sum of field widths != 32" << std::endl;
0074     // TO DO: throw an exception?
0075   }
0076 
0077   // Fields are counted from right to left!
0078 
0079   noise_shift = 0;
0080   pedestal_shift = noise_shift + noise_w;
0081   gain_shift = pedestal_shift + pedestal_w;
0082   status_shift = gain_shift + gain_w;
0083 
0084   // Ensure the complement of the correct
0085   // number of bits:
0086   PackedPixDbType zero32 = 0;  // 32-bit wide
0087 
0088   noise_mask = ~(~zero32 << noise_w);
0089   pedestal_mask = ~(~zero32 << pedestal_w);
0090   gain_mask = ~(~zero32 << gain_w);
0091   status_mask = ~(~zero32 << status_w);
0092 }
0093 
0094 //  Initialize the packing (order is: noise, pedestal, gain, status)
0095 SiPixelDbItem::Packing SiPixelDbItem::packing_(8, 8, 8, 8);  // TO DO: TBD