26#include "GaudiKernel/IToolSvc.h"
32#include "G4Version.hh"
41using CLHEP::Hep3Vector;
70 if (first->position().mag2() < second->position().mag2())
return true;
72 }
else if (
type ==
R) {
73 if (first->position().perp2() < second->position().perp2())
return true;
75 }
else if (
type ==
Z) {
76 if (first->position().z() < second->position().z())
return true;
89 std::vector< std::string >::const_iterator nameiter;
98 if (library ==
nullptr) {
99 ATH_MSG_WARNING (
"Library structure file " << (*nameiter) <<
" doesn't describe a valid library" );
103 std::stringstream location;
110 ATH_MSG_VERBOSE (
"Library named " << (*nameiter)+
".root" <<
"is stored at the location " << location.str() );
115 ATH_MSG_ERROR (
"No valid library structure files found. Further execution is pointless." );
116 return StatusCode::FAILURE;
119 ATH_MSG_DEBUG (
"LArG4GenShowerLib " << this->name() <<
" initialized" );
121 return StatusCode::SUCCESS;
131 return StatusCode::FAILURE;
136 double fraction = eventStepsES->
invalid_energy/theParticle->momentum().e();
138 ATH_MSG_WARNING (
"Shower deposited too much energy outside the calorimeter region (" << (
int)(fraction*100) <<
"%), ignoring" );
140 return StatusCode::SUCCESS;
147 if (eventStepsES->
size()>500) {
154 for (ShowerLib::StepInfoList::const_iterator iter = eventSteps->begin();iter != eventSteps->end();++iter) {
155 etot += (*iter)->dep();
158 std::stringstream location;
160 location << eventStepsES->
detector <<
"/" << theParticle->pdg_id();
165 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin());i != eventSteps->end(); ++i) {
170 return StatusCode::SUCCESS;
173 Hep3Vector origin(theParticle->production_vertex()->position().x(),
174 theParticle->production_vertex()->position().y(),
175 theParticle->production_vertex()->position().z());
179 Hep3Vector DirectionShower(theParticle->momentum().px(),
180 theParticle->momentum().py(),
181 theParticle->momentum().pz());
182 DirectionShower /= DirectionShower.mag();
183 Hep3Vector OrthoShower = DirectionShower.orthogonal();
184 OrthoShower /= OrthoShower.mag();
185 Hep3Vector CrossShower = DirectionShower.cross(OrthoShower);
186 CrossShower /= CrossShower.mag();
189 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin()); i != eventSteps->end(); ++i) {
190 Hep3Vector HitDir = (*i)->position() - origin;
192 (*i)->setX(HitDir*OrthoShower);
193 (*i)->setY(HitDir*CrossShower);
194 (*i)->setZ(HitDir*DirectionShower);
201 ATH_MSG_VERBOSE (
"Size after clusterization: " << eventSteps->size() );
203 if (eventSteps->size() > 10) {
208 double maxZ = 0, maxR = 0;
215 ShowerLib::StepInfoList::const_iterator iter = eventSteps->begin();
216 for (;(iter != eventSteps->end()) && (edep < containmentEnergy);++iter) {
217 edep += (*iter)->dep();
218 maxR = (*iter)->position().r();
224 iter = eventSteps->begin();
225 for (;(iter != eventSteps->end()) && (edep < containmentEnergy);++iter) {
226 edep += (*iter)->dep();
227 maxZ = (*iter)->position().z();
234 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin());i != eventSteps->end(); ++i) {
243 if ((*
m_libraries.find(location.str())).second->storeShower(std::move(theParticle), shower)) {
246 ATH_MSG_WARNING (
"Wasn't able to store shower (" << location.str() <<
")" );
252 return StatusCode::SUCCESS;
258 if (
evtStore()->retrieve(mcEvent,
"GEN_EVENT").isFailure())
return nullptr;
262 return !mcEvent->
at(0)->particles().empty() ? mcEvent->
at(0)->particles().back() :
nullptr;
270 if (
evtStore()->retrieve(eventStepsES,
"EventSteps").isFailure())
return nullptr;
283 rez->push_back(copy);
293 const double dsame = 1.;
305 if ( (i1)->diff2(*i2) < dsame) {
320 typedef std::multimap<double,Dist> distMap;
325 for (ShowerLib::StepInfoList::iterator i_h1(stepinfo->begin()); i_h1 != stepinfo->end(); ++i_h1) {
327 for (ShowerLib::StepInfoList::reverse_iterator i_h2(stepinfo->rbegin()); (*i_h2) != (*i_h1); ++i_h2) {
328 distances.insert(distMap::value_type((*i_h1)->diff2(**i_h2),
Dist(*i_h1, *i_h2)));
334 int cursize = stepinfo->size();
337 while (cursize > 1) {
338 distMap::iterator iter = distances.begin();
339 while ( !( (*iter).second.h1->valid() && (*iter).second.h2->valid() ) ) {
340 distances.erase(iter++);
343 if ((*iter).first > rmin )
break;
347 for (ShowerLib::StepInfoList::iterator i_h1(stepinfo->begin()); i_h1 != stepinfo->end(); ++i_h1) {
348 if ((*i_h1)->valid()){
349 distances.insert(distMap::value_type((*i_h1)->diff2(*mergedHit),
Dist(*i_h1, mergedHit)));
353 stepinfo->push_back(mergedHit);
358 for (ShowerLib::StepInfoList::iterator i = stepinfo->begin(); i != stepinfo->end();) {
363 i = stepinfo->erase(i);
374 for (ShowerLib::StepInfoList::const_iterator i(stepinfo->begin()); i != stepinfo->end(); ++i) {
380 ShowerLib::StepInfoList::iterator iterCut;
382 for (iterCut = stepinfo->begin(); (iterCut != stepinfo->end()) && (((*iterCut)->position().mag2() <
m_maxDistance) || (rsum < minEnergy));++iterCut) {
383 rsum += (*iterCut)->dep();
386 if (iterCut == stepinfo->end()) {
390 stepinfo->erase(iterCut,stepinfo->end());
396 const double inv_rsum = 1. / rsum;
397 for (iterCut = stepinfo->begin(); iterCut != stepinfo->end(); ++iterCut){
398 (*iterCut)->setE((*iterCut)->dep() * etot*inv_rsum);
405 char* atlasProject = getenv(
"AtlasProject");
406 char* atlasVersion = getenv(
"AtlasVersion");
407 std::string atlasReleaseTag = (atlasProject? std::string(atlasProject)+std::string(
"-") : std::string(
"Unknown-"));
409 atlasReleaseTag += std::string(atlasVersion);
412 atlasReleaseTag += std::string(
"Unknown");
417 const auto & version = geoModelSvc->atlasVersion();
420 std::string g4Version = G4Version;
421 size_t pos = g4Version.find(
"$Name: ");
422 if (pos != std::string::npos) {
423 g4Version.erase(pos, 7);
425 pos = g4Version.find(
" $");
426 if (pos != std::string::npos) {
427 g4Version.erase(pos, 2);
429 libMap::iterator itr;
432 (*itr).second->release(atlasReleaseTag);
433 (*itr).second->geometry(version);
437 (*itr).second->geantVersion(g4Version);
448 libMap::iterator itr;
450 ATH_MSG_DEBUG (
"Writing shower library to file " << (*itr).first );
452 TFile libr((*itr).first.c_str(),
"RECREATE");
454 if (!(*itr).second->writeToROOT(&libr)) {
455 ATH_MSG_ERROR (
"Wasn't able to write " << (*itr).first <<
". Probably empty lib." );
461 ATH_MSG_DEBUG (
"********Statistics for GenShowerLib********" );
466 ATH_MSG_DEBUG (
"*******************************************" );
467 std::stringstream
ss((*itr).second->statistics());
468 for(std::string line; std::getline(
ss,line);)
472 ATH_MSG_DEBUG (
"*******************************************" );
477 return StatusCode::SUCCESS;
482 double&
weights,
double& xavfra,
double& yavfra,
double& ravfra)
485 double xav(0.), yav(0.), xav2(0.), yav2(0.);
488 escal += (*i)->dep();
489 xav += (*i)->x()*(*i)->dep();
490 yav += (*i)->y()*(*i)->dep();
491 xav2 += (*i)->x()*(*i)->x()*(*i)->dep();
492 yav2 += (*i)->y()*(*i)->y()*(*i)->dep();
496 const double inv_escal = escal == 0 ? 1 : 1. / escal;
497 xavfra = xav*inv_escal;
498 yavfra = yav*inv_escal;
500 ravfra = std::sqrt(std::abs((xav2*inv_escal-xavfra*xavfra) +
501 (yav2*inv_escal-yavfra*yavfra)));
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
ServiceHandle< StoreGateSvc > & evtStore()
DataModel_detail::const_iterator< DataVector > const_iterator
const T * at(size_type n) const
Access an element, as an rvalue.
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
ShowerLib::StepInfo * merge()
Dist(ShowerLib::StepInfo *the_h1, ShowerLib::StepInfo *the_h2)
virtual StatusCode initialize() override
StringArrayProperty m_lib_struct_files
void addingTagsToLibrary()
adding tag information (release, detector description, ...) to library comment
libMap m_libraries
pointer to shower library
DoubleProperty m_containmentEnergy
property, see LArG4GenShowerLib::LArG4GenShowerLib
DoubleProperty m_energyFraction
property, see LArG4GenShowerLib::LArG4GenShowerLib
StringProperty m_physicslist_name
std::map< ShowerLib::IShowerLib *, int > m_stat_lib_notsaved
void truncate(ShowerLib::StepInfoList *stepinfo)
std::map< ShowerLib::IShowerLib *, int > m_stat_lib_saved
void clusterize(ShowerLib::StepInfoList *stepinfo)
DoubleProperty m_maxDistance
property, see LArG4GenShowerLib::LArG4GenShowerLib
void calculateMoments(const ShowerLib::StepInfoCollection &eventSteps, double &weights, double &xavfra, double &yavfra, double &ravfra)
calculate moments from StepInfoCollection
DoubleProperty m_minEnergy
property, see LArG4GenShowerLib::LArG4GenShowerLib
ShowerLib::StepInfoList * copyStepInfo(const ShowerLib::StepInfoCollection *stepinfo)
virtual StatusCode execute(const EventContext &ctx) override
Execute method.
DoubleProperty m_maxRadius
property, see LArG4GenShowerLib::LArG4GenShowerLib
HepMC::ConstGenParticlePtr getParticleFromMC()
return first MC truth particle for event
virtual StatusCode finalize() override
const ShowerLib::StepInfoCollection * getStepInfo()
ShowerLib::StepInfoList * copyStepInfoZeroCleanup(const ShowerLib::StepInfoCollection *stepinfo)
libMap m_libraries_by_filename
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
Class for shower library shower lib interface.
virtual int particle_id() const
get particle tag
virtual const std::string detector() const
get detector tag
Class for shower library shower.
void setZSize(const float zsize)
void setRSize(const float rsize)
Class for collection of StepInfo class (G4 hits).
Class to collect information about G4 steps.
bool sortFunction(const ShowerLib::StepInfo *first, const ShowerLib::StepInfo *second, const CompareType type) const
stepInfoDistCompare(const CompareType type)
bool operator()(const ShowerLib::StepInfo *first, const ShowerLib::StepInfo *second) const
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
IShowerLib * iterateStruct(const std::string &fname)
std::list< StepInfo * > StepInfoList