|
| 1 | + |
| 2 | +#include "FOCALWorkflow/FOCALDigitizerSpec.h" |
| 3 | +#include "CommonConstants/Triggers.h" |
| 4 | +#include "Framework/ConfigParamRegistry.h" |
| 5 | +#include "Framework/ControlService.h" |
| 6 | +#include "Framework/DataProcessorSpec.h" |
| 7 | +#include "Framework/DataRefUtils.h" |
| 8 | +#include "Framework/Lifetime.h" |
| 9 | +#include "Headers/DataHeader.h" |
| 10 | +#include "TStopwatch.h" |
| 11 | +#include "Steer/HitProcessingManager.h" // for DigitizationContext |
| 12 | +#include "TChain.h" |
| 13 | +#include <TGeoManager.h> |
| 14 | + |
| 15 | +#include "CommonDataFormat/EvIndex.h" |
| 16 | +#include "DetectorsCommonDataFormats/DetID.h" |
| 17 | +#include "DataFormatsParameters/GRPObject.h" |
| 18 | +#include "DataFormatsFOCAL/TriggerRecord.h" |
| 19 | +#include <Steer/MCKinematicsReader.h> |
| 20 | + |
| 21 | +using namespace o2::framework; |
| 22 | +using SubSpecificationType = o2::framework::DataAllocator::SubSpecificationType; |
| 23 | + |
| 24 | +namespace o2 |
| 25 | +{ |
| 26 | +namespace focal |
| 27 | +{ |
| 28 | + |
| 29 | +void DigitizerSpec::initDigitizerTask(framework::InitContext& ctx) |
| 30 | +{ |
| 31 | + if (!gGeoManager) { |
| 32 | + LOG(error) << "Geometry needs to be loaded before"; |
| 33 | + } |
| 34 | + |
| 35 | + auto geom = o2::focal::Geometry::GetInstance(); |
| 36 | + |
| 37 | + mSumDigitizer.setGeometry(geom); |
| 38 | + mDigitizer.setGeometry(geom); |
| 39 | + |
| 40 | + if (ctx.options().get<bool>("debug-stream")) { |
| 41 | + mDigitizer.setDebugStreaming(true); |
| 42 | + } |
| 43 | + |
| 44 | + if (ctx.options().get<bool>("disable-dig")) { |
| 45 | + mRunDigitizer = false; |
| 46 | + } |
| 47 | + |
| 48 | + mFinished = false; |
| 49 | +} |
| 50 | + |
| 51 | +void DigitizerSpec::run(framework::ProcessingContext& ctx) |
| 52 | +{ |
| 53 | + if (mFinished) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (!mIsConfigured) { |
| 58 | + configure(); |
| 59 | + mIsConfigured = true; |
| 60 | + } |
| 61 | + |
| 62 | + o2::focal::SimParam::Instance().printKeyValues(true, true); |
| 63 | + |
| 64 | + mDigitizer.flush(); |
| 65 | + |
| 66 | + auto context = ctx.inputs().get<o2::steer::DigitizationContext*>("collisioncontext"); |
| 67 | + |
| 68 | + auto intRate = context->getDigitizerInteractionRate(); |
| 69 | + context->initSimChains(o2::detectors::DetID::FOC, mSimChains); |
| 70 | + |
| 71 | + if (mcReader == nullptr) { |
| 72 | + mcReader = new o2::steer::MCKinematicsReader(context.get()); |
| 73 | + } |
| 74 | + |
| 75 | + auto& timesview = context->getEventRecords(); |
| 76 | + LOG(debug) << "GOT " << timesview.size() << " COLLISSION TIMES"; |
| 77 | + |
| 78 | + if (timesview.size() == 0) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + TStopwatch timer; |
| 83 | + timer.Start(); |
| 84 | + |
| 85 | + auto& eventParts = context->getEventParts(); |
| 86 | + |
| 87 | + int collisionN = 0; |
| 88 | + for (int collID = 0; collID < timesview.size(); ++collID) { |
| 89 | + |
| 90 | + if (mRunDigitizer == false) { |
| 91 | + break; |
| 92 | + } |
| 93 | + |
| 94 | + if (intRate < 10000.) { |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + collisionN++; |
| 99 | + |
| 100 | + mDigitizer.setEventTime(timesview[collID]); |
| 101 | + |
| 102 | + for (auto& part : eventParts[collID]) { |
| 103 | + |
| 104 | + mSumDigitizer.setCurrEvID(part.entryID); |
| 105 | + mSumDigitizer.setCurrSrcID(part.sourceID); |
| 106 | + |
| 107 | + mHits.clear(); |
| 108 | + context->retrieveHits(mSimChains, "FOCHit", part.sourceID, part.entryID, &mHits); |
| 109 | + |
| 110 | + std::vector<o2::focal::LabeledDigit> summedLabeledDigits; |
| 111 | + std::vector<o2::focal::Digit> summedDigits; |
| 112 | + if (mRunSDitizer) { |
| 113 | + summedLabeledDigits = mSumDigitizer.process(mHits); |
| 114 | + for (auto labeledsummeddigit : summedLabeledDigits) { |
| 115 | + summedDigits.push_back(labeledsummeddigit.getDigit()); |
| 116 | + } |
| 117 | + } else { |
| 118 | + for (auto& hit : mHits) { |
| 119 | + summedDigits.emplace_back(hit.GetDetectorID(), hit.GetEnergyLoss(), hit.GetTime()); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + mDigitizer.process(summedDigits); |
| 124 | + } |
| 125 | + } |
| 126 | + mDigitizer.finish(); |
| 127 | + |
| 128 | + LOG(info) << " CALLING FOCAL DIGITIZATION "; |
| 129 | + o2::dataformats::MCTruthContainer<o2::focal::MCLabel> labelAccum; |
| 130 | + |
| 131 | + for (int collID = 0; collID < timesview.size(); ++collID) { |
| 132 | + |
| 133 | + std::bitset<5> trigger{0x1}; |
| 134 | + |
| 135 | + if (!trigger.any()) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + mDigitizer.setEventTime(timesview[collID], trigger.any()); |
| 140 | + if (!mDigitizer.isCurrentEventTriggered()) { |
| 141 | + LOG(debug) << "reject collision"; |
| 142 | + continue; |
| 143 | + } |
| 144 | + LOG(debug) << "accept collision"; |
| 145 | + |
| 146 | + for (auto& part : eventParts[collID]) { |
| 147 | + |
| 148 | + mSumDigitizer.setCurrEvID(part.entryID); |
| 149 | + mSumDigitizer.setCurrSrcID(part.sourceID); |
| 150 | + |
| 151 | + auto& mcEventHeader = mcReader->getMCEventHeader(part.sourceID, part.entryID); |
| 152 | + mcEventHeader.print(); |
| 153 | + |
| 154 | + mHits.clear(); |
| 155 | + context->retrieveHits(mSimChains, "FOCHit", part.sourceID, part.entryID, &mHits); |
| 156 | + |
| 157 | + LOG(info) << "For collision " << collID << " eventID " << part.entryID << " found " << mHits.size() << " hits "; |
| 158 | + |
| 159 | + std::vector<o2::focal::LabeledDigit> summedLabeledDigits; |
| 160 | + if (mRunSDitizer) { |
| 161 | + summedLabeledDigits = mSumDigitizer.process(mHits); |
| 162 | + } else { |
| 163 | + for (auto& hit : mHits) { |
| 164 | + o2::focal::MCLabel digitlabel(hit.GetTrackID(), part.entryID, part.sourceID, false, 1.); |
| 165 | + if (hit.GetEnergyLoss() < __DBL_EPSILON__) { |
| 166 | + digitlabel.setAmplitudeFraction(0); |
| 167 | + } |
| 168 | + summedLabeledDigits.emplace_back(hit.GetDetectorID(), hit.GetEnergyLoss(), hit.GetTime(), digitlabel); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + mDigitizer.process(summedLabeledDigits); |
| 173 | + } |
| 174 | + } |
| 175 | + mDigitizer.finish(); |
| 176 | + |
| 177 | + ctx.outputs().snapshot(Output{"FOC", "DIGITS", 0}, mDigitizer.getDigits()); |
| 178 | + if (ctx.outputs().isAllowed({"FOC", "DIGITSMCTR", 0})) { |
| 179 | + ctx.outputs().snapshot(Output{"FOC", "DIGITSMCTR", 0}, mDigitizer.getMCLabels()); |
| 180 | + } |
| 181 | + const o2::parameters::GRPObject::ROMode roMode = o2::parameters::GRPObject::TRIGGERING; |
| 182 | + LOG(info) << "FOCAL: Sending ROMode= " << roMode << " to GRPUpdater"; |
| 183 | + ctx.outputs().snapshot(Output{"FOC", "ROMode", 0}, roMode); |
| 184 | + |
| 185 | + timer.Stop(); |
| 186 | + LOG(info) << "Digitization took " << timer.CpuTime() << "s"; |
| 187 | + ctx.services().get<ControlService>().readyToQuit(QuitRequest::Me); |
| 188 | + mFinished = true; |
| 189 | +} |
| 190 | + |
| 191 | +void DigitizerSpec::configure() |
| 192 | +{ |
| 193 | + mDigitizer.init(); |
| 194 | +} |
| 195 | + |
| 196 | +o2::framework::DataProcessorSpec getFOCALDigitizerSpec(int channel, bool mctruth) |
| 197 | +{ |
| 198 | + |
| 199 | + std::vector<OutputSpec> outputs; |
| 200 | + outputs.emplace_back("FOC", "DIGITS", 0, Lifetime::Timeframe); |
| 201 | + outputs.emplace_back("FOC", "TRGRDIG", 0, Lifetime::Timeframe); |
| 202 | + if (mctruth) { |
| 203 | + outputs.emplace_back("FOC", "DIGITSMCTR", 0, Lifetime::Timeframe); |
| 204 | + } |
| 205 | + outputs.emplace_back("FOC", "ROMode", 0, Lifetime::Timeframe); |
| 206 | + outputs.emplace_back("FOC", "TRIGGERINPUT", 0, Lifetime::Timeframe); |
| 207 | + |
| 208 | + std::vector<o2::framework::InputSpec> inputs; |
| 209 | + inputs.emplace_back("collisioncontext", "SIM", "COLLISIONCONTEXT", static_cast<SubSpecificationType>(channel), Lifetime::Timeframe); |
| 210 | + |
| 211 | + |
| 212 | + return DataProcessorSpec{ |
| 213 | + "FOCALDigitizer", |
| 214 | + inputs, |
| 215 | + outputs, |
| 216 | + Options{ |
| 217 | + {"pileup", VariantType::Int, 1, {"whether to run in continuous time mode"}}, |
| 218 | + {"disable-dig", VariantType::Bool, false, {"Disable digitisation"}}, |
| 219 | + {"debug-stream", VariantType::Bool, false, {"Enable debug streaming"}}} |
| 220 | + }; |
| 221 | +} |
| 222 | +} // end namespace focal |
| 223 | +} // end namespace o2 |
0 commit comments