26#include "GaudiKernel/IToolSvc.h"
32#include "G4Version.hh"
42using CLHEP::Hep3Vector;
71 if (first->position().mag2() < second->position().mag2())
return true;
73 }
else if (
type ==
R) {
74 if (first->position().perp2() < second->position().perp2())
return true;
76 }
else if (
type ==
Z) {
77 if (first->position().z() < second->position().z())
return true;
90 std::vector< std::string >::const_iterator nameiter;
99 if (library ==
nullptr) {
100 ATH_MSG_WARNING (
"Library structure file " << (*nameiter) <<
" doesn't describe a valid library" );
104 std::stringstream location;
111 ATH_MSG_VERBOSE (
"Library named " << (*nameiter)+
".root" <<
"is stored at the location " << location.str() );
116 ATH_MSG_ERROR (
"No valid library structure files found. Further execution is pointless." );
117 return StatusCode::FAILURE;
120 ATH_MSG_DEBUG (
"LArG4GenShowerLib " << this->name() <<
" initialized" );
122 return StatusCode::SUCCESS;
132 return StatusCode::FAILURE;
137 double fraction = eventStepsES->
invalid_energy/theParticle->momentum().e();
139 ATH_MSG_WARNING (
"Shower deposited too much energy outside the calorimeter region (" << (
int)(fraction*100) <<
"%), ignoring" );
141 return StatusCode::SUCCESS;
148 if (eventStepsES->
size()>500) {
155 for (ShowerLib::StepInfoList::const_iterator iter = eventSteps->begin();iter != eventSteps->end();++iter) {
156 etot += (*iter)->dep();
159 std::stringstream location;
161 location << eventStepsES->
detector <<
"/" << theParticle->pdg_id();
166 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin());i != eventSteps->end(); ++i) {
171 return StatusCode::SUCCESS;
174 Hep3Vector origin(theParticle->production_vertex()->position().x(),
175 theParticle->production_vertex()->position().y(),
176 theParticle->production_vertex()->position().z());
180 Hep3Vector DirectionShower(theParticle->momentum().px(),
181 theParticle->momentum().py(),
182 theParticle->momentum().pz());
183 DirectionShower /= DirectionShower.mag();
184 Hep3Vector OrthoShower = DirectionShower.orthogonal();
185 OrthoShower /= OrthoShower.mag();
186 Hep3Vector CrossShower = DirectionShower.cross(OrthoShower);
187 CrossShower /= CrossShower.mag();
190 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin()); i != eventSteps->end(); ++i) {
191 Hep3Vector HitDir = (*i)->position() - origin;
193 (*i)->setX(HitDir*OrthoShower);
194 (*i)->setY(HitDir*CrossShower);
195 (*i)->setZ(HitDir*DirectionShower);
202 ATH_MSG_VERBOSE (
"Size after clusterization: " << eventSteps->size() );
204 if (eventSteps->size() > 10) {
209 double maxZ = 0, maxR = 0;
216 ShowerLib::StepInfoList::const_iterator iter = eventSteps->begin();
217 for (;(iter != eventSteps->end()) && (edep < containmentEnergy);++iter) {
218 edep += (*iter)->dep();
219 maxR = (*iter)->position().r();
225 iter = eventSteps->begin();
226 for (;(iter != eventSteps->end()) && (edep < containmentEnergy);++iter) {
227 edep += (*iter)->dep();
228 maxZ = (*iter)->position().z();
235 for (ShowerLib::StepInfoList::iterator i(eventSteps->begin());i != eventSteps->end(); ++i) {
244 if ((*
m_libraries.find(location.str())).second->storeShower(std::move(theParticle), shower)) {
247 ATH_MSG_WARNING (
"Wasn't able to store shower (" << location.str() <<
")" );
253 return StatusCode::SUCCESS;
259 if (
evtStore()->retrieve(mcEvent,
"GEN_EVENT").isFailure())
return nullptr;
264 return !mcEvent->
at(0)->particles().empty() ? mcEvent->
at(0)->particles().back() :
nullptr;
266 return ( * (* mcEvent->
begin())->particles_end());
275 if (
evtStore()->retrieve(eventStepsES,
"EventSteps").isFailure())
return nullptr;
288 rez->push_back(copy);
298 const double dsame = 1.;
310 if ( (i1)->diff2(*i2) < dsame) {
325 typedef std::multimap<double,Dist> distMap;
330 for (ShowerLib::StepInfoList::iterator i_h1(stepinfo->begin()); i_h1 != stepinfo->end(); ++i_h1) {
332 for (ShowerLib::StepInfoList::reverse_iterator i_h2(stepinfo->rbegin()); (*i_h2) != (*i_h1); ++i_h2) {
333 distances.insert(distMap::value_type((*i_h1)->diff2(**i_h2),
Dist(*i_h1, *i_h2)));
339 int cursize = stepinfo->size();
342 while (cursize > 1) {
343 distMap::iterator iter = distances.begin();
344 while ( !( (*iter).second.h1->valid() && (*iter).second.h2->valid() ) ) {
345 distances.erase(iter++);
348 if ((*iter).first > rmin )
break;
352 for (ShowerLib::StepInfoList::iterator i_h1(stepinfo->begin()); i_h1 != stepinfo->end(); ++i_h1) {
353 if ((*i_h1)->valid()){
354 distances.insert(distMap::value_type((*i_h1)->diff2(*mergedHit),
Dist(*i_h1, mergedHit)));
358 stepinfo->push_back(mergedHit);
363 for (ShowerLib::StepInfoList::iterator i = stepinfo->begin(); i != stepinfo->end();) {
368 i = stepinfo->erase(i);
379 for (ShowerLib::StepInfoList::const_iterator i(stepinfo->begin()); i != stepinfo->end(); ++i) {
385 ShowerLib::StepInfoList::iterator iterCut;
387 for (iterCut = stepinfo->begin(); (iterCut != stepinfo->end()) && (((*iterCut)->position().mag2() <
m_maxDistance) || (rsum < minEnergy));++iterCut) {
388 rsum += (*iterCut)->dep();
391 if (iterCut == stepinfo->end()) {
395 stepinfo->erase(iterCut,stepinfo->end());
401 const double inv_rsum = 1. / rsum;
402 for (iterCut = stepinfo->begin(); iterCut != stepinfo->end(); ++iterCut){
403 (*iterCut)->setE((*iterCut)->dep() * etot*inv_rsum);
410 char* atlasProject = getenv(
"AtlasProject");
411 char* atlasVersion = getenv(
"AtlasVersion");
412 std::string atlasReleaseTag = (atlasProject? std::string(atlasProject)+std::string(
"-") : std::string(
"Unknown-"));
414 atlasReleaseTag += std::string(atlasVersion);
417 atlasReleaseTag += std::string(
"Unknown");
420 libMap::iterator itr;
423 (*itr).second->release(atlasReleaseTag);
427 (*itr).second->geometry(geoModelSvc->atlasVersion());
433 std::string g4Version = G4Version;
434 size_t pos = g4Version.find(
"$Name: ");
435 if (pos != std::string::npos) {
436 g4Version.erase(pos, 7);
438 pos = g4Version.find(
" $");
439 if (pos != std::string::npos) {
440 g4Version.erase(pos, 2);
442 (*itr).second->geantVersion(g4Version);
453 libMap::iterator itr;
455 ATH_MSG_DEBUG (
"Writing shower library to file " << (*itr).first );
457 TFile libr((*itr).first.c_str(),
"RECREATE");
459 if (!(*itr).second->writeToROOT(&libr)) {
460 ATH_MSG_ERROR (
"Wasn't able to write " << (*itr).first <<
". Probably empty lib." );
466 ATH_MSG_DEBUG (
"********Statistics for GenShowerLib********" );
471 ATH_MSG_DEBUG (
"*******************************************" );
472 std::stringstream
ss((*itr).second->statistics());
473 for(std::string line; std::getline(
ss,line);)
477 ATH_MSG_DEBUG (
"*******************************************" );
482 return StatusCode::SUCCESS;
487 double&
weights,
double& xavfra,
double& yavfra,
double& ravfra)
490 double xav(0.), yav(0.), xav2(0.), yav2(0.);
493 escal += (*i)->dep();
494 xav += (*i)->x()*(*i)->dep();
495 yav += (*i)->y()*(*i)->dep();
496 xav2 += (*i)->x()*(*i)->x()*(*i)->dep();
497 yav2 += (*i)->y()*(*i)->y()*(*i)->dep();
501 const double inv_escal = escal == 0 ? 1 : 1. / escal;
502 xavfra = xav*inv_escal;
503 yavfra = yav*inv_escal;
505 ravfra = std::sqrt(std::abs((xav2*inv_escal-xavfra*xavfra) +
506 (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
virtual StatusCode execute() override
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)
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
const GenParticle * ConstGenParticlePtr
IShowerLib * iterateStruct(const std::string &fname)
std::list< StepInfo * > StepInfoList