CMS 3D CMS Logo

LHCInfoPopConSourceHandler.cc
Go to the documentation of this file.
6 #include "RelationalAccess/ISessionProxy.h"
7 #include "RelationalAccess/ISchema.h"
8 #include "RelationalAccess/IQuery.h"
9 #include "RelationalAccess/ICursor.h"
10 #include "CoralBase/AttributeList.h"
11 #include "CoralBase/Attribute.h"
12 #include "CoralBase/AttributeSpecification.h"
13 #include "CoralBase/TimeStamp.h"
14 #include <iostream>
15 #include <memory>
16 #include <sstream>
17 #include <utility>
18 #include <vector>
19 
21  m_debug( pset.getUntrackedParameter<bool>( "debug", false ) )
22  ,m_firstFill( (unsigned short)pset.getUntrackedParameter<unsigned int>( "firstFill", 1 ) )
23  ,m_lastFill( (unsigned short)pset.getUntrackedParameter<unsigned int>( "lastFill", m_firstFill ) )
24  ,m_name( pset.getUntrackedParameter<std::string>( "name", "LHCInfoPopConSourceHandler" ) )
25  ,m_connectionString(pset.getUntrackedParameter<std::string>("connectionString",""))
26  ,m_dipSchema(pset.getUntrackedParameter<std::string>("DIPSchema",""))
27  ,m_authpath(pset.getUntrackedParameter<std::string>("authenticationPath","")) {}
28 //L1: try with different m_dipSchema
29 //L2: try with different m_name
31 
33  //reference to the last payload in the tag
34  Ref previousFill;
35 
36  //if a new tag is created, transfer fake fill from 1 to the first fill for the first time
37  if ( tagInfo().size == 0 ) {
38  edm::LogInfo( m_name ) << "New tag "<< tagInfo().name << "; from " << m_name << "::getNewObjects";
39  } else {
40  //check what is already inside the database
41  edm::LogInfo( m_name ) << "got info for tag " << tagInfo().name
42  << ", IOVSequence token " << tagInfo().token
43  << ": size " << tagInfo().size
44  << ", last object valid since " << tagInfo().lastInterval.first
45  << " ( "<< boost::posix_time::to_iso_extended_string( cond::time::to_boost( tagInfo().lastInterval.first ) )
46  << " ); from " << m_name << "::getNewObjects";
47  //retrieve the last payload...
48  previousFill = this->lastPayload();
49  //checking its content
50  edm::LogInfo( m_name ) << "The last payload in tag " << tagInfo().name
51  << " valid since " << tagInfo().lastInterval.first
52  << " has token " << tagInfo().lastPayloadToken
53  << " and values:\n" << *previousFill
54  << "from " << m_name << "::getNewObjects";
55  if( m_firstFill <= previousFill->fillNumber() ) {
56  //either we are trying to put the same value, or we want to put an older fill:
57  //the first fill will become the previous one plus one
58  std::ostringstream es;
59  es << "Trying to insert fill number " << m_firstFill
60  << ( ( m_firstFill < previousFill->fillNumber() ) ? ", which is an older fill than " : ", which is the same fill as " )
61  << "the last one in the destination tag " << previousFill->fillNumber()
62  << ": the first fill to be looked for will become " << previousFill->fillNumber() + 1;
63  edm::LogWarning( m_name ) << es.str() << "; from " << m_name << "::getNewObjects";
64  m_firstFill = previousFill->fillNumber() + 1;
65  }
66  }
67 
68  //if the last fill to be looked for is smaller than the first one send error message and return
69  //this check cannot be done before, as we should find which is the first fill to query
70  if( m_firstFill > m_lastFill ) {
71  edm::LogError( m_name ) << "WRONG CONFIGURATION! The first fill " << m_firstFill
72  << " cannot be larger than the last one " << m_lastFill
73  << " EXITING. from " << m_name << "::getNewObjects";
74  return;
75  }
76 
77  //retrieve the data from the relational database source
79  //configure the connection
80  if( m_debug ) {
81  connection.setMessageVerbosity( coral::Debug );
82  } else {
83  connection.setMessageVerbosity( coral::Error );
84  }
85  connection.setAuthenticationPath( m_authpath );
86  connection.configure();
87  //create a sessiom
89  //run the first query against the schema logging fill information
90  coral::ISchema& runTimeLoggerSchema = session.nominalSchema();
91  //start the transaction against the fill logging schema
92  session.transaction().start(true);
93  //prepare the query for table 1:
94  std::unique_ptr<coral::IQuery> fillDataQuery( runTimeLoggerSchema.newQuery() );
95  //FROM clause
96  fillDataQuery->addToTableList( std::string( "RUNTIME_SUMMARY" ) );
97  //SELECT clause
98  fillDataQuery->addToOutputList( std::string( "LHCFILL" ) );
99  fillDataQuery->addToOutputList( std::string( "NBUNCHESBEAM1" ) );
100  fillDataQuery->addToOutputList( std::string( "NBUNCHESBEAM2" ) );
101  fillDataQuery->addToOutputList( std::string( "NCOLLIDINGBUNCHES" ) );
102  fillDataQuery->addToOutputList( std::string( "NTARGETBUNCHES" ) );
103  fillDataQuery->addToOutputList( std::string( "RUNTIME_TYPE_ID" ) );
104  fillDataQuery->addToOutputList( std::string( "PARTY1" ) );
105  fillDataQuery->addToOutputList( std::string( "PARTY2" ) );
106  fillDataQuery->addToOutputList( std::string( "CROSSINGANGLE" ) );
107  fillDataQuery->addToOutputList( std::string( "BETASTAR" ) );
108  fillDataQuery->addToOutputList( std::string( "INTENSITYBEAM1" ) );
109  fillDataQuery->addToOutputList( std::string( "INTENSITYBEAM2" ) );
110  fillDataQuery->addToOutputList( std::string( "ENERGY" ) );
111  fillDataQuery->addToOutputList( std::string( "CREATETIME" ) );
112  fillDataQuery->addToOutputList( std::string( "BEGINTIME" ) );
113  fillDataQuery->addToOutputList( std::string( "ENDTIME" ) );
114  fillDataQuery->addToOutputList( std::string( "INJECTIONSCHEME" ) );
115  //WHERE clause
116  coral::AttributeList fillDataBindVariables;
117  fillDataBindVariables.extend( std::string( "firstFillNumber" ), typeid( unsigned short ) );
118  fillDataBindVariables[ std::string( "firstFillNumber" ) ].data<unsigned short>() = m_firstFill;
119  fillDataBindVariables.extend( std::string( "lastFillNumber" ), typeid( unsigned short ) );
120  fillDataBindVariables[ std::string( "lastFillNumber" ) ].data<unsigned short>() = m_lastFill;
121  //by imposing BEGINTIME IS NOT NULL, we remove fills which never went into stable beams,
122  //or the most recent one, just declared but not yet in stable beams
123  std::string conditionStr( "BEGINTIME IS NOT NULL AND LHCFILL BETWEEN :firstFillNumber AND :lastFillNumber" );
124  fillDataQuery->setCondition( conditionStr, fillDataBindVariables );
125  //ORDER BY clause
126  fillDataQuery->addToOrderList( std::string( "LHCFILL" ) );
127  //define query output
128  coral::AttributeList fillDataOutput;
129  fillDataOutput.extend<unsigned short>( std::string( "LHCFILL" ) );
130  fillDataOutput.extend<unsigned short>( std::string( "NBUNCHESBEAM1" ) );
131  fillDataOutput.extend<unsigned short>( std::string( "NBUNCHESBEAM2" ) );
132  fillDataOutput.extend<unsigned short>( std::string( "NCOLLIDINGBUNCHES" ) );
133  fillDataOutput.extend<unsigned short>( std::string( "NTARGETBUNCHES" ) );
134  fillDataOutput.extend<int>( std::string( "RUNTIME_TYPE_ID" ) );
135  fillDataOutput.extend<int>( std::string( "PARTY1" ) );
136  fillDataOutput.extend<int>( std::string( "PARTY2" ) );
137  fillDataOutput.extend<float>( std::string( "CROSSINGANGLE" ) );
138  fillDataOutput.extend<float>( std::string( "BETASTAR" ) );
139  fillDataOutput.extend<float>( std::string( "INTENSITYBEAM1" ) );
140  fillDataOutput.extend<float>( std::string( "INTENSITYBEAM2" ) );
141  fillDataOutput.extend<float>( std::string( "ENERGY" ) );
142  fillDataOutput.extend<coral::TimeStamp>( std::string( "CREATETIME" ) );
143  fillDataOutput.extend<coral::TimeStamp>( std::string( "BEGINTIME" ) );
144  fillDataOutput.extend<coral::TimeStamp>( std::string( "ENDTIME" ) );
145  fillDataOutput.extend<std::string>( std::string( "INJECTIONSCHEME" ) );
146  fillDataQuery->defineOutput( fillDataOutput );
147  //execute the query
148  coral::ICursor& fillDataCursor = fillDataQuery->execute();
149  //initialize loop variables
150  unsigned short previousFillNumber = 1, currentFill = m_firstFill;
151  cond::Time_t previousFillEndTime = 0ULL, afterPreviousFillEndTime = 0ULL, beforeStableBeamStartTime = 0ULL;
152  if( tagInfo().size > 0 ) {
153  previousFillNumber = previousFill->fillNumber();
154  previousFillEndTime = previousFill->endTime();
155  }
156  unsigned short bunches1 = 0, bunches2 = 0, collidingBunches = 0, targetBunches = 0;
158  LHCInfo::ParticleTypeId particleType1 = LHCInfo::NONE, particleType2 = LHCInfo::NONE;
159  float crossingAngle = 0., betastar = 0., intensityBeam1 = 0., intensityBeam2 = 0., energy = 0.;
160  coral::TimeStamp stableBeamStartTimeStamp, beamDumpTimeStamp;
161  cond::Time_t creationTime = 0ULL, stableBeamStartTime = 0ULL, beamDumpTime = 0ULL;
162  std::string injectionScheme( "None" );
163  std::ostringstream ss;
164 
165 //prepare the query for table 2:
166  std::unique_ptr<coral::IQuery> fillDataQuery2( runTimeLoggerSchema.newQuery() );
167  //FROM clause
168  fillDataQuery2->addToTableList( std::string( "LUMI_SECTIONS" ) );
169  //SELECT clause
170  fillDataQuery2->addToOutputList( std::string( "MAX(DELIVLUMI)" ) );
171  fillDataQuery2->addToOutputList( std::string( "MAX(LIVELUMI)" ) );
172  //WHERE clause
173  //by imposing BEGINTIME IS NOT NULL, we remove fills which never went into stable beams,
174  //or the most recent one, just declared but not yet in stable beams
175  conditionStr = "DELIVLUMI IS NOT NULL AND LHCFILL BETWEEN :firstFillNumber AND :lastFillNumber";
176  fillDataQuery2->setCondition( conditionStr, fillDataBindVariables );
177  //ORDER BY clause
178  fillDataQuery2->addToOrderList( std::string( "LHCFILL" ) );
179  fillDataQuery2->groupBy( std::string( "LHCFILL" ) );
180  //define query output*/
181  coral::AttributeList fillDataOutput2;
182  fillDataOutput2.extend<float>( std::string( "DELIVEREDLUMI" ) );
183  fillDataOutput2.extend<float>( std::string( "RECORDEDLUMI" ) );
184  fillDataQuery2->defineOutput( fillDataOutput2 );
185  //execute the query
186  coral::ICursor& fillDataCursor2 = fillDataQuery2->execute();
187  //initialize loop variables
188  float delivLumi = 0., recLumi = 0.;
189 
190 
191  //loop over the cursor where the result of the query were fetched
192  while( fillDataCursor.next() ) {
193  if( m_debug ) {
194  std::ostringstream qs;
195  fillDataCursor.currentRow().toOutputStream( qs );
196  edm::LogInfo( m_name ) << qs.str() << "\nfrom " << m_name << "::getNewObjects";
197  }
198  currentFill = fillDataCursor.currentRow()[ std::string( "LHCFILL" ) ].data<unsigned short>();
199  coral::Attribute const & bunches1Attribute = fillDataCursor.currentRow()[ std::string( "NBUNCHESBEAM1" ) ];
200  if( bunches1Attribute.isNull() ) {
201  bunches1 = 0;
202  } else {
203  bunches1 = bunches1Attribute.data<unsigned short>();
204  }
205  coral::Attribute const & bunches2Attribute = fillDataCursor.currentRow()[ std::string( "NBUNCHESBEAM2" ) ];
206  if( bunches2Attribute.isNull() ) {
207  bunches2 = 0;
208  } else {
209  bunches2 = bunches2Attribute.data<unsigned short>();
210  }
211  coral::Attribute const & collidingBunchesAttribute = fillDataCursor.currentRow()[ std::string( "NCOLLIDINGBUNCHES" ) ];
212  if( collidingBunchesAttribute.isNull() ) {
213  collidingBunches = 0;
214  } else {
215  collidingBunches = collidingBunchesAttribute.data<unsigned short>();
216  }
217  coral::Attribute const & targetBunchesAttribute = fillDataCursor.currentRow()[ std::string( "NTARGETBUNCHES" ) ];
218  if( targetBunchesAttribute.isNull() ) {
219  targetBunches = 0;
220  } else {
221  targetBunches = targetBunchesAttribute.data<unsigned short>();
222  }
223  //RUNTIME_TYPE_ID IS NOT NULL
224  fillType = static_cast<LHCInfo::FillTypeId>( fillDataCursor.currentRow()[ std::string( "RUNTIME_TYPE_ID" ) ].data<int>() );
225  coral::Attribute const & particleType1Attribute = fillDataCursor.currentRow()[ std::string( "PARTY1" ) ];
226  if( particleType1Attribute.isNull() ) {
227  particleType1 = LHCInfo::NONE;
228  } else {
229  particleType1 = static_cast<LHCInfo::ParticleTypeId>( particleType1Attribute.data<int>() );
230  }
231  coral::Attribute const & particleType2Attribute = fillDataCursor.currentRow()[ std::string( "PARTY2" ) ];
232  if( particleType2Attribute.isNull() ) {
233  particleType2 = LHCInfo::NONE;
234  } else {
235  particleType2 = static_cast<LHCInfo::ParticleTypeId>( particleType2Attribute.data<int>() );
236  }
237  coral::Attribute const & crossingAngleAttribute = fillDataCursor.currentRow()[ std::string( "CROSSINGANGLE" ) ];
238  if( crossingAngleAttribute.isNull() ) {
239  crossingAngle = 0.;
240  } else {
241  crossingAngle = crossingAngleAttribute.data<float>();
242  }
243  coral::Attribute const & betastarAttribute = fillDataCursor.currentRow()[ std::string( "BETASTAR" ) ];
244  if( betastarAttribute.isNull() ) {
245  betastar = 0.;
246  } else {
247  betastar = betastarAttribute.data<float>();
248  }
249  coral::Attribute const & intensityBeam1Attribute = fillDataCursor.currentRow()[ std::string( "INTENSITYBEAM1" ) ];
250  if( intensityBeam1Attribute.isNull() ) {
251  intensityBeam1 = 0.;
252  } else {
253  intensityBeam1 = intensityBeam1Attribute.data<float>();
254  }
255  coral::Attribute const & intensityBeam2Attribute = fillDataCursor.currentRow()[ std::string( "INTENSITYBEAM2" ) ];
256  if( intensityBeam2Attribute.isNull() ) {
257  intensityBeam2 = 0.;
258  } else {
259  intensityBeam2 = intensityBeam2Attribute.data<float>();
260  }
261  coral::Attribute const & energyAttribute = fillDataCursor.currentRow()[ std::string( "ENERGY" ) ];
262  if( energyAttribute.isNull() ){
263  energy = 0.;
264  } else {
265  energy = energyAttribute.data<float>();
266  }
267 
268  if( fillDataCursor2.next())
269  {
270  coral::Attribute const & delivLumiAttribute = fillDataCursor2.currentRow()[ std::string( "DELIVEREDLUMI" ) ];
271  if( delivLumiAttribute.isNull() ){
272  delivLumi = 0.;
273  }
274  else {
275  delivLumi = delivLumiAttribute.data<float>() / 1000.;
276  }
277 
278  coral::Attribute const & recLumiAttribute = fillDataCursor2.currentRow()[ std::string( "RECORDEDLUMI" ) ];
279  if( recLumiAttribute.isNull() ){
280  recLumi = 0.;
281  }
282  else {
283  recLumi = recLumiAttribute.data<float>() / 1000.;
284  }
285  }
286 
287  //CREATETIME IS NOT NULL
288  creationTime = cond::time::from_boost( fillDataCursor.currentRow()[ std::string( "CREATETIME" ) ].data<coral::TimeStamp>().time() );
289  //BEGINTIME is imposed to be NOT NULL in the WHERE clause
290  stableBeamStartTimeStamp = fillDataCursor.currentRow()[ std::string( "BEGINTIME" ) ].data<coral::TimeStamp>();
291  stableBeamStartTime = cond::time::from_boost( stableBeamStartTimeStamp.time() );
292  coral::Attribute const & beamDumpTimeAttribute = fillDataCursor.currentRow()[ std::string( "ENDTIME" ) ];
293  if( beamDumpTimeAttribute.isNull() ) {
294  beamDumpTime = 0;
295  } else {
296  beamDumpTimeStamp = beamDumpTimeAttribute.data<coral::TimeStamp>();
297  beamDumpTime = cond::time::from_boost( beamDumpTimeStamp.time() );
298  }
299  coral::Attribute const & injectionSchemeAttribute = fillDataCursor.currentRow()[ std::string( "INJECTIONSCHEME" ) ];
300  if( injectionSchemeAttribute.isNull() ) {
301  injectionScheme = std::string( "None" );
302  } else {
303  injectionScheme = injectionSchemeAttribute.data<std::string>();
304  }
305  //fix an inconsistency in RunTimeLogger: if the fill type is defined, the particle type should reflect it!
306  if( fillType != LHCInfo::UNKNOWN && ( particleType1 == LHCInfo::NONE || particleType2 == LHCInfo::NONE ) ) {
307  switch( fillType ) {
308  case LHCInfo::PROTONS :
309  particleType1 = LHCInfo::PROTON;
310  particleType2 = LHCInfo::PROTON;
311  break;
312  case LHCInfo::IONS :
313  particleType1 = LHCInfo::PB82;
314  particleType2 = LHCInfo::PB82;
315  break;
316  case LHCInfo::UNKNOWN :
317  case LHCInfo::COSMICS :
318  case LHCInfo::GAP :
319  break;
320  }
321  }
322  //if the end time of the fill is 0 (i.e. timestamp null), it is still ongoing: do not store!
323  if( beamDumpTime == 0 ) {
324  edm::LogWarning( m_name ) << "NO TRANSFER NEEDED: the fill number " << currentFill
325  << " is still ongoing"
326  << "; from " << m_name << "::getNewObjects";
327  continue;
328  }
329 
330  //run the third and fourth query against the schema hosting detailed DIP information
331  coral::ISchema& beamCondSchema = session.coralSession().schema( m_dipSchema );
332  //start the transaction against the DIP "deep" database backend schema
333  session.transaction().start( true );
334  //prepare the WHERE clause for both queries
335  coral::AttributeList bunchConfBindVariables;
336  bunchConfBindVariables.extend<coral::TimeStamp>( std::string( "stableBeamStartTimeStamp" ) );
337  bunchConfBindVariables[ std::string( "stableBeamStartTimeStamp" ) ].data<coral::TimeStamp>() = stableBeamStartTimeStamp;
338  conditionStr = std::string( "DIPTIME <= :stableBeamStartTimeStamp" );
339  //define the output types for both queries
340  coral::AttributeList bunchConfOutput;
341  bunchConfOutput.extend<coral::TimeStamp>( std::string( "DIPTIME" ) );
342  bunchConfOutput.extend<unsigned short>( std::string( "BUCKET" ) );
343  //execute query for Beam 1
344  std::unique_ptr<coral::IQuery> bunchConf1Query(beamCondSchema.newQuery());
345  bunchConf1Query->addToTableList( std::string( "LHC_CIRCBUNCHCONFIG_BEAM1" ), std::string( "BEAMCONF\", TABLE( BEAMCONF.VALUE ) \"BUCKETS" ) );
346  bunchConf1Query->addToOutputList( std::string( "BEAMCONF.DIPTIME" ), std::string( "DIPTIME" ) );
347  bunchConf1Query->addToOutputList( std::string( "BUCKETS.COLUMN_VALUE" ), std::string( "BUCKET" ) );
348  bunchConf1Query->setCondition( conditionStr, bunchConfBindVariables );
349  bunchConf1Query->addToOrderList( std::string( "DIPTIME DESC" ) );
350  bunchConf1Query->limitReturnedRows( LHCInfo::availableBunchSlots ); //maximum number of filled bunches
351  bunchConf1Query->defineOutput( bunchConfOutput );
352  coral::ICursor& bunchConf1Cursor = bunchConf1Query->execute();
353  std::bitset<LHCInfo::bunchSlots+1> bunchConfiguration1( 0ULL );
354 
355  while( bunchConf1Cursor.next() ) {
356  if( m_debug ) {
357  std::ostringstream b1s;
358  fillDataCursor.currentRow().toOutputStream( b1s );
359  edm::LogInfo( m_name ) << b1s.str() << "\nfrom " << m_name << "::getNewObjects";
360  }
361  if( bunchConf1Cursor.currentRow()[ std::string( "BUCKET" ) ].data<unsigned short>() != 0 ) {
362  unsigned short slot = ( bunchConf1Cursor.currentRow()[ std::string( "BUCKET" ) ].data<unsigned short>() - 1 ) / 10 + 1;
363  bunchConfiguration1[ slot ] = true;
364  }
365  }
366 
367 
368 //execute query for Beam 2
369  std::unique_ptr<coral::IQuery> bunchConf2Query(beamCondSchema.newQuery());
370  bunchConf2Query->addToTableList( std::string( "LHC_CIRCBUNCHCONFIG_BEAM2" ), std::string( "BEAMCONF\", TABLE( BEAMCONF.VALUE ) \"BUCKETS" ) );
371  bunchConf2Query->addToOutputList( std::string( "BEAMCONF.DIPTIME" ), std::string( "DIPTIME" ) );
372  bunchConf2Query->addToOutputList( std::string( "BUCKETS.COLUMN_VALUE" ), std::string( "BUCKET" ) );
373  bunchConf2Query->setCondition( conditionStr, bunchConfBindVariables );
374  bunchConf2Query->addToOrderList( std::string( "DIPTIME DESC" ) );
375  bunchConf2Query->limitReturnedRows( LHCInfo::availableBunchSlots ); //maximum number of filled bunches
376  bunchConf2Query->defineOutput( bunchConfOutput );
377  coral::ICursor& bunchConf2Cursor = bunchConf2Query->execute();
378  std::bitset<LHCInfo::bunchSlots+1> bunchConfiguration2( 0ULL );
379 
380  while( bunchConf2Cursor.next() ) {
381  if( m_debug ) {
382  std::ostringstream b2s;
383  fillDataCursor.currentRow().toOutputStream( b2s );
384  edm::LogInfo( m_name ) << b2s.str() << "\nfrom " << m_name << "::getNewObjects";
385  }
386  if( bunchConf2Cursor.currentRow()[ std::string( "BUCKET" ) ].data<unsigned short>() != 0 ) {
387  unsigned short slot = ( bunchConf2Cursor.currentRow()[ std::string( "BUCKET" ) ].data<unsigned short>() - 1 ) / 10 + 1;
388  bunchConfiguration2[ slot ] = true;
389  }
390  }
391 
392  //execute query for lumiPerBX
393  std::unique_ptr<coral::IQuery> lumiDataQuery(beamCondSchema.newQuery());
394  lumiDataQuery->addToTableList( std::string( "CMS_LHC_LUMIPERBUNCH" ), std::string( "LUMIPERBUNCH\", TABLE( LUMIPERBUNCH.LUMI_BUNCHINST ) \"VALUE" ) );
395  lumiDataQuery->addToOutputList( std::string( "LUMIPERBUNCH.DIPTIME" ), std::string( "DIPTIME" ) );
396  lumiDataQuery->addToOutputList( std::string( "VALUE.COLUMN_VALUE" ), std::string( "LUMI/BUNCH" ) );
397  coral::AttributeList lumiDataBindVariables;
398  lumiDataBindVariables.extend<coral::TimeStamp>( std::string( "stableBeamStartTimeStamp" ) );
399  lumiDataBindVariables[ std::string( "stableBeamStartTimeStamp" ) ].data<coral::TimeStamp>() = stableBeamStartTimeStamp;
400  lumiDataBindVariables.extend<coral::TimeStamp>( std::string( "beamDumpTimeStamp" ) );
401  lumiDataBindVariables[ std::string( "beamDumpTimeStamp" ) ].data<coral::TimeStamp>() = beamDumpTimeStamp;
402  conditionStr = std::string( "DIPTIME BETWEEN :stableBeamStartTimeStamp AND :beamDumpTimeStamp" );
403  lumiDataQuery->setCondition( conditionStr, lumiDataBindVariables );
404  lumiDataQuery->addToOrderList( std::string( "DIPTIME DESC" ) );
405  lumiDataQuery->limitReturnedRows(3564); //Maximum number of bunches.
406  //define query output
407  coral::AttributeList lumiDataOutput;
408  lumiDataOutput.extend<coral::TimeStamp>( std::string( "TIME" ) );
409  lumiDataOutput.extend<float>( std::string( "VALUE" ) );
410  lumiDataQuery->defineOutput( lumiDataOutput );
411  //execute the query
412  coral::ICursor& lumiDataCursor = lumiDataQuery->execute();
413  std::vector<float> lumiPerBX;
414 
415  while( lumiDataCursor.next() ) {
416  if( m_debug ) {
417  std::ostringstream lpBX;
418  lumiDataCursor.currentRow().toOutputStream( lpBX );
419  edm::LogInfo( m_name ) << lpBX.str() << "\nfrom " << m_name << "::getNewObjects";
420  }
421  if( lumiDataCursor.currentRow()[ std::string( "VALUE" ) ].data<float>() != 0.00 ) {
422  lumiPerBX.push_back(lumiDataCursor.currentRow()[ std::string( "VALUE" ) ].data<float>());
423  }
424  }
425 
426  //commit the transaction against the DIP "deep" database backend schema
427  session.transaction().commit();
428 
429  //run the fifth query against the CTPPS schema
430  //Initializing the CMS_CTP_CTPPS_COND schema.
431  coral::ISchema& CTPPS = session.coralSession().schema("CMS_CTP_CTPPS_COND");
432  session.transaction().start( true );
433  //execute query for CTPPS Data
434  std::unique_ptr<coral::IQuery> CTPPSDataQuery( CTPPS.newQuery() );
435  //FROM clause
436  CTPPSDataQuery->addToTableList( std::string( "CTPPS_LHC_MACHINE_PARAMS" ) );
437  //SELECT clause
438  CTPPSDataQuery->addToOutputList( std::string( "LHC_STATE" ) );
439  CTPPSDataQuery->addToOutputList( std::string( "LHC_COMMENT" ) );
440  CTPPSDataQuery->addToOutputList( std::string( "CTPPS_STATUS" ) );
441  CTPPSDataQuery->addToOutputList( std::string( "LUMI_SECTION" ) );
442  //WHERE CLAUSE
443  coral::AttributeList CTPPSDataBindVariables;
444  CTPPSDataBindVariables.extend<int>( std::string( "currentFill" ) );
445  CTPPSDataBindVariables[ std::string( "currentFill" ) ].data<int>() = currentFill;
446  conditionStr = std::string( "FILL_NUMBER = :currentFill" );
447  CTPPSDataQuery->setCondition( conditionStr, CTPPSDataBindVariables );
448  //ORDER BY clause
449  CTPPSDataQuery->addToOrderList( std::string( "DIP_UPDATE_TIME DESC" ) ); //Only the latest value is fetched.
450  //define query output
451  coral::AttributeList CTPPSDataOutput;
452  CTPPSDataOutput.extend<std::string>( std::string( "LHC_STATE" ) );
453  CTPPSDataOutput.extend<std::string>( std::string( "LHC_COMMENT" ) );
454  CTPPSDataOutput.extend<std::string>( std::string( "CTPPS_STATUS" ) );
455  CTPPSDataOutput.extend<int>( std::string( "LUMI_SECTION" ) );
456  CTPPSDataQuery->limitReturnedRows( 1 ); //Only one entry per payload.
457  CTPPSDataQuery->defineOutput( CTPPSDataOutput );
458  //execute the query
459  coral::ICursor& CTPPSDataCursor = CTPPSDataQuery->execute();
460  std::string lhcState, lhcComment, ctppsStatus;
461  unsigned int lumiSection;
462 
463  if( CTPPSDataCursor.next() ) {
464  if( m_debug ) {
465  std::ostringstream CTPPS;
466  CTPPSDataCursor.currentRow().toOutputStream( CTPPS );
467  edm::LogInfo( m_name ) << CTPPS.str() << "\nfrom " << m_name << "::getNewObjects";
468  }
469  coral::Attribute const & lhcStateAttribute = CTPPSDataCursor.currentRow()[ std::string( "LHC_STATE" ) ];
470  if( lhcStateAttribute.isNull() ) {
471  lhcState = "";
472  } else {
473  lhcState = lhcStateAttribute.data<std::string>();
474  }
475 
476  coral::Attribute const & lhcCommentAttribute = CTPPSDataCursor.currentRow()[ std::string( "LHC_COMMENT" ) ];
477  if( lhcCommentAttribute.isNull() ) {
478  lhcComment = "";
479  } else {
480  lhcComment = lhcCommentAttribute.data<std::string>();
481  }
482 
483  coral::Attribute const & ctppsStatusAttribute = CTPPSDataCursor.currentRow()[ std::string( "CTPPS_STATUS" ) ];
484  if( ctppsStatusAttribute.isNull() ) {
485  ctppsStatus = "";
486  } else {
487  ctppsStatus = ctppsStatusAttribute.data<std::string>();
488  }
489 
490  coral::Attribute const & lumiSectionAttribute = CTPPSDataCursor.currentRow()[ std::string( "LUMI_SECTION" ) ];
491  if( lumiSectionAttribute.isNull() ) {
492  lumiSection = 0;
493  } else {
494  lumiSection = lumiSectionAttribute.data<int>();
495  }
496  }
497  //commit the transaction against the CTPPS schema
498  session.transaction().commit();
499 
500  //run the sixth query against the CMS_DCS_ENV_PVSS_COND schema
501  cond::persistency::Session session2 = connection.createSession( std::string("oracle://cms_orcon_adg/CMS_DCS_ENV_PVSS_COND"), false );
502  //Initializing the CMS_DCS_ENV_PVSS_COND schema.
503  coral::ISchema& ECAL = session2.nominalSchema();
504  //start the transaction against the fill logging schema
505  session2.transaction().start( true );
506  //execute query for ECAL Data
507  std::unique_ptr<coral::IQuery> ECALDataQuery( ECAL.newQuery() );
508  //FROM clause
509  ECALDataQuery->addToTableList( std::string( "BEAM_PHASE" ) );
510  //SELECT clause
511  ECALDataQuery->addToOutputList( std::string( "DIP_value" ) );
512  ECALDataQuery->addToOutputList( std::string( "element_nr" ) );
513  //WHERE CLAUSE
514  coral::AttributeList ECALDataBindVariables;
515  conditionStr = std::string( "DIP_value LIKE '%beamPhaseMean%' OR DIP_value LIKE '%cavPhaseMean%'" );
516 
517  ECALDataQuery->setCondition( conditionStr, ECALDataBindVariables );
518  //ORDER BY clause
519  ECALDataQuery->addToOrderList( std::string( "CHANGE_DATE" ) );
520  ECALDataQuery->addToOrderList( std::string( "DIP_value" ) );
521  ECALDataQuery->addToOrderList( std::string( "element_nr" ) );
522  //define query output
523  coral::AttributeList ECALDataOutput;
524  ECALDataOutput.extend<std::string>( std::string( "DIP_value" ) );
525  ECALDataOutput.extend<float>( std::string( "element_nr" ) );
526  ECALDataQuery->limitReturnedRows( 14256 ); //3564 entries per vector.
527  ECALDataQuery->defineOutput( ECALDataOutput );
528  //execute the query
529  coral::ICursor& ECALDataCursor = ECALDataQuery->execute();
530  std::vector<float> beam1VC, beam2VC, beam1RF, beam2RF;
531  std::string dipVal;
532  std::map<std::string, int> vecMap;
533  vecMap[std::string("Beam1/beamPhaseMean")] = 1;
534  vecMap[std::string("Beam2/beamPhaseMean")] = 2;
535  vecMap[std::string("Beam1/cavPhaseMean")] = 3;
536  vecMap[std::string("Beam2/cavPhaseMean")] = 4;
537 
538  while( ECALDataCursor.next() ) {
539  if( m_debug ) {
540  std::ostringstream ECAL;
541  ECALDataCursor.currentRow().toOutputStream( ECAL );
542  edm::LogInfo( m_name ) << ECAL.str() << "\nfrom " << m_name << "::getNewObjects";
543  }
544  coral::Attribute const & dipValAttribute = ECALDataCursor.currentRow()[ std::string( "DIP_value" ) ];
545  if( dipValAttribute.isNull() ) {
546  dipVal = "";
547  } else {
548  dipVal = dipValAttribute.data<std::string>();
549  }
550 
551  coral::Attribute const & elementNrAttribute = ECALDataCursor.currentRow()[ std::string( "element_nr" ) ];
552  if( elementNrAttribute.isNull() ) { } //Nothing to record.
553  else {
554  switch( vecMap[dipVal] )
555  {
556  case 1:
557  beam1VC.push_back(elementNrAttribute.data<float>());
558  break;
559  case 2:
560  beam2VC.push_back(elementNrAttribute.data<float>());
561  break;
562  case 3:
563  beam1RF.push_back(elementNrAttribute.data<float>());
564  break;
565  case 4:
566  beam2RF.push_back(elementNrAttribute.data<float>());
567  break;
568  default:
569  break;
570  }
571  }
572  }
573  //commit the transaction against the ECAL schema
574  session2.transaction().commit();
575 
576  //store dummy fill information if empty fills are found beetween the two last ones in stable beams
577  afterPreviousFillEndTime = cond::time::pack( std::make_pair( cond::time::unpack( previousFillEndTime ).first, cond::time::unpack( previousFillEndTime ).second + 1 ) );
578  beforeStableBeamStartTime = cond::time::pack( std::make_pair( cond::time::unpack( stableBeamStartTime ).first, cond::time::unpack( stableBeamStartTime ).second - 1 ) );
579  if( afterPreviousFillEndTime < stableBeamStartTime ) {
580  edm::LogInfo( m_name ) << "Entering fake fill between fill number " << previousFillNumber
581  << " and current fill number " << currentFill
582  << ", from " << afterPreviousFillEndTime
583  << " ( " << boost::posix_time::to_iso_extended_string( cond::time::to_boost( afterPreviousFillEndTime ) )
584  << " ) to " << beforeStableBeamStartTime
585  << " ( " << boost::posix_time::to_iso_extended_string( cond::time::to_boost( beforeStableBeamStartTime ) )
586  << " ); from " << m_name << "::getNewObjects";
587  m_to_transfer.push_back( std::make_pair( new LHCInfo(), afterPreviousFillEndTime ) );
588  } else {
589  //the current fill cannot start before the previous one!
590  edm::LogError( m_name ) << "WRONG DATA! In the previous fill number " << previousFillNumber
591  << " beams were dumped at timestamp " << boost::posix_time::to_iso_extended_string( cond::time::to_boost( previousFillEndTime ) )
592  << ", which is not before the timestamp " << boost::posix_time::to_iso_extended_string( cond::time::to_boost( stableBeamStartTime ) )
593  << " when current fill number " << currentFill
594  << " entered stable beams. EXITING. from " << m_name << "::getNewObjects";
595  return;
596  }
597  //construct an instance of LHCInfo and set its values
598  LHCInfo *lhcInfo = new LHCInfo( currentFill );
599  lhcInfo->setInfo( const_cast<unsigned short const &>( bunches1 )
600  , const_cast<unsigned short const &>( bunches2 )
601  , const_cast<unsigned short const &>( collidingBunches )
602  , const_cast<unsigned short const &>( targetBunches )
603  , const_cast<LHCInfo::FillTypeId const &>( fillType )
604  , const_cast<LHCInfo::ParticleTypeId const &>( particleType1 )
605  , const_cast<LHCInfo::ParticleTypeId const &>( particleType2 )
606  , const_cast<float const &>( crossingAngle )
607  , const_cast<float const &>( betastar )
608  , const_cast<float const &>( intensityBeam1 )
609  , const_cast<float const &>( intensityBeam2 )
610  , const_cast<float const &>( energy )
611  , const_cast<float const &>( delivLumi )
612  , const_cast<float const &>( recLumi )
613  , const_cast<cond::Time_t const &>( creationTime )
614  , const_cast<cond::Time_t const &>( stableBeamStartTime )
615  , const_cast<cond::Time_t const &>( beamDumpTime )
616  , const_cast<std::string const &>( injectionScheme )
617  , const_cast<std::vector<float> const &>( lumiPerBX )
618  , const_cast<std::string const &>( lhcState )
619  , const_cast<std::string const &>( lhcComment )
620  , const_cast<std::string const &>( ctppsStatus )
621  , const_cast<unsigned int const &>( lumiSection )
622  , const_cast<std::vector<float> const &>( beam1VC )
623  , const_cast<std::vector<float> const &>( beam2VC )
624  , const_cast<std::vector<float> const &>( beam1RF )
625  , const_cast<std::vector<float> const &>( beam2RF )
626  , const_cast<std::bitset<LHCInfo::bunchSlots+1> const &>( bunchConfiguration1 )
627  , const_cast<std::bitset<LHCInfo::bunchSlots+1> const &>( bunchConfiguration2 ) );
628  //store this payload
629  m_to_transfer.push_back( std::make_pair( (LHCInfo*) lhcInfo, stableBeamStartTime ) );
630  edm::LogInfo( m_name ) << "The new payload to be inserted into tag " << tagInfo().name
631  << " with validity " << stableBeamStartTime
632  << " ( " << boost::posix_time::to_iso_extended_string( cond::time::to_boost( stableBeamStartTime ) )
633  << " ) has values:\n" << *lhcInfo
634  << "from " << m_name << "::getNewObjects";
635  //add log information
636  ss << " fill = " << currentFill
637  << ";\tinjection scheme: " << injectionScheme
638  << ";\tstart time: "
639  << boost::posix_time::to_iso_extended_string( stableBeamStartTimeStamp.time() )
640  << ";\tend time: "
641  << boost::posix_time::to_iso_extended_string( beamDumpTimeStamp.time() )
642  << "." << std::endl;
643  //prepare variables for next iteration
644  previousFillNumber = currentFill;
645  previousFillEndTime = beamDumpTime;
646  }
647 
648  //commit the transaction against the fill logging schema
649  session.transaction().commit();
650  //close the session
651  session.close();
652  //store log information
653  m_userTextLog = ss.str();
654  edm::LogInfo( m_name ) << "Transferring " << m_to_transfer.size() << " payload(s); from " << m_name << "::getNewObjects";
655  }
656 
657 
659  return m_name;
660 }
size
Write out results.
edm::ErrorSummaryEntry Error
void start(bool readOnly=true)
Definition: Session.cc:22
size_t size
Definition: Types.h:77
cond::Time_t pack(cond::UnpackedTime iValue)
void setInfo(unsigned short const &bunches1, unsigned short const &bunches2, unsigned short const &collidingBunches, unsigned short const &targetBunches, FillTypeId const &fillType, ParticleTypeId const &particleType1, ParticleTypeId const &particleType2, float const &angle, float const &beta, float const &intensity1, float const &intensity2, float const &energy, float const &delivLumi, float const &recLumi, cond::Time_t const &createTime, cond::Time_t const &beginTime, cond::Time_t const &endTime, std::string const &scheme, std::vector< float > const &lumiPerBX, std::string const &lhcState, std::string const &lhcComment, std::string const &ctppsStatus, unsigned int const &lumiSection, std::vector< float > const &beam1VC, std::vector< float > const &beam2VC, std::vector< float > const &beam1RF, std::vector< float > const &beam2RF, std::bitset< bunchSlots+1 > const &bunchConf1, std::bitset< bunchSlots+1 > const &bunchConf2)
Definition: LHCInfo.cc:390
Transaction & transaction()
Definition: Session.cc:66
std::string name
Definition: Types.h:73
U second(std::pair< T, U > const &p)
cond::ValidityInterval lastInterval
Definition: Types.h:75
unsigned long long Time_t
Definition: Time.h:16
coral::ISchema & nominalSchema()
Definition: Session.cc:233
Session createSession(const std::string &connectionString, bool writeCapable=false)
void setMessageVerbosity(coral::MsgLevel level)
Time_t from_boost(boost::posix_time::ptime bt)
ParticleType
Definition: LHCInfo.h:15
static size_t const availableBunchSlots
Definition: LHCInfo.h:32
std::string lastPayloadToken
Definition: Types.h:76
coral::ISessionProxy & coralSession()
Definition: Session.cc:228
std::string id() const override
void setAuthenticationPath(const std::string &p)
const bool Debug
std::string token
Definition: Types.h:74
boost::posix_time::ptime to_boost(Time_t iValue)
FillType
Definition: LHCInfo.h:14
cond::TagInfo_t const & tagInfo() const
cond::UnpackedTime unpack(cond::Time_t iValue)
LHCInfoPopConSourceHandler(const edm::ParameterSet &pset)