AGAPRO_TB_AGATA.cpp 26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/***************************************************************************
 *   Copyright (C) 2018 by Jeremie Dudouet                                 *
 *   jeremie.dudouet(AT)csnsm.in2p3.fr                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef AGAPRO_TB_AGATA
#include "AGAPRO_TB_AGATA.h"
#endif

25
#include <utility>
26 27
#include <algorithm>

28 29 30 31 32 33
#include "TrackedFrame.h"
#include "PSAFrame.h"

using namespace AGAPRO;


34 35
/// ****** CRYSTAL ******///

36
TB_AGATA_CRYSTAL::TB_AGATA_CRYSTAL(TString name, TString ADFKey) : TB_Detector(move(name),move(ADFKey))
37 38 39 40
{
    gActualClass = "TB_AGATA_CRYSTAL";
}

41
TB_AGATA_CRYSTAL::~TB_AGATA_CRYSTAL() = default;
42 43 44

void TB_AGATA_CRYSTAL::Init()
{
45 46 47 48 49 50 51 52 53 54 55 56
    if(fTriggerName.Contains("ccrystal")) {
        fTraceLenght = kCCrystalTraceLenght;
        fType = ccrystal;
    }
    else if(fTriggerName.Contains("crystal")) {
        fTraceLenght = kCrystalTraceLenght;
        fType = crystal;
    }
    else {
        fTraceLenght = kMaxTraceLenght;
        fType = undef;
    }
57 58 59 60 61 62 63 64 65 66 67
}

void TB_AGATA_CRYSTAL::InitValues()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitValues()");

    crystalTS = 0;
    crystalId = 0;

68 69 70 71 72 73 74 75
    if(fType == crystal) {
        memset(SegTracesu, 0, sizeof(SegTracesu));
        memset(CoreTracesu, 0, sizeof(CoreTracesu));
    }
    else if(fType == ccrystal) {
        memset(SegTraces, 0, sizeof(SegTraces));
        memset(CoreTraces, 0, sizeof(CoreTraces));
    }
76 77
    memset(SegE, 0, sizeof(SegE));
    memset(CoreE, 0, sizeof(CoreE));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
78
    memset(CoreT, 0, sizeof(CoreT));
79 80 81 82 83 84 85 86 87 88 89 90
}


void TB_AGATA_CRYSTAL::InitTree()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitTree()");

    if(fTree == nullptr)
        return;

91 92 93 94 95 96 97 98 99 100
    if(fType == crystal) {
        fTree->Branch("SegTraces",      SegTracesu,      Form("SegTraces[%d]/S",kNSG*fTraceLenght));
        fTree->Branch("CoreTraces",     CoreTracesu,     Form("CoreTraces[%d]/S",kNCC*fTraceLenght));
    }
    else if(fType == ccrystal){
        fTree->Branch("SegTraces",      SegTraces,      Form("SegTraces[%d]/F",kNSG*fTraceLenght));
        fTree->Branch("CoreTraces",     CoreTraces,     Form("CoreTraces[%d]/F",kNCC*fTraceLenght));
    }
    fTree->Branch("SegE",           SegE,           Form("SegE[%d]/F",kNSG));
    fTree->Branch("CoreE",          CoreE,          Form("CoreE[%d]/F",kNCC));
Jérémie Dudouet's avatar
Jérémie Dudouet committed
101
    fTree->Branch("CoreT",          CoreT,          Form("CoreT[%d]/F",kNCC));
102

103 104
    fTree->Branch("CrystalId",      &crystalId,     "CrystalId/I");
    fTree->Branch("CrystalTS",      &crystalTS,     "CrystalTS/l");
105 106 107 108 109 110 111 112 113 114 115
}

