17 #include "TFriendElement.h"
22 _chain(NULL), _firstEntryOfCurrentFile(-1), _detInfo(NULL) {
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 };
127 if (_CheckDetector(detector) && _CheckBranch(detBranchName)) {
129 std::string infoString(
"Set detector ");
130 infoString.append(detector);
133 TString branchName(detector);
134 branchName.Append(
"-GGSPartHits");
135 TBranch *branch = _chain->GetBranch(branchName);
137 infoString.append(
" (+PartHits)");
138 _partHitArrays.push_back(NULL);
139 _chain->SetBranchAddress(branchName, &(_partHitArrays.back()));
140 branches[1] = _chain->GetBranch(branchName);
141 _readPartHits.push_back(
true);
144 throw std::runtime_error(std::string(
"PartHits missing for detector ") + detector);
148 _partHitArrays.push_back(NULL);
149 _readPartHits.push_back(
false);
153 TString branchName(detector);
154 branchName.Append(
"-GGSPosHits");
155 TBranch *branch = _chain->GetBranch(branchName);
157 infoString.append(
" (+PosHits)");
158 _posHitArrays.push_back(NULL);
159 _chain->SetBranchAddress(branchName, &(_posHitArrays.back()));
160 branches[2] = _chain->GetBranch(branchName);
161 _readPosHits.push_back(
true);
164 _partHitArrays.pop_back();
165 _readPartHits.pop_back();
166 _chain->ResetBranchAddress(_chain->GetBranch((std::string(detector) +
"-GGSPartHits").c_str()));
167 throw std::runtime_error(std::string(
"PosHits missing for detector ") + detector);
171 _posHitArrays.push_back(NULL);
172 _readPosHits.push_back(
false);
175 _intHitArrays.push_back(NULL);
176 _chain->SetBranchAddress(detBranchName, &(_intHitArrays.back()));
177 branches[0] = _chain->GetBranch(detBranchName);
178 _detInfo = (TClonesArray*) (_chain->GetFile()->Get(
"GGSHitDetInfo"));
179 _timeBins.push_back((TArrayF*) (_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-TimeBins")));
180 _thresholds.push_back((std::vector<float>*) (_chain->GetFile()->Get(TString(detector) +
"-GGSIntHits-Thresholds")));
184 det.logVolName = detector;
185 det.logVolName = det.logVolName.substr(0, det.logVolName.find_first_of(
'.'));
186 det.detInfoArrayIndex = _GetDetectorIndexFromStorage(det.logVolName.c_str());
187 _detectors.push_back(det);
192 throw std::runtime_error(std::string(
"IntHits missing for detector ") + detector);
196 if (_chain->GetReadEntry() != -1) {
197 for (
int iBranch = 0; iBranch < 3; ++iBranch) {
198 if (branches[iBranch]) {
199 branches[iBranch]->GetEntry(_chain->GetReadEntry());
210 TString detectorString(detector);
211 detectorString.Append(
"-GGSIntHits");
212 return _CheckBranch(detectorString.Data());
223 static const std::string routineName(
"GGSTHitsReader::ListDetectors");
226 TObjArray *branches = _chain->GetListOfBranches();
227 COUT(INFO) <<
"List of detectors in hits tree:" <<
ENDL;
228 for (
int i = 0; i < branches->GetEntries(); i++) {
229 TBranch *branch = (TBranch*) (branches->At(i));
230 if (_CheckBranch(branch->GetName())) {
231 TString detName = branch->GetName();
232 detName.Remove(detName.Length() - 11);
233 std::string infoString(
" ");
234 infoString.append(detName.Data());
235 if (_chain->GetBranch(detName +
"-GGSPartHits"))
236 infoString.append(
" (PartHits)");
237 if (_chain->GetBranch(detName +
"-GGSPosHits"))
238 infoString.append(
" (PosHits)");
250 static const std::string routineName(
"GGSTHitsReader::GetListOfDetectors");
252 std::vector<TString> _list;
255 TObjArray *branches = _chain->GetListOfBranches();
256 for (
int i = 0; i < branches->GetEntries(); i++) {
257 TBranch *branch = (TBranch*) (branches->At(i));
258 if (_CheckBranch(branch->GetName())) {
259 TString detName = branch->GetName();
260 detName.Remove(detName.Length() - 11);
261 _list.emplace_back(detName);
273 static short int iHitArray;
276 return (_GetHitArray(iHitArray))->GetEntries();
285 TClonesArray *detArray = _GetHitArray(iDet);
287 return detArray->GetEntries();
298 return GetHit(iDet, iHit);
304 static TClonesArray* hitArray = NULL;
305 hitArray = _GetHitArray(iDet);
312 hit->_hitDetInfo = _GetDetectorInfo(_detectors[iDet].detInfoArrayIndex);
313 hit->_timeBins = _timeBins[iDet];
314 if (hit->_hitDetInfo)
317 hit->_hitVolInfo = NULL;
318 if (_readPartHits[iDet]) {
319 hit->_partHits = (TClonesArray*) (_partHitArrays[iDet]->At(iHit));
321 if (_readPosHits[iDet]) {
323 TClonesArray *partHits = hit->_partHits;
324 int nPartHits = partHits->GetEntries();
325 for (
int iPartHit = 0; iPartHit < nPartHits; iPartHit++) {
327 partHit->_posHits = (TClonesArray*) (_posHitArrays[iDet]->At(partHit->_posHitIndex));
339 for (UInt_t i = 0; i < _detectors.size(); i++) {
340 if (!strcmp(_detectors[i].name.data(), detector))
350 TString branchName(detector);
351 branchName.Append(
"-GGSPartHits");
352 TBranch *branch = _chain->GetBranch(branchName);
366 TString branchName(detector);
367 branchName.Append(
"-GGSPosHits");
368 TBranch *branch = _chain->GetBranch(branchName);
379 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
381 if (detIndex == -1) {
382 throw std::range_error(routineName +
": detector " + detector +
" not found");
385 if (_thresholds[detIndex]) {
386 return (*(_thresholds[detIndex]))[0];
389 throw std::runtime_error(routineName +
": thresholds for integrated hits not available for detector " + detector);
396 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
398 if (detIndex == -1) {
399 throw std::range_error(routineName +
": detector " + detector +
" not found");
402 if (_thresholds[detIndex]) {
403 return (*(_thresholds[detIndex]))[1];
406 throw std::runtime_error(routineName +
": thresholds for particle hits not available for detector " + detector);
413 const std::string routineName(
"GSTHitsReader::GetIntHitThreshold");
415 if (detIndex == -1) {
416 throw std::range_error(routineName +
": detector " + detector +
" not found");
419 if (_thresholds[detIndex]) {
420 return (*(_thresholds[detIndex]))[2];
423 throw std::runtime_error(routineName +
": thresholds for position hits not available for detector " + detector);
429 int GGSTHitsReader::_GetDetectorIndexFromStorage(
const char* detector) {
432 for (
int iDet = 0; iDet < _detInfo->GetEntries(); iDet++) {
444 bool GGSTHitsReader::_CheckBranch(
const char *branchName) {
446 static const std::string routineName(
"GGSTHitsReader::_CheckBranch");
447 TBranch* branch = _chain->GetBranch(branchName);
456 if (strcmp(branch->GetClassName(),
"TClonesArray")) {
462 char *oldBuffer = branch->GetAddress();
463 Long64_t oldReadEntry = branch->GetReadEntry();
464 TClonesArray *buffer = NULL;
465 branch->SetAddress(&buffer);
467 branch->SetAddress(oldBuffer);
468 branch->GetEntry(oldReadEntry);
469 std::string hitClassName(buffer->GetName());
470 hitClassName.erase(hitClassName.size() - 1);
471 TClass *clonesClass = TClass::GetClass(hitClassName.c_str());
473 COUT(ERROR) <<
"No dictionary for class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
477 if (!(clonesClass->GetBaseClass(
"GGSTIntHit"))) {
478 COUT(ERROR) <<
"Class " << hitClassName <<
" inside the TCloneArray in branch " << branchName
479 <<
" does not inherit from GGSTIntHit" <<
ENDL;
488 bool GGSTHitsReader::_CheckDetector(
const char *detector) {
491 for (
unsigned int i = 0; i < _detectors.size(); i++) {
492 if (_detectors[i].name == detector)
500 TClonesArray* GGSTHitsReader::_GetHitArray(
int iHitArray) {
502 if (iHitArray > (
int) (_intHitArrays.size() - 1) || iHitArray < 0)
504 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.