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];
48 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
51 for (
int iBranch = 0; iBranch < _chain->GetListOfBranches()->GetEntries(); iBranch++) {
52 if (TString(((TBranch *)(_chain->GetListOfBranches()->At(iBranch)))->GetName()).Contains(
"GGSIntHits")) {
58 if (_chain->GetListOfFriends()) {
59 for (
int iFriend = 0; iFriend < _chain->GetListOfFriends()->GetEntries(); iFriend++) {
60 TTree *currFriend = ((TFriendElement *)(_chain->GetListOfFriends()->At(iFriend)))->GetTree();
61 for (
int iBranch = 0; iBranch < currFriend->GetListOfBranches()->GetEntries(); iBranch++) {
62 if (TString(((TBranch *)(currFriend->GetListOfBranches()->At(iBranch)))->GetName()).Contains(
"GGSIntHits")) {
71 _firstEntryOfCurrentFile = -1;
79 static const std::string routineName(
"GGSTHitsReader::GetEntry");
81 if (entry != _chain->GetReadEntry()) {
82 _chain->GetEntry(entry);
85 if (_chain->GetChainEntryNumber(0) != _firstEntryOfCurrentFile) {
86 COUT(DEBUG) <<
"Switching to file " << _chain->GetFile()->GetName() <<
ENDL;
87 _firstEntryOfCurrentFile = _chain->GetChainEntryNumber(0);
90 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
93 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++)
94 _detectors[iDet].detInfoArrayIndex = _GetDetectorIndexFromStorage(_detectors[iDet].logVolName.data());
99 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++) {
100 delete _timeBins[iDet];
101 _timeBins.push_back((TArrayF *)(_chain->GetFile()->Get(TString(_detectors[iDet].name) +
"-GGSIntHits-TimeBins")));
102 delete _thresholds[iDet];
103 _thresholds.push_back(
104 (std::vector<float> *)(_chain->GetFile()->Get(TString(_detectors[iDet].name) +
"-GGSIntHits-Thresholds")));
112 static const std::string routineName(
"GGSTHitsReader::SetDetector");
121 TString detBranchName = detector;
122 detBranchName.Append(
"-GGSIntHits");
123 TBranch *branches[3] = {
nullptr,
nullptr,
nullptr};
124 if (_CheckBranch(detBranchName) == 0) {
126 std::string partHitsBranchName{std::string(detector).append(
"-GGSPartHits")};
127 std::string posHitsBranchName{std::string(detector).append(
"-GGSPartHits")};
128 TBranch *partHitsBranch = _chain->GetBranch(partHitsBranchName.c_str());
129 TBranch *posHitsBranch = _chain->GetBranch(posHitsBranchName.c_str());
131 std::string infoString(
"Set detector ");
132 infoString.append(detector);
137 infoString.append(
" (+PosHits)");
138 if (detIndex == -1) {
139 _posHitArrays.push_back(
nullptr);
140 _chain->SetBranchAddress(posHitsBranchName.c_str(), &(_posHitArrays.back()));
141 _readPosHits.push_back(
true);
143 _chain->SetBranchAddress(posHitsBranchName.c_str(), &(_posHitArrays[detIndex]));
144 _readPosHits[detIndex] =
true;
146 branches[2] = posHitsBranch;
148 throw std::runtime_error(std::string(
"PosHits missing for detector ") + detector);
151 if (detIndex == -1) {
152 _posHitArrays.push_back(
nullptr);
153 _readPosHits.push_back(
false);
156 posHitsBranch->ResetAddress();
158 _posHitArrays[detIndex] =
nullptr;
160 _readPosHits[detIndex] =
false;
166 if (partHitsBranch) {
167 infoString.append(
" (+PartHits)");
168 if (detIndex == -1) {
169 _partHitArrays.push_back(
nullptr);
170 _chain->SetBranchAddress(partHitsBranchName.c_str(), &(_partHitArrays.back()));
171 _readPartHits.push_back(
true);
173 _chain->SetBranchAddress(partHitsBranchName.c_str(), &(_partHitArrays[detIndex]));
174 _readPartHits[detIndex] =
true;
176 branches[1] = partHitsBranch;
178 throw std::runtime_error(std::string(
"PartHits missing for detector ") + detector);
181 if (detIndex == -1) {
182 _partHitArrays.push_back(
nullptr);
183 _readPartHits.push_back(
false);
185 if (partHitsBranch) {
186 partHitsBranch->ResetAddress();
188 _partHitArrays[detIndex] =
nullptr;
190 _readPartHits[detIndex] =
false;
194 if (detIndex == -1) {
195 _intHitArrays.push_back(
nullptr);
196 _chain->SetBranchAddress(detBranchName, &(_intHitArrays.back()));
197 branches[0] = _chain->GetBranch(detBranchName);
198 _detInfo = (TClonesArray *)(_chain->GetFile()->Get(
"GGSHitDetInfo"));
199 _timeBins.push_back((TArrayF *)(_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-TimeBins")));
200 _thresholds.push_back(
201 (std::vector<float> *)(_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-Thresholds")));
205 det.logVolName = detector;
207 det.logVolName.substr(0, det.logVolName.find_first_of(
'.'));
208 det.detInfoArrayIndex = _GetDetectorIndexFromStorage(det.logVolName.c_str());
209 _detectors.push_back(det);
213 throw std::runtime_error(std::string(
"IntHits missing for detector ") + detector);
217 if (_chain->GetReadEntry() != -1) {
218 for (
int iBranch = 0; iBranch < 3; ++iBranch) {
219 if (branches[iBranch]) {
220 branches[iBranch]->GetEntry(_chain->GetReadEntry());
231 std::string detectorString{detector};
232 detectorString +=
"-GGSIntHits";
233 type = _CheckBranch(detectorString.c_str());
235 if (type == 0 && particleHits) {
236 std::string detectorString{detector};
237 detectorString +=
"-GGSPartHits";
238 type = _CheckBranch(detectorString.c_str());
240 if (type == 1 && positionHits) {
241 std::string detectorString{detector};
242 detectorString +=
"-GGSPosHits";
243 type = _CheckBranch(detectorString.c_str());
252 static const std::string routineName(
"GGSTHitsReader::ListDetectors");
255 TObjArray *branches = _chain->GetListOfBranches();
256 COUT(INFO) <<
"List of detectors in hits tree:" <<
ENDL;
257 for (
int i = 0; i < branches->GetEntries(); i++) {
258 TBranch *branch = (TBranch *)(branches->At(i));
259 if (_CheckBranch(branch->GetName()) == 0) {
260 TString detName = branch->GetName();
261 detName.Remove(detName.Length() - 11);
262 std::string infoString(
" ");
263 infoString.append(detName.Data());
264 if (_CheckBranch(detName +
"-GGSPartHits") == 1)
265 infoString.append(
" (PartHits)");
266 if (_CheckBranch(detName +
"-GGSPosHits") == 2)
267 infoString.append(
" (PosHits)");
277 static const std::string routineName(
"GGSTHitsReader::GetListOfDetectors");
279 std::vector<TString> _list;
282 TObjArray *branches = _chain->GetListOfBranches();
283 for (
int i = 0; i < branches->GetEntries(); i++) {
284 TBranch *branch = (TBranch *)(branches->At(i));
285 std::string branchName(branch->GetName());
286 if (_CheckBranch(branch->GetName()) == 0) {
287 TString detName = branch->GetName();
288 detName.Remove(detName.Length() - 11);
289 _list.emplace_back(detName);
300 static short int iHitArray;
303 return (_GetHitArray(iHitArray))->GetEntries();
311 TClonesArray *detArray = _GetHitArray(iDet);
313 return detArray->GetEntries();
323 return GetHit(iDet, iHit);
329 static TClonesArray *hitArray =
nullptr;
330 hitArray = _GetHitArray(iDet);
336 hit->_hitDetInfo = _GetDetectorInfo(_detectors[iDet].detInfoArrayIndex);
337 hit->_timeBins = _timeBins[iDet];
338 if (hit->_hitDetInfo)
341 hit->_hitVolInfo =
nullptr;
342 if (_readPartHits[iDet]) {
343 hit->_partHits = (TClonesArray *)(_partHitArrays[iDet]->At(iHit));
345 if (_readPosHits[iDet]) {
347 TClonesArray *partHits = hit->_partHits;
348 int nPartHits = partHits->GetEntries();
349 for (
int iPartHit = 0; iPartHit < nPartHits; iPartHit++) {
351 partHit->_posHits = (TClonesArray *)(_posHitArrays[iDet]->At(partHit->_posHitIndex));
370 TClonesArray *hitArray = _GetHitArray(iDet);
372 std::string retValue = hitArray->GetName();
373 return retValue.substr(0, retValue.size() - 1);
381 for (UInt_t i = 0; i < _detectors.size(); i++) {
382 if (!strcmp(_detectors[i].name.data(), detector))
392 TString branchName(detector);
393 branchName.Append(
"-GGSPartHits");
394 TBranch *branch = _chain->GetBranch(branchName);
407 TString branchName(detector);
408 branchName.Append(
"-GGSPosHits");
409 TBranch *branch = _chain->GetBranch(branchName);
419 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
421 if (detIndex == -1) {
422 throw std::range_error(routineName +
": detector " + detector +
" not found");
425 if (_thresholds[detIndex]) {
426 return (*(_thresholds[detIndex]))[0];
428 throw std::runtime_error(routineName +
": thresholds for integrated hits not available for detector " + detector);
435 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
437 if (detIndex == -1) {
438 throw std::range_error(routineName +
": detector " + detector +
" not found");
441 if (_thresholds[detIndex]) {
442 return (*(_thresholds[detIndex]))[1];
444 throw std::runtime_error(routineName +
": thresholds for particle hits not available for detector " + detector);
451 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
453 if (detIndex == -1) {
454 throw std::range_error(routineName +
": detector " + detector +
" not found");
457 if (_thresholds[detIndex]) {
458 return (*(_thresholds[detIndex]))[2];
460 throw std::runtime_error(routineName +
": thresholds for position hits not available for detector " + detector);
466 int GGSTHitsReader::_GetDetectorIndexFromStorage(
const char *detector) {
468 for (
int iDet = 0; iDet < _detInfo->GetEntries(); iDet++) {
480 int GGSTHitsReader::_CheckBranch(
const char *branchName) {
481 const std::string routineName(
"GGSTHitsReader::_CheckBranch");
483 std::string branchNameStr{branchName};
485 if (branchNameStr.find(
"-GGSIntHits") != std::string::npos) {
488 }
else if (branchNameStr.find(
"-GGSPartHits") != std::string::npos) {
490 }
else if (branchNameStr.find(
"-GGSPosHits") != std::string::npos) {
496 TBranch *branch = _chain->GetBranch(branchName);
507 if (strcmp(branch->GetClassName(),
"TClonesArray")) {
511 if (strcmp(branch->GetClassName(),
"TObjArray")) {
518 char *oldBuffer = branch->GetAddress();
519 Long64_t oldReadEntry = branch->GetReadEntry();
520 TClonesArray *buffer =
nullptr;
521 branch->SetAddress(&buffer);
523 branch->SetAddress(oldBuffer);
524 branch->GetEntry(oldReadEntry);
525 std::string hitClassName(buffer->GetName());
526 hitClassName.erase(hitClassName.size() - 1);
528 TClass *clonesClass = TClass::GetClass(hitClassName.c_str());
530 COUT(ERROR) <<
"No dictionary for class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
534 if (!(clonesClass->GetBaseClass(
"GGSTIntHit"))) {
535 COUT(ERROR) <<
"Class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
536 <<
" does not inherit from GGSTIntHit" <<
ENDL;
546 TClonesArray *GGSTHitsReader::_GetHitArray(
int iHitArray) {
547 if (iHitArray > (
int)(_intHitArrays.size() - 1) || iHitArray < 0)
549 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.
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.
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
void ListDetectors()
Prints a list of detectors which are present in hits tree on standard output.
Class to store G4 particle hits.
GGSTHitsReader()
Constructor.
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
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.