// InSOCProducer.cc // Contact person: P G Jones // See InSOCProducer.hh for more details. //—————————————————————---------------------------------------------——// #include #include #include #include #include #include #include #include #include #include #include #include using namespace RAT; #include #include #include using namespace std; InSOCProducer::InSOCProducer() : fSOCTree("T"), fRunTree("runT") { fMainBlock = NULL; Init(); } InSOCProducer::InSOCProducer( ProcBlock* block ) : fSOCTree("T"), fRunTree("runT") { SetMainBlock(block); Init(); } void InSOCProducer::Init() { // Build commands G4UIdirectory* insocDir = new G4UIdirectory( "/rat/insoc/" ); insocDir->SetGuidance( "Read Events from ROOT file" ); fReadCmd = new G4UIcommand( "/rat/insoc/read", this ); fReadCmd->SetGuidance( "Start reading in the root file" ); fReadCmd->AvailableForStates( G4State_Idle ); fLoadDefaultCmd = new G4UIcommand( "/rat/insoc/load_default", this ); fLoadDefaultCmd->SetGuidance( "Load the default filename" ); fLoadDefaultCmd->AvailableForStates( G4State_PreInit ); fLoadCmd = new G4UIcmdWithAString( "/rat/insoc/load", this ); fLoadCmd->SetParameterName( "filename", false ); fLoadCmd->SetGuidance( "Load the filename" ); fLoadCmd->AvailableForStates( G4State_PreInit ); } G4String InSOCProducer::GetCurrentValue( G4UIcommand* ) { Log::Die( "InSOCProducer::GetCurrentValue:invalid insoc \"get\" command" ); return G4String( "You win! How did you manage to see this?." ); } void InSOCProducer::SetNewValue( G4UIcommand* command, G4String newValue ) { if( command == fLoadCmd ) LoadFile( newValue ); else if( command == fLoadDefaultCmd ) { std::vector default_files =DB::Get()->GetLink( "IO" )->GetSArray( "default_input_filename" ); for (size_t i = 0; i < default_files.size(); ++i) { LoadFile( default_files.at(i)); } } else if( command == fReadCmd ) ReadEntries(); else Log::Die( "InSOCProducer::SetNewValue: Invalid command" ); } void InSOCProducer::LoadFile( const std::string& fileName ) { // First of all check that the file actually exists TFile* testFile = TFile::Open( fileName.c_str() ); Log::Assert( testFile != NULL, "InSOCProducer::LoadFile: File seems to be invalid!" ); Log::Assert( testFile->IsZombie() != true, "InSOCProducer::LoadFile: File seems to be invalid!" ); fSOCTree.Add( fileName.c_str() ); fRunTree.Add( fileName.c_str() ); // Load the Meta information MetaInformation::Get()->Initialise( reinterpret_cast( testFile->Get( "meta" ) ) ); DBCommandLoader::LoadCommands( MetaInformation::Get()->GetDSMeta() ); testFile->Close(); delete testFile; } void InSOCProducer::ReadEntries() { Log::Assert( fMainBlock != NULL, "InSOCProducer::ReadEvents: No main block declared. (Place this command after processors)." ); const Long64_t socEntries = fSOCTree.GetEntries(); Log::Assert( socEntries > 0 && fRunTree.GetEntries() > 0,"InSOCProducer::ReadEvents: Cannot load the T or runT in the file." ); info << "InSOCProducer::ReadEvents:" << (int)socEntries << " entries found. " << (int)fRunTree.GetEntries() << " runs found." << newline; // Load the branches DS::SOC* socBranch = new DS::SOC(); fSOCTree.SetBranchAddress( "soc", &socBranch ); DS::Run* runBranch = new DS::Run(); fRunTree.SetBranchAddress( "run", &runBranch ); // Run over the run entries and collate map runs; long long defaultNumEvents = DB::Get()->GetLink( "IO" )->GetI( "default_num_events" ); long long nEntries; //number of entries to loop over //How many events to run over if (defaultNumEvents >= 0) nEntries = defaultNumEvents; else nEntries = fRunTree.GetEntries(); for( long long iRun = 0; iRun < nEntries && !SignalHandler::IsTermRequested(); iRun++ ) { fRunTree.GetEntry( iRun ); if( runs.count( runBranch->GetRunID() ) == 1 ) warn << "InSOCProducer::ReadEntries: Multiple duplicate Run entries found, ignoring all but first.\n"; else runs[runBranch->GetRunID()] = *runBranch; } // Now run over all the SOC entries and merge if required for( long long iSOC = 0; iSOC < fSOCTree.GetEntries() && !SignalHandler::IsTermRequested(); iSOC++ ) { fSOCTree.GetEntry( iSOC ); Log::Assert( runs.count( socBranch->GetRunID() ) == 1, "InSOCProducer::ReadEntries: SOC data found for a non-existant run." ); if( runs[socBranch->GetRunID()].SOCDataExists( socBranch->GetSourceID() ) ) // Must merge runs[socBranch->GetRunID()].GetSOCData( socBranch->GetSourceID() ).Merge( *socBranch ); else // Can just insert runs[socBranch->GetRunID()].SetSOCData( socBranch->GetSourceID(), *socBranch ); } // Now we have a collection of runs with SOC data to iterate over for( map::iterator iTer = runs.begin(); iTer != runs.end(); ++iTer ) { const size_t pass = MetaInformation::Get()->GetCurrentPass(); //Loading flags for Data Quality iTer->second.GetDataQualityFlags().SetFlags( pass, DS::BitMask() ); iTer->second.GetDataQualityFlags().SetApplied( pass, DS::BitMask() ); Producer::BeginOfRun( iTer->second ); fMainBlock->BeginOfRun( iTer->second ); fMainBlock->EndOfRun( iTer->second ); } // Cleanup delete socBranch; delete runBranch; }