Import Geant4 10.4.0.beta source tree

This commit is contained in:
Gabriele Cosmo
2017-06-30 10:49:55 +02:00
parent 3a5407696b
commit 1a1316fea4
2180 changed files with 237880 additions and 59109 deletions
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4RandomDirection.hh 96120 2016-03-16 20:29:21Z gcosmo $
// $Id: G4RandomDirection.hh 103484 2017-04-11 08:31:44Z gcosmo $
//
//
// ------------------------------------------------------------
@@ -39,6 +39,7 @@
// providing more performant results.
// History:
// 06.04.17 E. Tcherniaev, added G4RandomDirection(cosTheta)
// 15.03.16 E. Tcherniaev, removed unnecessary if and unit()
// 18.03.08 V. Grichine, unit radius sphere surface based algorithm
// ~ 2007 M. Kossov, algorithm based on 8 Quadrants technique
@@ -61,4 +62,12 @@ inline G4ThreeVector G4RandomDirection()
return G4ThreeVector(rho*std::cos(phi), rho*std::sin(phi), z);
}
inline G4ThreeVector G4RandomDirection(G4double cosTheta)
{
G4double z = (1. - cosTheta)*G4UniformRand() + cosTheta;
G4double rho = std::sqrt((1.+z)*(1.-z));
G4double phi = CLHEP::twopi*G4UniformRand();
return G4ThreeVector(rho*std::cos(phi), rho*std::sin(phi), z);
}
#endif /* G4RANDOMDIR_HH */
+31 -2
View File
@@ -17,11 +17,40 @@ committal in the SVN repository !
* Reverse chronological order (last date on top), please *
----------------------------------------------------------
December 19, 2016 G.Cosmo (global-V10-02-34)
June 21, 2017 G.Cosmo (global-V10-03-08)
- Corrected TLS setup in tls.hh for gcc compiler.
April 20, 2017 G.Cosmo (global-V10-03-07)
- Removed obsolete internal definition of abs() in templates.hh.
- Code cleanup/formatting in recent G4cout* classes.
April 18, 2017 A.Dotti (global-V10-03-06)
- Fixed WIN compilation error.
April 18, 2017 A.Dotti (global-V10-03-05)
- Fixed compiler error for gcc < 5.
- Fixed compilation warning on Mac OS 10.11 clang-8.
April 18, 2017 A.Dotti (global-V10-03-04)
- New structure for G4coutDestination functionality with division
between sink and filter.
April 6, 2017 E.Tcherniaev (global-V10-03-03)
- Added function G4RandomDirection(G4double cosTheta) to G4RandomDirection.hh.
Addressing enhancement request #1961.
March 14, 2017 G.Cosmo (global-V10-03-02)
- Fix in destructor of G4ThreadLocalSingleton to destroy mutex as last
action. Fixes contention reported by Valgrind/DRD.
March 13, 2017 G.Cosmo (global-V10-03-01)
- G4TWorkspacePool: implemented CleanUpAndDestroyAllWorkspaces(),
and Recycle() to be called by worker threads.
December 19, 2016 G.Cosmo (global-V10-03-00)
- Removed obsolete utility class G4SubString used internally in G4String,
as no longer necessary and also including unnecessary and buggy operators.
Addressing problem report #1864.
- Updated date and version for 10.3.p01.
November 16, 2016 G.Cosmo (global-V10-02-33)
- Fixed compilation warning on MacOS Sierra in MT mode in function
@@ -0,0 +1,86 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4BuffercoutDestination.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// Class Description:
//
// A cout destination that buffers received stream in a buffer and
// dumps the buffer only when full.
// Optionally allows for an infinite buffer.
// ---------------- G4BuffercoutDestination ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4BUFFERCOUTDESTINATION_HH_
#define G4BUFFERCOUTDESTINATION_HH_
#include <sstream>
#include "G4coutDestination.hh"
class G4BuffercoutDestination : public G4coutDestination
{
public:
explicit G4BuffercoutDestination(size_t maxSize = 0);
virtual ~G4BuffercoutDestination();
virtual G4int ReceiveG4cout(const G4String& msg) override;
virtual G4int ReceiveG4cerr(const G4String& msg) override;
// Flush buffer to std output
virtual G4int FlushG4cout();
// Flush buffer to std error
virtual G4int FlushG4cerr();
// Flsuh both buffers
virtual void Finalize();
// Set maximum size of buffer, when buffer grows to specified size,
// it will trigger flush. Dimension in char
void SetMaxSize(size_t max) { m_maxSize = max; }
size_t GetMaxSize() const { return m_maxSize; }
size_t GetCurrentSizeOut() const { return m_currentSize_out; }
size_t GetCurrentSizeErr() const { return m_currentSize_err; }
protected:
void ResetCout();
void ResetCerr();
std::ostringstream m_buffer_out;
std::ostringstream m_buffer_err;
size_t m_currentSize_out;
size_t m_currentSize_err;
size_t m_maxSize;
};
#endif /* G4BUFFERCOUTDESTINATION_HH_ */
@@ -0,0 +1,74 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4FilecoutDestination.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// Class Description:
//
// Implements a cout destination to a file.
// ---------------- G4FilecoutDestination ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4FILECOUTDESTINATION_HH_
#define G4FILECOUTDESTINATION_HH_
#include <fstream>
#include <memory>
#include "G4coutDestination.hh"
class G4FilecoutDestination : public G4coutDestination
{
public:
explicit G4FilecoutDestination(const G4String& fname ,
std::ios_base::openmode mode = std::ios_base::app )
: m_name(fname), m_mode(mode), m_output(nullptr) {}
virtual ~G4FilecoutDestination();
void SetFileName(const G4String& fname) { m_name = fname; }
void Open(std::ios_base::openmode mode=std::ios_base::app);
// By default append to existing file
void Close();
virtual G4int ReceiveG4cout(const G4String& msg) override;
virtual G4int ReceiveG4cerr(const G4String& msg) override;
private:
G4String m_name;
std::ios_base::openmode m_mode;
std::unique_ptr<std::ofstream> m_output;
};
#endif /* G4FILECOUTDESTINATION_HH_ */
@@ -0,0 +1,57 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4LockcoutDestination.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// Class Description:
//
// Implements a output destination to std::cout / std::cerr with a
// mutex lock access to the shared resource.
// ---------------- G4LockcoutDestination ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4LOCKCOUTDESTINATION_HH_
#define G4LOCKCOUTDESTINATION_HH_
#include "G4coutDestination.hh"
class G4LockcoutDestination : public G4coutDestination
{
public:
G4LockcoutDestination() = default;
virtual ~G4LockcoutDestination();
virtual G4int ReceiveG4cout(const G4String& msg) override;
virtual G4int ReceiveG4cerr(const G4String& msg) override;
};
#endif /* G4LOCKCOUTDESTINATION_HH_ */
@@ -36,58 +36,80 @@
#ifndef G4MTcoutDestination_H
#define G4MTcoutDestination_H
#include "globals.hh"
#include "G4coutDestination.hh"
#include <iostream>
#include <sstream>
#include <fstream>
class G4MTcoutDestination : public G4coutDestination
#include "globals.hh"
#include "G4MulticoutDestination.hh"
#include "G4StateManager.hh"
class G4LockcoutDestination;
class G4MTcoutDestination : public G4MulticoutDestination
{
public:
G4MTcoutDestination(const G4int& threadId,
std::ostream& co=std::cout, std::ostream& ce=std::cerr);
virtual ~G4MTcoutDestination();
explicit G4MTcoutDestination(const G4int& threadId);
virtual ~G4MTcoutDestination();
virtual G4int ReceiveG4cout(const G4String&);
virtual G4int ReceiveG4cerr(const G4String&);
virtual void Reset();
void SetDefaultOutput( G4bool addMasterDestination = true ,
G4bool formatAlsoMaster = true );
void SetCoutFileName(const G4String& fileN = "G4cout.txt",
G4bool ifAppend = true);
void AddCoutFileName(const G4String& fileN = "G4cout.txt",
G4bool ifAppend = true);
void SetCerrFileName(const G4String& fileN = "G4cerr.txt",
G4bool ifAppend = true);
void AddCerrFileName(const G4String& fileN = "G4cerr.txt",
G4bool ifAppend = true);
void EnableBuffering(G4bool flag=true);
inline void SetPrefixString(const G4String& wd = "G4WT") { prefix = wd; }
void SetCoutFileName(const G4String& fileN = "G4cout.txt", G4bool ifAppend = true);
void SetCerrFileName(const G4String& fileN = "G4cerr.txt", G4bool ifAppend = true);
void EnableBuffering(G4bool flag=true);
void SetPrefixString(const G4String& wd = "G4WT");
void SetIgnoreCout(G4int tid = 0);
void SetIgnoreInit(G4bool val=true) { ignoreInit = val; }
G4String GetPrefixString() const { return prefix; }
G4String GetFullPrefixString() const {
inline void SetIgnoreInit(G4bool val=true) { ignoreInit = val; }
inline G4String GetPrefixString() const { return prefix; }
inline G4String GetFullPrefixString() const
{
std::stringstream os;
os<<prefix<<id;
return os.str();
}
protected:
void AddMasterOutput( G4bool formatAlsoMaster);
void HandleFileCout( G4String fileN, G4bool appendFlag,
G4bool suppressDefault);
void HandleFileCerr( G4String fileN, G4bool appendFlag,
G4bool suppressDefault);
private:
void CloseCoutFile();
void CloseCerrFile();
void DumpBuffer();
void DumpBuffer();
private:
std::ostream& finalcout;
std::ostream& finalcerr;
const G4int id;
G4bool useBuffer;
G4bool threadCoutToFile;
G4bool threadCerrToFile;
G4bool ignoreCout;
G4bool ignoreInit;
// Reference to the default destination
G4coutDestination* ref_defaultOut;
// Reference to the master destination
G4coutDestination* ref_masterOut;
G4bool masterDestinationFlag;
G4bool masterDestinationFmtFlag;
const G4int id;
G4bool useBuffer;
G4bool ignoreCout;
G4bool ignoreInit;
std::ostringstream cout_buffer;
std::ostringstream cerr_buffer;
std::ofstream coutFile;
std::ofstream cerrFile;
G4String prefix;
G4StateManager* stateMgr;
};
#endif
@@ -0,0 +1,60 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4MasterForwardcoutDestination.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// Class description:
//
// This class has the simple role to forward the stream of messages
// to the master thread in a MT application.
// Master thread should implment a G4coutDestination to receive the messages.
// Messages are serialized via a Mutex.
// ---------------- G4LockcoutDestination ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4MASTERFORWARDCOUTDESTINATION_HH_
#define G4MASTERFORWARDCOUTDESTINATION_HH_
#include <G4coutDestination.hh>
class G4MasterForwardcoutDestination : public G4coutDestination
{
public:
G4MasterForwardcoutDestination() = default;
virtual ~G4MasterForwardcoutDestination();
virtual G4int ReceiveG4cout(const G4String& msg) override;
virtual G4int ReceiveG4cerr(const G4String& msg) override;
};
#endif /* G4MASTERFORWARDCOUTDESTINATION_HH_ */
@@ -0,0 +1,96 @@
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4MulticoutDestination.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// G4MulticoutDestination.hh
//
// Class description:
//
// Extends G4coutDestination and allows multiple G4coutDestination objects
// to be chained in the same job.
// Note that formatters can be attached to this destination that will apply
// them before passing over the message to the child destination.
//
// Usage:
// User should create and set-up G4coutDestination objects the usual way
// these should then be added to an instance of this container class
// this class should then be added to the streaming. E.g.
// class MyCout1 : public G4coutDestination {};
// class MyCout2 : public G4coutDestination {};
// auto multi = new G4MulticoutDestination();
// multi->push_back( G4coutDestinationUPtr( new MyCout1 ) );
// multi->push_back( G4coutDestinationUPtr( new MyCout2 ) );
// G4coutbuf.SetDestination( multi ); // or G4cerrbuf
// ---------------- G4MulticoutDestination ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4MULTICOUTDESTINATION_HH_
#define G4MULTICOUTDESTINATION_HH_
#include <memory>
#include <vector>
#include "G4coutDestination.hh"
using G4coutDestinationUPtr = std::unique_ptr<G4coutDestination>;
using G4coutDestinationVector = std::vector<G4coutDestinationUPtr>;
class G4MulticoutDestination : public G4coutDestination,
public G4coutDestinationVector
{
public:
G4MulticoutDestination() = default;
virtual ~G4MulticoutDestination() {}
// Forward call to contained destination. Note that the message may have
// been modified by formatters attached to this
virtual G4int ReceiveG4cout(const G4String& msg) override
{
G4bool result = true;
std::for_each( begin(), end(),
[&](G4coutDestinationUPtr& e) { result &= (e->ReceiveG4cout_(msg)==0); }
);
return ( result ? 0 : -1);
}
virtual G4int ReceiveG4cerr(const G4String& msg) override
{
G4bool result = true;
std::for_each( begin(), end(),
[&](G4coutDestinationUPtr& e) { result &= (e->ReceiveG4cerr_(msg)==0); }
);
return ( result ? 0 : -1);
}
};
#endif /* G4MULTICOUTDESTINATION_HH_ */
+1 -2
View File
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4String.hh 102537 2017-02-08 14:02:58Z gcosmo $
// $Id: G4String.hh 102049 2016-12-19 09:04:20Z gcosmo $
//
//
//---------------------------------------------------------------
@@ -55,7 +55,6 @@
#endif
typedef std::string::size_type str_size;
typedef std::string G4SubString;
class G4String : public std::string
{
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4String.icc 102536 2017-02-08 13:53:34Z gcosmo $
// $Id: G4String.icc 102049 2016-12-19 09:04:20Z gcosmo $
//
//
//---------------------------------------------------------------
@@ -23,20 +23,26 @@
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id:$
//
// ------------------------------------------------------------
// GEANT 4 class header file
//
// Class Description:
//
// Create and hold a pointer to Workspace.
// This class holds a thread-private static instance
// of the template parameter workspace.
//
// The concrete implementation of workspace objects
// are responsible for instantiating a singleton instance
// of this pool.
//
// Recycling of this pool can enable reuse among different
// threads in task-based - or 'on-demand' - simulation.
// Create and hold a pointer to Workspace.
//
// This class holds a thread-private static instance
// of the template parameter workspace.
//
// Note:
// The concrete implementation of workspace objects
// are responsible for instantiating a singleton instance
// of this pool.
//
// This class will be extended to recycle workspaces.
// This can enable their reuse between different threads,
// in task-based - or 'on-demand' - simulation.
// ------------------------------------------------------------
#ifndef G4TWORKSPACEPOOL_HH
#define G4TWORKSPACEPOOL_HH
@@ -47,38 +53,38 @@
template<class T>
class G4TWorkspacePool
{
public:
// For use with simple MT mode - each thread gets a workspace and uses it until end
T* CreateWorkspace();
public:
inline T* CreateWorkspace();
// For use with simple MT mode - each thread gets a workspace
// and uses it until end
void CreateAndUseWorkspace(); // Create it (as above) and use it
inline void CreateAndUseWorkspace();
// Create it (as above) and use it
inline T* FindOrCreateWorkspace();
// For use with 'dynamic' model of threading - workspaces can be recycled
T* FindOrCreateWorkspace();
// Reuse an existing workspace - or create a new one if needed.
// This will never fail, except if system is out of resources
inline T* GetWorkspace() { return fMyWorkspace; }
// Give back the existing, active workspace for my thread / task
//The recycling logic of workspace needs to be implemented
//void Recycle( T * );
// Keep the unused Workspace - for recycling : To be implemented
inline void Recycle( T * myWrkSpace );
// Keep the unused Workspace - for recycling
//void CleanUpAndDestroyAllWorkspaces();
inline void CleanUpAndDestroyAllWorkspaces();
// To be called once at the end of the job
//void ReleaseAndDestroyWorkspace(T*);
// Destroy workspace - after releasing it
public:
public:
G4TWorkspacePool() {}
~G4TWorkspacePool() {}
private:
static G4ThreadLocal T* fMyWorkspace;
// The thread's workspace - if assigned.
private:
static G4ThreadLocal T* fMyWorkspace;
// The thread's workspace - if assigned
};
template<typename T> G4ThreadLocal T* G4TWorkspacePool<T>::fMyWorkspace=0;
@@ -87,17 +93,24 @@ template<class T>
T* G4TWorkspacePool<T>::CreateWorkspace()
{
T* wrk = 0;
if ( !fMyWorkspace ) {
if ( !fMyWorkspace )
{
wrk = new T;
if ( !wrk ) {
G4Exception("G4TWorspacePool<someType>::CreateWorkspace", "Workspace01",
FatalException, "Failed to create workspace.");
} else {
if ( !wrk )
{
G4Exception("G4TWorspacePool<someType>::CreateWorkspace",
"MemoryError", FatalException,
"Failed to create workspace.");
}
else
{
fMyWorkspace = wrk;
}
} else {
G4Exception("ParticlesWorspacePool::CreateWorkspace", "Workspace02",
FatalException,
}
else
{
G4Exception("ParticlesWorspacePool::CreateWorkspace",
"InvalidCondition", FatalException,
"Cannot create workspace twice for the same thread.");
wrk = fMyWorkspace;
}
@@ -114,7 +127,8 @@ template<class T>
T* G4TWorkspacePool<T>::FindOrCreateWorkspace()
{
T* wrk= fMyWorkspace;
if( !wrk ){
if( !wrk )
{
wrk= this->CreateWorkspace();
}
wrk->UseWorkspace();
@@ -123,4 +137,22 @@ T* G4TWorkspacePool<T>::FindOrCreateWorkspace()
return wrk;
}
#endif //G4GEOMETRYWORKSPACEPOOL_HH
template<class T>
void G4TWorkspacePool<T>::Recycle( T * myWrkSpace )
{
myWrkSpace->ReleaseWorkspace();
delete myWrkSpace;
}
template<class T>
void G4TWorkspacePool<T>::CleanUpAndDestroyAllWorkspaces()
{
if (fMyWorkspace)
{
fMyWorkspace->DestroyWorkspace();
delete fMyWorkspace;
fMyWorkspace=0;
}
}
#endif // G4TWORKSPACEPOOL_HH
@@ -139,8 +139,8 @@ G4ThreadLocalSingleton<T>::G4ThreadLocalSingleton() : G4Cache<T*>() {
template<class T>
G4ThreadLocalSingleton<T>::~G4ThreadLocalSingleton() {
G4MUTEXDESTROY(listm);
Clear();
G4MUTEXDESTROY(listm);
}
template<class T>
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4Tokenizer.hh 102536 2017-02-08 13:53:34Z gcosmo $
// $Id: G4Tokenizer.hh 102049 2016-12-19 09:04:20Z gcosmo $
//
//
//---------------------------------------------------------------
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4Version.hh 102536 2017-02-08 13:53:34Z gcosmo $
// $Id: G4Version.hh 104933 2017-06-30 08:07:44Z gcosmo $
// GEANT4 tag $Name:$
//
// Version information
@@ -46,11 +46,11 @@
// |--> patch number
#ifndef G4VERSION_NUMBER
#define G4VERSION_NUMBER 1031
#define G4VERSION_NUMBER 1040
#endif
#ifndef G4VERSION_TAG
#define G4VERSION_TAG "$Name: geant4-10-03-patch-01 $"
#define G4VERSION_TAG "$Name: geant4-10-04-beta-01 $"
#endif
// as variables
@@ -58,10 +58,10 @@
#include "G4String.hh"
#ifdef G4MULTITHREADED
static const G4String G4Version = "$Name: geant4-10-03-patch-01 [MT]$";
static const G4String G4Version = "$Name: geant4-10-04-beta-01 [MT]$";
#else
static const G4String G4Version = "$Name: geant4-10-03-patch-01 $";
static const G4String G4Version = "$Name: geant4-10-04-beta-01 $";
#endif
static const G4String G4Date = "(24-February-2017)";
static const G4String G4Date = "(30-June-2017)";
#endif
@@ -24,35 +24,78 @@
// ********************************************************************
//
//
// $Id: G4coutDestination.hh 82279 2014-06-13 14:44:00Z gcosmo $
// $Id: G4coutDestination.hh 103661 2017-04-20 14:57:11Z gcosmo $
//
//
// ---------------------------------------------------------------
// --------------------------------------------------------------------
// GEANT 4 class header file
//
// G4coutDestination.hh
//
// ---------------------------------------------------------------
// --------------------------------------------------------------------
#ifndef G4COUTDESTINATION_HH
#define G4COUTDESTINATION_HH
#include <functional>
#include <vector>
#include <algorithm>
#include <iostream>
#include "globals.hh"
class G4coutDestination
{
public:
G4coutDestination();
G4coutDestination() = default;
virtual ~G4coutDestination();
// Note: limitation on ICC for MIC cannot use 'default';
virtual G4int ReceiveG4cout(const G4String&);
virtual G4int ReceiveG4cerr(const G4String&);
protected:
//For MT: if master G4coutDestination derived
//class wants to intercept the thread outputs
//derived class should set this pointer.
//Needed for some G4UIsession like GUIs
// The type of the functions defining a transformation of the message.
// The function manipulates the input message, for example, to add a prefix:
// G4coutDestination::AddCoutTransformer(
// [](G4String& msg) -> G4bool { msg="PREFIX "+msg; return true; }
// );
// Function should return false if message should not be processed
// anymore and discarded
//
using Transformer=std::function<G4bool(G4String&)>;
void AddCoutTransformer(const Transformer& t)
{ transformersCout.push_back(t); }
void AddCoutTransformer( Transformer&& t)
{ transformersCout.push_back(t); }
void AddCerrTransformer(const Transformer& t)
{ transformersCerr.push_back(t); }
void AddCerrTransformer( Transformer&& t)
{ transformersCerr.push_back(t); }
virtual void ResetTransformers();
// Derived class implements here handling of message.
// For example, streaming on std::cout or file.
// Return 0 for success, -1 otherwise
//
virtual G4int ReceiveG4cout(const G4String& msg);
virtual G4int ReceiveG4cerr(const G4String& msg);
// Methods called by G4strbuf when need to handle a message
//
G4int ReceiveG4cout_(const G4String& msg);
// Transformers cannot remove an error message from stream
//
G4int ReceiveG4cerr_(const G4String& msg);
protected:
// For MT: if master G4coutDestination derived
// class wants to intercept the thread outputs
// derived class should set this pointer.
// Needed for some G4UIsession like GUIs
//
static G4coutDestination* masterG4coutDestination;
std::vector<Transformer> transformersCout;
std::vector<Transformer> transformersCerr;
};
#endif
@@ -0,0 +1,98 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4coutFormatters.hh 103582 2017-04-18 17:24:45Z adotti $
//
//
// --------------------------------------------------------------------
// GEANT 4 header file
//
// Class Description:
//
// Utilities to handle transformations of cout/cerr streams
// ---------------- G4coutFormatters ----------------
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#ifndef G4COUTFORMATTERS_HH
#define G4COUTFORMATTERS_HH
#include <algorithm>
#include <sstream>
#include <vector>
#include <ctime>
#include <iomanip>
#include <functional>
#include <unordered_map>
#include "G4String.hh"
#include "G4ios.hh"
#include "G4strstreambuf.hh"
namespace G4coutFormatters
{
// Static definitions of provided formatters
namespace ID
{
static const G4String SYSLOG = "syslog";
static const G4String DEFAULT= "default";
}
// A function that set ups a style for the destination
// Example for a style that set to all capital the messages
// to G4cerr:
// setupStyle_f myStyle = [](G4coutDestination* dest)->G4int {
// dest->SetCerrTransformer(
// [](G4String& msg){
// msg->toUpper();
// return true; }
// );
// };
using SetupStyle_f = std::function<G4int(G4coutDestination*)>;
using String_V=std::vector<G4String>;
// Return list of formatter names
String_V Names();
// Setup style (by name) to destination
G4int HandleStyle( G4coutDestination* dest , const G4String& style );
// Set name of the style for the master thread
void SetMasterStyle(const G4String& );
G4String GetMasterStyle();
// This function should be called in user application main function
// to setup the style just after setting up RunManager
void SetupStyleGlobally(const G4String& news);
// To be used by user to register by name a new formatter.
// So it can be used via one of the previous functions
void RegisterNewStyle( const G4String& name , SetupStyle_f& formatter);
}
#endif // G4COUTFORMATTERS_HH
@@ -24,7 +24,8 @@
// ********************************************************************
//
//
// $Id: G4strstreambuf.hh 67970 2013-03-13 10:10:06Z gcosmo $
// $Id: G4strstreambuf.hh 103661 2017-04-20 14:57:11Z gcosmo $
//
// ====================================================================
//
// G4strstreambuf
@@ -33,9 +34,10 @@
#ifndef G4_STR_STREAM_BUF_HH
#define G4_STR_STREAM_BUF_HH
#include <streambuf>
#include "globals.hh"
#include "G4coutDestination.hh"
#include <streambuf>
class G4strstreambuf;
@@ -68,6 +70,7 @@ class G4strstreambuf : public std::basic_streambuf<char>
#endif
void SetDestination(G4coutDestination* dest);
inline G4coutDestination* GetDestination() const;
inline G4int ReceiveString ();
private:
@@ -84,4 +87,3 @@ class G4strstreambuf : public std::basic_streambuf<char>
#include "G4strstreambuf.icc"
#endif
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: G4strstreambuf.icc 67970 2013-03-13 10:10:06Z gcosmo $
// $Id: G4strstreambuf.icc 103661 2017-04-20 14:57:11Z gcosmo $
// ====================================================================
// G4strstreambuf.icc
//
@@ -119,6 +119,13 @@ inline void G4strstreambuf::SetDestination(G4coutDestination* dest)
destination= dest;
}
///////////////////////////////////////////////////////////////////
inline G4coutDestination* G4strstreambuf::GetDestination() const
{
return destination;
}
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
inline G4int G4strstreambuf::ReceiveString ()
@@ -127,21 +134,24 @@ inline G4int G4strstreambuf::ReceiveString ()
G4String stringToSend(buffer);
G4int result= 0;
if(this == &G4coutbuf && destination != 0) {
result= destination-> ReceiveG4cout(stringToSend);
} else if(this == &G4cerrbuf && destination != 0) {
result= destination-> ReceiveG4cerr(stringToSend);
} else if(this == &G4coutbuf && destination == 0) {
if(this == &G4coutbuf && destination != 0)
{
result= destination-> ReceiveG4cout_(stringToSend);
}
else if(this == &G4cerrbuf && destination != 0)
{
result= destination-> ReceiveG4cerr_(stringToSend);
}
else if(this == &G4coutbuf && destination == 0)
{
std::cout << stringToSend << std::flush;
result= 0;
} else if(this == &G4cerrbuf && destination == 0) {
}
else if(this == &G4cerrbuf && destination == 0)
{
std::cerr << stringToSend << std::flush;
result= 0;
}
return result;
}
+1 -13
View File
@@ -24,7 +24,7 @@
// ********************************************************************
//
//
// $Id: templates.hh 67970 2013-03-13 10:10:06Z gcosmo $
// $Id: templates.hh 103661 2017-04-20 14:57:11Z gcosmo $
//
//
// -*- C++ -*-
@@ -148,18 +148,6 @@ inline T sqr(const T& x)
}
#endif
#ifdef G4_ABS_DEFINED
#ifdef abs
#undef abs
#endif
template <class T>
inline T std::abs(const T& a)
{
return a < 0 ? -a : a;
}
#endif
inline int G4lrint(double ad)
{
return (ad>0) ? static_cast<int>(ad+.5) : static_cast<int>(ad-.5);
+1 -1
View File
@@ -44,7 +44,7 @@
# define G4ThreadLocal __thread
#endif
#elif ( (defined(__linux__) || defined(__MACH__)) && \
!defined(__INTEL_COMPILER) && defined(__GNUC__) && (__GNUC__>=4 && __GNUC_MINOR__<9) || __GNUC__>=5 )
!defined(__INTEL_COMPILER) && defined(__GNUC__) && (__GNUC__>=4 && __GNUC_MINOR__<9))
#if defined (G4USE_STD11)
# define G4ThreadLocalStatic static __thread
# define G4ThreadLocal thread_local
+21 -12
View File
@@ -11,7 +11,7 @@
#
# Generated on : 24/9/2010
#
# $Id: sources.cmake 98864 2016-08-15 11:53:26Z gcosmo $
# $Id: sources.cmake 103592 2017-04-19 08:09:03Z gcosmo $
#
#------------------------------------------------------------------------------
@@ -74,7 +74,7 @@ GEANT4_DEFINE_MODULE(NAME G4globman
G4String.hh
G4String.icc
G4SystemOfUnits.hh
G4Threading.hh
G4Threading.hh
G4ThreeVector.hh
G4Timer.hh
G4Timer.icc
@@ -92,14 +92,19 @@ GEANT4_DEFINE_MODULE(NAME G4globman
G4coutDestination.hh
G4ios.hh
G4strstreambuf.hh
G4ofstreamDestination.hh
G4MTcoutDestination.hh
G4CacheDetails.hh
G4Cache.hh
G4ThreadLocalSingleton.hh
G4AutoDelete.hh
G4CacheDetails.hh
G4Cache.hh
G4ThreadLocalSingleton.hh
G4AutoDelete.hh
G4TWorkspacePool.hh
G4MTBarrier.hh
G4MTBarrier.hh
G4coutFormatters.hh
G4MulticoutDestination.hh
G4LockcoutDestination.hh
G4MasterForwardcoutDestination.hh
G4FilecoutDestination.hh
G4BuffercoutDestination.hh
SOURCES
G4Allocator.cc
G4AllocatorPool.cc
@@ -122,7 +127,7 @@ GEANT4_DEFINE_MODULE(NAME G4globman
G4ReferenceCountedHandle.cc
G4SliceTimer.cc
G4StateManager.cc
G4Threading.cc
G4Threading.cc
G4Timer.cc
G4UnitsTable.cc
G4VExceptionHandler.cc
@@ -130,10 +135,14 @@ GEANT4_DEFINE_MODULE(NAME G4globman
G4VStateDependent.cc
G4coutDestination.cc
G4ios.cc
G4ofstreamDestination.cc
G4MTcoutDestination.cc
G4CacheDetails.cc
G4MTBarrier.cc
G4CacheDetails.cc
G4MTBarrier.cc
G4coutFormatters.cc
G4LockcoutDestination.cc
G4MasterForwardcoutDestination.cc
G4FilecoutDestination.cc
G4BuffercoutDestination.cc
GRANULAR_DEPENDENCIES
GLOBAL_DEPENDENCIES
LINK_LIBRARIES
@@ -0,0 +1,93 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
/*
* G4BuffercoutDestination.cc
*
* Created on: Apr 14, 2017
* Author: adotti
*/
#include "G4BuffercoutDestination.hh"
#include "G4AutoLock.hh"
#include <iostream>
G4BuffercoutDestination::G4BuffercoutDestination(size_t max) :
m_buffer_out("") , m_buffer_err(""), m_currentSize_out(0) ,
m_currentSize_err(0), m_maxSize(max) {}
G4BuffercoutDestination::~G4BuffercoutDestination() {
Finalize();
}
void G4BuffercoutDestination::Finalize() {
FlushG4cerr();
FlushG4cout();
}
G4int G4BuffercoutDestination::ReceiveG4cout(const G4String& msg) {
m_currentSize_out += msg.size();
m_buffer_out << msg;
//If there is a max size and it has been reached, flush
if ( m_maxSize>0 && m_currentSize_out >= m_maxSize ) {
FlushG4cout();
}
return 0;
}
G4int G4BuffercoutDestination::ReceiveG4cerr(const G4String& msg) {
m_currentSize_err += msg.size();
m_buffer_err << msg;
//If there is a max size and it has been reached, flush
if ( m_maxSize>0 && m_currentSize_err >= m_maxSize ) {
FlushG4cerr();
}
return 0;
}
G4int G4BuffercoutDestination::FlushG4cout() {
std::cout<<m_buffer_out.str()<<std::flush;
ResetCout();
return 0;
}
void G4BuffercoutDestination::ResetCout() {
m_buffer_out.str("");
m_buffer_out.clear();
m_currentSize_out = 0;
}
G4int G4BuffercoutDestination::FlushG4cerr() {
std::cerr<<m_buffer_err.str()<<std::flush;
ResetCerr();
return 0;
}
void G4BuffercoutDestination::ResetCerr() {
m_buffer_err.str("");
m_buffer_err.clear();
m_currentSize_err = 0;
}
@@ -0,0 +1,78 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4FilecoutDestination.cc 103582 2017-04-18 17:24:45Z adotti $
//
// --------------------------------------------------------------------
//
// G4FilecoutDestination.cc
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#include "G4FilecoutDestination.hh"
#include <ios>
G4FilecoutDestination::~G4FilecoutDestination()
{
Close();
if ( m_output ) m_output.reset();
}
void G4FilecoutDestination::Open(std::ios_base::openmode mode)
{
if ( m_name.isNull() )
{
#ifndef __MIC
// Cannot use G4Exception, because G4cout/G4cerr is not setup
throw std::ios_base::failure("No output file name specified");
#endif
}
if ( m_output != nullptr && m_output->is_open() ) Close();
m_output.reset( new std::ofstream(m_name , std::ios_base::out|mode) );
}
void G4FilecoutDestination::Close()
{
if ( m_output && m_output->is_open() )
{
m_output->close();
}
}
G4int G4FilecoutDestination::ReceiveG4cout(const G4String& msg)
{
if ( m_output == nullptr || ! m_output->is_open() ) Open(m_mode);
*m_output << msg;
return 0;
}
G4int G4FilecoutDestination::ReceiveG4cerr(const G4String& msg)
{
if ( m_output == nullptr || ! m_output->is_open() ) Open(m_mode);
*m_output << msg;
return 0;
}
@@ -0,0 +1,58 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4LockcoutDestination.cc 103582 2017-04-18 17:24:45Z adotti $
//
// --------------------------------------------------------------------
//
// G4LockcoutDestination.cc
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#include "G4LockcoutDestination.hh"
#include "G4AutoLock.hh"
namespace
{
G4Mutex out_mutex = G4MUTEX_INITIALIZER;
}
G4LockcoutDestination::~G4LockcoutDestination()
{
}
G4int G4LockcoutDestination::ReceiveG4cout(const G4String& msg )
{
G4AutoLock l(&out_mutex);
// Forward call to base class
return G4coutDestination::ReceiveG4cout(msg);
}
G4int G4LockcoutDestination::ReceiveG4cerr(const G4String& msg )
{
G4AutoLock l(&out_mutex);
return G4coutDestination::ReceiveG4cerr(msg);
}
@@ -23,160 +23,266 @@
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
//
// $Id: G4MTcoutDestination.cc 66241 2012-12-13 18:34:42Z gunter $
//
//
// ----------------------------------------------------------------------
// G4MTcoutDestination
// --------------------------------------------------------------------
//
// G4MTcoutDestination.cc
//
// --------------------------------------------------------------------
#include <sstream>
#include <assert.h>
#include "G4MTcoutDestination.hh"
#include "G4LockcoutDestination.hh"
#include "G4MasterForwardcoutDestination.hh"
#include "G4FilecoutDestination.hh"
#include "G4BuffercoutDestination.hh"
#include "G4strstreambuf.hh"
#include "G4AutoLock.hh"
G4MTcoutDestination::G4MTcoutDestination(const G4int& threadId,
std::ostream& co, std::ostream& ce)
: finalcout(co), finalcerr(ce), id(threadId), useBuffer(false),
threadCoutToFile(false), threadCerrToFile(false),
ignoreCout(false), ignoreInit(true)
namespace
{
G4String empty = "";
}
G4MTcoutDestination::G4MTcoutDestination(const G4int& threadId)
: ref_defaultOut(nullptr), ref_masterOut(nullptr),
masterDestinationFlag(true),masterDestinationFmtFlag(true),
id(threadId), useBuffer(false), ignoreCout(false), ignoreInit(true),
prefix("G4WT")
{
// TODO: Move these two out of here and in the caller
G4coutbuf.SetDestination(this);
G4cerrbuf.SetDestination(this);
prefix = "G4WT";
stateMgr=G4StateManager::GetStateManager();
SetDefaultOutput(masterDestinationFlag,masterDestinationFmtFlag);
}
void G4MTcoutDestination::SetDefaultOutput( G4bool addmasterDestination ,
G4bool formatAlsoMaster )
{
masterDestinationFlag = addmasterDestination;
masterDestinationFmtFlag = formatAlsoMaster;
// Formatter: add prefix to each thread
const auto f = [this](G4String& msg)->G4bool {
std::ostringstream str;
str<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) str<<id;
str<<" > "<<msg;
msg = str.str();
return true;
};
// Block cout if not in correct state
const auto filter_out = [this](G4String&)->G4bool {
if (this->ignoreCout ||
( this->ignoreInit &&
this->stateMgr->GetCurrentState() == G4State_Idle ) )
{ return false; }
return true;
};
// Default behavior, add a destination that uses cout and uses a mutex
auto output = G4coutDestinationUPtr( new G4LockcoutDestination );
ref_defaultOut = output.get();
output->AddCoutTransformer(filter_out);
output->AddCoutTransformer(f);
output->AddCerrTransformer(f);
push_back( std::move(output) );
if ( addmasterDestination )
{
AddMasterOutput(formatAlsoMaster);
}
}
void G4MTcoutDestination::AddMasterOutput(G4bool formatAlsoMaster )
{
// Add a destination, that forwards the message to the master thread
auto forwarder = G4coutDestinationUPtr( new G4MasterForwardcoutDestination );
ref_masterOut = forwarder.get();
const auto filter_out = [this](G4String&)->G4bool {
if (this->ignoreCout ||
( this->ignoreInit &&
this->stateMgr->GetCurrentState() == G4State_Idle ) )
{ return false; }
return true;
};
forwarder->AddCoutTransformer(filter_out);
if ( formatAlsoMaster )
{
// Formatter: add prefix to each thread
const auto f = [this](G4String& msg)->G4bool {
std::ostringstream str;
str<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) str<<id;
str<<" > "<<msg;
msg = str.str();
return true;
};
forwarder->AddCoutTransformer(f);
forwarder->AddCerrTransformer(f);
}
push_back( std::move(forwarder ) );
}
G4MTcoutDestination::~G4MTcoutDestination()
{
if( useBuffer ) DumpBuffer();
if( threadCoutToFile ) CloseCoutFile();
if( threadCerrToFile ) CloseCerrFile();
if ( useBuffer ) DumpBuffer();
}
namespace { G4Mutex coutm = G4MUTEX_INITIALIZER; }
#include "G4StateManager.hh"
G4int G4MTcoutDestination::ReceiveG4cout(const G4String& msg)
void G4MTcoutDestination::Reset()
{
if( threadCoutToFile )
{ coutFile<<msg<<std::flush; }
else if( useBuffer )
{ cout_buffer<<msg; }
else if( !ignoreCout )
clear();
SetDefaultOutput(masterDestinationFlag,masterDestinationFmtFlag);
}
void G4MTcoutDestination::HandleFileCout(G4String fileN, G4bool ifAppend,
G4bool suppressDefault)
{
// Logic: we create a file destination. We want this to get only the G4cout
// stream and should discard everything in G4cerr.
// First we create the destination with the appropriate open mode
std::ios_base::openmode mode = (ifAppend ? std::ios_base::app
: std::ios_base::trunc);
auto output = G4coutDestinationUPtr( new G4FilecoutDestination(fileN,mode));
// This reacts only to G4cout, so let's make a filter that removes everything
// from G4cerr
output->AddCerrTransformer( [](G4String&) { return false;} );
push_back(std::move(output));
// Silence G4cout from default formatter
if ( suppressDefault )
{
if(!ignoreInit ||
G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
{
G4AutoLock l(&coutm);
finalcout<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) finalcout<<id;
finalcout<<" > "<<msg<<std::flush;
}
ref_defaultOut->AddCoutTransformer( [](G4String&) { return false; } );
if ( ref_masterOut )
ref_masterOut->AddCoutTransformer( [](G4String&) { return false; } );
}
//forward message to master G4coutDestination if set
if ( masterG4coutDestination && !ignoreCout &&
( !ignoreInit || G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
){
G4AutoLock l(&coutm);
std::stringstream ss;
ss<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) ss<<id;
ss<<" > "<<msg;
masterG4coutDestination->ReceiveG4cout(ss.str());
}
return 0;
}
G4int G4MTcoutDestination::ReceiveG4cerr(const G4String& msg)
void G4MTcoutDestination::HandleFileCerr(G4String fileN, G4bool ifAppend,
G4bool suppressDefault)
{
if( threadCerrToFile )
{ cerrFile<<msg<<std::flush; }
if( useBuffer )
{ cerr_buffer<<msg; }
else
{ G4AutoLock l(&coutm);
finalcerr<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) finalcerr<<id;
finalcerr<<" > "<<msg<<std::flush;
}
//forward message to master G4coutDestination if set
if ( masterG4coutDestination && !ignoreCout &&
( !ignoreInit || G4StateManager::GetStateManager()->GetCurrentState() != G4State_Idle )
){
G4AutoLock l(&coutm);
std::stringstream ss;
ss<<prefix;
if ( id!=G4Threading::GENERICTHREAD_ID ) ss<<id;
ss<<" > "<<msg;
masterG4coutDestination->ReceiveG4cerr(ss.str());
}
return 0;
}
// See HandleFileCout for explanation, switching cout with cerr
void G4MTcoutDestination::SetCoutFileName(const G4String& fileN, G4bool ifAppend)
{
if( threadCoutToFile ) CloseCoutFile();
if( fileN == "**Screen**" ) return;
if( ! coutFile.is_open() )
std::ios_base::openmode mode = (ifAppend ? std::ios_base::app
: std::ios_base::trunc);
auto output = G4coutDestinationUPtr( new G4FilecoutDestination(fileN,mode));
output->AddCoutTransformer( [](G4String&) { return false;} );
push_back(std::move(output));
if ( suppressDefault )
{
std::ios::openmode mode = std::ios::out;
if ( ifAppend ) mode |= std::ios::app;
coutFile.open(fileN,mode);
ref_defaultOut->AddCerrTransformer( [](G4String&) { return false; } );
if ( ref_masterOut )
ref_masterOut->AddCerrTransformer( [](G4String&) { return false; } );
}
threadCoutToFile = true;
}
void G4MTcoutDestination::SetCerrFileName(const G4String& fileN, G4bool ifAppend)
void G4MTcoutDestination::SetCoutFileName(const G4String& fileN,
G4bool ifAppend)
{
if( threadCerrToFile ) CloseCerrFile();
if( fileN == "**Screen**" ) return;
if( ! cerrFile.is_open() )
// First let's go back to the default
Reset();
if ( fileN != "**Screen**" )
{
std::ios::openmode mode = std::ios::out;
if ( ifAppend ) mode |= std::ios::app;
cerrFile.open(fileN,mode);
HandleFileCout(fileN,ifAppend,true);
}
threadCerrToFile = true;
}
void G4MTcoutDestination::EnableBuffering(G4bool flag)
{
if(useBuffer && !flag) DumpBuffer();
// I was using buffered output and now I want to turn it off, dump current
// buffer content and reset output
if ( useBuffer && !flag )
{
DumpBuffer();
Reset();
}
else if ( useBuffer && flag ) { /* do nothing: already using */ }
else if ( !useBuffer && !flag ) { /* do nothing: not using */ }
else if ( !useBuffer && flag )
{
// Remove everything, in this case also removing the forward to the master
// thread, we want everything to be dumpled to a file
clear();
const size_t infiniteSize = 0;
push_back(G4coutDestinationUPtr(new G4BuffercoutDestination(infiniteSize)));
}
else { assert(false); } // Should never happen
useBuffer = flag;
}
void G4MTcoutDestination::SetPrefixString(const G4String& wd)
{ prefix = wd; }
void G4MTcoutDestination::AddCoutFileName(const G4String& fileN,
G4bool ifAppend)
{
// This is like the equivalent SetCoutFileName, but in this case we do not
// remove or silence what is already exisiting
HandleFileCout(fileN,ifAppend,false);
}
void G4MTcoutDestination::SetCerrFileName(const G4String& fileN,
G4bool ifAppend)
{
// See SetCoutFileName for explanation
Reset();
if ( fileN != "**Screen**")
{
HandleFileCerr(fileN,ifAppend,true);
}
}
void G4MTcoutDestination::AddCerrFileName(const G4String& fileN,
G4bool ifAppend)
{
HandleFileCerr(fileN,ifAppend,false);
}
void G4MTcoutDestination::SetIgnoreCout(G4int tid)
{
if(tid<0)
{ ignoreCout = false; }
else
{ ignoreCout = (tid!=id); }
if (tid<0) { ignoreCout = false; }
else { ignoreCout = (tid!=id); }
}
void G4MTcoutDestination::CloseCoutFile()
namespace
{
if( coutFile.is_open() ) coutFile.close();
threadCoutToFile = false;
}
void G4MTcoutDestination::CloseCerrFile()
{
if( cerrFile.is_open() ) cerrFile.close();
threadCerrToFile = false;
G4Mutex coutm = G4MUTEX_INITIALIZER;
}
void G4MTcoutDestination::DumpBuffer()
{
G4AutoLock l(&coutm);
finalcout<<"====================="<<std::endl;
finalcout<<"cout buffer for worker with ID:"<<id<<std::endl;
finalcout<<cout_buffer.str()<<std::endl;
finalcerr<<"====================="<<std::endl;
finalcerr<<"cerr buffer for worker with ID:"<<id<<std::endl;
finalcerr<<cerr_buffer.str()<<std::endl;
finalcerr<<"====================="<<std::endl;
std::ostringstream msg;
msg << "=======================\n";
msg << "cout buffer(s) for worker with ID:" << id << std::endl;
G4coutDestination::ReceiveG4cout(msg.str());
G4bool sep = false;
std::for_each( begin() , end(),
[this,&sep](G4coutDestinationUPtr& el) {
auto cout = dynamic_cast<G4BuffercoutDestination*>(el.get());
if ( cout != nullptr ) {
cout->FlushG4cout();
if ( sep ) { G4coutDestination::ReceiveG4cout("==========\n"); }
else { sep = true; }
}
} );
sep = false;
msg.str("");
msg.clear();
msg << "=======================\n";
msg << "cerr buffer(s) for worker with ID:" << id
<< " (goes to std error)" << std::endl;
G4coutDestination::ReceiveG4cout(msg.str());
std::for_each( begin() , end(),
[this,&sep](G4coutDestinationUPtr& el) {
auto cout = dynamic_cast<G4BuffercoutDestination*>(el.get());
if ( cout != nullptr ) {
cout->FlushG4cerr();
if (sep ) { G4coutDestination::ReceiveG4cout("==========\n"); }
else { sep = true; }
}
} );
G4coutDestination::ReceiveG4cout("=======================\n");
}
@@ -0,0 +1,69 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4MasterForwardcoutDestination.cc 103582 2017-04-18 17:24:45Z adotti $
//
// --------------------------------------------------------------------
//
// G4MasterForwardcoutDestination.cc
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#include "G4MasterForwardcoutDestination.hh"
#include "G4AutoLock.hh"
namespace
{
G4Mutex out_mutex = G4MUTEX_INITIALIZER;
}
G4MasterForwardcoutDestination::~G4MasterForwardcoutDestination()
{
}
G4int G4MasterForwardcoutDestination::ReceiveG4cout(const G4String& msg )
{
// If a master destination is set check that we are not in a recursive
// situation, send the message to the master, using a lock to serialize calls
// Master is probably a (G)UI that is not thread-safe
if ( masterG4coutDestination && this!=masterG4coutDestination)
{
G4AutoLock l(&out_mutex);
return masterG4coutDestination->ReceiveG4cout_(msg);
}
return 0;
}
G4int G4MasterForwardcoutDestination::ReceiveG4cerr(const G4String& msg )
{
if ( masterG4coutDestination && this!=masterG4coutDestination)
{
G4AutoLock l(&out_mutex);
return masterG4coutDestination->ReceiveG4cerr_(msg);
}
return 0;
}
@@ -24,31 +24,72 @@
// ********************************************************************
//
//
// $Id: G4coutDestination.cc 82279 2014-06-13 14:44:00Z gcosmo $
// $Id: G4coutDestination.cc 103661 2017-04-20 14:57:11Z gcosmo $
//
//
// ----------------------------------------------------------------------
// G4coutDestination
//
// ----------------------------------------------------------------------
#include "G4coutDestination.hh"
G4coutDestination* G4coutDestination::masterG4coutDestination = 0;
#include <algorithm>
G4coutDestination::G4coutDestination()
{
}
G4coutDestination* G4coutDestination::masterG4coutDestination = 0;
G4coutDestination::~G4coutDestination()
{
}
G4int G4coutDestination::ReceiveG4cout(const G4String&)
void G4coutDestination::ResetTransformers()
{
transformersCout.clear(); transformersCerr.clear();
}
G4int G4coutDestination::ReceiveG4cout(const G4String& msg)
{
std::cout<<msg<<std::flush;
return 0;
}
G4int G4coutDestination::ReceiveG4cerr(const G4String&)
G4int G4coutDestination::ReceiveG4cerr(const G4String& msg)
{
std::cerr<<msg<<std::flush;
return 0;
}
G4int G4coutDestination::ReceiveG4cout_(const G4String& msg)
{
// Avoid copy of string if not necessary
if( transformersCout.size() > 0 )
{
G4String m = msg;
G4bool result = true;
for ( const auto& el : transformersCout )
{
result &= el(m);
if ( ! result ) break;
}
return ( result ? ReceiveG4cout(m) : 0 );
}
else
{
return ReceiveG4cout(msg);
}
}
G4int G4coutDestination::ReceiveG4cerr_(const G4String& msg)
{
if( transformersCout.size() > 0 )
{
G4String m = msg;
std::for_each( transformersCerr.begin() , transformersCerr.end() ,
[&m](const Transformer& t) { t(m); }
// Call transforming function on message
);
return ReceiveG4cerr(m);
}
else
{
return ReceiveG4cerr(msg);
}
}
@@ -0,0 +1,167 @@
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// $Id: G4coutFormatters.cc 103582 2017-04-18 17:24:45Z adotti $
//
// --------------------------------------------------------------------
//
// G4coutFormatters.cc
//
// Author: A.Dotti (SLAC), April 2017
// --------------------------------------------------------------------
#include "G4coutFormatters.hh"
namespace G4coutFormatters
{
// Internal functions and utilites used to setup default formatters
namespace
{
// Split a single string in an array of strings
String_V split(const G4String& input, char separator='\n')
{
String_V output;
G4String::size_type prev_pos=0, pos=0;
while( (pos=input.find(separator,pos))!=G4String::npos)
{
G4String substr( input.substr(prev_pos,pos-prev_pos)) ;
output.push_back(substr);
prev_pos = ++pos;
}
// output.push_back( input.substr(prev_pos,pos-prev_pos));
return output;
}
// Return a syslog style message with input message, type identifies
// the type of the message
G4bool transform( G4String& input , const G4String& type)
{
std::time_t result = std::time(nullptr);
std::ostringstream newm;
#if __GNUC__ >= 5
newm << std::put_time(std::localtime(&result),"%d/%b/%Y:%H:%M:%S %z");
#else
std::tm* time_ = std::localtime(&result);
newm << time_->tm_mday << "/" << time_->tm_mon << "/" << time_->tm_year;
newm << ":" << time_->tm_hour << ":"<<time_->tm_min<<":"<<time_->tm_sec;
#endif
newm<<" "<<type<<" [";
G4String delimiter = "";
for (const auto& el : split(input) )
{
if ( !el.empty() )
{
newm << delimiter << el ;
delimiter = "\\n";
}
}
newm<<" ]"<<G4endl;
input = newm.str();
return true;
}
// Style used in master thread
G4String masterStyle = "";
// Modify output to look like syslog messages:
// DATE TIME **LOG|ERROR** [ "multi","line","message"]
SetupStyle_f SysLogStyle = [](G4coutDestination* dest)->G4int
{
if ( dest != nullptr )
{
dest->AddCoutTransformer(std::bind(&transform,std::placeholders::_1,
"INFO"));
dest->AddCerrTransformer(std::bind(&transform,std::placeholders::_1,
"ERROR"));
}
return 0;
};
// Bring back destination to original state
SetupStyle_f DefaultStyle = [](G4coutDestination* dest)->G4int
{
if ( dest != nullptr )
{
dest->ResetTransformers();
}
return 0;
};
std::unordered_map<std::string,SetupStyle_f> transformers =
{
{ID::SYSLOG,SysLogStyle},
{ID::DEFAULT,DefaultStyle}
};
}
void SetMasterStyle(const G4String& news )
{
masterStyle = news;
}
G4String GetMasterStyle()
{
return masterStyle;
}
void SetupStyleGlobally(const G4String& news)
{
static G4coutDestination ss;
G4coutbuf.SetDestination(&ss);
G4cerrbuf.SetDestination(&ss);
G4coutFormatters::HandleStyle(&ss,news);
G4coutFormatters::SetMasterStyle(news);
}
String_V Names()
{
String_V result;
for ( const auto& el : transformers )
{
result.push_back(el.first);
}
return result;
}
G4int HandleStyle( G4coutDestination* dest , const G4String& style)
{
const auto& handler = transformers.find(style);
return ( handler != transformers.end() ) ? (handler->second)(dest) : -1;
}
void RegisterNewStyle(const G4String& name , SetupStyle_f& fmt)
{
if ( transformers.find(name) != transformers.end() )
{
G4ExceptionDescription msg;
msg << "Format Style with name " << name
<< " already exists. Replacing existing.";
G4Exception("G4coutFormatters::RegisterNewStyle()",
"FORMATTER001", JustWarning, msg);
}
// transformers.insert(std::make_pair(name,fmt));
transformers[name]=fmt;
}
}