Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 27 // Author: Ivana Hrivnacova, 01/09/2020 (ivan 28 29 #include "G4NtupleBookingManager.hh" 30 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisUtilities.hh" 32 33 using namespace G4Analysis; 34 using std::to_string; 35 36 // 37 // private template functions 38 // 39 40 //____________________________________________ 41 G4NtupleBookingManager::G4NtupleBookingManager 42 const G4AnalysisManagerState& state) 43 : G4BaseAnalysisManager(state) 44 {} 45 46 //____________________________________________ 47 G4NtupleBookingManager::~G4NtupleBookingManage 48 { 49 for ( auto ntupleBooking : fNtupleBookingVec 50 delete ntupleBooking; 51 } 52 } 53 54 //____________________________________________ 55 G4NtupleBooking* 56 G4NtupleBookingManager::GetNtupleBookingInFunc 57 G4int id, std::string_view functionName, G4b 58 { 59 auto index = id - fFirstId; 60 if ( index < 0 || index >= G4int(fNtupleBook 61 if ( warn) { 62 Warn("Ntuple booking " + to_string(id) + 63 fkClass, functionName); 64 } 65 return nullptr; 66 } 67 68 return fNtupleBookingVector[index]; 69 } 70 71 //____________________________________________ 72 G4bool G4NtupleBookingManager::CheckName( 73 const G4String& name, const G4String& object 74 { 75 if (name.size() == 0u) { 76 Warn("Empty " + objectType + " name is not 77 objectType + " was not created.", fkCla 78 return false; 79 } 80 return true; 81 } 82 83 // 84 // protected functions 85 // 86 87 //____________________________________________ 88 G4bool G4NtupleBookingManager::IsEmpty() const 89 { 90 return fNtupleBookingVector.size() == 0u; 91 } 92 93 //____________________________________________ 94 G4int G4NtupleBookingManager::CreateNtuple( 95 const G4String& name, const G4String& title) 96 { 97 if ( ! CheckName(name, "Ntuple") ) return kI 98 99 Message(kVL4, "create", "ntuple booking", na 100 101 G4int index = 0; 102 G4NtupleBooking* ntupleBooking = nullptr; 103 if (fFreeIds.empty()) { 104 index = (G4int)fNtupleBookingVector.size() 105 ntupleBooking = new G4NtupleBooking(); 106 fNtupleBookingVector.push_back(ntupleBooki 107 ntupleBooking->fNtupleId = G4int(index + f 108 } 109 else { 110 // Get the first freed Id 111 index = *(fFreeIds.begin()) - GetFirstId() 112 ntupleBooking = fNtupleBookingVector[index 113 ntupleBooking->fNtupleBooking = tools::ntu 114 ntupleBooking->Reset(); 115 116 // Remove the id from the set 117 fFreeIds.erase(fFreeIds.begin()); 118 } 119 120 // Save name & title in ntuple booking 121 ntupleBooking->fNtupleBooking.set_name(name) 122 ntupleBooking->fNtupleBooking.set_title(titl 123 124 // Update related data 125 fLockFirstId = true; 126 fCurrentNtupleId = ntupleBooking->fNtupleId; 127 128 Message(kVL2, "create", "ntuple booking", 129 name + " ntupleId " + to_string(ntupleBoo 130 131 return fCurrentNtupleId; 132 } 133 134 //____________________________________________ 135 G4int G4NtupleBookingManager::CreateNtupleICol 136 137 { 138 return CreateNtupleIColumn(GetCurrentNtupleI 139 } 140 141 //____________________________________________ 142 G4int G4NtupleBookingManager::CreateNtupleFCol 143 144 { 145 return CreateNtupleFColumn(GetCurrentNtupleI 146 } 147 148 //____________________________________________ 149 G4int G4NtupleBookingManager::CreateNtupleDCol 150 151 { 152 return CreateNtupleDColumn(GetCurrentNtupleI 153 } 154 155 //____________________________________________ 156 G4int G4NtupleBookingManager::CreateNtupleSCol 157 158 { 159 return CreateNtupleSColumn(GetCurrentNtupleI 160 } 161 162 //____________________________________________ 163 G4NtupleBooking* G4NtupleBookingManager::Fini 164 { 165 return FinishNtuple(GetCurrentNtupleId()); 166 } 167 168 //____________________________________________ 169 G4int G4NtupleBookingManager::CreateNtupleICol 170 G4int ntupleId, const G4String& name, std::v 171 { 172 return CreateNtupleTColumn<int>(ntupleId, na 173 } 174 175 //____________________________________________ 176 G4int G4NtupleBookingManager::CreateNtupleFCol 177 G4int ntupleId, const G4String& name, std::v 178 { 179 return CreateNtupleTColumn<float>(ntupleId, 180 } 181 182 //____________________________________________ 183 G4int G4NtupleBookingManager::CreateNtupleDCol 184 G4int ntupleId, const G4String& name, std::v 185 { 186 return CreateNtupleTColumn<double>(ntupleId, 187 } 188 189 //____________________________________________ 190 G4int G4NtupleBookingManager::CreateNtupleSCol 191 G4int ntupleId, const G4String& name, std::v 192 { 193 return CreateNtupleTColumn<std::string>(ntup 194 } 195 196 //____________________________________________ 197 G4NtupleBooking* G4NtupleBookingManager::Fini 198 G4int ntupleId) 199 { 200 // Nothing to be done for booking, 201 return GetNtupleBookingInFunction(ntupleId, 202 } 203 204 //____________________________________________ 205 G4bool G4NtupleBookingManager::SetFirstNtupleC 206 { 207 if ( fLockFirstNtupleColumnId ) { 208 Warn("Cannot set FirstNtupleColumnId as it 209 fkClass, "SetFirstNtupleColumnId"); 210 return false; 211 } 212 213 fFirstNtupleColumnId = firstId; 214 return true; 215 } 216 217 //____________________________________________ 218 void G4NtupleBookingManager::SetActivation( 219 G4bool activation) 220 { 221 for ( auto ntupleBooking : fNtupleBookingVec 222 ntupleBooking->fActivation = activation; 223 } 224 } 225 226 //____________________________________________ 227 void G4NtupleBookingManager::SetActivation( 228 G4int ntupleId, G4bool activation) 229 { 230 auto ntupleBooking 231 = GetNtupleBookingInFunction(ntupleId, "Se 232 if (ntupleBooking == nullptr) return; 233 234 ntupleBooking->fActivation = activation; 235 } 236 237 //____________________________________________ 238 G4bool G4NtupleBookingManager::GetActivation( 239 G4int ntupleId) const 240 { 241 auto ntupleBooking 242 = GetNtupleBookingInFunction(ntupleId, "Ge 243 if (ntupleBooking == nullptr) return false; 244 245 return ntupleBooking->fActivation; 246 } 247 248 //____________________________________________ 249 void G4NtupleBookingManager::SetFileName( 250 const G4String& fileName) 251 { 252 for ( auto ntupleBooking : fNtupleBookingVec 253 ntupleBooking->fFileName = fileName; 254 } 255 } 256 257 //____________________________________________ 258 void G4NtupleBookingManager::SetFileName( 259 G4int ntupleId, const G4String& fileName) 260 { 261 auto ntupleBooking 262 = GetNtupleBookingInFunction(ntupleId, "Se 263 if (ntupleBooking == nullptr) return; 264 265 // Do nothing if file name does not change 266 if ( ntupleBooking->fFileName == fileName ) 267 268 auto ntupleFileName = fileName; 269 auto extension = GetExtension(fileName); 270 if (extension.size() != 0u) { 271 // Check if valid extension (if present) 272 auto output = G4Analysis::GetOutput(extens 273 if ( output == G4AnalysisOutput::kNone ) { 274 Warn("The file extension " + extension + 275 fkClass, "SetFileName"); 276 return; 277 } 278 } 279 else { 280 if (fFileType.size() != 0u) { 281 //add extension if missing and file type 282 ntupleFileName = fileName + "." + fFileT 283 } 284 } 285 286 // Save the fileName in booking 287 // If extension is still missing (possible w 288 // it will be completed with the default one 289 ntupleBooking->fFileName = std::move(ntupleF 290 } 291 292 //____________________________________________ 293 G4String G4NtupleBookingManager::GetFileName( 294 G4int ntupleId) const 295 { 296 auto ntupleBooking 297 = GetNtupleBookingInFunction(ntupleId, "Ge 298 if (ntupleBooking == nullptr) return ""; 299 300 return ntupleBooking->fFileName; 301 } 302 303 //____________________________________________ 304 void G4NtupleBookingManager::ClearData() 305 { 306 for ( auto ntupleBooking : fNtupleBookingVec 307 delete ntupleBooking; 308 } 309 fNtupleBookingVector.clear(); 310 fLockFirstNtupleColumnId = false; 311 312 Message(G4Analysis::kVL2, "clear", "ntupleBo 313 } 314 315 //____________________________________________ 316 G4bool G4NtupleBookingManager::Delete(G4int id 317 { 318 Message(kVL4, "delete", "ntuple booking ntup 319 320 auto ntupleBooking = GetNtupleBookingInFunct 321 322 if (ntupleBooking == nullptr) return false; 323 324 // Update ntuple booking 325 ntupleBooking->SetDeleted(true, keepSetting) 326 327 // Register freed Id 328 fFreeIds.insert(id); 329 330 Message(G4Analysis::kVL2, "delete", "ntuple 331 332 return true; 333 } 334 335 //____________________________________________ 336 G4bool G4NtupleBookingManager::List(std::ostre 337 { 338 // Save current output stream formatting 339 std::ios_base::fmtflags outputFlags(output.f 340 341 // Define optimal field widths 342 size_t maxNameLength = 0; 343 size_t maxTitleLength = 0; 344 // size_t maxEntries = 0; 345 size_t nofActive = 0; 346 for (auto g4NtupleBooking : fNtupleBookingVe 347 const auto& ntupleBooking = g4NtupleBookin 348 if (ntupleBooking.name().length() > maxNam 349 maxNameLength = ntupleBooking.name().len 350 } 351 if (ntupleBooking.title().length() > maxTi 352 maxTitleLength = ntupleBooking.title().l 353 } 354 // if (ntuple->entries() > maxEntries) { 355 // maxEntries = ntuple->entries(); 356 // } 357 if (g4NtupleBooking->fActivation) { 358 ++nofActive; 359 } 360 } 361 size_t maxIdWidth = std::to_string(fNtupleBo 362 // update strings width for added double quo 363 maxNameLength += 2; 364 maxTitleLength += 2; 365 366 // List general info 367 output << "Ntuple: " << nofActive << " activ 368 if (! onlyIfActive) { 369 output << " of " << GetNofNtuples(true) < 370 } 371 output << G4endl; 372 373 // List objects 374 G4int counter = 0; 375 for (auto g4NtupleBooking : fNtupleBookingVe 376 const auto& ntupleBooking = g4NtupleBookin 377 378 // skip inactivated objcets 379 if (fState.GetIsActivation() && onlyIfActi 380 381 // print selected info 382 output << " id: " << std::setw((G4int)ma 383 << " name: \"" << std::setw((G4int)maxNa 384 << " title: \"" << std::setw((G4int)maxT 385 // << " entries: " << std::setw((G4int)m 386 if (! onlyIfActive) { 387 output << " active: " << std::boolalpha 388 } 389 output << G4endl; 390 } 391 392 // Restore the output stream formatting 393 output.flags(outputFlags); 394 395 return output.good(); 396 } 397 398 // 399 // public methods 400 // 401 402 //____________________________________________ 403 void G4NtupleBookingManager::SetFileType(const 404 { 405 // do nothing if file type is defined and is 406 if ( fFileType == fileType ) return; 407 408 // save the type 409 fFileType = fileType; 410 411 // Give warning and redefine file extension 412 // with file name of different fileTypes 413 for ( auto ntupleBooking : fNtupleBookingVec 414 if ((ntupleBooking->fFileName).size() == 0 415 416 auto extension = GetExtension(ntupleBookin 417 if ( fFileType == extension ) continue; 418 419 // multiple file types are not suported 420 auto baseFileName = GetBaseName(ntupleBook 421 auto ntupleFileName = baseFileName + "." + 422 if (extension.size() != 0u) { 423 Warn("Writing ntuples in files of differ 424 fFileType + ", " + extension + " is 425 fkClass, "SetFileType"); 426 } 427 428 // Save the info in ntuple description 429 ntupleBooking->fFileName = std::move(ntupl 430 } 431 } 432 433 //____________________________________________ 434 tools::ntuple_booking* G4NtupleBookingManager: 435 G4bool warn, G4bool onlyIfActive) const 436 { 437 return GetNtuple(fFirstId, warn, onlyIfActiv 438 } 439 440 //____________________________________________ 441 tools::ntuple_booking* G4NtupleBookingManager: 442 G4int ntupleId, G4bool warn, G4bool onlyIfAc 443 { 444 auto g4Booking = GetNtupleBookingInFunction( 445 446 if (g4Booking == nullptr) return nullptr; 447 448 if ( ( g4Booking->GetDeleted() ) || 449 ( onlyIfActive && (! g4Booking->fActiva 450 451 return &(g4Booking->fNtupleBooking); 452 } 453