CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SiStripBadStripFromASCIIFile.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <cstdio>
4 #include <string>
5 
6 // user include files
22 
23 class SiStripBadStripFromASCIIFile : public ConditionDBWriter<SiStripBadStrip> {
24 public:
26  ~SiStripBadStripFromASCIIFile() override = default;
28 
29 private:
30  std::unique_ptr<SiStripBadStrip> getNewObject() override;
31  const bool printdebug_;
32  const bool isFlagAvailable_;
34 };
35 
36 using namespace std;
39  printdebug_(iConfig.getParameter<bool>("printDebug")),
40  isFlagAvailable_(iConfig.getParameter<bool>("isFlagAvailable")),
41  fp_(iConfig.getParameter<edm::FileInPath>("file")) {}
42 
45  ConditionDBWriter::fillPSetDescription(desc); // inherited from mother class
46  desc.setComment("Conditions Builder for SiStripBadStrip Objects from input ASCII file");
47  desc.add<edm::FileInPath>("file", edm::FileInPath("CondTools/SiStrip/data/DefectsFromConstructionDB.dat"));
48  desc.add<bool>("printDebug", false)->setComment("prints debug level messages");
49  desc.add<bool>("isFlagAvailable", true)->setComment("is the flag available in the input file");
50  descriptions.addWithDefaultLabel(desc);
51 }
52 
53 std::unique_ptr<SiStripBadStrip> SiStripBadStripFromASCIIFile::getNewObject() {
54  auto SiStripBadStrip_ = std::make_unique<SiStripBadStrip>();
55 
56  // open file and fill DB
57  ifstream infile((fp_.fullPath()).c_str());
58  if (!infile) {
59  edm::LogError("SiStripBadStripFromASCIIFile") << "[SiStripBadStripFromASCIIFile::GetNewObject]"
60  << " Problem while trying to open File: " << (fp_.fullPath()).c_str();
61  }
62 
63  //variables needed for reading file and filling of SiStripBadStripObject
64  uint32_t detid;
65  short flag;
66  short channel;
67 
68  bool firstrun = true;
69  short tempchannel = 0;
70  int count = 0;
71  std::vector<unsigned int> theSiStripVector;
72  short tempflag = 0;
73  uint32_t tempdetid = 0;
74 
75  while (!infile.eof()) {
76  // get data from file:
77  if (isFlagAvailable_) {
78  infile >> detid >> channel >> flag;
79  } else {
80  //if no flag is available, use the following:
81  infile >> detid >> channel;
82  flag = 1;
83  }
84 
85  unsigned int theBadStripRange = 0;
86 
87  // first loop ?
88  if (firstrun) {
89  tempdetid = detid;
90  tempchannel = channel;
91  tempflag = flag;
92  count = 0;
93  firstrun = false;
94  }
95 
96  if (detid == tempdetid) {
97  if (channel != tempchannel + count || flag != tempflag) {
98  // 1.badstrip, nconsectrips, flag
99  theBadStripRange = SiStripBadStrip_->encode(
100  tempchannel - 1,
101  count,
102  tempflag); // In the quality object, strips are counted from 0 to 767!!! Therefore "tempchannel-1"!
103  // In the input txt-file, they have to be from 1 to 768 instead!!!
104 
105  if (printdebug_) {
106  edm::LogInfo("SiStripBadStripFromASCIIFile")
107  << "detid " << tempdetid << " \t"
108  << " firstBadStrip " << tempchannel << "\t "
109  << " NconsecutiveBadStrips " << count << "\t "
110  << "flag " << tempflag << "\t"
111  << " packed integer " << std::hex << theBadStripRange << std::dec;
112  }
113 
114  theSiStripVector.push_back(theBadStripRange);
115 
116  if (infile.eof()) { // Don't forget to save the last strip before eof!!!
117  SiStripBadStrip::Range range(theSiStripVector.begin(), theSiStripVector.end());
118  if (!SiStripBadStrip_->put(tempdetid, range))
119  edm::LogError("SiStripBadStripFromASCIIFile")
120  << "[SiStripBadStripFromASCIIFile::GetNewObject] detid already exists";
121  theSiStripVector.clear();
122  }
123 
124  count = 1;
125  tempchannel = channel;
126  tempflag = flag;
127 
128  } else {
129  count++;
130  }
131  }
132 
133  if (detid != tempdetid) {
134  // 1.badstrip, nconsectrips, flag
135  theBadStripRange = SiStripBadStrip_->encode(
136  tempchannel - 1,
137  count,
138  tempflag); // In the quality object, strips are counted from 0 to 767!!! Therefore "tempchannel-1"!
139  // In the input txt-file, they have to be from 1 to 768 instead!!!
140  if (printdebug_) {
141  edm::LogInfo("SiStripBadStripFromASCIIFile") << "detid " << tempdetid << " \t"
142  << " firstBadStrip " << tempchannel << "\t "
143  << " NconsecutiveBadStrips " << count << "\t "
144  << "flag " << tempflag << "\t"
145  << " packed integer " << std::hex << theBadStripRange << std::dec;
146  }
147 
148  theSiStripVector.push_back(theBadStripRange);
149 
150  // populate db object
151  SiStripBadStrip::Range range(theSiStripVector.begin(), theSiStripVector.end());
152  if (!SiStripBadStrip_->put(tempdetid, range))
153  edm::LogError("SiStripBadStripFromASCIIFile")
154  << "[SiStripBadStripFromASCIIFile::GetNewObject] detid already exists";
155  theSiStripVector.clear();
156 
157  count = 1;
158  tempdetid = detid;
159  tempchannel = channel;
160  tempflag = flag;
161  }
162  }
163 
164  return SiStripBadStrip_;
165 }
166 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
SiStripBadStripFromASCIIFile(const edm::ParameterSet &iConfig)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Log< level::Error, false > LogError
~SiStripBadStripFromASCIIFile() override=default
const uint16_t range(const Frame &aFrame)
static void fillPSetDescription(edm::ParameterSetDescription &desc)
void setComment(std::string const &value)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Log< level::Info, false > LogInfo
static void fillDescriptions(edm::ConfigurationDescriptions &)
std::pair< ContainerIterator, ContainerIterator > Range
std::string fullPath() const
Definition: FileInPath.cc:161
std::unique_ptr< SiStripBadStrip > getNewObject() override