void TB_AGATA_CRYSTAL::Process(Int_t idet)
{
    Log.ClearMessage();
    Log.GetProcessName() = "TB_AGATA_CRYSTAL";
    Log.SetProcessMethod(Form("Process(%d)",idet));

    fSFrame = fTrigger->GetInputSharedFP();
    fSFrame->GetFrame()->Read();

116
    auto *data = GetDataPointer<CrystalInterface>(fSFrame);
117

118
    crystalTS = (dynamic_cast<AgataKey *>(fSFrame->GetFrame()->GetKey()))->GetTimeStamp();
119 120
    crystalId = data->GetUID();

121 122
    if(!WarnedDone && data && data->GetCore(0)->GetSignal()->GetLength()!=fTraceLenght) {
        Log << error << " Signal length: " << data->GetCore(0)->GetSignal()->GetLength() << " ; Stored length in tree: " << fTraceLenght << nline;
123 124 125 126
        WarnedDone = true;
    }

    for (Int_t i = 0; i < kNCC; i++ ) {
127 128 129 130 131
        if(fType==crystal)
            memcpy(&CoreTracesu[i*fTraceLenght],data->GetCore(i)->GetSignal()->Address(CoreTracesu),sizeof(UShort_t)*fTraceLenght);
        else if(fType == ccrystal)
            memcpy(&CoreTraces[i*fTraceLenght],data->GetCore(i)->GetSignal()->Address(CoreTraces),sizeof(Float_t)*fTraceLenght);

132
        CoreE[i] = data->GetCore(i)->GetE();
Jérémie Dudouet's avatar
Jérémie Dudouet committed
133
        CoreT[i] = data->GetCore(i)->GetT();
134 135 136
    }

    for (Int_t i = 0; i < kNSG; i++ ) {
137 138 139 140 141
        if(fType==crystal)
            memcpy(&SegTracesu[i*fTraceLenght],data->GetSegment(i)->GetSignal()->Address(SegTracesu),sizeof(UShort_t)*fTraceLenght);
        if(fType==ccrystal)
            memcpy(&SegTraces[i*fTraceLenght],data->GetSegment(i)->GetSignal()->Address(SegTraces),sizeof(Float_t)*fTraceLenght);

142 143 144 145 146 147
        SegE[i] = data->GetSegment(i)->GetE();
    }
}

/// ****** PSA ******///

148
TB_AGATA_PSA::TB_AGATA_PSA(TString name, TString ADFKey) : TB_Detector(move(name),move(ADFKey))
149 150 151 152
{
    gActualClass = "TB_AGATA_PSA";
}

153
TB_AGATA_PSA::~TB_AGATA_PSA() = default;
154 155 156

void TB_AGATA_PSA::Init()
{
157
    ReadTransformation();
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

void TB_AGATA_PSA::InitValues()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitValues()");

    nbHits = 0;

    coreId = 0.;
    coreE0 = 0.;
    coreE1 = 0.;
    coreT0 = 0.;
    coreT1 = 0.;
    coreTS = 0.;
}


void TB_AGATA_PSA::InitTree()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitPSAHits()");

    if(fTree == nullptr)
        return;

    fTree->Branch("nbHits",         &nbHits,        "nbHits/I");

    fTree->Branch("hitE",           hitE,           "hitE[nbHits]/F");
    fTree->Branch("hitX",           hitX,           "hitX[nbHits]/F");
    fTree->Branch("hitY",           hitY,           "hitY[nbHits]/F");
    fTree->Branch("hitZ",           hitZ,           "hitZ[nbHits]/F");
    fTree->Branch("hitGX",          hitGX,          "hitGX[nbHits]/F");
    fTree->Branch("hitGY",          hitGY,          "hitGY[nbHits]/F");
    fTree->Branch("hitGZ",          hitGZ,          "hitGZ[nbHits]/F");
    fTree->Branch("hitSg",          hitSg,          "hitSg[nbHits]/I");

    fTree->Branch("coreId",         &coreId,        "coreId/I");
    fTree->Branch("coreE0",         &coreE0,        "coreE0/F");
    fTree->Branch("coreE1",         &coreE1,        "coreE1/F");
    fTree->Branch("coreT0",         &coreT0,        "coreT0/F");
    fTree->Branch("coreT1",         &coreT1,        "coreT1/F");
    fTree->Branch("coreTS",         &coreTS,        "coreTS/l");
}

