Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4ElectronOccupancy class implementation 27 // 28 // Author: Hisaya Kurashige, 17 Aug 1999 29 // --------------------------------------------------------------- 30 31 #include "G4ElectronOccupancy.hh" 32 33 #include <sstream> 34 35 G4Allocator<G4ElectronOccupancy>*& aElectronOccupancyAllocator() 36 { 37 G4ThreadLocalStatic G4Allocator<G4ElectronOccupancy>* _instance = nullptr; 38 return _instance; 39 } 40 41 G4ElectronOccupancy::G4ElectronOccupancy(G4int sizeOrbit) : theSizeOfOrbit(sizeOrbit) 42 { 43 // check size 44 if ((theSizeOfOrbit < 1) || (theSizeOfOrbit > MaxSizeOfOrbit)) { 45 theSizeOfOrbit = MaxSizeOfOrbit; 46 } 47 48 // allocate and clear the array of theOccupancies 49 theOccupancies = new G4int[theSizeOfOrbit]; 50 for (G4int index = 0; index < theSizeOfOrbit; ++index) { 51 theOccupancies[index] = 0; 52 } 53 54 theTotalOccupancy = 0; 55 } 56 57 G4ElectronOccupancy::~G4ElectronOccupancy() 58 { 59 theSizeOfOrbit = -1; 60 61 delete[] theOccupancies; 62 theOccupancies = nullptr; 63 theTotalOccupancy = 0; 64 } 65 66 G4ElectronOccupancy::G4ElectronOccupancy(const G4ElectronOccupancy& right) 67 { 68 theSizeOfOrbit = right.theSizeOfOrbit; 69 70 // allocate and clear the array of theOccupancies 71 theOccupancies = new G4int[theSizeOfOrbit]; 72 for (G4int index = 0; index < theSizeOfOrbit; ++index) { 73 theOccupancies[index] = right.theOccupancies[index]; 74 } 75 76 theTotalOccupancy = right.theTotalOccupancy; 77 } 78 79 G4ElectronOccupancy& G4ElectronOccupancy::operator=(const G4ElectronOccupancy& right) 80 { 81 if (this != &right) { 82 theSizeOfOrbit = right.theSizeOfOrbit; 83 84 // allocate and clear the array of theOccupancies 85 delete[] theOccupancies; 86 theOccupancies = new G4int[theSizeOfOrbit]; 87 for (G4int index = 0; index < theSizeOfOrbit; ++index) { 88 theOccupancies[index] = right.theOccupancies[index]; 89 } 90 91 theTotalOccupancy = right.theTotalOccupancy; 92 } 93 return *this; 94 } 95 96 G4bool G4ElectronOccupancy::operator==(const G4ElectronOccupancy& right) const 97 { 98 G4bool value = true; 99 for (G4int index = 0; index < MaxSizeOfOrbit; ++index) { 100 if ((index < theSizeOfOrbit) && (index < right.theSizeOfOrbit)) { 101 value = value && (theOccupancies[index] == right.theOccupancies[index]); 102 } 103 else if ((index < theSizeOfOrbit) && (index >= right.theSizeOfOrbit)) { 104 value = value && (!static_cast<G4bool>(theOccupancies[index])); 105 } 106 else if ((index >= theSizeOfOrbit) && (index < right.theSizeOfOrbit)) { 107 value = value && (!static_cast<G4bool>(right.theOccupancies[index])); 108 } 109 } 110 return value; 111 } 112 113 G4bool G4ElectronOccupancy::operator!=(const G4ElectronOccupancy& right) const 114 { 115 return !(*this == right); 116 } 117 118 void G4ElectronOccupancy::DumpInfo() const 119 { 120 G4cout << " -- Electron Occupancy -- " << G4endl; 121 for (G4int index = 0; index < theSizeOfOrbit; ++index) { 122 G4cout << " " << index << "-th orbit " << theOccupancies[index] << G4endl; 123 } 124 } 125 126 G4int G4ElectronOccupancy::AddElectron(G4int orbit, G4int number) 127 { 128 G4int value = 0; 129 if (orbit >= theSizeOfOrbit) { 130 std::ostringstream smsg; 131 smsg << "Orbit (" << orbit << ") exceeds the maximum(" << theSizeOfOrbit - 1 << ") "; 132 G4String msg = smsg.str(); 133 G4Exception("G4ElectronOccupancy::AddElectron()", "PART131", JustWarning, msg); 134 } 135 else if (orbit >= 0) { 136 theOccupancies[orbit] += number; 137 theTotalOccupancy += number; 138 value = number; 139 } 140 return value; 141 } 142 143 G4int G4ElectronOccupancy::RemoveElectron(G4int orbit, G4int number) 144 { 145 G4int value = 0; 146 if (orbit >= theSizeOfOrbit) { 147 std::ostringstream smsg; 148 smsg << "Orbit (" << orbit << ") exceeds the maximum(" << theSizeOfOrbit - 1 << ") "; 149 G4String msg = smsg.str(); 150 G4Exception("G4ElectronOccupancy::RemoveElectron()", "PART131", JustWarning, msg); 151 } 152 else if (orbit >= 0) { 153 if (theOccupancies[orbit] < number) number = theOccupancies[orbit]; 154 theOccupancies[orbit] -= number; 155 theTotalOccupancy -= number; 156 value = number; 157 } 158 return value; 159 } 160