File indexing completed on 2024-04-06 12:03:19
0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0003 #include "CondCore/CondDB/interface/ConnectionPool.h"
0004 #include "CondFormats/Common/interface/TimeConversions.h"
0005 #include "CondTools/RunInfo/interface/FillInfoPopConSourceHandler.h"
0006 #include "RelationalAccess/ISessionProxy.h"
0007 #include "RelationalAccess/ISchema.h"
0008 #include "RelationalAccess/IQuery.h"
0009 #include "RelationalAccess/ICursor.h"
0010 #include "CoralBase/AttributeList.h"
0011 #include "CoralBase/Attribute.h"
0012 #include "CoralBase/AttributeSpecification.h"
0013 #include "CoralBase/TimeStamp.h"
0014 #include <iostream>
0015 #include <memory>
0016 #include <sstream>
0017 #include <utility>
0018 #include <vector>
0019
0020 FillInfoPopConSourceHandler::FillInfoPopConSourceHandler(edm::ParameterSet const &pset)
0021 : m_debug(pset.getUntrackedParameter<bool>("debug", false)),
0022 m_firstFill((unsigned short)pset.getUntrackedParameter<unsigned int>("firstFill", 1)),
0023 m_lastFill((unsigned short)pset.getUntrackedParameter<unsigned int>("lastFill", m_firstFill)),
0024 m_name(pset.getUntrackedParameter<std::string>("name", "FillInfoPopConSourceHandler")),
0025 m_connectionString(pset.getUntrackedParameter<std::string>("connectionString", "")),
0026 m_dipSchema(pset.getUntrackedParameter<std::string>("DIPSchema", "")),
0027 m_authpath(pset.getUntrackedParameter<std::string>("authenticationPath", "")) {}
0028
0029 FillInfoPopConSourceHandler::~FillInfoPopConSourceHandler() {}
0030
0031 void FillInfoPopConSourceHandler::getNewObjects() {
0032
0033 Ref previousFill;
0034
0035
0036 if (tagInfo().size == 0) {
0037 edm::LogInfo(m_name) << "New tag " << tagInfo().name << "; from " << m_name << "::getNewObjects";
0038 } else {
0039
0040 edm::LogInfo(m_name) << "got info for tag " << tagInfo().name << ": size " << tagInfo().size
0041 << ", last object valid since " << tagInfo().lastInterval.since << " ( "
0042 << boost::posix_time::to_iso_extended_string(
0043 cond::time::to_boost(tagInfo().lastInterval.since))
0044 << " ); from " << m_name << "::getNewObjects";
0045
0046 previousFill = this->lastPayload();
0047
0048 edm::LogInfo(m_name) << "The last payload in tag " << tagInfo().name << " valid since "
0049 << tagInfo().lastInterval.since << " has token " << tagInfo().lastInterval.payloadId
0050 << " and values:\n"
0051 << *previousFill << "from " << m_name << "::getNewObjects";
0052 if (m_firstFill <= previousFill->fillNumber()) {
0053
0054
0055 std::ostringstream es;
0056 es << "Trying to insert fill number " << m_firstFill
0057 << ((m_firstFill < previousFill->fillNumber()) ? ", which is an older fill than "
0058 : ", which is the same fill as ")
0059 << "the last one in the destination tag " << previousFill->fillNumber()
0060 << ": the first fill to be looked for will become " << previousFill->fillNumber() + 1;
0061 edm::LogWarning(m_name) << es.str() << "; from " << m_name << "::getNewObjects";
0062 m_firstFill = previousFill->fillNumber() + 1;
0063 }
0064 }
0065
0066
0067
0068 if (m_firstFill > m_lastFill) {
0069 edm::LogError(m_name) << "WRONG CONFIGURATION! The first fill " << m_firstFill
0070 << " cannot be larger than the last one " << m_lastFill << " EXITING. from " << m_name
0071 << "::getNewObjects";
0072 return;
0073 }
0074
0075
0076 cond::persistency::ConnectionPool connection;
0077
0078 if (m_debug) {
0079 connection.setMessageVerbosity(coral::Debug);
0080 } else {
0081 connection.setMessageVerbosity(coral::Error);
0082 }
0083 connection.setAuthenticationPath(m_authpath);
0084 connection.configure();
0085
0086 cond::persistency::Session session = connection.createSession(m_connectionString, true);
0087
0088 coral::ISchema &runTimeLoggerSchema = session.nominalSchema();
0089
0090 session.transaction().start(true);
0091
0092 std::unique_ptr<coral::IQuery> fillDataQuery(runTimeLoggerSchema.newQuery());
0093
0094 fillDataQuery->addToTableList(std::string("RUNTIME_SUMMARY"));
0095
0096 fillDataQuery->addToOutputList(std::string("LHCFILL"));
0097 fillDataQuery->addToOutputList(std::string("NBUNCHESBEAM1"));
0098 fillDataQuery->addToOutputList(std::string("NBUNCHESBEAM2"));
0099 fillDataQuery->addToOutputList(std::string("NCOLLIDINGBUNCHES"));
0100 fillDataQuery->addToOutputList(std::string("NTARGETBUNCHES"));
0101 fillDataQuery->addToOutputList(std::string("RUNTIME_TYPE_ID"));
0102 fillDataQuery->addToOutputList(std::string("PARTY1"));
0103 fillDataQuery->addToOutputList(std::string("PARTY2"));
0104 fillDataQuery->addToOutputList(std::string("CROSSINGANGLE"));
0105 fillDataQuery->addToOutputList(std::string("BETASTAR"));
0106 fillDataQuery->addToOutputList(std::string("INTENSITYBEAM1"));
0107 fillDataQuery->addToOutputList(std::string("INTENSITYBEAM2"));
0108 fillDataQuery->addToOutputList(std::string("ENERGY"));
0109 fillDataQuery->addToOutputList(std::string("CREATETIME"));
0110 fillDataQuery->addToOutputList(std::string("BEGINTIME"));
0111 fillDataQuery->addToOutputList(std::string("ENDTIME"));
0112 fillDataQuery->addToOutputList(std::string("INJECTIONSCHEME"));
0113
0114 coral::AttributeList fillDataBindVariables;
0115 fillDataBindVariables.extend(std::string("firstFillNumber"), typeid(unsigned short));
0116 fillDataBindVariables[std::string("firstFillNumber")].data<unsigned short>() = m_firstFill;
0117 fillDataBindVariables.extend(std::string("lastFillNumber"), typeid(unsigned short));
0118 fillDataBindVariables[std::string("lastFillNumber")].data<unsigned short>() = m_lastFill;
0119
0120
0121 std::string conditionStr("BEGINTIME IS NOT NULL AND LHCFILL BETWEEN :firstFillNumber AND :lastFillNumber");
0122 fillDataQuery->setCondition(conditionStr, fillDataBindVariables);
0123
0124 fillDataQuery->addToOrderList(std::string("LHCFILL"));
0125
0126 coral::AttributeList fillDataOutput;
0127 fillDataOutput.extend<unsigned short>(std::string("LHCFILL"));
0128 fillDataOutput.extend<unsigned short>(std::string("NBUNCHESBEAM1"));
0129 fillDataOutput.extend<unsigned short>(std::string("NBUNCHESBEAM2"));
0130 fillDataOutput.extend<unsigned short>(std::string("NCOLLIDINGBUNCHES"));
0131 fillDataOutput.extend<unsigned short>(std::string("NTARGETBUNCHES"));
0132 fillDataOutput.extend<int>(std::string("RUNTIME_TYPE_ID"));
0133 fillDataOutput.extend<int>(std::string("PARTY1"));
0134 fillDataOutput.extend<int>(std::string("PARTY2"));
0135 fillDataOutput.extend<float>(std::string("CROSSINGANGLE"));
0136 fillDataOutput.extend<float>(std::string("BETASTAR"));
0137 fillDataOutput.extend<float>(std::string("INTENSITYBEAM1"));
0138 fillDataOutput.extend<float>(std::string("INTENSITYBEAM2"));
0139 fillDataOutput.extend<float>(std::string("ENERGY"));
0140 fillDataOutput.extend<coral::TimeStamp>(std::string("CREATETIME"));
0141 fillDataOutput.extend<coral::TimeStamp>(std::string("BEGINTIME"));
0142 fillDataOutput.extend<coral::TimeStamp>(std::string("ENDTIME"));
0143 fillDataOutput.extend<std::string>(std::string("INJECTIONSCHEME"));
0144 fillDataQuery->defineOutput(fillDataOutput);
0145
0146 coral::ICursor &fillDataCursor = fillDataQuery->execute();
0147
0148 unsigned short previousFillNumber = 1;
0149 unsigned short currentFill = m_firstFill;
0150 cond::Time_t previousFillEndTime = 0ULL, afterPreviousFillEndTime = 0ULL, beforeStableBeamStartTime = 0ULL;
0151 if (tagInfo().size > 0) {
0152 previousFillNumber = previousFill->fillNumber();
0153 previousFillEndTime = previousFill->endTime();
0154 }
0155 unsigned short bunches1 = 0, bunches2 = 0, collidingBunches = 0, targetBunches = 0;
0156 FillInfo::FillTypeId fillType = FillInfo::UNKNOWN;
0157 FillInfo::ParticleTypeId particleType1 = FillInfo::NONE, particleType2 = FillInfo::NONE;
0158 float crossingAngle = 0., betastar = 0., intensityBeam1 = 0., intensityBeam2 = 0., energy = 0.;
0159 coral::TimeStamp stableBeamStartTimeStamp, beamDumpTimeStamp;
0160 cond::Time_t creationTime = 0ULL, stableBeamStartTime = 0ULL, beamDumpTime = 0ULL;
0161 std::string injectionScheme("None");
0162 std::ostringstream ss;
0163
0164 while (fillDataCursor.next()) {
0165 if (m_debug) {
0166 std::ostringstream qs;
0167 fillDataCursor.currentRow().toOutputStream(qs);
0168 edm::LogInfo(m_name) << qs.str() << "\nfrom " << m_name << "::getNewObjects";
0169 }
0170 currentFill = fillDataCursor.currentRow()[std::string("LHCFILL")].data<unsigned short>();
0171 coral::Attribute const &bunches1Attribute = fillDataCursor.currentRow()[std::string("NBUNCHESBEAM1")];
0172 if (bunches1Attribute.isNull()) {
0173 bunches1 = 0;
0174 } else {
0175 bunches1 = bunches1Attribute.data<unsigned short>();
0176 }
0177 coral::Attribute const &bunches2Attribute = fillDataCursor.currentRow()[std::string("NBUNCHESBEAM2")];
0178 if (bunches2Attribute.isNull()) {
0179 bunches2 = 0;
0180 } else {
0181 bunches2 = bunches2Attribute.data<unsigned short>();
0182 }
0183 coral::Attribute const &collidingBunchesAttribute = fillDataCursor.currentRow()[std::string("NCOLLIDINGBUNCHES")];
0184 if (collidingBunchesAttribute.isNull()) {
0185 collidingBunches = 0;
0186 } else {
0187 collidingBunches = collidingBunchesAttribute.data<unsigned short>();
0188 }
0189 coral::Attribute const &targetBunchesAttribute = fillDataCursor.currentRow()[std::string("NTARGETBUNCHES")];
0190 if (targetBunchesAttribute.isNull()) {
0191 targetBunches = 0;
0192 } else {
0193 targetBunches = targetBunchesAttribute.data<unsigned short>();
0194 }
0195
0196 fillType =
0197 static_cast<FillInfo::FillTypeId>(fillDataCursor.currentRow()[std::string("RUNTIME_TYPE_ID")].data<int>());
0198 coral::Attribute const &particleType1Attribute = fillDataCursor.currentRow()[std::string("PARTY1")];
0199 if (particleType1Attribute.isNull()) {
0200 particleType1 = FillInfo::NONE;
0201 } else {
0202 particleType1 = static_cast<FillInfo::ParticleTypeId>(particleType1Attribute.data<int>());
0203 }
0204 coral::Attribute const &particleType2Attribute = fillDataCursor.currentRow()[std::string("PARTY2")];
0205 if (particleType2Attribute.isNull()) {
0206 particleType2 = FillInfo::NONE;
0207 } else {
0208 particleType2 = static_cast<FillInfo::ParticleTypeId>(particleType2Attribute.data<int>());
0209 }
0210 coral::Attribute const &crossingAngleAttribute = fillDataCursor.currentRow()[std::string("CROSSINGANGLE")];
0211 if (crossingAngleAttribute.isNull()) {
0212 crossingAngle = 0.;
0213 } else {
0214 crossingAngle = crossingAngleAttribute.data<float>();
0215 }
0216 coral::Attribute const &betastarAttribute = fillDataCursor.currentRow()[std::string("BETASTAR")];
0217 if (betastarAttribute.isNull()) {
0218 betastar = 0.;
0219 } else {
0220 betastar = betastarAttribute.data<float>();
0221 }
0222 coral::Attribute const &intensityBeam1Attribute = fillDataCursor.currentRow()[std::string("INTENSITYBEAM1")];
0223 if (intensityBeam1Attribute.isNull()) {
0224 intensityBeam1 = 0.;
0225 } else {
0226 intensityBeam1 = intensityBeam1Attribute.data<float>();
0227 }
0228 coral::Attribute const &intensityBeam2Attribute = fillDataCursor.currentRow()[std::string("INTENSITYBEAM2")];
0229 if (intensityBeam2Attribute.isNull()) {
0230 intensityBeam2 = 0.;
0231 } else {
0232 intensityBeam2 = intensityBeam2Attribute.data<float>();
0233 }
0234 coral::Attribute const &energyAttribute = fillDataCursor.currentRow()[std::string("ENERGY")];
0235 if (energyAttribute.isNull()) {
0236 energy = 0.;
0237 } else {
0238 energy = energyAttribute.data<float>();
0239 }
0240
0241 creationTime =
0242 cond::time::from_boost(fillDataCursor.currentRow()[std::string("CREATETIME")].data<coral::TimeStamp>().time());
0243
0244 stableBeamStartTimeStamp = fillDataCursor.currentRow()[std::string("BEGINTIME")].data<coral::TimeStamp>();
0245 stableBeamStartTime = cond::time::from_boost(stableBeamStartTimeStamp.time());
0246 coral::Attribute const &beamDumpTimeAttribute = fillDataCursor.currentRow()[std::string("ENDTIME")];
0247 if (beamDumpTimeAttribute.isNull()) {
0248 beamDumpTime = 0;
0249 } else {
0250 beamDumpTimeStamp = beamDumpTimeAttribute.data<coral::TimeStamp>();
0251 beamDumpTime = cond::time::from_boost(beamDumpTimeStamp.time());
0252 }
0253 coral::Attribute const &injectionSchemeAttribute = fillDataCursor.currentRow()[std::string("INJECTIONSCHEME")];
0254 if (injectionSchemeAttribute.isNull()) {
0255 injectionScheme = std::string("None");
0256 } else {
0257 injectionScheme = injectionSchemeAttribute.data<std::string>();
0258 }
0259
0260 if (fillType != FillInfo::UNKNOWN && (particleType1 == FillInfo::NONE || particleType2 == FillInfo::NONE)) {
0261 switch (fillType) {
0262 case FillInfo::PROTONS:
0263 particleType1 = FillInfo::PROTON;
0264 particleType2 = FillInfo::PROTON;
0265 break;
0266 case FillInfo::IONS:
0267 particleType1 = FillInfo::PB82;
0268 particleType2 = FillInfo::PB82;
0269 break;
0270 case FillInfo::UNKNOWN:
0271 case FillInfo::COSMICS:
0272 case FillInfo::GAP:
0273 break;
0274 }
0275 }
0276
0277 if (beamDumpTime == 0) {
0278 edm::LogWarning(m_name) << "NO TRANSFER NEEDED: the fill number " << currentFill << " is still ongoing"
0279 << "; from " << m_name << "::getNewObjects";
0280 continue;
0281 }
0282
0283 coral::ISchema &beamCondSchema = session.coralSession().schema(m_dipSchema);
0284
0285 session.transaction().start(true);
0286
0287 coral::AttributeList bunchConfBindVariables;
0288 bunchConfBindVariables.extend<coral::TimeStamp>(std::string("stableBeamStartTimeStamp"));
0289 bunchConfBindVariables[std::string("stableBeamStartTimeStamp")].data<coral::TimeStamp>() = stableBeamStartTimeStamp;
0290 conditionStr = std::string("DIPTIME <= :stableBeamStartTimeStamp");
0291
0292 coral::AttributeList bunchConfOutput;
0293 bunchConfOutput.extend<coral::TimeStamp>(std::string("DIPTIME"));
0294 bunchConfOutput.extend<unsigned short>(std::string("BUCKET"));
0295
0296 std::unique_ptr<coral::IQuery> bunchConf1Query(beamCondSchema.newQuery());
0297 bunchConf1Query->addToTableList(std::string("LHC_CIRCBUNCHCONFIG_BEAM1"),
0298 std::string("BEAMCONF\", TABLE( BEAMCONF.VALUE ) \"BUCKETS"));
0299 bunchConf1Query->addToOutputList(std::string("BEAMCONF.DIPTIME"), std::string("DIPTIME"));
0300 bunchConf1Query->addToOutputList(std::string("BUCKETS.COLUMN_VALUE"), std::string("BUCKET"));
0301 bunchConf1Query->setCondition(conditionStr, bunchConfBindVariables);
0302 bunchConf1Query->addToOrderList(std::string("DIPTIME DESC"));
0303 bunchConf1Query->limitReturnedRows(FillInfo::availableBunchSlots);
0304 bunchConf1Query->defineOutput(bunchConfOutput);
0305 coral::ICursor &bunchConf1Cursor = bunchConf1Query->execute();
0306 std::bitset<FillInfo::bunchSlots + 1> bunchConfiguration1(0ULL);
0307 while (bunchConf1Cursor.next()) {
0308 if (m_debug) {
0309 std::ostringstream b1s;
0310 fillDataCursor.currentRow().toOutputStream(b1s);
0311 edm::LogInfo(m_name) << b1s.str() << "\nfrom " << m_name << "::getNewObjects";
0312 }
0313
0314 if (bunchConf1Cursor.currentRow()[std::string("BUCKET")].data<unsigned short>() != 0) {
0315 unsigned short slot =
0316 (bunchConf1Cursor.currentRow()[std::string("BUCKET")].data<unsigned short>() - 1) / 10 + 1;
0317 bunchConfiguration1[slot] = true;
0318 }
0319 }
0320
0321 std::unique_ptr<coral::IQuery> bunchConf2Query(beamCondSchema.newQuery());
0322 bunchConf2Query->addToTableList(std::string("LHC_CIRCBUNCHCONFIG_BEAM2"),
0323 std::string("BEAMCONF\", TABLE( BEAMCONF.VALUE ) \"BUCKETS"));
0324 bunchConf2Query->addToOutputList(std::string("BEAMCONF.DIPTIME"), std::string("DIPTIME"));
0325 bunchConf2Query->addToOutputList(std::string("BUCKETS.COLUMN_VALUE"), std::string("BUCKET"));
0326 bunchConf2Query->setCondition(conditionStr, bunchConfBindVariables);
0327 bunchConf2Query->addToOrderList(std::string("DIPTIME DESC"));
0328 bunchConf2Query->limitReturnedRows(FillInfo::availableBunchSlots);
0329 bunchConf2Query->defineOutput(bunchConfOutput);
0330 coral::ICursor &bunchConf2Cursor = bunchConf2Query->execute();
0331 std::bitset<FillInfo::bunchSlots + 1> bunchConfiguration2(0ULL);
0332 while (bunchConf2Cursor.next()) {
0333 if (m_debug) {
0334 std::ostringstream b2s;
0335 fillDataCursor.currentRow().toOutputStream(b2s);
0336 edm::LogInfo(m_name) << b2s.str() << "\nfrom " << m_name << "::getNewObjects";
0337 }
0338 if (bunchConf2Cursor.currentRow()[std::string("BUCKET")].data<unsigned short>() != 0) {
0339 unsigned short slot =
0340 (bunchConf2Cursor.currentRow()[std::string("BUCKET")].data<unsigned short>() - 1) / 10 + 1;
0341 bunchConfiguration2[slot] = true;
0342 }
0343 }
0344
0345 session.transaction().commit();
0346
0347
0348 afterPreviousFillEndTime = cond::time::pack(std::make_pair(cond::time::unpack(previousFillEndTime).first,
0349 cond::time::unpack(previousFillEndTime).second + 1));
0350 beforeStableBeamStartTime = cond::time::pack(std::make_pair(cond::time::unpack(stableBeamStartTime).first,
0351 cond::time::unpack(stableBeamStartTime).second - 1));
0352 if (afterPreviousFillEndTime < stableBeamStartTime) {
0353 edm::LogInfo(m_name) << "Entering fake fill between fill number " << previousFillNumber
0354 << " and current fill number " << currentFill << ", from " << afterPreviousFillEndTime
0355 << " ( "
0356 << boost::posix_time::to_iso_extended_string(cond::time::to_boost(afterPreviousFillEndTime))
0357 << " ) to " << beforeStableBeamStartTime << " ( "
0358 << boost::posix_time::to_iso_extended_string(cond::time::to_boost(beforeStableBeamStartTime))
0359 << " ); from " << m_name << "::getNewObjects";
0360 m_to_transfer.push_back(std::make_pair(new FillInfo(), afterPreviousFillEndTime));
0361 } else {
0362
0363 edm::LogError(m_name) << "WRONG DATA! In the previous fill number " << previousFillNumber
0364 << " beams were dumped at timestamp "
0365 << boost::posix_time::to_iso_extended_string(cond::time::to_boost(previousFillEndTime))
0366 << ", which is not before the timestamp "
0367 << boost::posix_time::to_iso_extended_string(cond::time::to_boost(stableBeamStartTime))
0368 << " when current fill number " << currentFill << " entered stable beams. EXITING. from "
0369 << m_name << "::getNewObjects";
0370 return;
0371 }
0372
0373 FillInfo *fillInfo = new FillInfo(currentFill);
0374 fillInfo->setBeamInfo(const_cast<unsigned short const &>(bunches1),
0375 const_cast<unsigned short const &>(bunches2),
0376 const_cast<unsigned short const &>(collidingBunches),
0377 const_cast<unsigned short const &>(targetBunches),
0378 const_cast<FillInfo::FillTypeId const &>(fillType),
0379 const_cast<FillInfo::ParticleTypeId const &>(particleType1),
0380 const_cast<FillInfo::ParticleTypeId const &>(particleType2),
0381 const_cast<float const &>(crossingAngle),
0382 const_cast<float const &>(betastar),
0383 const_cast<float const &>(intensityBeam1),
0384 const_cast<float const &>(intensityBeam2),
0385 const_cast<float const &>(energy),
0386 const_cast<cond::Time_t const &>(creationTime),
0387 const_cast<cond::Time_t const &>(stableBeamStartTime),
0388 const_cast<cond::Time_t const &>(beamDumpTime),
0389 const_cast<std::string const &>(injectionScheme),
0390 const_cast<std::bitset<FillInfo::bunchSlots + 1> const &>(bunchConfiguration1),
0391 const_cast<std::bitset<FillInfo::bunchSlots + 1> const &>(bunchConfiguration2));
0392
0393 m_to_transfer.push_back(std::make_pair((FillInfo *)fillInfo, stableBeamStartTime));
0394 edm::LogInfo(m_name) << "The new payload to be inserted into tag " << tagInfo().name << " with validity "
0395 << stableBeamStartTime << " ( "
0396 << boost::posix_time::to_iso_extended_string(cond::time::to_boost(stableBeamStartTime))
0397 << " ) has values:\n"
0398 << *fillInfo << "from " << m_name << "::getNewObjects";
0399
0400 ss << " fill = " << currentFill << ";\tinjection scheme: " << injectionScheme
0401 << ";\tstart time: " << boost::posix_time::to_iso_extended_string(stableBeamStartTimeStamp.time())
0402 << ";\tend time: " << boost::posix_time::to_iso_extended_string(beamDumpTimeStamp.time()) << "." << std::endl;
0403
0404 previousFillNumber = currentFill;
0405 previousFillEndTime = beamDumpTime;
0406 }
0407
0408 session.transaction().commit();
0409
0410 session.close();
0411
0412 m_userTextLog = ss.str();
0413 edm::LogInfo(m_name) << "Transferring " << m_to_transfer.size() << " payload(s); from " << m_name
0414 << "::getNewObjects";
0415 }
0416
0417 std::string FillInfoPopConSourceHandler::id() const { return m_name; }