void TB_AGATA_PSA::Process(Int_t idet)
{
    Log.ClearMessage();
    Log.GetProcessName() = "TB_AGATA_PSA";
    Log.SetProcessMethod(Form("Process(%d)",idet));

    fSFrame = fTrigger->GetInputSharedFP();
    fSFrame->GetFrame()->Read();

214
    auto *data = GetDataPointer<PSAInterface>(fSFrame);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    nbHits = data->GetNbHits();

    if ( nbHits > MaxHits ) {
        Log << error << " Number of Hits : " << nbHits << " ; Max nbHits = " << MaxHits << nline;
        InitValues();
        return;
    }

    // Core like branches
    coreId        = data->GetUID();
    coreE0        = data->GetE(0);
    coreE1        = data->GetE(1);
    coreT0        = data->GetT(0);
    coreT1        = data->GetT(1);
230
    coreTS        = dynamic_cast<AgataKey *>(fSFrame->GetFrame()->GetKey())->GetTimeStamp();
231 232 233

    // Loop on the hits
    for (Int_t j = 0; j < nbHits ; ++j) {
234
        const PSAHit* pHit = dynamic_cast<PSAHit *>(data->GetHit(j));
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

        hitE[j]   = pHit->GetE();
        hitX[j]   = pHit->GetX();
        hitY[j]   = pHit->GetY();
        hitZ[j]   = pHit->GetZ();
        hitSg[j]  = pHit->GetID();

        //Get hit position
        Double_t xLocal, yLocal, zLocal, xGlobal, yGlobal, zGlobal;
        pHit->GetXYZ(xLocal, yLocal, zLocal);
        //To the AGATA global ref
        Local2Global(data->GetUID(), xLocal, yLocal, zLocal, xGlobal, yGlobal, zGlobal);

        // Set the Position of the hit in global ref
        hitGX[j]= xGlobal; // inversion X-Y and opposite direction
        hitGY[j]= yGlobal; // inversion Y-X and opposite direction
        hitGZ[j]= zGlobal; // z
    }
}

255 256
/// ****** Builder CRYSTAL ******///

257
TB_AGATA_Builder_CRYSTAL::TB_AGATA_Builder_CRYSTAL(TString name, TString ADFKey) : TB_Detector(move(name),move(ADFKey))
258
{
259
    gActualClass = "TB_AGATA_Builder_CRYSTAL";
260 261
}

262
TB_AGATA_Builder_CRYSTAL::~TB_AGATA_Builder_CRYSTAL() = default;
263 264

void TB_AGATA_Builder_CRYSTAL::Init()
265
{
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    if(fTriggerName.Contains("ccrystal")) {
        fSFrame = fTrigger->AddUtility("data:ccrystal",ConfAgent::theGlobalAgent());
        fTraceLenght = kCCrystalTraceLenght;
        fType = ccrystal;
    }
    else if(fTriggerName.Contains("crystal")){
        fSFrame = fTrigger->AddUtility("data:crystal",ConfAgent::theGlobalAgent());
        fTraceLenght = kCrystalTraceLenght;
        fType = crystal;
    }
    else {
        fTraceLenght = kMaxTraceLenght;
    }
}

void TB_AGATA_Builder_CRYSTAL::InitValues()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitValues()");

    nbCrystals = 0;
    SegTracesSize = 0;
    CoreTracesSize = 0;
    SegESize = 0;
    CoreESize = 0;
    EvtCrystalTS = 0;
}


void TB_AGATA_Builder_CRYSTAL::InitTree()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitTree()");

    if(fTree == nullptr)
        return;

    fTree->Branch("nbCrystals",         &nbCrystals,            "nbCrystals/I");
    fTree->Branch("CrystalId",          crystalId,              "CrystalId[nbCrystals]/F");
    fTree->Branch("CrystalTS",          crystalTS,              "CrystalTS[nbCrystals]/F");

    fTree->Branch("SegTracesSize",      &SegTracesSize,         "SegTracesSize/I");
    if(fType == crystal)
        fTree->Branch("SegTraces",      SegTracesu,             "SegTraces[SegTracesSize]/S");
    else if(fType == ccrystal)
        fTree->Branch("SegTraces",      SegTraces,              "SegTraces[SegTracesSize]/F");

    fTree->Branch("CoreTracesSize",     &CoreTracesSize,        "CoreTracesSize/I");
    if(fType == crystal)
        fTree->Branch("CoreTraces",     CoreTracesu,            "CoreTraces[CoreTracesSize]/S");
    else if(fType == ccrystal)
        fTree->Branch("CoreTraces",     CoreTraces,             "CoreTraces[CoreTracesSize]/F");

    fTree->Branch("SegESize",           &SegESize,              "SegESize/I");
    fTree->Branch("SegE",               SegE,                   "SegE[SegESize]/F");

    fTree->Branch("CoreESize",          &CoreESize,             "CoreESize/I");
    fTree->Branch("CoreE",              CoreE,                  "CoreE[CoreESize]/F");

    fTree->Branch("EvtCrystalTS",       &EvtCrystalTS,          "EvtCrystalTS/l");
