diff -urpN geant4.9.5.p01/source/event/GNUmakefile geant4.9.5.p01-new/source/event/GNUmakefile
--- geant4.9.5.p01/source/event/GNUmakefile	2012-03-27 10:42:36.000000000 +0300
+++ geant4.9.5.p01-new/source/event/GNUmakefile	2012-09-07 21:07:16.409936447 +0300
@@ -16,7 +16,7 @@ GLOBLIBS += libG4intercoms.lib libG4glob
 
 include $(G4INSTALL)/config/architecture.gmk
 
-CPPFLAGS += -DG4EVENT_ALLOC_EXPORT
+CPPFLAGS += -DG4EVENT_ALLOC_EXPORT -DG4_USESMARTSTACK
 CPPFLAGS += \
             -I$(G4BASE)/global/management/include \
             -I$(G4BASE)/global/HEPRandom/include \
@@ -53,4 +53,3 @@ include $(G4INSTALL)/config/common.gmk
 .PHONY: global
  
 global: lib
-
diff -urpN geant4.9.5.p01/source/event/include/G4SmartTrackStack.hh geant4.9.5.p01-new/source/event/include/G4SmartTrackStack.hh
--- geant4.9.5.p01/source/event/include/G4SmartTrackStack.hh	2012-03-27 10:42:35.000000000 +0300
+++ geant4.9.5.p01-new/source/event/include/G4SmartTrackStack.hh	2012-09-07 21:07:16.404399875 +0300
@@ -36,7 +36,7 @@
 #define G4SmartTrackStack_h 1
 
 #include "G4StackedTrack.hh"
-#include "G4TrackStack.hh"
+#include "G4TrackStackDQ.hh"
 #include "globals.hh"
 
 // class description:
@@ -61,20 +61,21 @@ class G4SmartTrackStack 
       void PushToStack(G4StackedTrack * aStackedTrack);
       G4StackedTrack * PopFromStack();
       void clear();
-      void TransferTo(G4TrackStack * aStack);
+      void TransferTo(G4TrackStackDQ * aStack);
+      G4double getEnergyOfStack(G4TrackStackDQ *aTrackStack);
+      void dumpStatistics();
+
 
   private:
       G4int fTurn;
       G4int nTurn; // should be 5
-      G4TrackStack* stacks[5];
+      G4double energies[5];
+      G4TrackStackDQ* stacks[5];
       // = 0 : all primaries and secondaries except followings
       // = 1 : secondary neutrons
       // = 2 : secondary electrons
       // = 3 : secondary gammas
       // = 4 : secondary positrons
-      G4int nStick;
-      G4int safetyValve1; 
-      G4int safetyValve2; 
       G4int maxNTracks;
 
   public:
@@ -86,8 +87,11 @@ class G4SmartTrackStack 
   private:
       inline G4int n_stackedTrack() const
       {
-        return stacks[0]->GetNTrack()+stacks[1]->GetNTrack()
-         +stacks[2]->GetNTrack()+stacks[3]->GetNTrack()+stacks[4]->GetNTrack();
+	      return stacks[0]->GetNTrack() +
+		     stacks[1]->GetNTrack() +
+		     stacks[2]->GetNTrack() +
+		     stacks[3]->GetNTrack() +
+		     stacks[4]->GetNTrack();
       }
 };
 
diff -urpN geant4.9.5.p01/source/event/include/G4StackManager.hh geant4.9.5.p01-new/source/event/include/G4StackManager.hh
--- geant4.9.5.p01/source/event/include/G4StackManager.hh	2012-03-27 10:42:35.000000000 +0300
+++ geant4.9.5.p01-new/source/event/include/G4StackManager.hh	2012-09-07 21:07:16.388912611 +0300
@@ -35,7 +35,7 @@
 
 #include "G4UserStackingAction.hh"
 #include "G4StackedTrack.hh"
-#include "G4TrackStack.hh"
+#include "G4TrackStackDQ.hh"
 #include "G4SmartTrackStack.hh"
 #include "G4ClassificationOfNewTrack.hh"
 #include "G4Track.hh"
@@ -113,12 +113,12 @@ class G4StackManager 
 #ifdef G4_USESMARTSTACK
       G4SmartTrackStack * urgentStack;
 #else
-      G4TrackStack * urgentStack;
+      G4TrackStackDQ * urgentStack;
 #endif
