CMS 3D CMS Logo

SiStripGainFromAsciiFile.cc
Go to the documentation of this file.
4 
6 
8 
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 
14  : ConditionDBWriter<SiStripApvGain>(iConfig) {
15  Asciifilename_ = iConfig.getParameter<std::string>("InputFileName");
16  referenceValue_ = iConfig.getParameter<double>("referenceValue");
18 }
19 
21  edm::LogInfo("SiStripGainFromAsciiFile::~SiStripGainFromAsciiFile");
22 }
23 
24 std::unique_ptr<SiStripApvGain> SiStripGainFromAsciiFile::getNewObject() {
25  auto obj = std::make_unique<SiStripApvGain>();
26 
27  std::stringstream ss;
28  ss.str("");
29  ss << "[SiStripGainFromAsciiFile::getNewObject]\n Reading Ascii File\n";
30  FILE* infile = fopen(Asciifilename_.c_str(), "r");
31  char line[4096];
32  if (infile) {
33  while (fgets(line, 4096, infile) != nullptr) {
34  uint32_t detid;
35  ModuleGain MG;
36  MG.apv[0] = 0.0;
37  MG.apv[1] = 0.0;
38  MG.apv[2] = 0.0;
39  MG.apv[3] = 0.0;
40  MG.apv[4] = 0.0;
41  MG.apv[5] = 0.0;
42  char* saveptr;
43  char* pch = strtok_r(line, " ", &saveptr);
44  int Arg = 0;
45  while (pch != nullptr) {
46  if (Arg == 0) {
47  sscanf(pch, "%d", &detid);
48  } else if (Arg <= 6) {
49  sscanf(pch, "%f", &(MG.apv[Arg - 1]));
50  } else {
51  //nothing to do here
52  }
53  pch = strtok_r(nullptr, " ", &saveptr);
54  Arg++;
55  }
56  ss << detid << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4]
57  << " " << MG.apv[5] << std::endl;
58  GainsMap.insert(std::pair<unsigned int, ModuleGain>(detid, MG));
59  }
60  fclose(infile);
61  edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
62  } else {
63  edm::LogError("SiStripGainFromAsciiFile")
64  << " [SiStripGainFromAsciiFile::getNewObject] Error opening file " << Asciifilename_ << std::endl;
65  assert(0);
66  }
67 
68  const auto detInfo = SiStripDetInfoFileReader::read(fp_.fullPath());
69 
70  ss.str("");
71  ss << "[SiStripGainFromAsciiFile::getNewObject]\n Filling SiStripApvGain object";
72  short nApvPair;
73  for (const auto it : detInfo.getAllDetIds()) {
74  ModuleGain MG;
75  if (DetId(it).det() != DetId::Tracker)
76  continue;
77 
78  nApvPair = detInfo.getNumberOfApvsAndStripLength(it).first / 2;
79 
80  ss << "Looking at detid " << it << " nApvPairs " << nApvPair << std::endl;
81  auto iter = GainsMap.find(it);
82  if (iter != GainsMap.end()) {
83  MG = iter->second;
84  ss << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4] << " "
85  << MG.apv[5] << std::endl;
86  } else {
87  ss << "Hard reset for detid " << it << std::endl;
89  }
90 
91  std::vector<float> DetGainsVector;
92 
93  if (nApvPair == 2) {
94  DetGainsVector.push_back(MG.apv[0] / referenceValue_);
95  DetGainsVector.push_back(MG.apv[1] / referenceValue_);
96  DetGainsVector.push_back(MG.apv[2] / referenceValue_);
97  DetGainsVector.push_back(MG.apv[3] / referenceValue_);
98  } else if (nApvPair == 3) {
99  DetGainsVector.push_back(MG.apv[0] / referenceValue_);
100  DetGainsVector.push_back(MG.apv[1] / referenceValue_);
101  DetGainsVector.push_back(MG.apv[2] / referenceValue_);
102  DetGainsVector.push_back(MG.apv[3] / referenceValue_);
103  DetGainsVector.push_back(MG.apv[4] / referenceValue_);
104  DetGainsVector.push_back(MG.apv[5] / referenceValue_);
105  } else {
106  edm::LogError("SiStripGainFromAsciiFile") << " SiStripGainFromAsciiFile::getNewObject] ERROR for detid " << it
107  << " not expected number of APV pairs " << nApvPair << std::endl;
108  }
109 
110  SiStripApvGain::Range range(DetGainsVector.begin(), DetGainsVector.end());
111  if (!obj->put(it, range)) {
112  edm::LogError("SiStripGainFromAsciiFile")
113  << " [SiStripGainFromAsciiFile::getNewObject] detid already exists" << std::endl;
114  ss << " [SiStripGainFromAsciiFile::getNewObject] detid already exists" << std::endl;
115  }
116  }
117  edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
118 
119  return obj;
120 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
SiStripGainFromAsciiFile(const edm::ParameterSet &)
Log< level::Error, false > LogError
assert(be >=bs)
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
T getUntrackedParameter(std::string const &, T const &) const
std::pair< ContainerIterator, ContainerIterator > Range
std::unique_ptr< SiStripApvGain > getNewObject() override
SiStripDetInfo read(std::string filePath)
std::unordered_map< unsigned int, ModuleGain > GainsMap
Log< level::Info, false > LogInfo
Definition: DetId.h:17
static constexpr char const *const kDefaultFile
const std::string & fullPath() const
Definition: FileInPath.cc:144