17 #include "TFriendElement.h"
22 _chain(nullptr), _firstEntryOfCurrentFile(-1), _detInfo(nullptr) {
23 _detectors.reserve(50);
27 _intHitArrays.reserve(50);
28 _partHitArrays.reserve(50);
29 _posHitArrays.reserve(50);
36 for (
unsigned int i = 0; i < _intHitArrays.size(); i++)
37 delete _intHitArrays[i];
38 for (
unsigned int i = 0; i < _partHitArrays.size(); i++)
39 delete _partHitArrays[i];
40 for (
unsigned int i = 0; i < _posHitArrays.size(); i++)
41 delete _posHitArrays[i];
50 _detInfo = (TClonesArray*) (_chain->GetFile()->Get(
"GGSHitDetInfo"));
53 for (
int iBranch = 0; iBranch < _chain->GetListOfBranches()->GetEntries(); iBranch++) {
54 if (TString(((TBranch*) (_chain->GetListOfBranches()->At(iBranch)))->GetName()).Contains(
"GGSIntHits")) {
60 if (_chain->GetListOfFriends()) {
61 for (
int iFriend = 0; iFriend < _chain->GetListOfFriends()->GetEntries(); iFriend++) {
62 TTree *currFriend = ((TFriendElement*) (_chain->GetListOfFriends()->At(iFriend)))->GetTree();
63 for (
int iBranch = 0; iBranch < currFriend->GetListOfBranches()->GetEntries(); iBranch++) {
64 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 COUT(DEBUG) <<
"Switching to file " << _chain->GetFile()->GetName() <<
ENDL;
89 _firstEntryOfCurrentFile = _chain->GetChainEntryNumber(0);
92 _detInfo = (TClonesArray*) (_chain->GetFile()->Get(
"GGSHitDetInfo"));
95 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++)
96 _detectors[iDet].detInfoArrayIndex = _GetDetectorIndexFromStorage(_detectors[iDet].logVolName.data());
101 for (
unsigned int iDet = 0; iDet < _detectors.size(); iDet++) {
102 delete _timeBins[iDet];
103 _timeBins.push_back((TArrayF*) (_chain->GetFile()->Get(TString(_detectors[iDet].name) +
"-GGSIntHits-TimeBins")));
104 delete _thresholds[iDet];
105 _thresholds.push_back(
106 (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 };
128 if (_CheckBranch(detBranchName)) {
131 TBranch *partHitsBranch = _chain->GetBranch(std::string(detector).append(
"-GGSPartHits").c_str());
132 TBranch *posHitsBranch = _chain->GetBranch(std::string(detector).append(
"-GGSPosHits").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 posHitsBranch->SetAddress(&(_posHitArrays.back()));
144 _readPosHits.push_back(
true);
147 posHitsBranch->SetAddress(&(_posHitArrays[detIndex]));
148 _readPosHits[detIndex] =
true;
150 branches[2] = posHitsBranch;
153 throw std::runtime_error(std::string(
"PosHits missing for detector ") + detector);
157 if (detIndex == -1) {
158 _posHitArrays.push_back(
nullptr);
159 _readPosHits.push_back(
false);
163 posHitsBranch->ResetAddress();
165 _posHitArrays[detIndex] =
nullptr;
166 _readPosHits[detIndex] =
false;
172 if (partHitsBranch) {
173 infoString.append(
" (+PartHits)");
174 if (detIndex == -1) {
175 _partHitArrays.push_back(
nullptr);
176 partHitsBranch->SetAddress(&(_partHitArrays.back()));
177 _readPartHits.push_back(
true);
180 partHitsBranch->SetAddress(&(_partHitArrays[detIndex]));
181 _readPartHits[detIndex] =
true;
183 branches[1] = partHitsBranch;
186 throw std::runtime_error(std::string(
"PartHits missing for detector ") + detector);
190 if (detIndex == -1) {
191 _partHitArrays.push_back(
nullptr);
192 _readPartHits.push_back(
false);
195 if (partHitsBranch) {
196 partHitsBranch->ResetAddress();
198 _partHitArrays[detIndex] =
nullptr;
199 _readPartHits[detIndex] =
false;
203 if (detIndex == -1) {
204 _intHitArrays.push_back(
nullptr);
205 _chain->SetBranchAddress(detBranchName, &(_intHitArrays.back()));
206 branches[0] = _chain->GetBranch(detBranchName);
207 _detInfo = (TClonesArray*) (_chain->GetFile()->Get(
"GGSHitDetInfo"));
208 _timeBins.push_back((TArrayF*) (_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-TimeBins")));
209 _thresholds.push_back(
210 (std::vector<float>*) (_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-Thresholds")));
214 det.logVolName = detector;
215 det.logVolName = det.logVolName.substr(0, det.logVolName.find_first_of(
'.'));
216 det.detInfoArrayIndex = _GetDetectorIndexFromStorage(det.logVolName.c_str());
217 _detectors.push_back(det);
222 throw std::runtime_error(std::string(
"IntHits missing for detector ") + detector);
226 if (_chain->GetReadEntry() != -1) {
227 for (
int iBranch = 0; iBranch < 3; ++iBranch) {
228 if (branches[iBranch]) {
229 branches[iBranch]->GetEntry(_chain->GetReadEntry());
240 TString detectorString(detector);
241 detectorString.Append(
"-GGSIntHits");
242 return _CheckBranch(detectorString.Data());
253 static const std::string routineName(
"GGSTHitsReader::ListDetectors");
256 TObjArray *branches = _chain->GetListOfBranches();
257 COUT(INFO) <<
"List of detectors in hits tree:" <<
ENDL;
258 for (
int i = 0; i < branches->GetEntries(); i++) {
259 TBranch *branch = (TBranch*) (branches->At(i));
260 GGSSmartLog::MuteOutput();
261 bool isDetBranch = _CheckBranch(branch->GetName());
262 GGSSmartLog::UnmuteOutput();
264 TString detName = branch->GetName();
265 detName.Remove(detName.Length() - 11);
266 std::string infoString(
" ");
267 infoString.append(detName.Data());
268 if (_chain->GetBranch(detName +
"-GGSPartHits"))
269 infoString.append(
" (PartHits)");
270 if (_chain->GetBranch(detName +
"-GGSPosHits"))
271 infoString.append(
" (PosHits)");
283 static const std::string routineName(
"GGSTHitsReader::GetListOfDetectors");
285 std::vector<TString> _list;
288 TObjArray *branches = _chain->GetListOfBranches();
289 for (
int i = 0; i < branches->GetEntries(); i++) {
290 TBranch *branch = (TBranch*) (branches->At(i));
291 if (_CheckBranch(branch->GetName())) {
292 TString detName = branch->GetName();
293 detName.Remove(detName.Length() - 11);
294 _list.emplace_back(detName);
306 static short int iHitArray;
309 return (_GetHitArray(iHitArray))->GetEntries();
318 TClonesArray *detArray = _GetHitArray(iDet);
320 return detArray->GetEntries();
331 return GetHit(iDet, iHit);
337 static TClonesArray *hitArray =
nullptr;
338 hitArray = _GetHitArray(iDet);
345 hit->_hitDetInfo = _GetDetectorInfo(_detectors[iDet].detInfoArrayIndex);
346 hit->_timeBins = _timeBins[iDet];
347 if (hit->_hitDetInfo)
350 hit->_hitVolInfo =
nullptr;
351 if (_readPartHits[iDet]) {
352 hit->_partHits = (TClonesArray*) (_partHitArrays[iDet]->At(iHit));
354 if (_readPosHits[iDet]) {
356 TClonesArray *partHits = hit->_partHits;
357 int nPartHits = partHits->GetEntries();
358 for (
int iPartHit = 0; iPartHit < nPartHits; iPartHit++) {
360 partHit->_posHits = (TClonesArray*) (_posHitArrays[iDet]->At(partHit->_posHitIndex));
372 for (UInt_t i = 0; i < _detectors.size(); i++) {
373 if (!strcmp(_detectors[i].name.data(), detector))
383 TString branchName(detector);
384 branchName.Append(
"-GGSPartHits");
385 TBranch *branch = _chain->GetBranch(branchName);
399 TString branchName(detector);
400 branchName.Append(
"-GGSPosHits");
401 TBranch *branch = _chain->GetBranch(branchName);
412 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
414 if (detIndex == -1) {
415 throw std::range_error(routineName +
": detector " + detector +
" not found");
418 if (_thresholds[detIndex]) {
419 return (*(_thresholds[detIndex]))[0];
422 throw std::runtime_error(routineName +
": thresholds for integrated hits not available for detector " + detector);
429 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
431 if (detIndex == -1) {
432 throw std::range_error(routineName +
": detector " + detector +
" not found");
435 if (_thresholds[detIndex]) {
436 return (*(_thresholds[detIndex]))[1];
439 throw std::runtime_error(routineName +
": thresholds for particle hits not available for detector " + detector);
446 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
448 if (detIndex == -1) {
449 throw std::range_error(routineName +
": detector " + detector +
" not found");
452 if (_thresholds[detIndex]) {
453 return (*(_thresholds[detIndex]))[2];
456 throw std::runtime_error(routineName +
": thresholds for position hits not available for detector " + detector);
462 int GGSTHitsReader::_GetDetectorIndexFromStorage(
const char *detector) {
465 for (
int iDet = 0; iDet < _detInfo->GetEntries(); iDet++) {
477 bool GGSTHitsReader::_CheckBranch(
const char *branchName) {
479 static const std::string routineName(
"GGSTHitsReader::_CheckBranch");
480 TBranch *branch = _chain->GetBranch(branchName);
489 if (strcmp(branch->GetClassName(),
"TClonesArray")) {
495 char *oldBuffer = branch->GetAddress();
496 Long64_t oldReadEntry = branch->GetReadEntry();
497 TClonesArray *buffer =
nullptr;
498 branch->SetAddress(&buffer);
500 branch->SetAddress(oldBuffer);
501 branch->GetEntry(oldReadEntry);
502 std::string hitClassName(buffer->GetName());
503 hitClassName.erase(hitClassName.size() - 1);
504 TClass *clonesClass = TClass::GetClass(hitClassName.c_str());
506 COUT(ERROR) <<
"No dictionary for class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
510 if (!(clonesClass->GetBaseClass(
"GGSTIntHit"))) {
511 COUT(ERROR) <<
"Class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
512 <<
" does not inherit from GGSTIntHit" <<
ENDL;
521 TClonesArray* GGSTHitsReader::_GetHitArray(
int iHitArray) {
523 if (iHitArray > (
int) (_intHitArrays.size() - 1) || iHitArray < 0)
525 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.
float GetPartHitThreshold(const std::string &detector)
Retrieves the thresholds used when saving the particle hits.
Float_t eDep
Deposited energy (transient).
#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.
GGSTIntHit * GetHit(const char *detector, unsigned int iHit)
Get the specified hit.
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
TClonesArray volumes
Array of GGSTHitVolInfo objects.
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.
TArrayF eDepTimeBin
Energy deposit in each time bin.
Class to store G4 position hits.
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.
bool DetectorExists(const char *detector)
Checks the existence of a given detector within the hits tree.
void GetEntry(Long64_t entry)
std::vector< TString > GetListOfDetectors()
Returns the list of detectors which are present in hits tree on standard output.
GGSTHitVolInfo.h GGSTHitVolInfo class declaration.
TString detectorName
Name of detector associated to integrated hits.
void SetDetector(const char *detector, bool readPartHits=false, bool readPosHits=false)
Enables reading hit informations of the specified detector.