CMS 3D CMS Logo

SiStripGainFromAsciiFile.cc
Go to the documentation of this file.
4 
6 
8 
9 
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 
14 
16 
17 
18  Asciifilename_=iConfig.getParameter<std::string>("InputFileName");
19  referenceValue_ = iConfig.getParameter<double>("referenceValue");
20  fp_ = iConfig.getUntrackedParameter<edm::FileInPath>("file",edm::FileInPath("CalibTracker/SiStripCommon/data/SiStripDetInfo.dat"));
21 }
22 
23 
25  edm::LogInfo("SiStripGainFromAsciiFile::~SiStripGainFromAsciiFile");
26 }
27 
28 std::unique_ptr<SiStripApvGain> SiStripGainFromAsciiFile::getNewObject(){
29 
30  auto obj = std::make_unique<SiStripApvGain>();
31 
32 
33  std::stringstream ss;
34  ss.str("");
35  ss << "[SiStripGainFromAsciiFile::getNewObject]\n Reading Ascii File\n";
36  FILE* infile = fopen (Asciifilename_.c_str(), "r");
37  char line[4096];
38  if (infile){
39  while(fgets(line, 4096, infile)!=nullptr){
40  uint32_t detid;
41  ModuleGain MG;
42  MG.apv[0] = 0.0; MG.apv[1] = 0.0; MG.apv[2] = 0.0; MG.apv[3] = 0.0; MG.apv[4] = 0.0; MG.apv[5] = 0.0;
43  char* saveptr;
44  char* pch=strtok_r(line," ",&saveptr); 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);Arg++;
54  }
55  ss << detid << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4] << " " << MG.apv[5] << std::endl;
56  GainsMap.insert(std::pair<unsigned int,ModuleGain>(detid,MG));
57  }
58  fclose(infile);
59  edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
60  } else {
61  edm::LogError("SiStripGainFromAsciiFile")<< " [SiStripGainFromAsciiFile::getNewObject] Error opening file " << Asciifilename_ << std::endl;
62  assert(0);
63  }
64 
65 
66 
68 
69  const std::vector<uint32_t>& DetIds = reader.getAllDetIds();
70 
71  ss.str("");
72  ss << "[SiStripGainFromAsciiFile::getNewObject]\n Filling SiStripApvGain object";
73  short nApvPair;
74  for(std::vector<uint32_t>::const_iterator it=DetIds.begin(); it!=DetIds.end(); it++){
75  ModuleGain MG;
76  if (DetId(*it).det()!=DetId::Tracker)
77  continue;
78 
79  nApvPair=reader.getNumberOfApvsAndStripLength(*it).first/2;
80 
81  ss << "Looking at detid " << *it << " nApvPairs " << nApvPair << std::endl;
82  auto iter=GainsMap.find(*it);
83  if (iter!=GainsMap.end()){
84  MG = iter->second;
85  ss << " " << MG.apv[0] << " " << MG.apv[1] << " " << MG.apv[2] << " " << MG.apv[3] << " " << MG.apv[4] << " " << 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 << " not expected number of APV pairs " << nApvPair <<std::endl;
107  }
108 
109  SiStripApvGain::Range range(DetGainsVector.begin(),DetGainsVector.end());
110  if ( ! obj->put(*it,range) ){
111  edm::LogError("SiStripGainFromAsciiFile")<<" [SiStripGainFromAsciiFile::getNewObject] detid already exists"<<std::endl;
112  ss <<" [SiStripGainFromAsciiFile::getNewObject] detid already exists"<<std::endl;
113  }
114  }
115  edm::LogInfo("SiStripGainFromAsciiFile") << ss.str();
116 
117  return obj;
118 }
119 
120 
121 
122 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
SiStripGainFromAsciiFile(const edm::ParameterSet &)
std::pair< ContainerIterator, ContainerIterator > Range
std::unique_ptr< SiStripApvGain > getNewObject() override
Definition: DetId.h:18
std::unordered_map< unsigned int, ModuleGain > GainsMap
std::string fullPath() const
Definition: FileInPath.cc:163
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:39