-      G4TrackStack * waitingStack;
-      G4TrackStack * postponeStack;
+      G4TrackStackDQ * waitingStack;
+      G4TrackStackDQ * postponeStack;
       G4StackingMessenger* theMessenger;
-      std::vector<G4TrackStack*> additionalWaitingStacks;
+      std::vector<G4TrackStackDQ*> additionalWaitingStacks;
       G4int numberOfAdditionalWaitingStacks;
 
   public:
diff -urpN geant4.9.5.p01/source/event/include/G4TrackStack.hh geant4.9.5.p01-new/source/event/include/G4TrackStack.hh
--- geant4.9.5.p01/source/event/include/G4TrackStack.hh	2012-03-27 10:42:35.000000000 +0300
+++ geant4.9.5.p01-new/source/event/include/G4TrackStack.hh	2012-09-07 21:07:16.401639094 +0300
@@ -48,37 +48,37 @@ class G4SmartTrackStack;
 
 class G4TrackStack 
 {
-  public:
-      G4TrackStack();
-      ~G4TrackStack();
-
-  private:
-      const G4TrackStack & operator=
-                          (const G4TrackStack &right);
-      G4int operator==(const G4TrackStack &right) const;
-      G4int operator!=(const G4TrackStack &right) const;
-
-  public:
-      void PushToStack(G4StackedTrack * aStackedTrack);
-      G4StackedTrack * PopFromStack();
-      void GrabFromStack(G4StackedTrack * aStackedTrack);
-      void clear();
-      void TransferTo(G4TrackStack * aStack);
-      void TransferTo(G4SmartTrackStack * aStack);
-
-  private:
-      G4int n_stackedTrack;
-      G4StackedTrack * firstStackedTrack;
-      G4StackedTrack * lastStackedTrack;
-      G4int maxNTracks;
-
-  public:
-      inline G4int GetNTrack() const
-      { return n_stackedTrack; }
-      inline G4int GetMaxNTrack() const
-      { return maxNTracks; }
-
+public:
+	G4TrackStack();
+	~G4TrackStack();
+
+private:
+	const G4TrackStack & operator=(const G4TrackStack &right);
+	G4int operator==(const G4TrackStack &right) const;
+	G4int operator!=(const G4TrackStack &right) const;
+
+public:
+	void PushToStack(G4StackedTrack * aStackedTrack);
+	G4StackedTrack * PopFromStack();
+	void GrabFromStack(G4StackedTrack * aStackedTrack);
+	void clear();
+	void TransferTo(G4TrackStack * aStack);
+	void TransferTo(G4SmartTrackStack * aStack);
+
+private:
+	G4int n_stackedTrack;
+	G4StackedTrack * firstStackedTrack;
+	G4StackedTrack * lastStackedTrack;
+	G4int maxNTracks;
+
+	G4int safetyValve1, safetyValve2;
+	G4int nstick;
+
+public:
+	inline G4int GetNTrack() const
+	{ return n_stackedTrack; }
+	inline G4int GetMaxNTrack() const
+	{ return maxNTracks; }
 };
 
 #endif