328 329
}

330 331

void TB_AGATA_Builder_CRYSTAL::Process(Int_t idet)
332
{
333 334 335 336 337
    Log.ClearMessage();
    Log.GetProcessName() = "TB_AGATA_Builder_CRYSTAL";
    Log.SetProcessMethod(Form("Process(%d)",idet));

    if(!fMergerMode)
338
        fCompFrame = dynamic_cast<AgataCompositeFrame*>(fTrigger->GetInputSharedFP()->GetFrame());
339
    else
340
        fCompFrame = dynamic_cast<AgataCompositeFrame*>(fTrigger->GetInputSharedFP(idet+1)->GetFrame());
341 342 343

    fCompFrame->Scan();

344
    EvtCrystalTS = dynamic_cast<AgataKey*>(fCompFrame->GetKey())->GetTimeStamp();
345 346 347 348 349 350 351 352 353 354 355 356 357

    nbCrystals = fCompFrame->GetNbSubFrame();

    CoreTracesSize = nbCrystals*kNCC*fTraceLenght;
    CoreESize = nbCrystals*kNCC;

    SegTracesSize = nbCrystals*kNSG*fTraceLenght;
    SegESize = nbCrystals*kNSG;

    for(Int_t i=0 ; i < nbCrystals ; i++) {
        fCompFrame->LinkSubFrame(i,fSFrame->GetFrame());
        fSFrame->GetFrame()->Read();

358
        auto *data = GetDataPointer<CrystalInterface>(fSFrame);
359 360 361 362 363 364

        if(!WarnedDone && data && data->GetCore(0)->GetSignal()->GetLength()!=fTraceLenght) {
            Log << error << " Signal length: " << data->GetCore(0)->GetSignal()->GetLength() << " ; Stored length in tree: " << fTraceLenght << nline;
            WarnedDone = true;
        }

365
        crystalTS[i] = dynamic_cast<AgataKey*>(fSFrame->GetFrame()->GetKey())->GetTimeStamp();
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        crystalId[i] = data->GetUID();

        for (Int_t j = 0; j < kNCC; j++ ) {
            if(fType==crystal)
                memcpy(&CoreTracesu[i*fTraceLenght*kNCC+fTraceLenght*j],data->GetCore(j)->GetSignal()->Address(CoreTracesu),sizeof(UShort_t)*fTraceLenght);
            else if(fType == ccrystal)
                memcpy(&CoreTraces[i*fTraceLenght*kNCC+fTraceLenght*j],data->GetCore(j)->GetSignal()->Address(CoreTraces),sizeof(Float_t)*fTraceLenght);

            CoreE[i*kNCC+j] = data->GetCore(j)->GetE();
        }

        for (Int_t j = 0; j < kNSG; j++ ) {
            if(fType==crystal)
                memcpy(&SegTracesu[i*fTraceLenght*kNSG+fTraceLenght*j],data->GetSegment(j)->GetSignal()->Address(SegTracesu),sizeof(UShort_t    )*fTraceLenght);
            else if(fType == ccrystal)
                memcpy(&SegTraces[i*fTraceLenght*kNSG+fTraceLenght*j],data->GetSegment(j)->GetSignal()->Address(SegTraces),sizeof(Float_t)*fTraceLenght);

            SegE[i*kNSG+j] = data->GetSegment(j)->GetE();
        }
    }
}

/// ****** Builder PSA ******///
389

390
TB_AGATA_Builder_PSA::TB_AGATA_Builder_PSA(TString name, TString ADFKey) : TB_Detector(move(name),move(ADFKey))
391
{
392
    gActualClass = "TB_AGATA_Builder_PSA";
393 394
}

