Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MicromegasCalibrationData.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MicromegasCalibrationData.cc
1 
7 #include "MicromegasMapping.h"
8 
9 #include <cdbobjects/CDBTTree.h>
10 
11 #include <TFile.h>
12 #include <fstream>
13 #include <sstream>
14 #include <iostream>
15 
16 namespace
17 {
18  static const std::string m_pedestal_key = "pedestal";
19  static const std::string m_rms_key = "rms";
20 }
21 
22 //________________________________________________________________________-
23 std::ostream& operator << (std::ostream& out, const MicromegasCalibrationData& calib_data )
24 {
25  out << "MicromegasCalibrationData" << std::endl;
26  size_t total_entries = 0;
27  for( const auto& [fee_id, data_array] : calib_data.m_raw_calibration_map )
28  {
29  total_entries += data_array.size();
30  out << "fee_id: " << fee_id << " entries: " << data_array.size() << std::endl;
31  for( size_t i=0; i<data_array.size(); ++i )
32  {
33  const auto& data = data_array[i];
34  out << "fee_id: " << fee_id << " channel: " << i << " pedestal: " << data.m_pedestal << " rms: " << data.m_rms << std::endl;
35  }
36 
37  }
38  out << "total entries: " << total_entries << std::endl;
39 
40 
41  return out;
42 }
43 
44 //________________________________________________________________________-
46 {
47  std::cout << "MicromegasCalibrationData::read - filename: " << filename << std::endl;
48 
49  // clear existing data
50  m_raw_calibration_map.clear();
52 
53  // make sure file exists before loading, otherwise crashes
54  if( !std::ifstream( filename.c_str() ).good() )
55  {
56  std::cout << "MicromegasCalibrationData::read -"
57  << " filename: " << filename << " does not exist."
58  << " No calibration loaded" << std::endl;
59  return;
60  }
61 
62  // use generic CDBTree to load
63  CDBTTree cdbttree( filename );
64  cdbttree.LoadCalibrations();
65 
66  // loop over registered fee ids
67  MicromegasMapping mapping;
68  for( const auto& fee:mapping.get_fee_id_list() )
69  {
70  // get corresponding hitset key
71  const auto hitsetkey = mapping.get_hitsetkey(fee);
72 
73  // loop over channels
74  for( int i = 0; i < m_nchannels_fee; ++i )
75  {
76  // get matching strip id
77  const auto strip = mapping.get_physical_strip( fee, i );
78 
79  // read pedestal and rms
80  int channel = fee*m_nchannels_fee + i;
81  double pedestal = cdbttree.GetDoubleValue( channel, m_pedestal_key, 0 );
82  double rms = cdbttree.GetDoubleValue( channel, m_rms_key, 0 );
83 
84  // insert in local structure
85  if( !std::isnan( rms ) )
86  {
87  // create calibration data
88  const calibration_data_t calibration_data( pedestal, rms );
89 
90  // insert in fee,channel structure
91  m_raw_calibration_map[fee].at(i) = calibration_data;
92 
93  // also insert in hitsetkey, strip structure
94  if( hitsetkey > 0 && strip >= 0 )
95  { m_mapped_calibration_map[hitsetkey].at(strip) = calibration_data; }
96  }
97  }
98  }
99 }
100 
101 //________________________________________________________________________-
103 { m_raw_calibration_map[fee].at(channel).m_pedestal = value; }
104 
105 //________________________________________________________________________-
107 { m_raw_calibration_map[fee].at(channel).m_rms = value; }
108 
109 //________________________________________________________________________-
111 {
112  std::cout << "MicromegasCalibrationData::write - filename: " << filename << std::endl;
113  if( m_raw_calibration_map.empty() ) return;
114 
115  // use generic CDBTree to load
116  CDBTTree cdbttree( filename );
117  for( const auto& [fee,array]:m_raw_calibration_map )
118  {
119  for( size_t i = 0; i < array.size(); ++i )
120  {
121  int channel = fee*m_nchannels_fee + i;
122  const auto& pedestal = array[i].m_pedestal;
123  const auto& rms = array[i].m_rms;
124  cdbttree.SetDoubleValue( channel, m_pedestal_key, pedestal );
125  cdbttree.SetDoubleValue( channel, m_rms_key, rms );
126  }
127  }
128 
129  // commit and write
130  cdbttree.Commit();
131  cdbttree.WriteCDBTTree();
132 }
133 
134 //________________________________________________________________________-
136 {
137  const auto iter = m_raw_calibration_map.find(fee);
138  return (iter != m_raw_calibration_map.end()) ? iter->second.at(channel).m_pedestal: -1;
139 }
140 
141 //________________________________________________________________________-
142 double MicromegasCalibrationData::get_rms( int fee, int channel ) const
143 {
144  const auto iter = m_raw_calibration_map.find(fee);
145  return (iter != m_raw_calibration_map.end()) ? iter->second.at(channel).m_rms: -1;
146 }
147 
148 //________________________________________________________________________-
150 {
151  const auto iter = m_mapped_calibration_map.find(hitsetkey);
152  return (iter != m_mapped_calibration_map.end()) ? iter->second.at(strip).m_pedestal: -1;
153 }
154 
155 //________________________________________________________________________-
157 {
158  const auto iter = m_mapped_calibration_map.find(hitsetkey);
159  return (iter != m_mapped_calibration_map.end()) ? iter->second.at(strip).m_rms: -1;
160 }