Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-05 02:38:59

0001 // system include files
0002 #include <memory>
0003 #include <cstdio>
0004 #include <string>
0005 
0006 // user include files
0007 #include "CommonTools/ConditionDBWriter/interface/ConditionDBWriter.h"
0008 #include "CondFormats/SiStripObjects/interface/SiStripBadStrip.h"
0009 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/FileInPath.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0015 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
0016 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
0017 #include "Geometry/CommonTopologies/interface/StripTopology.h"
0018 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0019 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetType.h"
0020 #include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetUnit.h"
0021 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0022 
0023 class SiStripBadStripFromASCIIFile : public ConditionDBWriter<SiStripBadStrip> {
0024 public:
0025   explicit SiStripBadStripFromASCIIFile(const edm::ParameterSet& iConfig);
0026   ~SiStripBadStripFromASCIIFile() override = default;
0027   static void fillDescriptions(edm::ConfigurationDescriptions&);
0028 
0029 private:
0030   std::unique_ptr<SiStripBadStrip> getNewObject() override;
0031   const bool printdebug_;
0032   const bool isFlagAvailable_;
0033   const edm::FileInPath fp_;
0034 };
0035 
0036 using namespace std;
0037 SiStripBadStripFromASCIIFile::SiStripBadStripFromASCIIFile(const edm::ParameterSet& iConfig)
0038     : ConditionDBWriter<SiStripBadStrip>(iConfig),
0039       printdebug_(iConfig.getParameter<bool>("printDebug")),
0040       isFlagAvailable_(iConfig.getParameter<bool>("isFlagAvailable")),
0041       fp_(iConfig.getParameter<edm::FileInPath>("file")) {}
0042 
0043 void SiStripBadStripFromASCIIFile::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0044   edm::ParameterSetDescription desc;
0045   ConditionDBWriter::fillPSetDescription(desc);  // inherited from mother class
0046   desc.setComment("Conditions Builder for SiStripBadStrip Objects from input ASCII file");
0047   desc.add<edm::FileInPath>("file", edm::FileInPath("CondTools/SiStrip/data/DefectsFromConstructionDB.dat"));
0048   desc.add<bool>("printDebug", false)->setComment("prints debug level messages");
0049   desc.add<bool>("isFlagAvailable", true)->setComment("is the flag available in the input file");
0050   descriptions.addWithDefaultLabel(desc);
0051 }
0052 
0053 std::unique_ptr<SiStripBadStrip> SiStripBadStripFromASCIIFile::getNewObject() {
0054   auto SiStripBadStrip_ = std::make_unique<SiStripBadStrip>();
0055 
0056   // open file and fill DB
0057   ifstream infile((fp_.fullPath()).c_str());
0058   if (!infile) {
0059     edm::LogError("SiStripBadStripFromASCIIFile") << "[SiStripBadStripFromASCIIFile::GetNewObject]"
0060                                                   << " Problem while trying to open File: " << (fp_.fullPath()).c_str();
0061   }
0062 
0063   //variables needed for reading file and filling of SiStripBadStripObject
0064   uint32_t detid;
0065   short flag;
0066   short channel;
0067 
0068   bool firstrun = true;
0069   short tempchannel = 0;
0070   int count = 0;
0071   std::vector<unsigned int> theSiStripVector;
0072   short tempflag = 0;
0073   uint32_t tempdetid = 0;
0074 
0075   while (!infile.eof()) {
0076     // get data from file:
0077     if (isFlagAvailable_) {
0078       infile >> detid >> channel >> flag;
0079     } else {
0080       //if no flag is available, use the following:
0081       infile >> detid >> channel;
0082       flag = 1;
0083     }
0084 
0085     unsigned int theBadStripRange = 0;
0086 
0087     // first loop ?
0088     if (firstrun) {
0089       tempdetid = detid;
0090       tempchannel = channel;
0091       tempflag = flag;
0092       count = 0;
0093       firstrun = false;
0094     }
0095 
0096     if (detid == tempdetid) {
0097       if (channel != tempchannel + count || flag != tempflag) {
0098         // 1.badstrip, nconsectrips, flag
0099         theBadStripRange = SiStripBadStrip_->encode(
0100             tempchannel - 1,
0101             count,
0102             tempflag);  // In the quality object, strips are counted from 0 to 767!!! Therefore "tempchannel-1"!
0103                         // In the input txt-file, they have to be from 1 to 768 instead!!!
0104 
0105         if (printdebug_) {
0106           edm::LogInfo("SiStripBadStripFromASCIIFile")
0107               << "detid " << tempdetid << " \t"
0108               << " firstBadStrip " << tempchannel << "\t "
0109               << " NconsecutiveBadStrips " << count << "\t "
0110               << "flag " << tempflag << "\t"
0111               << " packed integer " << std::hex << theBadStripRange << std::dec;
0112         }
0113 
0114         theSiStripVector.push_back(theBadStripRange);
0115 
0116         if (infile.eof()) {  // Don't forget to save the last strip before eof!!!
0117           SiStripBadStrip::Range range(theSiStripVector.begin(), theSiStripVector.end());
0118           if (!SiStripBadStrip_->put(tempdetid, range))
0119             edm::LogError("SiStripBadStripFromASCIIFile")
0120                 << "[SiStripBadStripFromASCIIFile::GetNewObject] detid already exists";
0121           theSiStripVector.clear();
0122         }
0123 
0124         count = 1;
0125         tempchannel = channel;
0126         tempflag = flag;
0127 
0128       } else {
0129         count++;
0130       }
0131     }
0132 
0133     if (detid != tempdetid) {
0134       // 1.badstrip, nconsectrips, flag
0135       theBadStripRange = SiStripBadStrip_->encode(
0136           tempchannel - 1,
0137           count,
0138           tempflag);  // In the quality object, strips are counted from 0 to 767!!! Therefore "tempchannel-1"!
0139                       // In the input txt-file, they have to be from 1 to 768 instead!!!
0140       if (printdebug_) {
0141         edm::LogInfo("SiStripBadStripFromASCIIFile") << "detid " << tempdetid << " \t"
0142                                                      << " firstBadStrip " << tempchannel << "\t "
0143                                                      << " NconsecutiveBadStrips " << count << "\t "
0144                                                      << "flag " << tempflag << "\t"
0145                                                      << " packed integer " << std::hex << theBadStripRange << std::dec;
0146       }
0147 
0148       theSiStripVector.push_back(theBadStripRange);
0149 
0150       // populate db  object
0151       SiStripBadStrip::Range range(theSiStripVector.begin(), theSiStripVector.end());
0152       if (!SiStripBadStrip_->put(tempdetid, range))
0153         edm::LogError("SiStripBadStripFromASCIIFile")
0154             << "[SiStripBadStripFromASCIIFile::GetNewObject] detid already exists";
0155       theSiStripVector.clear();
0156 
0157       count = 1;
0158       tempdetid = detid;
0159       tempchannel = channel;
0160       tempflag = flag;
0161     }
0162   }
0163 
0164   return SiStripBadStrip_;
0165 }
0166 
0167 #include "FWCore/Framework/interface/MakerMacros.h"
0168 DEFINE_FWK_MODULE(SiStripBadStripFromASCIIFile);