9#include <b2bii/modules/B2BIIMdstInput/B2BIIConvertBeamParamsModule.h>
10#include <belle_legacy/benergy/BeamEnergy.h>
11#include <belle_legacy/ip/IpProfile.h>
12#include <belle_legacy/panther/panther.h>
13#include <belle_legacy/tables/belletdf.h>
15#include <framework/database/Database.h>
16#include <framework/database/EventDependency.h>
17#include <framework/database/IntervalOfValidity.h>
18#include <framework/dbobjects/BeamParameters.h>
19#include <framework/geometry/B2Vector3.h>
20#include <framework/utilities/FileSystem.h>
21#include <mdst/dbobjects/BeamSpot.h>
22#include <mdst/dbobjects/CollisionAxisCMS.h>
23#include <mdst/dbobjects/CollisionBoostVector.h>
24#include <mdst/dbobjects/CollisionInvariantMass.h>
26#include <Math/VectorUtil.h>
35 int check_beginrun(
bool) {
return 0; }
42 return TVector3(point.x(), point.y(), point.z());
45 TMatrixDSym CLHEPtoROOT(
const HepSymMatrix& matrix)
47 TMatrixDSym result(matrix.num_col());
48 for (
int i = 0; i < matrix.num_row(); ++i) {
49 for (
int j = 0; j < matrix.num_col(); ++j) {
50 result(i, j) = matrix(i + 1, j + 1);
76 "Store the CollisionAxisCMS payloads in the localDB",
95 Belle::Belle_runhead_Manager& runMgr = Belle::Belle_runhead_Manager::get_manager();
97 Belle::Belle_runhead& runhead = runMgr[0];
99 runhead.ExpNo(
m_event->getExperiment());
100 runhead.RunNo(
m_event->getRun());
101 B2INFO(
"Obtaining values for exp " <<
m_event->getExperiment() <<
", run " <<
m_event->getRun());
103 Belle::BeamEnergy::flag_database(0);
104 Belle::IpProfile::begin_run();
105 Belle::Ip_profile_general_Manager& ipMgr = Belle::Ip_profile_general_Manager::get_manager();
107 if (!Belle::BeamEnergy::usable()) {
110 B2ERROR(
"No BeamEnergy for exp " <<
m_event->getExperiment() <<
", run " <<
m_event->getRun());
113 file <<
m_event->getExperiment() <<
"," <<
m_event->getRun() << std::endl;
118 const double Eher = Belle::BeamEnergy::E_HER();
119 const double Eler = Belle::BeamEnergy::E_LER();
120 const double crossingAngle = Belle::BeamEnergy::Cross_angle();
121 const double angleLer = M_PI;
122 const double angleHer = crossingAngle;
129 std::vector<double> covarianceHer = {0.00513 * 0.00513};
130 std::vector<double> covarianceLer = {0.002375 * 0.002375};
135 beamParams.
setLER(Eler, angleLer, 0, covarianceLer);
136 beamParams.
setHER(Eher, angleHer, 0, covarianceHer);
153 ROOT::Math::PxPyPzEVector momentumHER = beamParams.
getHER();
154 ROOT::Math::PxPyPzEVector cms = momentumHER + beamParams.
getLER();
155 ROOT::Math::XYZVector boost = -cms.BoostToCM();
156 ROOT::Math::VectorUtil::boost(momentumHER, boost);
157 double angleXZ = std::atan(momentumHER.X() / momentumHER.Z());
158 double angleYZ = std::atan(momentumHER.Y() / momentumHER.Z());
159 collisionAxisCMS.
setAngles(angleXZ, angleYZ, TMatrixTSym<double>(2));
160 collisionAxisCMS.
setSpread(TMatrixTSym<double>(2), 0, 0, 0);
163 collisionInvM.
setMass(cms.M(), 0.0, 0.0);
168 "CollisionAxisCMS", &collisionAxisCMS, iov);
172 "CollisionBoostVector", &collisionBoostVector, iov);
176 "CollisionInvariantMass", &collisionInvM, iov);
180 if (!Belle::IpProfile::usable()) {
183 B2ERROR(
"No IpProfile for exp " <<
m_event->getExperiment() <<
", run " <<
m_event->getRun());
185 std::ofstream file(
m_missingIp.c_str(), std::ios::app);
186 file <<
m_event->getExperiment() <<
"," <<
m_event->getRun() << std::endl;
188 std::vector<double> covariance;
189 beamParams.
setVertex(ROOT::Math::XYZVector(
190 std::numeric_limits<double>::quiet_NaN(),
191 std::numeric_limits<double>::quiet_NaN(),
192 std::numeric_limits<double>::quiet_NaN()), covariance);
198 TVector3(std::numeric_limits<double>::quiet_NaN(),
199 std::numeric_limits<double>::quiet_NaN(),
200 std::numeric_limits<double>::quiet_NaN()
201 ), TMatrixTSym<double>(3)
205 B2INFO(
"No IpProfile, created BeamSpot Payload");
210 int nbins = std::max(ipMgr.count() - 1, 1);
211 B2INFO(
"exp " <<
m_event->getExperiment() <<
", run " <<
m_event->getRun() <<
": " << nbins <<
" IpProfile bins");
214 std::list<BeamParameters> beamparamsList;
215 std::list<BeamSpot> beamspotList;
217 std::unique_ptr<EventDependency> beamparams;
218 std::unique_ptr<EventDependency> beamspots;
220 Belle::Belle_event_Manager& evtMgr = Belle::Belle_event_Manager::get_manager();
221 if (!evtMgr.count()) evtMgr.add();
222 for (
int i = 0; i < nbins; ++i) {
223 int evtNr = i * BIN_EVENTS;
225 Belle::Belle_event& evt = evtMgr[0];
227 evt.ExpNo(
m_event->getExperiment());
230 Belle::IpProfile::set_evtbin_number();
231 B2ASSERT(
"something wrong: " << Belle::IpProfile::EvtBinNo() <<
"!=" << i, Belle::IpProfile::EvtBinNo() == i);
234 HepPoint3D ip = Belle::IpProfile::e_position();
235 HepSymMatrix ipErr = Belle::IpProfile::e_position_err();
237 beamParams.
setVertex(ROOT::Math::XYZVector(ip.x(), ip.y(), ip.z()));
239 beamparamsList.emplace_back(beamParams);
242 beamSpot.
setIP(CLHEPtoROOT(ip), CLHEPtoROOT(ipErr));
243 beamspotList.emplace_back(beamSpot);
246 beamparams = std::make_unique<EventDependency>(&beamparamsList.back(),
false);
247 beamspots = std::make_unique<EventDependency>(&beamspotList.back(),
false);
249 beamparams->add(evtNr, &beamparamsList.back());
250 beamspots->add(evtNr, &beamspotList.back());
253 if (beamparamsList.size() < 1) {
254 B2ERROR(
"Something is wrong with exp " <<
m_event->getExperiment() <<
", run " <<
m_event->getRun() <<
": no bins found");
257 if (beamparamsList.size() == 1) {
263 B2INFO(
"Created event independent payload");
270 B2INFO(
"Created event dependent payload with " << beamparamsList.size() <<
" entries");
bool m_SmearDirection
Smear beam direction when generating initial events.
std::string m_missingBenergy
Where to store information about runs without beam energy information.
bool m_storeCollisionAxisCMS
Store the CollisionAxisCMS payloads in the local database.
std::string m_missingIp
Where to store information about runs without IP profile information.
bool m_storeCollisionInvariantMass
Store the CollisionInvariantMass payloads in the local database.
StoreObjPtr< EventMetaData > m_event
Event metadata.
bool m_SmearVertex
Smear vertex position when generating initial events.
bool m_storeBeamParameters
Store the BeamParameters payloads in the local database.
bool m_GenerateCMS
Generate events in CMS, not lab system.
bool m_storeBeamSpot
Store the BeamSpot payloads in the local database.
bool m_SmearEnergy
Smear energy when generating initial events.
bool m_storeCollisionBoostVector
Store the CollisionBoostVector payloads in the local database.
int m_mcFlag
mcFlag to use when getting belle database content
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
void setHER(double energy, double angleX, double angleY, const std::vector< double > &cov)
Set the HER FourVector and error matrix from beam energy, angle and covariance entries.
void setLER(double energy, double angleX, double angleY, const std::vector< double > &cov)
Set the LER FourVector and error matrix from beam energy, angle and covariance entries.
void setVertex(const ROOT::Math::XYZVector &vertex, const std::vector< double > &cov)
Set the vertex position and error matrix.
void setGenerationFlags(int flags) override
Set the generation flags to be used for event generation (ORed combination of EGenerationFlags).
void setCovVertex(const TMatrixDSym &cov)
Set the covariance matrix of the vertex position.
This class contains the beam spot position and size modeled as a gaussian distribution in space.
void setIP(const TVector3 &ipPosition, const TMatrixDSym &covariance)
Set the IP position and its error matrix.
This class contains the measured values of the orientation of the collision axis in the CM system obt...
void setSpread(const TMatrixDSym &spreadCovariance, double spreadXZunc, double spreadYZunc, double spreadPhiUnc)
Set spread covariance and uncertainties of the eigenvalues of this matrix.
void setAngles(double angleXZ, double angleYZ, const TMatrixDSym ¢erCovariance)
Set the central values and uncertainty of them.
This class contains the measured average boost vector vec(beta) = (beta_x, beta_y,...
void setBoost(const TVector3 &boost, const TMatrixDSym &covariance)
Set the boost vector and its error matrix.
This class contains the measured average center-of-mass energy, which is equal to the invariant mass ...
void setMass(double mass, double error, double spread)
Set the CMS energy and its uncertainty.
Helper class for locking a file.
bool lock(int timeout=300, bool ignoreErrors=false)
Try to lock the file.
A class that describes the interval of experiments/runs for which an object in the database is valid.
const ROOT::Math::PxPyPzEVector & getHER() const
Get 4vector of the high energy beam.
const ROOT::Math::PxPyPzEVector & getLER() const
Get 4vector of the low energy beam.
@ c_smearVertex
smear vertex
@ c_generateCMS
generate initial event in CMS instead of lab
@ c_smearBeamEnergy
smear energy of HER and LER (but not direction)
@ c_smearBeamDirection
smear direction of HER and LER (but not energy)
void initialize() override
Initialize phanter banks.
B2BIIConvertBeamParamsModule()
Create parameters.
void beginRun() override
Set run info in panther and load IPProfile/Benergy and convert the values to payloads.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
static Database & Instance()
Instance of a singleton Database.
bool storeData(const std::string &name, TObject *object, const IntervalOfValidity &iov)
Store an object in the database.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
B2Vector3< double > B2Vector3D
typedef for common usage with double
Abstract base class for different kinds of events.