Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <iomanip>
0002 #include <fstream>
0003 #include <iostream>
0004 #include <cmath>
0005 #include <memory>
0006 #include "FWCore/ParameterSet/interface/FileInPath.h"
0007 
0008 #include "FWCore/Framework/interface/ESHandle.h"
0009 #include "FWCore/Framework/interface/EventSetup.h"
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0012 #include "FWCore/Framework/interface/ESWatcher.h"
0013 
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/MakerMacros.h"
0016 
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Framework/interface/ConsumesCollector.h"
0019 #include "FWCore/Utilities/interface/ESGetToken.h"
0020 
0021 #include "MagneticField/Engine/interface/MagneticField.h"
0022 #include "CondFormats/SiPixelObjects/interface/SiPixel2DTemplateDBObject.h"
0023 #include "CondFormats/DataRecord/interface/SiPixel2DTemplateDBObjectRcd.h"
0024 #include "CalibTracker/Records/interface/SiPixel2DTemplateDBObjectESProducerRcd.h"
0025 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0026 
0027 class SiPixel2DTemplateDBObjectReader : public edm::one::EDAnalyzer<> {
0028 public:
0029   explicit SiPixel2DTemplateDBObjectReader(const edm::ParameterSet&);
0030   ~SiPixel2DTemplateDBObjectReader() override;
0031 
0032 private:
0033   void beginJob() override;
0034   void analyze(const edm::Event&, const edm::EventSetup&) override;
0035   void endJob() override;
0036 
0037   edm::ESWatcher<SiPixel2DTemplateDBObjectESProducerRcd> SiPix2DTemplDBObjectWatcher_;
0038   edm::ESWatcher<SiPixel2DTemplateDBObjectRcd> SiPix2DTemplDBObjWatcher_;
0039 
0040   std::string the2DTemplateCalibrationLocation;
0041   bool theDetailed2DTemplateDBErrorOutput;
0042   bool theFull2DTemplateDBOutput;
0043   bool testGlobalTag;
0044   bool hasTriggeredWatcher;
0045   edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magneticFieldToken_;
0046   edm::ESGetToken<SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectESProducerRcd> the2DTemplateESProdToken_;
0047   edm::ESGetToken<SiPixel2DTemplateDBObject, SiPixel2DTemplateDBObjectRcd> the2DTemplateToken_;
0048 };
0049 
0050 SiPixel2DTemplateDBObjectReader::SiPixel2DTemplateDBObjectReader(const edm::ParameterSet& iConfig)
0051     : the2DTemplateCalibrationLocation(iConfig.getParameter<std::string>("siPixel2DTemplateCalibrationLocation")),
0052       theDetailed2DTemplateDBErrorOutput(iConfig.getParameter<bool>("wantDetailed2DTemplateDBErrorOutput")),
0053       theFull2DTemplateDBOutput(iConfig.getParameter<bool>("wantFull2DTemplateDBOutput")),
0054       testGlobalTag(iConfig.getParameter<bool>("TestGlobalTag")),
0055       hasTriggeredWatcher(false),
0056       magneticFieldToken_(esConsumes()),
0057       the2DTemplateESProdToken_(esConsumes()),
0058       the2DTemplateToken_(esConsumes()) {}
0059 
0060 SiPixel2DTemplateDBObjectReader::~SiPixel2DTemplateDBObjectReader() = default;
0061 
0062 void SiPixel2DTemplateDBObjectReader::beginJob() {}
0063 
0064 void SiPixel2DTemplateDBObjectReader::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0065   //To test with the ESProducer
0066   SiPixel2DTemplateDBObject dbobject;
0067   if (testGlobalTag) {
0068     // Get magnetic field
0069     GlobalPoint center(0.0, 0.0, 0.0);
0070     edm::ESHandle<MagneticField> magfield = iSetup.getHandle(magneticFieldToken_);
0071     float theMagField = magfield.product()->inTesla(center).mag();
0072 
0073     edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nTesting global tag at magnetic field = " << theMagField;
0074     if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
0075       edm::LogPrint("SiPixel2DTemplateDBObjectESProducerRcd") << "With record SiPixel2DTemplateDBObjectESProducerRcd";
0076       dbobject = *&iSetup.getData(the2DTemplateESProdToken_);
0077       hasTriggeredWatcher = true;
0078     }
0079   } else {
0080     edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nLoading from file " << std::endl;
0081     if (SiPix2DTemplDBObjWatcher_.check(iSetup)) {
0082       edm::LogPrint("SiPixelTemplateDBObjectReader") << "With record SiPixel2DTemplateDBObjectRcd";
0083       dbobject = *&iSetup.getData(the2DTemplateToken_);
0084       hasTriggeredWatcher = true;
0085     }
0086   }
0087 
0088   if (hasTriggeredWatcher) {
0089     std::vector<short> tempMapId;
0090 
0091     if (theFull2DTemplateDBOutput) {
0092       edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "Map info" << std::endl;
0093       std::map<unsigned int, short> templMap = dbobject.getTemplateIDs();
0094       for (std::map<unsigned int, short>::const_iterator it = templMap.begin(); it != templMap.end(); ++it) {
0095         if (tempMapId.empty())
0096           tempMapId.push_back(it->second);
0097         for (unsigned int i = 0; i < tempMapId.size(); ++i) {
0098           if (tempMapId[i] == it->second)
0099             continue;
0100           else if (i == tempMapId.size() - 1) {
0101             tempMapId.push_back(it->second);
0102             break;
0103           }
0104         }
0105         edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0106             << "DetId: " << it->first << " 2DTemplateID: " << it->second << "\n";
0107       }
0108     }
0109 
0110     edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\nMap stores 2DTemplate Id(s): ";
0111     for (unsigned int vindex = 0; vindex < tempMapId.size(); ++vindex)
0112       edm::LogPrint("SiPixel2DTemplateDBObjectReader") << tempMapId[vindex] << " ";
0113     edm::LogPrint("SiPixel2DTemplateDBObjectReader") << std::endl;
0114 
0115     //local variables
0116     const char* tempfile;
0117     int numOfTempl = dbobject.numOfTempl();
0118     int index = 0;
0119     float tempnum = 0, diff = 0;
0120     float tol = 1.0E-23;
0121     bool error = false, givenErrorMsg = false;
0122     ;
0123 
0124     edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0125         << "\nChecking 2DTemplate 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 2DTemplate ID and version are -- note that version is only valid for >=13
0132       edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0133           << "Calibration " << i + 1 << " of " << numOfTempl << ", with 2DTemplate ID " << dbobject.sVector()[index]
0134           << "\tand Version " << dbobject.sVector()[index + 1] << "\t--------  ";
0135 
0136       //Opening the text-based 2DTemplate calibration
0137       std::ostringstream tout;
0138       //tout << the2DTemplateCalibrationLocation.c_str() << "/data/generror_summary_zp"
0139       tout << the2DTemplateCalibrationLocation.c_str() << "/data/template2D_IOV5/template_summary2D_zp" << std::setw(4)
0140            << std::setfill('0') << std::right << dbobject.sVector()[index] << ".out" << std::ends;
0141 
0142       edm::FileInPath file(tout.str());
0143       tempfile = (file.fullPath()).c_str();
0144       std::ifstream in_file(tempfile, 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("SiPixel2DTemplateDBObjectReader") << "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 (theDetailed2DTemplateDBErrorOutput) {
0166               edm::LogPrint("SiPixel2DTemplateDBObjectReader")
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("SiPixel2DTemplateDBObjectReader") << "MATCHES\n";
0178       }  //end current file
0179       in_file.close();
0180       givenErrorMsg = false;
0181     }  //end loop over all files
0182 
0183     if (error && !theDetailed2DTemplateDBErrorOutput)
0184       edm::LogPrint("SiPixel2DTemplateDBObjectReader")
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 (theFull2DTemplateDBOutput)
0193       edm::LogPrint("SiPixel2DTemplateDBObjectReader") << dbobject << std::endl;
0194   }
0195 }
0196 
0197 void SiPixel2DTemplateDBObjectReader::endJob() {}
0198 
0199 std::ostream& operator<<(std::ostream& s, const SiPixel2DTemplateDBObject& dbobject) {
0200   //!-index to keep track of where we are in the object
0201   int index = 0;
0202   //!-these are modifiable parameters for the extended 2DTemplates
0203   int txsize[4] = {7, 13, 0, 0};
0204   int tysize[4] = {21, 21, 0, 0};
0205   //!-entries takes the number of entries in By,Bx,Fy,Fx from the object
0206   int entries[4] = {0};
0207   //!-local indicies for loops
0208   int i, j, k, l, m, n, entry_it;
0209   //!-changes the size of the 2DTemplates based on the version
0210   int sizeSetter = 0, generrorVersion = 0;
0211 
0212   edm::LogPrint("SiPixel2DTemplateDBObjectReader") << "\n\nDBobject version: " << dbobject.version() << std::endl;
0213 
0214   for (m = 0; m < dbobject.numOfTempl(); ++m) {
0215     //To change the size of the output based on which 2DTemplate version we are using"
0216     generrorVersion = (int)dbobject.sVector_[index + 21];
0217     if (generrorVersion <= 10) {
0218       edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0219           << "*****WARNING***** This code will not format this 2DTemplate version properly *****WARNING*****\n";
0220       sizeSetter = 0;
0221     } else if (generrorVersion <= 16)
0222       sizeSetter = 1;
0223     else
0224       edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0225           << "*****WARNING***** This code has not been tested at formatting this version *****WARNING*****\n";
0226 
0227     edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0228         << "\n\n*********************************************************************************************"
0229         << std::endl;
0230     edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0231         << "***************                  Reading 2DTemplate ID " << dbobject.sVector_[index + 20] << "\t(" << m + 1
0232         << "/" << dbobject.numOfTempl_ << ")                 ***************" << std::endl;
0233     edm::LogPrint("SiPixel2DTemplateDBObjectReader")
0234         << "*********************************************************************************************\n\n"
0235         << std::endl;
0236 
0237     //Header Title
0238     SiPixel2DTemplateDBObject::char2float temp;
0239     for (n = 0; n < 20; ++n) {
0240       temp.f = dbobject.sVector_[index];
0241       s << temp.c[0] << temp.c[1] << temp.c[2] << temp.c[3];
0242       ++index;
0243     }
0244 
0245     entries[0] = (int)dbobject.sVector_[index + 3];                                   // Y
0246     entries[1] = (int)(dbobject.sVector_[index + 4] * dbobject.sVector_[index + 5]);  // X
0247 
0248     //Header
0249     s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
0250       << "\t" << dbobject.sVector_[index + 3] << "\t" << dbobject.sVector_[index + 4] << "\t"
0251       << dbobject.sVector_[index + 5] << "\t" << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7]
0252       << "\t" << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
0253       << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\t" << dbobject.sVector_[index + 12]
0254       << "\t" << dbobject.sVector_[index + 13] << "\t" << dbobject.sVector_[index + 14] << "\t"
0255       << dbobject.sVector_[index + 15] << "\t" << dbobject.sVector_[index + 16] << std::endl;
0256     index += 17;
0257 
0258     //Loop over By,Bx,Fy,Fx
0259     for (entry_it = 0; entry_it < 4; ++entry_it) {
0260       //Run,costrk,qavg,...,clslenx
0261       for (i = 0; i < entries[entry_it]; ++i) {
0262         s << dbobject.sVector_[index] << "\t" << dbobject.sVector_[index + 1] << "\t" << dbobject.sVector_[index + 2]
0263           << "\t" << dbobject.sVector_[index + 3] << "\n"
0264           << dbobject.sVector_[index + 4] << "\t" << dbobject.sVector_[index + 5] << "\t"
0265           << dbobject.sVector_[index + 6] << "\t" << dbobject.sVector_[index + 7] << "\t"
0266           << dbobject.sVector_[index + 8] << "\t" << dbobject.sVector_[index + 9] << "\t"
0267           << dbobject.sVector_[index + 10] << "\t" << dbobject.sVector_[index + 11] << "\n"
0268           << dbobject.sVector_[index + 12] << "\t" << dbobject.sVector_[index + 13] << "\t"
0269           << dbobject.sVector_[index + 14] << "\t" << dbobject.sVector_[index + 15] << "\t"
0270           << dbobject.sVector_[index + 16] << "\t" << dbobject.sVector_[index + 17] << "\t"
0271           << dbobject.sVector_[index + 18] << std::endl;
0272         index += 19;
0273         //YPar
0274         for (j = 0; j < 2; ++j) {
0275           for (k = 0; k < 5; ++k) {
0276             s << dbobject.sVector_[index] << "\t";
0277             ++index;
0278           }
0279           s << std::endl;
0280         }
0281         //YTemp
0282         for (j = 0; j < 9; ++j) {
0283           for (k = 0; k < tysize[sizeSetter]; ++k) {
0284             s << dbobject.sVector_[index] << "\t";
0285             ++index;
0286           }
0287           s << std::endl;
0288         }
0289         //XPar
0290         for (j = 0; j < 2; ++j) {
0291           for (k = 0; k < 5; ++k) {
0292             s << dbobject.sVector_[index] << "\t";
0293             ++index;
0294           }
0295           s << std::endl;
0296         }
0297         //XTemp
0298         for (j = 0; j < 9; ++j) {
0299           for (k = 0; k < txsize[sizeSetter]; ++k) {
0300             s << dbobject.sVector_[index] << "\t";
0301             ++index;
0302           }
0303           s << std::endl;
0304         }
0305         //Y average reco params
0306         for (j = 0; j < 4; ++j) {
0307           for (k = 0; k < 4; ++k) {
0308             s << dbobject.sVector_[index] << "\t";
0309             ++index;
0310           }
0311           s << std::endl;
0312         }
0313         //Yflpar
0314         for (j = 0; j < 4; ++j) {
0315           for (k = 0; k < 6; ++k) {
0316             s << dbobject.sVector_[index] << "\t";
0317             ++index;
0318           }
0319           s << std::endl;
0320         }
0321         //X average reco params
0322         for (j = 0; j < 4; ++j) {
0323           for (k = 0; k < 4; ++k) {
0324             s << dbobject.sVector_[index] << "\t";
0325             ++index;
0326           }
0327           s << std::endl;
0328         }
0329         //Xflpar
0330         for (j = 0; j < 4; ++j) {
0331           for (k = 0; k < 6; ++k) {
0332             s << dbobject.sVector_[index] << "\t";
0333             ++index;
0334           }
0335           s << std::endl;
0336         }
0337         //Chi2X,Y
0338         for (j = 0; j < 4; ++j) {
0339           for (k = 0; k < 2; ++k) {
0340             for (l = 0; l < 2; ++l) {
0341               s << dbobject.sVector_[index] << "\t";
0342               ++index;
0343             }
0344           }
0345           s << std::endl;
0346         }
0347         //Y average Chi2 params
0348         for (j = 0; j < 4; ++j) {
0349           for (k = 0; k < 4; ++k) {
0350             s << dbobject.sVector_[index] << "\t";
0351             ++index;
0352           }
0353           s << std::endl;
0354         }
0355         //X average Chi2 params
0356         for (j = 0; j < 4; ++j) {
0357           for (k = 0; k < 4; ++k) {
0358             s << dbobject.sVector_[index] << "\t";
0359             ++index;
0360           }
0361           s << std::endl;
0362         }
0363         //Y average reco params for CPE Generic
0364         for (j = 0; j < 4; ++j) {
0365           for (k = 0; k < 4; ++k) {
0366             s << dbobject.sVector_[index] << "\t";
0367             ++index;
0368           }
0369           s << std::endl;
0370         }
0371         //X average reco params for CPE Generic
0372         for (j = 0; j < 4; ++j) {
0373           for (k = 0; k < 4; ++k) {
0374             s << dbobject.sVector_[index] << "\t";
0375             ++index;
0376           }
0377           s << std::endl;
0378         }
0379         //SpareX,Y
0380         for (j = 0; j < 20; ++j) {
0381           s << dbobject.sVector_[index] << "\t";
0382           ++index;
0383           if (j == 9 || j == 19)
0384             s << std::endl;
0385         }
0386       }
0387     }
0388   }
0389   return s;
0390 }
0391 
0392 DEFINE_FWK_MODULE(SiPixel2DTemplateDBObjectReader);