ATLAS Offline Software
Loading...
Searching...
No Matches
LArShapeFromStdNtuple.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
12#include "GaudiKernel/ThreadLocalContext.h"
13
14#include "TFile.h"
15#include "TBranch.h"
16#include "TTree.h"
17#include "TChain.h"
18
19#include <cmath>
20#include <fstream>
21#include <iostream>
22#include <string>
23#include <vector>
24
25
27
29{
30 ATH_CHECK ( m_mcSymKey.initialize() );
31 return StatusCode::SUCCESS ;
32}
33
34
36{
37 if(m_done) return StatusCode::SUCCESS;
38
39 ATH_MSG_INFO ( "... in stop()" );
40
41 const EventContext& ctx = Gaudi::Hive::currentContext();
43
44 // get LArOnlineID helper
45 const LArOnlineID* onlineHelper = nullptr;
46 ATH_CHECK( detStore()->retrieve(onlineHelper, "LArOnlineID") );
47
48 TChain* outfit = new TChain(m_ntuple_name.value().c_str());
49 for (const std::string& s : m_root_file_names) {
50 outfit->Add(s.c_str());
51 }
52
53
54 Int_t phase=0;
55 Int_t det=0;
56 Float_t phaseTime=0;
57 Float_t timeOffset=0;
58 Int_t channelId=0;
59 Int_t FT=0, slot=0, channel=0;
60
61 Float_t Shape[2000]; // The function
62 Float_t ShapeDer[2000]; // The function
63 Int_t gain = 0; // LARHIGHGAIN = 0, LARMEDIUMGAIN = 1, LARLOWGAIN = 2,
64 Int_t nsamples=0;
65
66 outfit->SetBranchAddress("channelId", &channelId);
67 outfit->SetBranchAddress("FT", &FT);
68 outfit->SetBranchAddress("slot", &slot);
69 outfit->SetBranchAddress("channel", &channel);
70 outfit->SetBranchAddress("detector", &det);
71 if(m_isComplete) {
72 outfit->SetBranchAddress("PhaseTime", &phaseTime);
73 outfit->SetBranchAddress("Phase", &phase);
74 outfit->SetBranchAddress("timeOffset", &timeOffset);
75 }
76 outfit->SetBranchAddress("nSamples", &nsamples);
77 outfit->SetBranchAddress("Gain", &gain);
78 outfit->SetBranchAddress("Shape", Shape);
79 outfit->SetBranchAddress("ShapeDer", ShapeDer);
80
81 Float_t timeBinWidth=-1, prevTime=-1, timeOff=-1;
82 Int_t prevPhase=-1;
83
84 // Create new LArShapeContainer
85 std::unique_ptr<LArShapeComplete> larShapeComplete;
86 std::unique_ptr<LArShape32MC> larShapeMC;
87
88 if(m_isComplete) {
89 larShapeComplete = std::make_unique<LArShapeComplete>();
90 ATH_CHECK( larShapeComplete->setGroupingType(m_groupingType, msg()) );
91 ATH_CHECK( larShapeComplete->initialize() );
92 } else {
93 larShapeMC = std::make_unique<LArShape32MC>();
94 ATH_CHECK( larShapeMC->setGroupingType(m_groupingType, msg()) );
95 ATH_CHECK( larShapeMC->initialize() );
96 }
97
98 std::vector<float> shapemc;
99 std::vector<float> shape_dermc;
100 typedef std::vector<std::vector<float> > wave2d;
101 std::map<std::pair<unsigned int,int>, wave2d> shape;
102 std::map<std::pair<unsigned int,int>, wave2d> shape_der;
103 unsigned int hwid;
104 // loop over entries in the Tuple, one entry = one channel
105 Long64_t nentries = outfit->GetEntries();
106 for ( Long64_t iev = 0; iev < nentries; ++iev )
107 {
108 outfit->GetEvent(iev);
109 ATH_MSG_INFO ( " Chan " << std::hex << channelId << " det. "<< det << std::dec );
110
111 hwid = channelId;
112 //if (det != 4) hwid = (channelId<<4);
113 HWIdentifier id(hwid);
114
115 /*
116 if(FT != onlineHelper->feedthrough(id) || slot != onlineHelper->slot(id) || channel != onlineHelper->channel(id)) {
117 ATH_MSG_ERROR ( "Inconsistency in decoding HWID !!!!" );
118 ATH_MSG_INFO ( "Trying to fix..." );
119 hwid = (channelId<<4);
120 id=HWIdentifier(hwid);
121 */
122 if(FT != onlineHelper->feedthrough(id) || slot != onlineHelper->slot(id) || channel != onlineHelper->channel(id)) {
123 ATH_MSG_ERROR ( "Inconsistency in decoding HWID !!!!" );
124 ATH_MSG_ERROR ( FT << " - " << onlineHelper->feedthrough(id) );
125 ATH_MSG_ERROR ( slot << " - " << onlineHelper->slot(id) );
126 ATH_MSG_ERROR ( channel << " - " << onlineHelper->channel(id) );
127 //if(det == 4) {
128 // ATH_MSG_ERROR ( "Ignoring for sFcal" );
129 //} else {
130 // ATH_MSG_ERROR ( "Not creating Shape !!!!" );
131 // continue;
132 // }
133 continue;
134 /* }
135 ATH_MSG_INFO ( "Fixed....." );
136 */
137 }
138
139 // Catch potential array index out of range error.
140 if ( nsamples >= 2000 ) {
141 ATH_MSG_ERROR ( " Too many points specified vs the expected content of the ntuple ! " );
142 ATH_MSG_ERROR ( "Not creating Shape !!!!" );
143 continue;
144 } else {
145 ATH_MSG_DEBUG ( "Working with " << nsamples << " samples" );
146 }
147 if(timeBinWidth < 0) {
148 if(prevTime < 0) {
149 prevTime=phaseTime;
150 prevPhase=phase;
151 } else {
152 if(abs(phase-prevPhase) == 1) {
153 timeBinWidth=std::fabs(phaseTime - prevTime);
154 }
155 }
156 }
157
158 if(timeOff < 0) timeOff=timeOffset;
159
160 if(m_isComplete) {
161 if(shape[std::make_pair(hwid,gain)].empty()) shape[std::make_pair(hwid,gain)].reserve(50);
162 if(shape_der[std::make_pair(hwid,gain)].empty()) shape_der[std::make_pair(hwid,gain)].reserve(50);
163 shape[std::make_pair(hwid,gain)][phase].reserve(nsamples);
164 shape_der[std::make_pair(hwid,gain)][phase].reserve(nsamples);
165 for(int i=0;i<nsamples; ++i) {shape[std::make_pair(hwid,gain)][phase][i]=0.;
166 shape_der[std::make_pair(hwid,gain)][phase][i]=0.;}
167 } else {
168 shapemc.resize(nsamples);
169 shape_dermc.resize(nsamples);
170 for(int i=0;i<nsamples; ++i) {shapemc[i]=0.; shape_dermc[i]=0.;}
171 }
172 unsigned int skipped = 0;
173 unsigned int limit = nsamples;
174 if ( m_skipPoints < m_prefixPoints ) limit = nsamples+m_skipPoints-m_prefixPoints;
175 for ( unsigned int i = 0; i < limit; i++ ) {
176 if ( skipped >= m_skipPoints ) {
177 if(m_isComplete) { // accumulate into map
178 shape[std::make_pair(hwid,gain)][phase][i-m_skipPoints+m_prefixPoints]=Shape[i];
179 shape_der[std::make_pair(hwid,gain)][phase][i-m_skipPoints+m_prefixPoints]=ShapeDer[i];
180
181 } else {
182 shapemc[i-m_skipPoints+m_prefixPoints]=Shape[i];
183 shape_dermc[i-m_skipPoints+m_prefixPoints]=ShapeDer[i];
184 }
185 } else skipped++;
186 }
187
188 if(!m_isComplete) {
189 if (id != mcsym->ZPhiSymOnl(id) ) {
190 ATH_MSG_INFO( "Symmetrized, not stored" );
191 } else {
192
193 ATH_MSG_INFO( "Storing shape with length: " << shapemc.size() );
194
195 //LArShapeP1 t(shapemc,shape_dermc);
196
197 //larShapeMC->setPdata(id, LArShapeP1(shapemc,shape_dermc),gain);
198 larShapeMC->set(id, gain, shapemc, shape_dermc);
199 ATH_MSG_INFO( larShapeMC->Shape(id,gain).size() << " " << larShapeMC->ShapeDer(id,gain).size() );
200 ATH_MSG_INFO( "Shape[2] =" << larShapeMC->Shape(id,gain)[2] << "shapemc[2] =" << shapemc[2] );
201 }
202 }
203
204 }
205 // for complete, could fill only now
206 if(m_isComplete){
207 std::map<std::pair<unsigned int,int>, wave2d>::iterator ibeg = shape.begin();
208 std::map<std::pair<unsigned int,int>, wave2d>::iterator iend = shape.end();
209 for(;ibeg != iend; ++ibeg) {
210 larShapeComplete->set(HWIdentifier((ibeg->first).first),(ibeg->first).second,
211 ibeg->second, shape_der[std::make_pair((ibeg->first).first, (ibeg->first).second)],
212 timeOff,timeBinWidth);
213 }
214 }
215
216 if(m_isComplete) {
217 ATH_CHECK( detStore()->record(std::move(larShapeComplete),m_store_key) );
218 } else {
219 ATH_CHECK( detStore()->record(std::move(larShapeMC),m_store_key) );
220 }
221 return StatusCode::SUCCESS;
222}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
static const Attributes_t empty
const ServiceHandle< StoreGateSvc > & detStore() const
int feedthrough(const HWIdentifier id) const
Return the feedthrough of a hardware cell identifier : feedthrough = [0,31] Barrel - A/C side or H/...
int slot(const HWIdentifier id) const
Return the slot number of a hardware cell identifier: slot = [1,15] Slot-ID in top part of the crat...
int channel(const HWIdentifier id) const
Return the channel number of a hardware cell identifier channel = [0,127] in all FEB.
StringProperty m_ntuple_name
ntuple name
virtual ~LArShapeFromStdNtuple()
virtual StatusCode initialize() override
UnsignedIntegerProperty m_skipPoints
the first m_skipPoints points of the waveform in the ntuple are skipped
virtual StatusCode stop() override
UnsignedIntegerProperty m_prefixPoints
make a Shape with the first m_prefixPoints as zeros
StringArrayProperty m_root_file_names
list of input ntuple file names
StringProperty m_store_key
key of the LArShape collection in Storegate
BooleanProperty m_isComplete
Shape type.
StringProperty m_groupingType
Grouping type.
SG::ReadCondHandleKey< LArMCSym > m_mcSymKey