395
TB_AGATA_Builder_PSA::~TB_AGATA_Builder_PSA() = default;
396

397
void TB_AGATA_Builder_PSA::Init()
398
{
399
    fSFrame = fTrigger->AddUtility("data:psa",ConfAgent::theGlobalAgent());
400 401 402 403

    ReadTransformation();
}

404
void TB_AGATA_Builder_PSA::InitValues()
405 406 407 408 409 410 411 412 413 414 415 416
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitValues()");

    nbHits = 0;
    nbCores = 0;
    nbAdd = 0;
    TSHit = 0;
}


417
void TB_AGATA_Builder_PSA::InitTree()
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitPSAHits()");

    if(fTree == nullptr)
        return;

    fTree->Branch("nbHits",         &nbHits,"nbHits/I");

    fTree->Branch("hitE",           hitE,           "hitE[nbHits]/F");
    fTree->Branch("hitX",           hitX,           "hitX[nbHits]/F");
    fTree->Branch("hitY",           hitY,           "hitY[nbHits]/F");
    fTree->Branch("hitZ",           hitZ,           "hitZ[nbHits]/F");
    fTree->Branch("hitGX",          hitGX,          "hitGX[nbHits]/F");
    fTree->Branch("hitGY",          hitGY,          "hitGY[nbHits]/F");
    fTree->Branch("hitGZ",          hitGZ,          "hitGZ[nbHits]/F");
    fTree->Branch("hitId",          hitId,          "hitId[nbHits]/I");
    fTree->Branch("hitSg",          hitSg,          "hitSg[nbHits]/I");

    fTree->Branch("nbCores",        &nbCores,       "nbCores/I");
    fTree->Branch("nbHitsperCry",   nbHitsperCry,   "nbHitsperCry[nbCores]/I");
    fTree->Branch("coreId",         coreId,         "coreId[nbCores]/I");
    fTree->Branch("coreE0",         coreE0,         "coreE0[nbCores]/F");
442 443
    fTree->Branch("coreE1",         coreE1,         "coreE1[nbCores]/F");
    fTree->Branch("coreT0",         coreT0,         "coreT0[nbCores]/F");
444 445 446 447 448 449 450 451 452 453 454 455
    fTree->Branch("coreTS",         coreTS,         "coreTS[nbCores]/l");

    fTree->Branch("nbAdd",          &nbAdd,         "nbAdd/I");
    fTree->Branch("AddId",          AddId,          "AddId[nbAdd]/I");
    fTree->Branch("AddE",           AddE,           "AddE[nbAdd]/F");
    fTree->Branch("AddX",           AddX,           "AddX[nbAdd]/F");
    fTree->Branch("AddY",           AddY,           "AddY[nbAdd]/F");
    fTree->Branch("AddZ",           AddZ,           "AddZ[nbAdd]/F");
    fTree->Branch("AddTS",          AddTS,          "AddTS[nbAdd]/l");

    fTree->Branch("TSHit",          &TSHit,         "TSHit/l");

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    const auto NCrystals = 45;
    auto it=0;
    array<TString,NCrystals> temparray;
    temparray[it++]  =	"1	2	13	12  3";
    temparray[it++]  = "2	0	3	5	22	20";
    temparray[it++]  = "0	1	20	17	16	13";
    temparray[it++]  = "0	6	4	5	1 ";
    temparray[it++]  = "3	5	26	28	8	6 ";
    temparray[it++]  = "1	3	4	26	23	22";
    temparray[it++]  = "3	4	8	7	9";
    temparray[it++]  = "6	9	11	34	32	8 ";
    temparray[it++]  = "4	6	7	32	29	28";
    temparray[it++]  = "12	10	11	7	6";
    temparray[it++] = "9	12	14	40	38	11";
    temparray[it++] = "7	9	10	38	35	34";
    temparray[it++] = "9	10	14	13	0";
    temparray[it++] = "0	2	16	44	14	12";
    temparray[it++] = "10	12	13	44	41	40";
    temparray[it++] = "17	16	42";
    temparray[it++] = "2	17	15	42	44	13";
    temparray[it++] = "15	16	2	20	19";
    temparray[it++] = "19	20	22	21";
    temparray[it++] = "17	20	18";
    temparray[it++] = "1	22	18	19	17	2 ";
    temparray[it++] = "18	22	23";
    temparray[it++] = "1	5	23	21	18	20";
    temparray[it++] = "21	22	5	26	25";
    temparray[it++] = "25	26	28	27";
    temparray[it++] = "23	26	24";
    temparray[it++] = "5	4	28	24	25	23";
    temparray[it++] = "24	28	29";
    temparray[it++] = "24	26	4	8	29	27";
    temparray[it++] = "27	28	8	32	31";
    temparray[it++] = "31	32	34	33";
    temparray[it++] = "29	32	30";
    temparray[it++] = "31	29	8	7	34	30";
    temparray[it++] = "30	34	35";
    temparray[it++] = "30	32	7	11	35	33";
    temparray[it++] = "33	34	11	38	37";
    temparray[it++] = "37	38	40	39";
    temparray[it++] = "35	38	36";
    temparray[it++] = "11	10	40	36	37	35";
    temparray[it++] = "36	40	41";
    temparray[it++] = "10	14	41	39	36	38";
    temparray[it++] = "39	40	14	44	43";
    temparray[it++] = "43	44	16	15";
    temparray[it++] = "41	44	42";
    temparray[it++] = "13	16	42	43	41	14";

    for(int i=0 ; i<NCrystals ; i++) {
506
        TObjArray *arr = temparray[i].ReplaceAll("\t"," ").Tokenize(" ");
507
        for(int j=0 ; j<arr->GetEntries() ; j++) {
508 509 510 511 512 513 514 515
            Int_t det = ((TString)arr->At(j)->GetName()).Atoi();
            TString name = Form("%.2d:%.2d",i,det);
            fAreNeighbours[name] = true;
        }
    }
}


