17 #include "TFriendElement.h"
23 _detectors.reserve(50);
27 _intHitArrays.reserve(50);
28 _partHitArrays.reserve(50);
29 _posHitArrays.reserve(50);
35 for (
unsigned int i = 0; i < _intHitArrays.size(); i++)
36 delete _intHitArrays[i];
37 for (
unsigned int i = 0; i < _partHitArrays.size(); i++)
38 delete _partHitArrays[i];
39 for (
unsigned int i = 0; i < _posHitArrays.size(); i++)
40 delete _posHitArrays[i];
49 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
52 for (
int iBranch = 0; iBranch < _chain->GetListOfBranches()->GetEntries(); iBranch++) {
53 if (TString(((TBranch *)(_chain->GetListOfBranches()->At(iBranch)))->GetName()).Contains(
"GGSIntHits")) {
59 if (_chain->GetListOfFriends()) {
60 for (
int iFriend = 0; iFriend < _chain->GetListOfFriends()->GetEntries(); iFriend++) {
61 TTree *currFriend = ((TFriendElement *)(_chain->GetListOfFriends()->At(iFriend)))->GetTree();
62 for (
int iBranch = 0; iBranch < currFriend->GetListOfBranches()->GetEntries(); iBranch++) {
63 if (TString(((TBranch *)(currFriend->GetListOfBranches()->At(iBranch)))->GetName()).Contains(
"GGSIntHits")) {
73 _firstEntryOfCurrentFile = -1;
81 static const std::string routineName(
"GGSTHitsReader::GetEntry");
83 if (entry != _chain->GetReadEntry()) {
84 _chain->GetEntry(entry);
87 if (_chain->GetChainEntryNumber(0) != _firstEntryOfCurrentFile) {
88 GGSCOUT(DEBUG) <<
"Switching to file " << _chain->GetFile()->GetName() <<
GGSENDL;
89 _firstEntryOfCurrentFile = _chain->GetChainEntryNumber(0);
93 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
96 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++)
97 _detectors[iDet].detInfoArrayIndex = _GetDetectorIndexFromStorage(_detectors[iDet].logVolName.data());
102 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++) {
103 delete _timeBins[iDet];
104 _timeBins.push_back((TArrayF *)(_chain->GetFile()->Get(TString(_detectors[iDet].name) +
"-GGSIntHits-TimeBins")));
105 delete _thresholds[iDet];
106 _thresholds.push_back(
107 (std::vector<float> *)(_chain->GetFile()->Get(TString(_detectors[iDet].name) +
"-GGSIntHits-Thresholds")));
115 static const std::string routineName(
"GGSTHitsReader::SetDetector");
124 TString detBranchName = detector;
125 detBranchName.Append(
"-GGSIntHits");
126 TBranch *branches[3] = {
nullptr,
nullptr,
nullptr};
127 if (_CheckBranch(detBranchName) == 0) {
129 std::string partHitsBranchName{std::string(detector).append(
"-GGSPartHits")};
130 std::string posHitsBranchName{std::string(detector).append(
"-GGSPartHits")};
131 TBranch *partHitsBranch = _chain->GetBranch(partHitsBranchName.c_str());
132 TBranch *posHitsBranch = _chain->GetBranch(posHitsBranchName.c_str());
134 std::string infoString(
"Set detector ");
135 infoString.append(detector);
140 infoString.append(
" (+PosHits)");
141 if (detIndex == -1) {
142 _posHitArrays.push_back(
nullptr);
143 _chain->SetBranchAddress(posHitsBranchName.c_str(), &(_posHitArrays.back()));
144 _readPosHits.push_back(
true);
146 _chain->SetBranchAddress(posHitsBranchName.c_str(), &(_posHitArrays[detIndex]));
147 _readPosHits[detIndex] =
true;
149 branches[2] = posHitsBranch;
151 throw std::runtime_error(std::string(
"PosHits missing for detector ") + detector);
154 if (detIndex == -1) {
155 _posHitArrays.push_back(
nullptr);
156 _readPosHits.push_back(
false);
159 posHitsBranch->ResetAddress();
161 _posHitArrays[detIndex] =
nullptr;
163 _readPosHits[detIndex] =
false;
169 if (partHitsBranch) {
170 infoString.append(
" (+PartHits)");
171 if (detIndex == -1) {
172 _partHitArrays.push_back(
nullptr);
173 _chain->SetBranchAddress(partHitsBranchName.c_str(), &(_partHitArrays.back()));
174 _readPartHits.push_back(
true);
176 _chain->SetBranchAddress(partHitsBranchName.c_str(), &(_partHitArrays[detIndex]));
177 _readPartHits[detIndex] =
true;
179 branches[1] = partHitsBranch;
181 throw std::runtime_error(std::string(
"PartHits missing for detector ") + detector);
184 if (detIndex == -1) {
185 _partHitArrays.push_back(
nullptr);
186 _readPartHits.push_back(
false);
188 if (partHitsBranch) {
189 partHitsBranch->ResetAddress();
191 _partHitArrays[detIndex] =
nullptr;
193 _readPartHits[detIndex] =
false;
197 if (detIndex == -1) {
198 _intHitArrays.push_back(
nullptr);
199 _chain->SetBranchAddress(detBranchName, &(_intHitArrays.back()));
200 branches[0] = _chain->GetBranch(detBranchName);
202 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
203 _timeBins.push_back((TArrayF *)(_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-TimeBins")));
204 _thresholds.push_back(
205 (std::vector<float> *)(_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-Thresholds")));
209 det.logVolName = detector;
211 det.logVolName.substr(0, det.logVolName.find_first_of(
'.'));
212 det.detInfoArrayIndex = _GetDetectorIndexFromStorage(det.logVolName.c_str());
213 _detectors.push_back(det);
215 GGSCOUT(INFO) << infoString <<
GGSENDL;
217 throw std::runtime_error(std::string(
"IntHits missing for detector ") + detector);
221 if (_chain->GetReadEntry() != -1) {
222 for (
int iBranch = 0; iBranch < 3; ++iBranch) {
223 if (branches[iBranch]) {
224 branches[iBranch]->GetEntry(_chain->GetReadEntry());
235 std::string detectorString{detector};
236 detectorString +=
"-GGSIntHits";
237 type = _CheckBranch(detectorString.c_str());
239 if (type == 0 && particleHits) {
240 std::string detectorString{detector};
241 detectorString +=
"-GGSPartHits";
242 type = _CheckBranch(detectorString.c_str());
244 if (type == 1 && positionHits) {
245 std::string detectorString{detector};
246 detectorString +=
"-GGSPosHits";
247 type = _CheckBranch(detectorString.c_str());
256 static const std::string routineName(
"GGSTHitsReader::ListDetectors");
259 TObjArray *branches = _chain->GetListOfBranches();
260 GGSCOUT(INFO) <<
"List of detectors in hits tree:" <<
GGSENDL;
261 for (
int i = 0; i < branches->GetEntries(); i++) {
262 TBranch *branch = (TBranch *)(branches->At(i));
263 if (_CheckBranch(branch->GetName()) == 0) {
264 TString detName = branch->GetName();
265 detName.Remove(detName.Length() - 11);
266 std::string infoString(
" ");
267 infoString.append(detName.Data());
268 if (_CheckBranch(detName +
"-GGSPartHits") == 1)
269 infoString.append(
" (PartHits)");
270 if (_CheckBranch(detName +
"-GGSPosHits") == 2)
271 infoString.append(
" (PosHits)");
281 static const std::string routineName(
"GGSTHitsReader::GetListOfDetectors");
283 std::vector<TString> _list;
286 TObjArray *branches = _chain->GetListOfBranches();
287 for (
int i = 0; i < branches->GetEntries(); i++) {
288 TBranch *branch = (TBranch *)(branches->At(i));
289 std::string branchName(branch->GetName());
290 if (_CheckBranch(branch->GetName()) == 0) {
291 TString detName = branch->GetName();
292 detName.Remove(detName.Length() - 11);
293 _list.emplace_back(detName);
304 static short int iHitArray;
307 return (_GetHitArray(iHitArray))->GetEntries();
315 TClonesArray *detArray = _GetHitArray(iDet);
317 return detArray->GetEntries();
327 return GetHit(iDet, iHit);
333 static TClonesArray *hitArray =
nullptr;
334 hitArray = _GetHitArray(iDet);
340 hit->_hitDetInfo = _GetDetectorInfo(_detectors[iDet].detInfoArrayIndex);
341 hit->_timeBins = _timeBins[iDet];
342 if (hit->_hitDetInfo)
345 hit->_hitVolInfo =
nullptr;
346 if (_readPartHits[iDet]) {
347 hit->_partHits = (TClonesArray *)(_partHitArrays[iDet]->At(iHit));
349 if (_readPosHits[iDet]) {
351 TClonesArray *partHits = hit->_partHits;
352 int nPartHits = partHits->GetEntries();
353 for (
int iPartHit = 0; iPartHit < nPartHits; iPartHit++) {
355 partHit->_posHits = (TClonesArray *)(_posHitArrays[iDet]->At(partHit->_posHitIndex));
374 TClonesArray *hitArray = _GetHitArray(iDet);
376 std::string retValue = hitArray->GetName();
377 return retValue.substr(0, retValue.size() - 1);
385 for (UInt_t i = 0; i < _detectors.size(); i++) {
386 if (!strcmp(_detectors[i].name.data(), detector))
396 TString branchName(detector);
397 branchName.Append(
"-GGSPartHits");
398 TBranch *branch = _chain->GetBranch(branchName);
411 TString branchName(detector);
412 branchName.Append(
"-GGSPosHits");
413 TBranch *branch = _chain->GetBranch(branchName);
423 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
425 if (detIndex == -1) {
426 throw std::range_error(routineName +
": detector " + detector +
" not found");
429 if (_thresholds[detIndex]) {
430 return (*(_thresholds[detIndex]))[0];
432 throw std::runtime_error(routineName +
": thresholds for integrated hits not available for detector " + detector);
439 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
441 if (detIndex == -1) {
442 throw std::range_error(routineName +
": detector " + detector +
" not found");
445 if (_thresholds[detIndex]) {
446 return (*(_thresholds[detIndex]))[1];
448 throw std::runtime_error(routineName +
": thresholds for particle hits not available for detector " + detector);
455 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
457 if (detIndex == -1) {
458 throw std::range_error(routineName +
": detector " + detector +
" not found");
461 if (_thresholds[detIndex]) {
462 return (*(_thresholds[detIndex]))[2];
464 throw std::runtime_error(routineName +
": thresholds for position hits not available for detector " + detector);
470 int GGSTHitsReader::_GetDetectorIndexFromStorage(
const char *detector) {
472 for (
int iDet = 0; iDet < _detInfo->GetEntries(); iDet++) {
484 int GGSTHitsReader::_CheckBranch(
const char *branchName) {
485 const std::string routineName(
"GGSTHitsReader::_CheckBranch");
487 std::string branchNameStr{branchName};
489 if (branchNameStr.find(
"-GGSIntHits") != std::string::npos) {
492 }
else if (branchNameStr.find(
"-GGSPartHits") != std::string::npos) {
494 }
else if (branchNameStr.find(
"-GGSPosHits") != std::string::npos) {
500 TBranch *branch = _chain->GetBranch(branchName);
511 if (strcmp(branch->GetClassName(),
"TClonesArray")) {
515 if (strcmp(branch->GetClassName(),
"TObjArray")) {
522 TClonesArray **bufferAddr =
523 (TClonesArray **)(branch->GetAddress());
524 bool addressNotSet =
false;
526 bufferAddr =
new TClonesArray *;
527 *bufferAddr =
nullptr;
528 branch->SetAddress(bufferAddr);
529 addressNotSet =
true;
532 auto oldReadEntry = branch->GetReadEntry();
534 std::string hitClassName((*bufferAddr)->GetName());
536 branch->ResetAddress();
537 bufferAddr =
nullptr;
539 branch->GetEntry(oldReadEntry);
540 assert(branch->GetReadEntry() == oldReadEntry);
542 hitClassName.erase(hitClassName.size() - 1);
544 TClass *clonesClass = TClass::GetClass(hitClassName.c_str());
546 GGSCOUT(ERROR) <<
"No dictionary for class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
550 if (!(clonesClass->GetBaseClass(
"GGSTIntHit"))) {
551 GGSCOUT(ERROR) <<
"Class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
552 <<
" does not inherit from GGSTIntHit" <<
GGSENDL;
562 TClonesArray *GGSTHitsReader::_GetHitArray(
int iHitArray) {
563 if (iHitArray > (
int)(_intHitArrays.size() - 1) || iHitArray < 0)
565 return _intHitArrays[iHitArray];
bool HasPartHits(const char *detector)
Check if particle hits data is available for the specified detector.
float GetIntHitThreshold(const std::string &detector)
Retrieves the thresholds used when saving the integrated hits.
#define GGSCCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Class to store detector informations.
GGSTIntHitBase * GetHit(const char *detector, unsigned int iHit)
Get the specified hit.
float GetPartHitThreshold(const std::string &detector)
Retrieves the thresholds used when saving the particle hits.
void ListDetectors()
Prints a list of detectors which are present in hits tree on standard output.
Class to store G4 particle hits.
GGSTHitsReader()
Constructor.
TClonesArray volumes
Array of GGSTHitVolInfo objects.
std::string GetIntHitsClassName(const char *detector)
Gets the name of the class of the integrated hits for the given detector.
bool DetectorExists(const char *detector, bool particleHits=false, bool positionHits=false)
Checks the existence of a given detector within the hits tree.
Float_t eDep
Deposited energy (transient).
Int_t GetNHits(const char *detector)
Gets the number of hits for the specified detector.
~GGSTHitsReader()
Destructor.
bool HasPosHits(const char *detector)
Check if position hits data is available for the specified detector.
float GetPosHitThreshold(const std::string &detector)
Retrieves the thresholds used when saving the position hits.
bool SetChain(TChain *hitsChain)
Sets the chain.
int GetDetectorIndex(const char *detector)
Retrieves the index of requested detector.
void GetEntry(Long64_t entry)
std::vector< TString > GetListOfDetectors()
Returns the list of detectors which are present in hits tree on standard output.
TArrayF eDepTimeBin
Energy deposit in each time bin.
GGSTHitVolInfo.h GGSTHitVolInfo class declaration.
TString detectorName
Name of detector associated to integrated hits.
Base class for storing G4 integrated hits.
void SetDetector(const char *detector, bool readPartHits=false, bool readPosHits=false)
Enables reading hit informations of the specified detector.