Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:25

0001 #include <iomanip>
0002 #include <fstream>
0003 #include <iostream>
0004 #include <cmath>
0005 #include <memory>
0006 
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 #include "FWCore/Framework/interface/ESWatcher.h"
0010 
0011 #include "FWCore/Framework/interface/Event.h"
0012 #include "FWCore/Framework/interface/MakerMacros.h"
0013 
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/Framework/interface/ConsumesCollector.h"
0016 #include "FWCore/Utilities/interface/ESGetToken.h"
0017 
0018 #include "CondFormats/SiPixelObjects/interface/SiPixelTemplateDBObject.h"
0019 #include "CondFormats/DataRecord/interface/SiPixelTemplateDBObjectRcd.h"
0020 #include "CalibTracker/Records/interface/SiPixelTemplateDBObjectESProducerRcd.h"
0021 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0022 #include "MagneticField/Engine/interface/MagneticField.h"
0023 #include "FWCore/ParameterSet/interface/FileInPath.h"
0024 
0025 #include "FWCore/Framework/interface/ESHandle.h"
0026 #include "FWCore/Framework/interface/EventSetup.h"
0027 
0028 class SiPixelTemplateDBObjectReader : public edm::one::EDAnalyzer<> {
0029 public:
0030   explicit SiPixelTemplateDBObjectReader(const edm::ParameterSet&);
0031   ~SiPixelTemplateDBObjectReader() override;
0032 
0033 private:
0034   void beginJob() override;
0035   void analyze(const edm::Event&, const edm::EventSetup&) override;
0036   void endJob() override;
0037 
0038   edm::ESWatcher<SiPixelTemplateDBObjectESProducerRcd> SiPixTemplDBObjectWatcher_;
0039   edm::ESWatcher<SiPixelTemplateDBObjectRcd> SiPixTemplDBObjWatcher_;
0040 
0041   std::string theTemplateCalibrationLocation;
0042   bool theDetailedTemplateDBErrorOutput;
0043   bool theFullTemplateDBOutput;
0044   bool testGlobalTag;
0045   bool hasTriggeredWatcher;
0046   edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magneticFieldToken_;
0047   edm::ESGetToken<SiPixelTemplateDBObject, SiPixelTemplateDBObjectESProducerRcd> the1DTemplateESProdToken_;
0048   edm::ESGetToken<SiPixelTemplateDBObject, SiPixelTemplateDBObjectRcd> the1DTemplateToken_;
0049 };
0050 
0051 SiPixelTemplateDBObjectReader::SiPixelTemplateDBObjectReader(const edm::ParameterSet& iConfig)
0052     : theTemplateCalibrationLocation(iConfig.getParameter<std::string>("siPixelTemplateCalibrationLocation")),
0053       theDetailedTemplateDBErrorOutput(iConfig.getParameter<bool>("wantDetailedTemplateDBErrorOutput")),
0054       theFullTemplateDBOutput(iConfig.getParameter<bool>("wantFullTemplateDBOutput")),
0055       testGlobalTag(iConfig.getParameter<bool>("TestGlobalTag")),
0056       hasTriggeredWatcher(false),
0057       magneticFieldToken_(esConsumes()),
0058       the1DTemplateESProdToken_(esConsumes()),
0059       the1DTemplateToken_(esConsumes()) {}
0060 
0061 SiPixelTemplateDBObjectReader::~SiPixelTemplateDBObjectReader() = default;
0062 
0063 void SiPixelTemplateDBObjectReader::beginJob() {}
0064 
0065 void SiPixelTemplateDBObjectReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0066   //To test with the ESProducer
0067   SiPixelTemplateDBObject dbobject;
0068   if (testGlobalTag) {
0069     // Get magnetic field
0070     GlobalPoint center(0.0, 0.0, 0.0);
0071     edm::ESHandle<MagneticField> magfield = iSetup.getHandle(magneticFieldToken_);
0072     float theMagField = magfield.product()->inTesla(center).mag();
0073 
0074     edm::LogPrint("SiPixelTemplateDBObjectReader") << "\nTesting global tag at magfield = " << theMagField;
0075     if (SiPixTemplDBObjWatcher_.check(iSetup)) {
0076       edm::LogPrint("SiPixelTemplateDBObjectReader") << "With record SiPixelTemplateDBObjectESProducerRcd";
0077       dbobject = *&iSetup.getData(the1DTemplateESProdToken_);
0078       hasTriggeredWatcher = true;
0079     }
0080   } else {
0081     edm::LogPrint("SiPixelTemplateDBObjectReader") << "\nLoading from file " << std::endl;
0082     if (SiPixTemplDBObjWatcher_.check(iSetup)) {
0083       edm::LogPrint("SiPixelTemplateDBObjectReader") << "With record SiPixelTemplateDBObjectRcd";
0084       dbobject = *&iSetup.getData(the1DTemplateToken_);
0085       hasTriggeredWatcher = true;
0086     }
0087   }
0088 
0089   if (hasTriggeredWatcher) {
0090     std::vector<short> tempMapId;
0091 
0092     if (theFullTemplateDBOutput)
0093       edm::LogPrint("SiPixelTemplateDBObjectReader") << "Map info" << std::endl;
0094     std::map<unsigned int, short> templMap = dbobject.getTemplateIDs();
0095     for (std::map<unsigned int, short>::const_iterator it = templMap.begin(); it != templMap.end(); ++it) {
0096       if (tempMapId.empty())
0097         tempMapId.push_back(it->second);
0098       for (unsigned int i = 0; i < tempMapId.size(); ++i) {
0099         if (tempMapId[i] == it->second)
0100           continue;
0101         else if (i == tempMapId.size() - 1) {
0102           tempMapId.push_back(it->second);
0103           break;
0104         }
0105       }
0106       if (theFullTemplateDBOutput)
0107         edm::LogPrint("SiPixelTemplateDBObjectReader")
0108             << "DetId: " << it->first << " TemplateID: " << it->second << "\n";
0109     }
0110 
0111     edm::LogPrint("SiPixelTemplateDBObjectReader") << "\nMap stores template Id(s): ";
0112     for (unsigned int vindex = 0; vindex < tempMapId.size(); ++vindex)
0113       edm::LogPrint("SiPixelTemplateDBObjectReader") << tempMapId[vindex] << " ";
0114     edm::LogPrint("SiPixelTemplateDBObjectReader") << std::endl;
0115 
0116     //local variables
0117     const char* tempfile;
0118     int numOfTempl = dbobject.numOfTempl();
0119     int index = 0;
0120     float tempnum = 0, diff = 0;
0121     float tol = 1.0E-23;
0122     bool error = false, givenErrorMsg = false;
0123 
0124     edm::LogPrint("SiPixelTemplateDBObjectReader")
0125         << "\nChecking Template DB object version " << dbobject.version() << " containing " << numOfTempl
0126         << " calibration(s) at " << dbobject.sVector()[index + 22] << "T\n";
0127     for (int i = 0; i < numOfTempl; ++i) {
0128       //Removes header in db object from diff
0129       index += 20;
0130 
0131       //Tell the person viewing the output what the template ID and version are -- note that version is only valid for >=13
0132       edm::LogPrint("SiPixelTemplateDBObjectReader")
0133           << "Calibration " << i + 1 << " of " << numOfTempl << ", with Template ID " << dbobject.sVector()[index]
0134           << "\tand Version " << dbobject.sVector()[index + 1] << "\t--------  ";
0135 
0136       //Opening the text-based template calibration
0137       std::ostringstream tout;
0138       tout << theTemplateCalibrationLocation.c_str() << "/data/template_summary_zp" << std::setw(4) << std::setfill('0')
0139            << std::right << dbobject.sVector()[index] << ".out" << std::ends;
0140 
0141       edm::FileInPath file(tout.str());
0142       tempfile = (file.fullPath()).c_str();
0143       std::ifstream in_file(tempfile, std::ios::in);
0144 
0145       if (in_file.is_open()) {
0146         //Removes header in textfile from diff
0147         //First read in from the text file -- this will be compared with index = 20
0148         in_file >> tempnum;
0149 
0150         //Read until the end of the current text file
0151         while (!in_file.eof()) {
0152           //Calculate the difference between the text file and the db object
0153           diff = std::abs(tempnum - dbobject.sVector()[index]);
0154 
0155           //Is there a difference?
0156           if (diff > tol) {
0157             //We have the if statement to output the message only once
0158             if (!givenErrorMsg)
0159               edm::LogPrint("SiPixelTemplateDBObjectReader") << "does NOT match\n";
0160             //If there is an error we want to display a message upon completion
0161             error = true;
0162             givenErrorMsg = true;
0163             //Do we want more detailed output?
0164             if (theDetailedTemplateDBErrorOutput) {
0165               edm::LogPrint("SiPixelTemplateDBObjectReader")
0166                   << "from file = " << tempnum << "\t from dbobject = " << dbobject.sVector()[index]
0167                   << "\tdiff = " << diff << "\t db index = " << index << std::endl;
0168             }
0169           }
0170           //Go to the next entries
0171           in_file >> tempnum;
0172           ++index;
0173         }
0174         //There were no errors, the two files match.
0175         if (!givenErrorMsg)
0176           edm::LogPrint("SiPixelTemplateDBObjectReader") << "MATCHES\n";
0177       }  //end current file
0178       in_file.close();
0179       givenErrorMsg = false;
0180     }  //end loop over all files
0181 
0182     if (error && !theDetailedTemplateDBErrorOutput)
0183       edm::LogPrint("SiPixelTemplateDBObjectReader")
0184           << "\nThe were differences found between the files and the database.\n"
0185           << "If you would like more detailed information please set\n"
0186           << "wantDetailedOutput = True in the cfg file. If you would like a\n"
0187           << "full output of the contents of the database file please set\n"
0188           << "wantFullOutput = True. Make sure that you pipe the output to a\n"
0189           << "log file. This could take a few minutes.\n\n";
0190 
0191     if (theFullTemplateDBOutput)
0192       edm::LogPrint("SiPixelTemplateDBObjectReader") << dbobject << std::endl;
0193   }
0194 }
0195 
0196 void SiPixelTemplateDBObjectReader::endJob() {}
0197 
0198 std::ostream& operator<<(std::ostream& s, const SiPixelTemplateDBObject& dbobject) {
0199   //!-index to keep track of where we are in the object
0200   int index = 0;
0201   //!-these are modifiable parameters for the extended templates
0202   int txsize[4] = {7, 13, 0, 0};
0203   int tysize[4] = {21, 21, 0, 0};
0204   //!-entries takes the number of entries in By,Bx,Fy,Fx from the object
0205   int entries[4] = {0};
0206   //!-local indicies for loops
0207   int i, j, k, l, m, n, entry_it;
0208   //!-changes the size of the templates based on the version
0209   int sizeSetter = 0, templateVersion = 0;
0210 
0211   edm::LogPrint("SiPixelTemplateDBObjectReader") << "\n\nDBobject version: " << dbobject.version() << std::endl;
0212 
0213   for (m = 0; m < dbobject.numOfTempl(); ++m) {
0214     //To change the size of the output based on which template version we are using"
0215     templateVersion = (int)dbobject.sVector_[index + 21];
0216     if (templateVersion <= 10) {
0217       edm::LogPrint("SiPixelTemplateDBObjectReader")
0218           << "*****WARNING***** This code will not format this template version properly *****WARNING*****\n";
0219       sizeSetter = 0;
0220     } else if (templateVersion <= 16)
0221       sizeSetter = 1;
0222     else
0223       edm::LogPrint("SiPixelTemplateDBObjectReader")
0224           << "*****WARNING***** This code has not been tested at formatting this version *****WARNING*****\n";
0225 
0226     edm::LogPrint("SiPixelTemplateDBObjectReader")
0227         << "\n\n*********************************************************************************************"
0228         << std::endl;
0229     edm::LogPrint("SiPixelTemplateDBObjectReader")
0230         << "***************                  Reading Template ID " << dbobject.sVector_[index + 20] << "\t(" << m + 1
0231         << "/" << dbobject.numOfTempl_ << ")                 ***************" << std::endl;
0232     edm::LogPrint("SiPixelTemplateDBObjectReader")
0233         << "*********************************************************************************************\n\n"
0234         << std::endl;
0235 
0236     //Header Title
0237     SiPixelTemplateDBObject::char2float temp;
0238     for (n = 0; n < 20; ++n) {
0239       temp.f = dbobject.sVector_[index];
0240       s << temp.c[0] << temp.c[1] << temp.c[2] << temp.c[3];
0241       ++index;
0242     }
0243 
0244     entries[0] = (int)dbobject.sVector_[index + 3];                                   // Y
0245     entries[1] = (int)(dbobject.sVector_[index + 4] * dbobject.sVector_[index + 5]);  // X
0246 
0247     //Header
0248     s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
0249       << "\t" << dbobject.sVector_[index + 3] << "\t" << dbobject.sVector_[index + 4] << "\t"
0250       << dbobject.sVector_[index + 5] << "\t" << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7]
0251       << "\t" << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
0252       << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\t" << dbobject.sVector_[index + 12]
0253       << "\t" << dbobject.sVector_[index + 13] << "\t" << dbobject.sVector_[index + 14] << "\t"
0254       << dbobject.sVector_[index + 15] << "\t" << dbobject.sVector_[index + 16] << std::endl;
0255     index += 17;
0256 
0257     //Loop over By,Bx,Fy,Fx
0258     for (entry_it = 0; entry_it < 4; ++entry_it) {
0259       //Run,costrk,qavg,...,clslenx
0260       for (i = 0; i < entries[entry_it]; ++i) {
0261         s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
0262           << "\t" << dbobject.sVector_[index + 3] << "\n"
0263           << dbobject.sVector_[index + 4] << "\t" << dbobject.sVector_[index + 5] << "\t"
0264           << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7] << "\t"
0265           << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
0266           << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\n"
0267           << dbobject.sVector_[index + 12] << "\t" << dbobject.sVector_[index + 13] << "\t"
0268           << dbobject.sVector_[index + 14] << "\t" << dbobject.sVector_[index + 15] << "\t"
0269           << dbobject.sVector_[index + 16] << "\t" << dbobject.sVector_[index + 17] << "\t"
0270           << dbobject.sVector_[index + 18] << std::endl;
0271         index += 19;
0272         //YPar
0273         for (j = 0; j < 2; ++j) {
0274           for (k = 0; k < 5; ++k) {
0275             s << dbobject.sVector_[index] << "\t";
0276             ++index;
0277           }
0278           s << std::endl;
0279         }
0280         //YTemp
0281         for (j = 0; j < 9; ++j) {
0282           for (k = 0; k < tysize[sizeSetter]; ++k) {
0283             s << dbobject.sVector_[index] << "\t";
0284             ++index;
0285           }
0286           s << std::endl;
0287         }
0288         //XPar
0289         for (j = 0; j < 2; ++j) {
0290           for (k = 0; k < 5; ++k) {
0291             s << dbobject.sVector_[index] << "\t";
0292             ++index;
0293           }
0294           s << std::endl;
0295         }
0296         //XTemp
0297         for (j = 0; j < 9; ++j) {
0298           for (k = 0; k < txsize[sizeSetter]; ++k) {
0299             s << dbobject.sVector_[index] << "\t";
0300             ++index;
0301           }
0302           s << std::endl;
0303         }
0304         //Y average reco params
0305         for (j = 0; j < 4; ++j) {
0306           for (k = 0; k < 4; ++k) {
0307             s << dbobject.sVector_[index] << "\t";
0308             ++index;
0309           }
0310           s << std::endl;
0311         }
0312         //Yflpar
0313         for (j = 0; j < 4; ++j) {
0314           for (k = 0; k < 6; ++k) {
0315             s << dbobject.sVector_[index] << "\t";
0316             ++index;
0317           }
0318           s << std::endl;
0319         }
0320         //X average reco params
0321         for (j = 0; j < 4; ++j) {
0322           for (k = 0; k < 4; ++k) {
0323             s << dbobject.sVector_[index] << "\t";
0324             ++index;
0325           }
0326           s << std::endl;
0327         }
0328         //Xflpar
0329         for (j = 0; j < 4; ++j) {
0330           for (k = 0; k < 6; ++k) {
0331             s << dbobject.sVector_[index] << "\t";
0332             ++index;
0333           }
0334           s << std::endl;
0335         }
0336         //Chi2X,Y
0337         for (j = 0; j < 4; ++j) {
0338           for (k = 0; k < 2; ++k) {
0339             for (l = 0; l < 2; ++l) {
0340               s << dbobject.sVector_[index] << "\t";
0341               ++index;
0342             }
0343           }
0344           s << std::endl;
0345         }
0346         //Y average Chi2 params
0347         for (j = 0; j < 4; ++j) {
0348           for (k = 0; k < 4; ++k) {
0349             s << dbobject.sVector_[index] << "\t";
0350             ++index;
0351           }
0352           s << std::endl;
0353         }
0354         //X average Chi2 params
0355         for (j = 0; j < 4; ++j) {
0356           for (k = 0; k < 4; ++k) {
0357             s << dbobject.sVector_[index] << "\t";
0358             ++index;
0359           }
0360           s << std::endl;
0361         }
0362         //Y average reco params for CPE Generic
0363         for (j = 0; j < 4; ++j) {
0364           for (k = 0; k < 4; ++k) {
0365             s << dbobject.sVector_[index] << "\t";
0366             ++index;
0367           }
0368           s << std::endl;
0369         }
0370         //X average reco params for CPE Generic
0371         for (j = 0; j < 4; ++j) {
0372           for (k = 0; k < 4; ++k) {
0373             s << dbobject.sVector_[index] << "\t";
0374             ++index;
0375           }
0376           s << std::endl;
0377         }
0378         //SpareX,Y
0379         for (j = 0; j < 20; ++j) {
0380           s << dbobject.sVector_[index] << "\t";
0381           ++index;
0382           if (j == 9 || j == 19)
0383             s << std::endl;
0384         }
0385       }
0386     }
0387   }
0388   return s;
0389 }
0390 
0391 DEFINE_FWK_MODULE(SiPixelTemplateDBObjectReader);