516
void TB_AGATA_Builder_PSA::Process(Int_t idet)
517 518 519
{
    Log.ClearMessage();
    Log.GetProcessName() = "TB_AGATA_Builder";
520
    Log.SetProcessMethod(Form("Process(%d)",idet));
521

522
    if(!fMergerMode)
523
        fCompFrame = dynamic_cast<AgataCompositeFrame*>(fTrigger->GetInputSharedFP()->GetFrame());
524
    else
525
        fCompFrame = dynamic_cast<AgataCompositeFrame*>(fTrigger->GetInputSharedFP(idet+1)->GetFrame());
526

527 528
    fCompFrame->Scan();

529
    TSHit = dynamic_cast<AgataKey*>(fCompFrame->GetKey())->GetTimeStamp();
530 531 532 533 534

    Double_t xLocal, yLocal, zLocal, xGlobal, yGlobal, zGlobal;

    nbCores = fCompFrame->GetNbSubFrame();

535 536 537 538 539 540
    for(auto i=0 ;i<nbCores ; i++) {
        MaxESeg.at(i) = 0;
        MaxCoreX.at(i) = 0;
        MaxCoreY.at(i) = 0;
        MaxCoreZ.at(i) = 0;
    }
541

542 543 544
    for(Int_t i=0 ; i < nbCores ; i++) {
        fCompFrame->LinkSubFrame(i,fSFrame->GetFrame());
        fSFrame->GetFrame()->Read();
545

546
        auto *data = GetDataPointer<PSAInterface>(fSFrame);
547 548 549

        nbHitsperCry[i] = data->GetNbHits();

550
        if ( nbHitsperCry[i] > MaxHits ) {
551 552 553 554 555 556 557 558
            Log << error << " Number of Hits to hit in : " << nbHitsperCry[i] << "Max nbHits = " << MaxHits << nline;

            InitValues();
            return;
        }

        // Core like branches
        coreId[i]        = data->GetUID();
559 560 561
        coreE0[i]        = data->GetE(0);
        coreE1[i]        = data->GetE(1);
        coreT0[i]        = data->GetT(0);
562
        coreTS[i]        =  dynamic_cast<AgataKey*>(fSFrame->GetFrame()->GetKey())->GetTimeStamp();
563 564

        // Loop on the hit to get over the hits with the max of the energy
565
        for (Int_t j = 0; j < nbHitsperCry[i]; ++j) {
566
            const PSAHit* pHit = dynamic_cast<PSAHit*>(data->GetHit(j));
567 568 569 570 571 572 573 574 575 576 577 578 579

            hitE[nbHits+j]   = pHit->GetE();
            hitX[nbHits+j]   = pHit->GetX();
            hitY[nbHits+j]   = pHit->GetY();
            hitZ[nbHits+j]   = pHit->GetZ();
            hitId[nbHits+j]  = data->GetUID() ;
            hitSg[nbHits+j]  = pHit->GetID();

            //Get hit position
            pHit->GetXYZ(xLocal, yLocal, zLocal);
            //To the AGATA global ref
            Local2Global(data->GetUID(), xLocal, yLocal, zLocal, xGlobal, yGlobal, zGlobal);

580 581 582 583 584
            if(pHit->GetE()>MaxESeg.at(i)) {
                MaxESeg.at(i)  = pHit->GetE();
                MaxCoreX.at(i) = xGlobal;
                MaxCoreY.at(i) = yGlobal;
                MaxCoreZ.at(i) = zGlobal;
585 586
            }

587
            // Set the Position of the hit in global ref
588 589 590 591 592 593 594 595 596 597
            hitGX[nbHits+j]= xGlobal; // inversion X-Y and opposite direction
            hitGY[nbHits+j]= yGlobal; // inversion Y-X and opposite direction
            hitGZ[nbHits+j]= zGlobal; // z
        }

        nbHits += nbHitsperCry[i];
    }

    //Addback calcluation
    std::vector< core_property > corelist;
598
    for(int i=0 ; i<nbCores ; i++) {
599
        core_property acore{};
600 601
        acore.E = coreE0[i];
        acore.Id = coreId[i];
602 603 604 605
        acore.HighHitE = MaxESeg.at(i) ;
        acore.HighHitX = MaxCoreX.at(i);
        acore.HighHitY = MaxCoreY.at(i);
        acore.HighHitZ = MaxCoreZ.at(i);
606 607 608 609 610 611 612 613
        acore.HighHitTS = coreTS[i];
        acore.HighHitId = coreId[i];

        corelist.push_back(acore);
    }

    std::sort(corelist.begin(), corelist.end(), coreCompare);

614 615 616
    for(int i=0 ; i<nbCores ; i++) {
        for(int j=i+1 ; j<nbCores ; j++) {
            if(fAreNeighbours[Form("%.2d:%.2d",corelist[i].Id,corelist[j].Id)]) {
617 618
                corelist[i].E += corelist[j].E;

619
                if(corelist[j].HighHitE > corelist[i].HighHitE) {
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
                    corelist[i].HighHitE = corelist[j].HighHitE;
                    corelist[i].HighHitX = corelist[j].HighHitX;
                    corelist[i].HighHitY = corelist[j].HighHitY;
                    corelist[i].HighHitZ = corelist[j].HighHitZ;
                    corelist[i].HighHitTS = corelist[j].HighHitTS;
                    corelist[i].HighHitId = corelist[j].HighHitId;
                }

                corelist[j].E = 0.;
            }
        }

        std::sort(corelist.begin(), corelist.end(), coreCompare);
    }

635 636
    for(int i=0 ; i<nbCores ; i++) {
        if(corelist[i].E>0) {
637 638 639 640 641 642 643 644 645 646 647 648 649 650
            AddE[nbAdd] = corelist[i].E;
            AddX[nbAdd] = corelist[i].HighHitX;
            AddY[nbAdd] = corelist[i].HighHitY;
            AddZ[nbAdd] = corelist[i].HighHitZ;
            AddTS[nbAdd] = corelist[i].HighHitTS;
            AddId[nbAdd] = corelist[i].HighHitId;

            nbAdd++;
        }
    }
}

