[807] | 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 | // |
---|
| 27 | // File name: RadmonAnalysis.cc |
---|
| 28 | // Creation date: Nov 2005 |
---|
| 29 | // Main author: Riccardo Capra <capra@ge.infn.it> |
---|
| 30 | // |
---|
| 31 | // Id: $Id: RadmonAnalysis.cc,v 1.2.2.2 2006/06/29 16:07:35 gunter Exp $ |
---|
| 32 | // Tag: $Name: geant4-09-01-patch-02 $ |
---|
| 33 | // |
---|
| 34 | |
---|
| 35 | // Include files |
---|
| 36 | #include "RadmonAnalysis.hh" |
---|
| 37 | |
---|
| 38 | #include "RadmonVDataAnalysisFactory.hh" |
---|
| 39 | #include "RadmonVDataAnalysis.hh" |
---|
| 40 | #include "RadmonVAnalysisLayout.hh" |
---|
| 41 | #include "RadmonSensitiveDetector.hh" |
---|
| 42 | #include "G4UIcommand.hh" |
---|
| 43 | #include "G4Event.hh" |
---|
| 44 | #include "G4SDManager.hh" |
---|
| 45 | #include "G4RunManager.hh" |
---|
| 46 | #include "G4Run.hh" |
---|
| 47 | #include <fstream> |
---|
| 48 | #include <exception> |
---|
| 49 | |
---|
| 50 | using namespace std; |
---|
| 51 | |
---|
| 52 | RadmonAnalysis :: RadmonAnalysis(RadmonVAnalysisLayout * layout, RadmonVDataAnalysisFactory * factory, AIDA::IAnalysisFactory * analysis) |
---|
| 53 | : |
---|
| 54 | analysisLayout(layout), |
---|
| 55 | dataFactory(factory), |
---|
| 56 | analysisFactory(analysis), |
---|
| 57 | treeFactory(0), |
---|
| 58 | tree(0), |
---|
| 59 | changed(true) |
---|
| 60 | { |
---|
| 61 | if (analysisLayout==0) |
---|
| 62 | G4Exception("RadmonAnalysis::RadmonAnalysis: layout==0."); |
---|
| 63 | |
---|
| 64 | if (dataFactory==0) |
---|
| 65 | G4Exception("RadmonAnalysis::RadmonAnalysis: factory==0."); |
---|
| 66 | |
---|
| 67 | if (analysisFactory==0) |
---|
| 68 | G4Exception("RadmonAnalysis::RadmonAnalysis: analysis==0."); |
---|
| 69 | |
---|
| 70 | analysisLayout->AttachObserver(this); |
---|
| 71 | |
---|
| 72 | treeFactory=analysisFactory->createTreeFactory(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | RadmonAnalysis :: ~RadmonAnalysis() |
---|
| 78 | { |
---|
| 79 | analysisLayout->DetachObserver(this); |
---|
| 80 | |
---|
| 81 | Destruct(); |
---|
| 82 | |
---|
| 83 | delete treeFactory; |
---|
| 84 | delete analysisFactory; |
---|
| 85 | delete dataFactory; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | void RadmonAnalysis :: OnLayoutChange(void) |
---|
| 93 | { |
---|
| 94 | changed=true; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | void RadmonAnalysis :: OnBeginOfEvent(const G4Event * /* event */) |
---|
| 102 | { |
---|
| 103 | if (!changed) |
---|
| 104 | return; |
---|
| 105 | |
---|
| 106 | Destruct(); |
---|
| 107 | |
---|
| 108 | list<G4String> tupleLabels; |
---|
| 109 | G4String columns("int runId; int eventId"); |
---|
| 110 | |
---|
| 111 | if (!InitializeSensitiveDetectorsList(tupleLabels, columns)) |
---|
| 112 | { |
---|
| 113 | changed=true; |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if (!OpenFile()) |
---|
| 118 | { |
---|
| 119 | changed=true; |
---|
| 120 | return; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if (!InitializeTuple(tupleLabels, columns)) |
---|
| 124 | { |
---|
| 125 | changed=false; |
---|
| 126 | return; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | indexRunId=tuple->findColumn("runId"); |
---|
| 130 | indexEventId=tuple->findColumn("eventId"); |
---|
| 131 | |
---|
| 132 | changed=false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | void RadmonAnalysis :: OnEndOfEvent(const G4Event * event) |
---|
| 140 | { |
---|
| 141 | if (!tuple) |
---|
| 142 | return; |
---|
| 143 | |
---|
| 144 | if (sensitiveDetectorsList.empty()) |
---|
| 145 | return; |
---|
| 146 | |
---|
| 147 | SensitiveDetectorsList::iterator sd(sensitiveDetectorsList.begin()); |
---|
| 148 | const SensitiveDetectorsList::iterator sdEnd(sensitiveDetectorsList.end()); |
---|
| 149 | |
---|
| 150 | G4SDManager * manager(G4SDManager::GetSDMpointer()); |
---|
| 151 | |
---|
| 152 | if (!manager) |
---|
| 153 | return; |
---|
| 154 | |
---|
| 155 | tuple->fill(indexRunId, G4RunManager::GetRunManager()->GetCurrentRun()->GetRunID()); |
---|
| 156 | tuple->fill(indexEventId, event->GetEventID()); |
---|
| 157 | |
---|
| 158 | while (sd!=sdEnd) |
---|
| 159 | { |
---|
| 160 | RadmonSensitiveDetector * radmonSensitiveDetector(sd->first); |
---|
| 161 | DataAnalysesList * dataAnalysesList(sd->second); |
---|
| 162 | |
---|
| 163 | RadmonHitsCollection * collection(radmonSensitiveDetector->GetDetectorCollection()); |
---|
| 164 | |
---|
| 165 | DataAnalysesList::iterator i(dataAnalysesList->begin()); |
---|
| 166 | DataAnalysesList::iterator end(dataAnalysesList->end()); |
---|
| 167 | |
---|
| 168 | while (i!=end) |
---|
| 169 | { |
---|
| 170 | (*i)->StoreIntoTuple(collection, tuple); |
---|
| 171 | i++; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | sd++; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | tuple->addRow(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | G4bool RadmonAnalysis :: InitializeSensitiveDetectorsList(list<G4String> & tupleLabels, G4String & columns) |
---|
| 185 | { |
---|
| 186 | G4SDManager * manager(G4SDManager::GetSDMpointer()); |
---|
| 187 | |
---|
| 188 | if (!manager) |
---|
| 189 | return false; |
---|
| 190 | |
---|
| 191 | RadmonVAnalysisLayout const * const constLayout(analysisLayout); |
---|
| 192 | G4int n(constLayout->GetNSensitiveDetectors()); |
---|
| 193 | |
---|
| 194 | tupleLabels.clear(); |
---|
| 195 | |
---|
| 196 | while (n>0) |
---|
| 197 | { |
---|
| 198 | n--; |
---|
| 199 | |
---|
| 200 | const G4String & sensitiveDetectorLabel(constLayout->GetSensitiveDetectorLabel(n)); |
---|
| 201 | G4VSensitiveDetector * sensitiveDetector(manager->FindSensitiveDetector(sensitiveDetectorLabel, false)); |
---|
| 202 | |
---|
| 203 | if (!sensitiveDetector) |
---|
| 204 | { |
---|
| 205 | G4cout << "RadmonAnalysis::InitializeSensitiveDetectorsList: \"" << sensitiveDetectorLabel << "\" is not attached to any volume." << G4endl; |
---|
| 206 | continue; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | RadmonSensitiveDetector * radmonSensitiveDetector; |
---|
| 210 | try |
---|
| 211 | { |
---|
| 212 | radmonSensitiveDetector=dynamic_cast<RadmonSensitiveDetector *>(sensitiveDetector); |
---|
| 213 | } |
---|
| 214 | catch (exception e) |
---|
| 215 | { |
---|
| 216 | G4cout << "RadmonAnalysis::InitializeSensitiveDetectorsList: \"" << sensitiveDetectorLabel << "\" is not a RadmonSensitiveDetector object." << G4endl; |
---|
| 217 | radmonSensitiveDetector=0; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | if (!radmonSensitiveDetector) |
---|
| 221 | continue; |
---|
| 222 | |
---|
| 223 | const G4String & sensitiveDetectorType(constLayout->GetSensitiveDetectorType(sensitiveDetectorLabel)); |
---|
| 224 | |
---|
| 225 | G4int m(constLayout->GetNDataAnalyses(sensitiveDetectorType)); |
---|
| 226 | |
---|
| 227 | if (m==0) |
---|
| 228 | continue; |
---|
| 229 | |
---|
| 230 | DataAnalysesList * dataAnalysesList(new DataAnalysesList); |
---|
| 231 | |
---|
| 232 | G4String sensitiveDetectorColumns; |
---|
| 233 | while (m>0) |
---|
| 234 | { |
---|
| 235 | m--; |
---|
| 236 | |
---|
| 237 | const G4String & dataAnalysisLabel(constLayout->GetDataAnalysisLabel(sensitiveDetectorType, m)); |
---|
| 238 | const G4String & dataAnalysisType(constLayout->GetDataAnalysisType(sensitiveDetectorType, dataAnalysisLabel)); |
---|
| 239 | |
---|
| 240 | RadmonVDataAnalysis * dataAnalysis(dataFactory->CreateDataAnalysis(dataAnalysisType)); |
---|
| 241 | |
---|
| 242 | if (dataAnalysis==0) |
---|
| 243 | { |
---|
| 244 | G4cout << "RadmonAnalysis::InitializeSensitiveDetectorsList: \"" << dataAnalysisLabel << "\" defined in \"" << sensitiveDetectorType << "\" not found in the analysis factory." << G4endl; |
---|
| 245 | continue; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | G4String tupleLabel(sensitiveDetectorLabel); |
---|
| 249 | tupleLabel+='_'; |
---|
| 250 | tupleLabel+=dataAnalysisLabel; |
---|
| 251 | |
---|
| 252 | G4String dataAnalysisColumns(dataAnalysis->ObtainColumnsDeclaration(tupleLabel)); |
---|
| 253 | |
---|
| 254 | if (dataAnalysisColumns.empty()) |
---|
| 255 | continue; |
---|
| 256 | |
---|
| 257 | dataAnalysesList->push_back(dataAnalysis); |
---|
| 258 | tupleLabels.push_back(tupleLabel); |
---|
| 259 | |
---|
| 260 | columns+="; "; |
---|
| 261 | columns+=dataAnalysisColumns; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (dataAnalysesList->empty()) |
---|
| 265 | delete dataAnalysesList; |
---|
| 266 | else |
---|
| 267 | sensitiveDetectorsList.push_back(SensitiveDetectorPair(radmonSensitiveDetector, dataAnalysesList)); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | return !sensitiveDetectorsList.empty(); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | G4bool RadmonAnalysis :: OpenFile(void) |
---|
| 276 | { |
---|
| 277 | RadmonVAnalysisLayout const * const constLayout(analysisLayout); |
---|
| 278 | const G4String & original(constLayout->GetOutputFileName()); |
---|
| 279 | G4String fileName(original); |
---|
| 280 | |
---|
| 281 | for(G4int index(1); ; index++) |
---|
| 282 | { |
---|
| 283 | ifstream in(fileName); |
---|
| 284 | |
---|
| 285 | if (!in.is_open()) |
---|
| 286 | break; |
---|
| 287 | |
---|
| 288 | fileName=(original+'_')+G4UIcommand::ConvertToString(index); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | if (fileName!=original) |
---|
| 292 | G4cout << "RadmonAnalysis::OpenFile: \"" << original << "\" just exists. \"" << fileName << "\" will be used." << G4endl; |
---|
| 293 | |
---|
| 294 | try |
---|
| 295 | { |
---|
| 296 | tree=treeFactory->create(fileName, constLayout->GetOutputFileFormat(), false, true); |
---|
| 297 | } |
---|
| 298 | catch (exception e) |
---|
| 299 | { |
---|
| 300 | G4cout << "RadmonAnalysis::OpenFile: " << e.what() << G4endl; |
---|
| 301 | tree=0; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | if (!tree) |
---|
| 305 | return false; |
---|
| 306 | |
---|
| 307 | tupleFactory=analysisFactory->createTupleFactory(* tree); |
---|
| 308 | |
---|
| 309 | if (!tupleFactory) |
---|
| 310 | { |
---|
| 311 | delete tree; |
---|
| 312 | tree=0; |
---|
| 313 | return false; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | return true; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | G4bool RadmonAnalysis :: InitializeTuple(const list<G4String> & tupleLabels, const G4String & columns) |
---|
| 322 | { |
---|
| 323 | try |
---|
| 324 | { |
---|
| 325 | tuple=tupleFactory->create("1", "Radmon", columns); |
---|
| 326 | } |
---|
| 327 | catch (exception e) |
---|
| 328 | { |
---|
| 329 | G4cout << "RadmonAnalysis::InitializeTuple: " << e.what() << G4endl; |
---|
| 330 | tuple=0; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | SensitiveDetectorsList::iterator sd(sensitiveDetectorsList.begin()); |
---|
| 334 | const SensitiveDetectorsList::iterator sdEnd(sensitiveDetectorsList.end()); |
---|
| 335 | list<G4String>::const_iterator j(tupleLabels.begin()); |
---|
| 336 | |
---|
| 337 | while (sd!=sdEnd) |
---|
| 338 | { |
---|
| 339 | RadmonSensitiveDetector * radmonSensitiveDetector(sd->first); |
---|
| 340 | DataAnalysesList * dataAnalysesList(sd->second); |
---|
| 341 | |
---|
| 342 | DataAnalysesList::iterator i(dataAnalysesList->begin()); |
---|
| 343 | DataAnalysesList::iterator end(dataAnalysesList->end()); |
---|
| 344 | |
---|
| 345 | if (tuple==0) |
---|
| 346 | { |
---|
| 347 | while (i!=end) |
---|
| 348 | { |
---|
| 349 | delete *i; |
---|
| 350 | i=dataAnalysesList->erase(i); |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | if (dataAnalysesList->empty()) |
---|
| 355 | { |
---|
| 356 | delete dataAnalysesList; |
---|
| 357 | sd=sensitiveDetectorsList.erase(sd); |
---|
| 358 | } |
---|
| 359 | else |
---|
| 360 | { |
---|
| 361 | radmonSensitiveDetector->Activate(true); |
---|
| 362 | |
---|
| 363 | i=dataAnalysesList->begin(); |
---|
| 364 | end=dataAnalysesList->end(); |
---|
| 365 | |
---|
| 366 | while (i!=end) |
---|
| 367 | { |
---|
| 368 | radmonSensitiveDetector->AttachDataStorer(*i); |
---|
| 369 | (*i)->InitializeFromTuple(*j, tuple); |
---|
| 370 | j++; |
---|
| 371 | i++; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | sd++; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | return !sensitiveDetectorsList.empty(); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | |
---|
| 382 | |
---|
| 383 | |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | void RadmonAnalysis :: Destruct(void) |
---|
| 387 | { |
---|
| 388 | if (tree) |
---|
| 389 | { |
---|
| 390 | tree->commit(); |
---|
| 391 | tree->close(); |
---|
| 392 | |
---|
| 393 | delete tupleFactory; |
---|
| 394 | delete tree; |
---|
| 395 | tree=0; |
---|
| 396 | tupleFactory=0; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | while (!sensitiveDetectorsList.empty()) |
---|
| 400 | { |
---|
| 401 | SensitiveDetectorPair sensitiveDetectorPair(sensitiveDetectorsList.back()); |
---|
| 402 | sensitiveDetectorsList.pop_back(); |
---|
| 403 | sensitiveDetectorPair.first->Activate(false); |
---|
| 404 | sensitiveDetectorPair.first->ClearDataStorersList(); |
---|
| 405 | |
---|
| 406 | DataAnalysesList::iterator i(sensitiveDetectorPair.second->begin()); |
---|
| 407 | const DataAnalysesList::iterator end(sensitiveDetectorPair.second->end()); |
---|
| 408 | |
---|
| 409 | while (i!=end) |
---|
| 410 | { |
---|
| 411 | delete (*i); |
---|
| 412 | i++; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | delete sensitiveDetectorPair.second; |
---|
| 416 | } |
---|
| 417 | } |
---|