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 157 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

void TB_AGATA_PSA::Init()
{
}

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();

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

    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);
229
    coreTS        = dynamic_cast<AgataKey *>(fSFrame->GetFrame()->GetKey())->GetTimeStamp();
230 231 232

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

        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
    }
}

254 255
/// ****** Builder CRYSTAL ******///

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

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

void TB_AGATA_Builder_CRYSTAL::Init()
264
{
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
    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");
327 328
}

329 330

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

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

    fCompFrame->Scan();

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

    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();

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

        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;
        }

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

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

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

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

    ReadTransformation();
}

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

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


416
void TB_AGATA_Builder_PSA::InitTree()
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
{
    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");
441 442
    fTree->Branch("coreE1",         coreE1,         "coreE1[nbCores]/F");
    fTree->Branch("coreT0",         coreT0,         "coreT0[nbCores]/F");
443 444 445 446 447 448 449 450 451 452 453 454
    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");

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


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

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

526 527
    fCompFrame->Scan();

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

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

    nbCores = fCompFrame->GetNbSubFrame();

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

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

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

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

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

            InitValues();
            return;
        }

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

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

            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);

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

586
            // Set the Position of the hit in global ref
587 588 589 590 591 592 593 594 595 596
            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;
597
    for(int i=0 ; i<nbCores ; i++) {
598
        core_property acore{};
599 600
        acore.E = coreE0[i];
        acore.Id = coreId[i];
601 602 603 604
        acore.HighHitE = MaxESeg.at(i) ;
        acore.HighHitX = MaxCoreX.at(i);
        acore.HighHitY = MaxCoreY.at(i);
        acore.HighHitZ = MaxCoreZ.at(i);
605 606 607 608 609 610 611 612
        acore.HighHitTS = coreTS[i];
        acore.HighHitId = coreId[i];

        corelist.push_back(acore);
    }

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

613 614 615
    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)]) {
616 617
                corelist[i].E += corelist[j].E;

618
                if(corelist[j].HighHitE > corelist[i].HighHitE) {
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
                    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);
    }

634 635
    for(int i=0 ; i<nbCores ; i++) {
        if(corelist[i].E>0) {
636 637 638 639 640 641 642 643 644 645 646 647 648 649
            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 ******///

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

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

void TB_AGATA_Tracking::Init()
{
659
    fSFrame = fTrigger->AddUtility("data:tracked",ConfAgent::theGlobalAgent());
660 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
}

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");
687 688 689 690 691
    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");
692 693 694 695 696 697 698 699 700 701
    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;
702
    Log.SetProcessMethod(Form("Process(%d)",idet));
703

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

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

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

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

717 718
    nbTrack = data->GetNbGamma();

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

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

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

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

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

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

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

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


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

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