/// ****** Tracking ******///

651
TB_AGATA_Tracking::TB_AGATA_Tracking(TString name, TString ADFKey) : TB_Detector(move(name),move(ADFKey))
652 653 654 655
{
    gActualClass = "TB_AGATA_Tracking";
}

656
TB_AGATA_Tracking::~TB_AGATA_Tracking() = default;
657 658 659

void TB_AGATA_Tracking::Init()
{
660
    fSFrame = fTrigger->AddUtility("data:tracked",ConfAgent::theGlobalAgent());
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
}

void TB_AGATA_Tracking::InitValues()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitValues()");

    nbTrack = 0;
    TSTrack = 0;
}


void TB_AGATA_Tracking::InitTree()
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
    Log.SetProcessMethod("InitTree()");

    if(fTree == nullptr)
        return;

    fTree->Branch("nbTrack",        &nbTrack,       "nbTrack/I");
    fTree->Branch("trackE",         trackE,         "trackE[nbTrack]/F");
    fTree->Branch("trackX1",        trackX1,        "trackX1[nbTrack]/F");
    fTree->Branch("trackY1",        trackY1,        "trackY1[nbTrack]/F");
    fTree->Branch("trackZ1",        trackZ1,        "trackZ1[nbTrack]/F");
688 689 690 691 692
    fTree->Branch("trackX2",        trackX2,        "trackX2[nbTrack]/F");
    fTree->Branch("trackY2",        trackY2,        "trackY2[nbTrack]/F");
    fTree->Branch("trackZ2",        trackZ2,        "trackZ2[nbTrack]/F");
    fTree->Branch("trackType",      trackType,      "trackType[nbTrack]/I");
    fTree->Branch("trackFOM",       trackFOM,       "trackFOM[nbTrack]/F");
