Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-17 04:58:11

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