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