693 694 695 696 697 698 699 700 701 702
    fTree->Branch("trackT",         trackT,         "trackT[nbTrack]/F");
    fTree->Branch("trackCrystalID", trackCrystalID, "trackCrystalID[nbTrack]/I");
    fTree->Branch("TStrack",        &TSTrack,       "TStrack/l");
}


void TB_AGATA_Tracking::Process(Int_t idet)
{
    Log.ClearMessage();
    Log.GetProcessName() = gActualClass;
703
    Log.SetProcessMethod(Form("Process(%d)",idet));
704

705 706
    if(!fMergerMode)
        fSFrame = fTrigger->GetInputSharedFP();
707
    else
708
        fSFrame = fTrigger->GetInputSharedFP(idet+1);
709

710
    fSFrame->GetFrame()->Read();
711 712

    // to get the data part of the frame
713
    const auto *data = GetCstDataPointer<GammaTrackedInterface>(fSFrame);
714

715 716 717
    if(data == nullptr)
        return;

718 719
    nbTrack = data->GetNbGamma();

720
    if(nbTrack>MaxTrackedGamma) {
721 722 723 724 725 726
        Log << error << " Number of tracked gamma : " << nbTrack << "Max nbTrack = " << MaxTrackedGamma << " event ignored !" << nline;
        InitValues();
        return;
    }

    //! get time stamp
727
    TSTrack = dynamic_cast<AgataKey*>(fSFrame->GetFrame()->GetKey())->GetTimeStamp();
728

729
    for (auto i = 0; i < nbTrack; i++) {
730 731 732 733 734
        const TrackedHit *gamma1 = data->GetGamma(i);
        const Hit *hit1 = gamma1->GetHit();

        trackE[i]           = gamma1->GetE();

735 736 737
        trackType[i]        = gamma1->GetType();
        trackFOM[i]         = gamma1->GetFOM();

738 739 740 741 742 743 744
        trackT[i]           = hit1->GetT();
        trackCrystalID[i]   = hit1->GetID(1);

        trackX1[i]          = gamma1->GetX();
        trackY1[i]          = gamma1->GetY();
        trackZ1[i]          = gamma1->GetZ();

745
        //if tracked gamma is photo or pair, the second int point is a copy of the first one (because not useful)
746 747 748 749
        if ( gamma1->GetHit(1) ) {
            trackX2[i] = gamma1->GetHit(1)->GetX();
            trackY2[i] = gamma1->GetHit(1)->GetY();
            trackZ2[i] = gamma1->GetHit(1)->GetZ();
750
        }
751
        else {
752 753 754 755 756 757
            trackX2[i]      = trackX1[i];
            trackY2[i]      = trackY1[i];
            trackZ2[i]      = trackZ1[i];
        }


758 759 760 761
        if(trackE[i]==0)
            nbTrack--;
    }

762
    if(nbTrack==0) {
763 764 765 766
        InitValues();
        return;
    }
}