-
diff -urpN geant4.9.5.p01/source/event/include/G4TrackStackDQ.hh geant4.9.5.p01-new/source/event/include/G4TrackStackDQ.hh
--- geant4.9.5.p01/source/event/include/G4TrackStackDQ.hh	1970-01-01 02:00:00.000000000 +0200
+++ geant4.9.5.p01-new/source/event/include/G4TrackStackDQ.hh	2012-09-07 21:07:16.389313533 +0300
@@ -0,0 +1,87 @@
+//
+// ********************************************************************
+// * 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.          *
+// ********************************************************************
+
+#ifndef G4TrackStackDQ_h
+#define G4TrackStackDQ_h 1
+
+#include <vector>
+#include "G4StackedTrack.hh"
+#include "globals.hh"
+
+class G4SmartTrackStack;
+
+// class description:
+//
+// This is a stack class used by G4StackManager. This class object
+// stores G4StackedTrack class objects in the form of bi-directional
+// linked list.
+
+class G4TrackStackDQ 
+{
+public:
+        G4TrackStackDQ();
+	G4TrackStackDQ(G4ParticleDefinition *partDef);
+	~G4TrackStackDQ();
+
+private:
+	const G4TrackStackDQ & operator=(const G4TrackStackDQ &right);
+	G4int operator==(const G4TrackStackDQ &right) const;
+	G4int operator!=(const G4TrackStackDQ &right) const;
+
+public:
+	void PushToStack(G4StackedTrack *aStackedTrack);
+	G4StackedTrack *PopFromStack();
+	void clear();
+	void TransferTo(G4TrackStackDQ *aStack);
+	void TransferTo(G4SmartTrackStack *aStack);
+
+private:
+	G4int n_stackedTrack;
+	G4StackedTrack *firstStackedTrack;
+	G4StackedTrack *lastStackedTrack;
+	G4int maxNTracks;
+	G4int safetyValve1, safetyValve2;
+	G4int nstick;
+
+public: // XXX
+	std::vector<G4StackedTrack*> dqStackedTracks;
+
+public:
+	inline G4int GetNTrack()    const { return n_stackedTrack; }
+	inline G4int GetMaxNTrack() const { return maxNTracks; }
+
+	inline  G4int GetSafetyValve1() const
+	{ return safetyValve1; }
+	inline G4int GetSafetyValve2() const
+	{ return safetyValve2; }
+	inline G4int GetNStick() const
+	{ return nstick; }
+
+	G4double getTotalEnergy(void) const;
+
+	void SetSafetyValve2(int x) { if (x < 0) { x = 0; } safetyValve2 = x; }
+};
+
+#endif
diff -urpN geant4.9.5.p01/source/event/sources.cmake geant4.9.5.p01-new/source/event/sources.cmake
--- geant4.9.5.p01/source/event/sources.cmake	2012-03-27 10:42:36.000000000 +0300
+++ geant4.9.5.p01-new/source/event/sources.cmake	2012-09-07 21:07:16.388492037 +0300
@@ -79,6 +79,7 @@ GEANT4_DEFINE_MODULE(NAME G4event
         G4StackedTrack.hh
         G4StackingMessenger.hh
         G4TrackStack.hh
+	G4TrackStackDQ.hh
         G4TrajectoryContainer.hh
         G4UserEventAction.hh
         G4UserStackingAction.hh
@@ -115,6 +116,7 @@ GEANT4_DEFINE_MODULE(NAME G4event
         G4StackedTrack.cc
         G4StackingMessenger.cc
         G4TrackStack.cc
+	G4TrackStackDQ.cc
         G4TrajectoryContainer.cc
         G4UserEventAction.cc
         G4UserStackingAction.cc
diff -urpN geant4.9.5.p01/source/event/src/G4SmartTrackStack.cc geant4.9.5.p01-new/source/event/src/G4SmartTrackStack.cc
--- geant4.9.5.p01/source/event/src/G4SmartTrackStack.cc	2012-03-27 10:42:36.000000000 +0300
+++ geant4.9.5.p01-new/source/event/src/G4SmartTrackStack.cc	2012-09-07 21:07:16.405815740 +0300
@@ -28,112 +28,162 @@
 // GEANT4 tag $Name: not supported by cvs2svn $
 //
 
+#ifdef ENABLE_DTRACE
+#include "simple_probes.h"
+#endif
+
 #include "G4SmartTrackStack.hh"
 #include "G4VTrajectory.hh"
 
+#include "G4Electron.hh"
+
+void
+G4SmartTrackStack::dumpStatistics()
+{
+	// Print to stderr so that we can split stats output from normal
+	// output of Geant4 which is typically being printed to stdout
+	for (int i = 0; i < nTurn; i++) {
+		G4cerr << stacks[i]->GetNTrack() << " ";
+		G4cerr << stacks[i]->getTotalEnergy() << " ";
+#if 0
+		G4cerr << stacks[i]->GetSafetyValve1() << " ";
+		G4cerr << stacks[i]->GetSafetyValve2() << " ";
+		G4cerr << stacks[i]->GetNStick();
+		G4cerr << G4endl;
+#endif
+	}
+	G4cerr << G4endl;
+}
+
 G4SmartTrackStack::G4SmartTrackStack()
-:fTurn(0),nTurn(5)
+	:fTurn(0), nTurn(5)
 {
-  for(int i=0;i<nTurn;i++)
-  { stacks[i] = new G4TrackStack(); }
-  // If entry of one sub-stack exceeds safetyValve1, we will stick
-  // to that sub-stack until entry of that sub-stack goes down
-  // to safetyValve2.
-  nStick = 100;
-  safetyValve1 = 3000; 
-  safetyValve2 = safetyValve1 - nStick;
-  maxNTracks = 0;
+	stacks[0] = new G4TrackStackDQ();
+	stacks[1] = new G4TrackStackDQ();
+	stacks[2] = new G4TrackStackDQ(G4Electron::Definition()); // electrons
+	stacks[3] = new G4TrackStackDQ();
+	stacks[4] = new G4TrackStackDQ();
+
+	maxNTracks = 0;
 }
 
 G4SmartTrackStack::~G4SmartTrackStack()
 {
-  for(int i=0;i<nTurn;i++)
-  { delete stacks[i]; }
+	for (int i  = 0; i < nTurn; i++) {
+		delete stacks[i];
+	}
+}
+
+const G4SmartTrackStack &
+G4SmartTrackStack::operator=(const G4SmartTrackStack &)
+{
+	return *this;
 }
 
-const G4SmartTrackStack & G4SmartTrackStack::operator=(const G4SmartTrackStack &) 
-{ return *this; }
 int G4SmartTrackStack::operator==(const G4SmartTrackStack &right) const
-{ return (this==&right); }
+{
+	return (this==&right);
+}
+
 int G4SmartTrackStack::operator!=(const G4SmartTrackStack &right) const
-{ return (this!=&right); }
+{
+	return (this!=&right);
+}
 
-void G4SmartTrackStack::TransferTo(G4TrackStack * aStack)
+void G4SmartTrackStack::TransferTo(G4TrackStackDQ * aStack)
 {
-  for(int i=0;i<nTurn;i++)
-  { stacks[i]->TransferTo(aStack); }
+	for (int i = 0; i < nTurn; i++) {
+		stacks[i]->TransferTo(aStack);
+	}
 }
 
 G4StackedTrack * G4SmartTrackStack::PopFromStack()
 {
-  if( n_stackedTrack() == 0 ) return 0;
-  G4StackedTrack * aStackedTrack = 0;
-  while(!aStackedTrack)
-  {
-    if(stacks[fTurn]->GetNTrack()==0)
-    {
-      fTurn = (fTurn+1)%nTurn;
-      //G4cout<<"++++++++ Shift to Stack ["<<fTurn<<"] with "<<stacks[fTurn]->GetNTrack()<<" stacked tracks."<<G4endl;
-    }
-    else
-    { aStackedTrack = stacks[fTurn]->PopFromStack(); }
-  }
-  return aStackedTrack;
+	G4StackedTrack * aStackedTrack = 0;
+
+	if (n_stackedTrack()) {
+		while (!aStackedTrack) {
+			if (stacks[fTurn]->GetNTrack()) {
+				aStackedTrack = stacks[fTurn]->PopFromStack();
+#ifdef ENABLE_DTRACE
+				// Export the size of stacks
+				SIMPLE_POPTRACK(
+					stacks[0]->GetNTrack(),
+					stacks[1]->GetNTrack(),
+					stacks[2]->GetNTrack(),
+					stacks[3]->GetNTrack(),
+					stacks[4]->GetNTrack());
+#endif
+				energies[fTurn] -=
+				    aStackedTrack->GetTrack()->GetDynamicParticle()->GetTotalEnergy();
+			} else {
+				fTurn = (fTurn+1) % nTurn;
+			}
+		}
+	}
+
+	// dumpStatistics();
+
+	return aStackedTrack;
 }
 
 #include "G4Neutron.hh"
 #include "G4Gamma.hh"
-#include "G4Electron.hh"
 #include "G4Positron.hh"
 
 void G4SmartTrackStack::PushToStack( G4StackedTrack * aStackedTrack )
 {
-  static G4ParticleDefinition* neutDef = G4Neutron::Definition();
-  static G4ParticleDefinition* elecDef = G4Electron::Definition();
-  static G4ParticleDefinition* gammDef = G4Gamma::Definition();
-  static G4ParticleDefinition* posiDef = G4Positron::Definition();
-
-  if(!aStackedTrack) return;
-
-  G4int iDest = 0;
-  if( aStackedTrack->GetTrack()->GetParentID() == 0 )
-  {
-    // We have a primary track, which should go first.
-    fTurn = 0; // reseting the turn
-  }
-  else
-  {
-    G4ParticleDefinition* partDef = aStackedTrack->GetTrack()->GetDefinition();
-    if(partDef==neutDef)
-    { iDest = 1; }
-    else if(partDef==elecDef)
-    { iDest = 2; }
-    else if(partDef==gammDef)
-    { iDest = 3; }
-    else if(partDef==posiDef)
-    { iDest = 4; }
-  }
-
-  stacks[iDest]->PushToStack(aStackedTrack);
-  if(stacks[iDest]->GetNTrack()>safetyValve1)
-  {
-    // Too many tracks in the stack. Process tracks in this stack first
-    // unless the current stack also have too many tracks.
-    if(stacks[fTurn]->GetNTrack()<safetyValve2)
-    {
-      fTurn = iDest;
-      safetyValve2 = stacks[iDest]->GetNTrack() - nStick;
-      //G4cout<<"++++++++ Shift to Stack ["<<fTurn<<"] with "<<stacks[fTurn]->GetNTrack()<<" stacked tracks."<<G4endl;
-    }
-  }
+	static G4ParticleDefinition* neutDef = G4Neutron::Definition();
+	static G4ParticleDefinition* elecDef = G4Electron::Definition();
+	static G4ParticleDefinition* gammDef = G4Gamma::Definition();
+	static G4ParticleDefinition* posiDef = G4Positron::Definition();
+
+	if (aStackedTrack) {
+		G4int iDest = 0;
+		if (aStackedTrack->GetTrack()->GetParentID()) {
+			G4ParticleDefinition* partDef = aStackedTrack->GetTrack()->GetDefinition();
+			if (partDef == elecDef)
+				iDest = 2;
+			else if (partDef == gammDef)
+				iDest = 3;
+			else if (partDef == posiDef)
+				iDest = 4;
+			else if (partDef == neutDef)
+				iDest = 1;
+		} else {
+			// We have a primary track, which should go first.
+			fTurn = 0; // reseting the turn
+		}
+		stacks[iDest]->PushToStack(aStackedTrack);
+#ifdef ENABLE_DTRACE
+		SIMPLE_PUSHTRACK(
+			stacks[0]->GetNTrack(),
+			stacks[1]->GetNTrack(),
+			stacks[2]->GetNTrack(),
+			stacks[3]->GetNTrack(),
+			stacks[4]->GetNTrack());
+#endif
+		energies[iDest] +=
+		    aStackedTrack->GetTrack()->GetDynamicParticle()->GetTotalEnergy();
+
+		G4int dy1 = stacks[iDest]->GetNTrack() - stacks[iDest]->GetSafetyValve1();
+		G4int dy2 = stacks[fTurn]->GetNTrack() - stacks[fTurn]->GetSafetyValve2();
+
+		if (dy1 > 0 || dy1 > dy2 ||
+		    (iDest == 2 &&
+		     stacks[iDest]->GetNTrack() < 50 && energies[iDest] < energies[fTurn])) {
+			fTurn = iDest;
+		}
+
+		if (n_stackedTrack() > maxNTracks)
+			maxNTracks = n_stackedTrack();
 
-  if(n_stackedTrack()>maxNTracks) maxNTracks = n_stackedTrack();
+		// dumpStatistics();
+	}
 }
 
 void G4SmartTrackStack::clear()
 {
-  for(int i=0;i<nTurn;i++)
-  { stacks[i]->clear(); }
+	for (int i = 0; i < nTurn; i++)
+		stacks[i]->clear();
 }
-
-
diff -urpN geant4.9.5.p01/source/event/src/G4StackManager.cc geant4.9.5.p01-new/source/event/src/G4StackManager.cc
--- geant4.9.5.p01/source/event/src/G4StackManager.cc	2012-03-27 10:42:36.000000000 +0300
+++ geant4.9.5.p01-new/source/event/src/G4StackManager.cc	2012-09-07 21:07:16.407961578 +0300
@@ -45,11 +45,11 @@ G4StackManager::G4StackManager()
   urgentStack = new G4SmartTrackStack;
   G4cout<<"+++ G4StackManager uses G4SmartTrackStack. +++"<<G4endl;
 #else
-  urgentStack = new G4TrackStack;
+  urgentStack = new G4TrackStackDQ;
 //  G4cout<<"+++ G4StackManager uses ordinary G4TrackStack. +++"<<G4endl;
 #endif
-  waitingStack = new G4TrackStack;
-  postponeStack = new G4TrackStack;
+  waitingStack = new G4TrackStackDQ;
+  postponeStack = new G4TrackStackDQ;
 }
 
 G4StackManager::~G4StackManager()
@@ -190,7 +190,7 @@ G4Track * G4StackManager::PopNextTrack(G
 void G4StackManager::ReClassify()
 {
   G4StackedTrack * aStackedTrack;
-  G4TrackStack tmpStack;
+  G4TrackStackDQ tmpStack;
 
   if( !userStackingAction ) return;
   if( GetNUrgentTrack() == 0 ) return;
@@ -248,7 +248,7 @@ G4int G4StackManager::PrepareNewEvent()
 #endif
 
     G4StackedTrack * aStackedTrack;
-    G4TrackStack tmpStack;
+    G4TrackStackDQ tmpStack;
 
     postponeStack->TransferTo(&tmpStack);
 
@@ -307,7 +307,7 @@ void G4StackManager::SetNumberOfAddition
   {
     for(int i=numberOfAdditionalWaitingStacks;i<iAdd;i++)
     {
-      G4TrackStack* newStack = new G4TrackStack;
+      G4TrackStackDQ* newStack = new G4TrackStackDQ;
       additionalWaitingStacks.push_back(newStack);
     }
     numberOfAdditionalWaitingStacks = iAdd;
@@ -325,7 +325,7 @@ void G4StackManager::TransferStackedTrac
 {
   if(origin==destination) return;
   if(origin==fKill) return;
-  G4TrackStack* originStack = 0;
+  G4TrackStackDQ* originStack = 0;
   switch(origin)
   {
     case fUrgent:
@@ -352,7 +352,7 @@ void G4StackManager::TransferStackedTrac
   } 
   else
   {
-    G4TrackStack* targetStack = 0;
+    G4TrackStackDQ* targetStack = 0;
     switch(destination)
     {
       case fUrgent:
@@ -386,7 +386,7 @@ void G4StackManager::TransferOneStackedT
 {
   if(origin==destination) return;
   if(origin==fKill) return;
-  G4TrackStack* originStack = 0;
+  G4TrackStackDQ* originStack = 0;
   switch(origin)
   {
     case fUrgent:
@@ -420,7 +420,7 @@ void G4StackManager::TransferOneStackedT
   } 
   else
   {
-    G4TrackStack* targetStack = 0;
+    G4TrackStackDQ* targetStack = 0;
     switch(destination)
     {
       case fUrgent:
diff -urpN geant4.9.5.p01/source/event/src/G4TrackStackDQ.cc geant4.9.5.p01-new/source/event/src/G4TrackStackDQ.cc
--- geant4.9.5.p01/source/event/src/G4TrackStackDQ.cc	1970-01-01 02:00:00.000000000 +0200
+++ geant4.9.5.p01-new/source/event/src/G4TrackStackDQ.cc	2012-09-07 21:07:16.408225514 +0300
@@ -0,0 +1,178 @@
+//
+// ********************************************************************
+// * 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.          *
+// ********************************************************************
+
+#include "G4TrackStackDQ.hh"
+#include "G4SmartTrackStack.hh"
+#include "G4VTrajectory.hh"
+#include "G4Electron.hh"
+
+G4TrackStackDQ::G4TrackStackDQ()
+	:n_stackedTrack(0), firstStackedTrack(0), lastStackedTrack(0)
+{
+	maxNTracks = 0;
+
+        G4cout << "+++ Using the DQ Track Stack +++ " << G4endl;
+
+	dqStackedTracks.reserve(5000);
+	nstick = 100;
+	safetyValve1 = 4000;
+	safetyValve2 = safetyValve1 - nstick;
+}
+
+G4TrackStackDQ::G4TrackStackDQ(G4ParticleDefinition *partDef)
+	:n_stackedTrack(0), firstStackedTrack(0), lastStackedTrack(0)
+{
+	maxNTracks = 0;
+
+	G4cout << "+++ Using the DQ Track Stack +++ " << G4endl;
+
+	// Currently electrons have the same values as the others stacks,
+	// but this overloaded constructor allows to assign different
+	// parameters on a per particle/stack basis
+	if (partDef == G4Electron::Definition()) {
+		G4cout << "+++ Electron stack created +++" << G4endl;
+		dqStackedTracks.reserve(5000);
+		nstick = 100;
+		safetyValve1 = 4000;
+		safetyValve2 = safetyValve1 - nstick;
+	} else {
+		G4cout << "+++ Default stack created +++" << G4endl;
+                dqStackedTracks.reserve(5000);
+                nstick = 100;
+                safetyValve1 = 4000;
+                safetyValve2 = safetyValve1 - nstick;
+	}
+}
+
+G4TrackStackDQ::~G4TrackStackDQ()
+{
+	clear();
+}
+
+const G4TrackStackDQ & G4TrackStackDQ::operator=(const G4TrackStackDQ &right) 
+{
+	n_stackedTrack    = right.n_stackedTrack;
+	firstStackedTrack = right.firstStackedTrack;
+	lastStackedTrack  = right.lastStackedTrack;
+
+	return *this; 
+}
+
+int G4TrackStackDQ::operator==(const G4TrackStackDQ &right) const
+{
+	return (firstStackedTrack == right.firstStackedTrack);
+}
+
+int G4TrackStackDQ::operator!=(const G4TrackStackDQ &right) const
+{
+	return (firstStackedTrack != right.firstStackedTrack);
+}
+
+void G4TrackStackDQ::TransferTo(G4TrackStackDQ *aStack)
+{
+	if (aStack->n_stackedTrack) {
+		aStack->dqStackedTracks.insert(
+			aStack->dqStackedTracks.end(),
+			dqStackedTracks.begin(),
+			dqStackedTracks.end());
+
+		aStack->lastStackedTrack = lastStackedTrack;
+		aStack->n_stackedTrack += n_stackedTrack;
+	} else {
+		*aStack = *this;
+	}
+	
+	n_stackedTrack    = 0;
+	firstStackedTrack = 0;
+	lastStackedTrack  = 0;
+}
+
+void G4TrackStackDQ::TransferTo(G4SmartTrackStack *aStack)
+{
+	while (n_stackedTrack)
+		aStack->PushToStack(PopFromStack());
+}
+
+G4StackedTrack *G4TrackStackDQ::PopFromStack()
+{
+	G4StackedTrack *aStackedTrack = NULL;
+
+	if (n_stackedTrack) {
+		aStackedTrack = dqStackedTracks.back();
+		dqStackedTracks.pop_back();
+		n_stackedTrack--;
+
+		firstStackedTrack = dqStackedTracks.front();
+		if (!dqStackedTracks.empty())
+			lastStackedTrack  = dqStackedTracks.back();
+		else
+			lastStackedTrack = NULL;
+	}
+
+	return aStackedTrack;
+}
+
+void G4TrackStackDQ::PushToStack(G4StackedTrack *aStackedTrack)
+{
+	if (aStackedTrack) {
+		dqStackedTracks.push_back(aStackedTrack);
+		lastStackedTrack = aStackedTrack;
+		n_stackedTrack++;
+		if (n_stackedTrack > maxNTracks)
+			maxNTracks = n_stackedTrack;
+	}
+}
+
+void G4TrackStackDQ::clear()
+{
+	// delete tracks in the stack
+	std::vector<G4StackedTrack*>::iterator itr;
+	for (itr  = dqStackedTracks.begin();
+	     itr != dqStackedTracks.end(); itr++) {  
+		delete (*itr)->GetTrack();
+		delete (*itr)->GetTrajectory();
+		delete (*itr);
+	}
+
+	n_stackedTrack    = 0;
+	firstStackedTrack = 0;
+	lastStackedTrack  = 0;
+}
+
+G4double
+G4TrackStackDQ::getTotalEnergy(void) const
+{
+	G4double totalEnergy = 0.0f;
+
+	std::vector<G4StackedTrack*>::const_iterator itr;
+
+	for (itr  = dqStackedTracks.begin();
+	     itr != dqStackedTracks.end(); itr++) {
+		totalEnergy +=
+		    (*itr)->GetTrack()->GetDynamicParticle()->GetTotalEnergy();
+	}
+
+	return totalEnergy;
+}
