GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StogsAFP.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #include <iostream>
22 #include <iomanip>
23 #include <fstream>
24 #include <sstream>
25 #include <cstdio>
26 
27 #include <list>
28 #include <utility>
29 #include <vector>
30 
31 #include "TString.h"
32 #include "TObjArray.h"
33 #include "TObject.h"
34 #include "TSystem.h"
35 #include "TList.h"
36 #include "CompositeFrame.h"
37 #include "TFile.h"
38 #include "TChain.h"
39 #include "TEntryList.h"
40 #include "TKey.h"
41 #include "TChainElement.h"
42 #include "TError.h"
43 #include "TVector3.h"
44 #include "TRandom3.h"
45 #include "TROOT.h"
46 
47 #include "PSAFrame.h"
48 #include "BashColor.h"
49 #include "AgataFrameFactory.h"
50 #include "AgataKeyFactory.h"
51 #include "BufferIO.h"
52 #include "SToGS_BaseROOTEvents.h"
53 
54 #include "StogsAFP.h"
55 
59 
62  fSourceOfFrames(aMByte,ConfAgent::kRead),
63  fEndOfFrame("EndOfFrame"),
64  fFrameEventPsa_Out(NULL),
65  fSPSAFrame(NULL),
66  fTrigger("event:data:psa"),
67  fdebug(false),
68  fBashColor(new BashColor()),
69  fNEvts(0),
70  fRejectedEvents(0),
71  fEventNumber(0),
72  fNEntries(0),
73  fDataChain(new TChain("TreeMaster","TreeMaster")),
74  fEntryList(0x0),
75  fCurrentEntry(0),
76  IsADHitTreeModeOn(false),
77  fStogsEv(new SBREvent()),
78  fProcessPacking(true),
79  fSpatialResolution(5.), //default spatial resolution in mm
80  fApplyEnergyResolution(true),
81  fApplySmearing(true)
82 {
83  if(fdebug) std::cout<<"\E[31;1m"<<"Construct in"<<"\E[m"<<std::endl;
84 
85  Log.GetProcessName() = "StogsAFP"; GetFrameIO().SetName("StogsAFP");
86 
87  // to be sure unknown frames are written in the ouput
88  GetFrameIO().SetModel(ConfAgent::kSafe);
89  // to protect writings in the source of frames
90  fSourceOfFrames.SetModeIO(ConfAgent::kRead);
91 
92  // the input datablock is owned by BasicAFP
93  GetFrameIO().Attach(&fSourceOfFrames,0x0);
94 
95  gRandom->SetSeed();
96 
97  if(fdebug) std::cout<<"\E[31;1m"<<"Construct out"<<"\E[m"<<std::endl;
98 }
99 
101 
103 {
104  // in principle not needed ... just in case reset it has not been called by narval
105  unsigned int error = 0u; process_reset(&error) ;
106 }
107 
109 
110 void StogsAFP::process_config (const char *directory_path, unsigned int *error_code)
111 {
112  // first init narval and ADF stuff (if required)
113  NarvalInterface::process_config(directory_path,error_code);
114 
115  if ( (*error_code) == 0u ) { // now init your stuff
116  }
117 }
118 
120 
122 {
123  if(fdebug) std::cout<<"\E[31;1m"<<"ProcessBlock in"<<"\E[m"<<std::endl;
124 
125  // attach the input/output buffer to the FrameIO system
126 
127  GetFrameIO().Attach(NULL, &out);
128 
129  // start the processing
130 
131  while(true)
132  {
133  bool EventOk;
134  if(IsADHitTreeModeOn) EventOk = ReadEventADHitTree();
135  else EventOk = ReadEventStogs();
136 
137  if(!EventOk)
138  {
140  {
141  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
142  out.SetEoB(true);
143  break;
144  }
145  else continue;
146  }
147 
149  unsigned int EventLenght = frame_out->GetLength();
150 
151  if(fdebug)
152  {
153  fBashColor->SetErrorOut();
154  cout<<"EventLenght = "<<EventLenght<<endl;
155  cout<<out.GetBufferIO()->FreeSize()<<endl;
156  fBashColor->ResetColor();
157  }
158 
159  if(out.Reserve(EventLenght,true))
160  {
161  fNEvts++;
162  fTrigger.Fired(true);
163  GetFrameIO().Record();
165  {
166  GetFrameIO().SetStatus(BaseFrameIO::kFinished);
167  out.SetEoB(true);
168  break;
169  }
170  else continue;
171  }
172  else
173  {
174  fCurrentEntry--;
175  fEventNumber--;
176  break;
177  }
178  }
179 
180  GetFrameIO().Detach(NULL, &out);
181 
182  if(fdebug) std::cout<<"\E[31;1m"<<"ProcessBlock out"<<"\E[m"<<std::endl;
183  if ( GetFrameIO().GetStatus() == BaseFrameIO::kFinished) return 1u;
184  else return 0u;
185 }
186 
188 
189 void StogsAFP::process_initialise (unsigned int *error_code)
190 {
191  if(fdebug) std::cout<<"\E[31;1m"<<"process_initialise in"<<"\E[m"<<std::endl;
192 
193  *error_code = 0u; Log.ClearMessage(); Log.SetProcessMethod("process_initialise");
194 
195  // read an input file to get the base filename and the path
196  std::string conffile = GetConfPath();
197  conffile += "StogsAFP.conf";
198 
199  ReadConfFile(conffile.data());
200 
201  if(fEntryList == 0x0) fNEntries = fDataChain->GetEntries();
202  else fNEntries = fEntryList->GetN();
203 
204  if(fNEntries)
205  {
206  fBashColor->SetInfoOut();
207  std::cout<<"TChain created: "<<fDataChain->GetName()<<std::endl;
208  std::cout<<"Number of entry to Treat: "<<fNEntries<<std::endl;
209  fBashColor->ResetColor();
210 
212  else InitDataChainStogs();
213  }
214  else
215  {
216  fBashColor->SetErrorOut();
217  std::cout<<"Empty TChain: "<<fDataChain->GetName()<<std::endl;
218  fBashColor->ResetColor();
219  gROOT->ProcessLine(".qqqqqqqqqqqqqqqq");
220  }
221 
222  // FactoryItem keyItem("Agata", "data:psa",ADF::Version(4,0));
223  // FactoryItem fraItem("Agata", "data:psa",ADF::Version(0,0));
224  // fSPSAFrame = MainFrameFactory::theMainFactory().NewSharedFrame(keyItem, fraItem);
225 
226  fFrameEventPsa_Out = fTrigger.SetOutputFrame("Agata", "event:data:psa");
227 
229 
231 
232  Log << dolog;
233 
234  if(fdebug) std::cout<<"\E[31;1m"<<"process_initialise out"<<"\E[m"<<std::endl;
235 }
236 
238 
239 void StogsAFP::process_reset (unsigned int *error_code)
240 {
241  *error_code = 0u;
242  Log.ClearMessage(); Log.SetProcessMethod("process_reset");
243 
244  GetFrameIO().Print( Log() );
245 
246  Log << dolog;
247 }
248 
250 
251 bool StogsAFP::ReadConfFile(TString ConfFilePath)
252 {
253  TList *ListOfRootFiles;
254  ListOfRootFiles = new TList();
255 
256  TString TreeName = "TreeMaster";
257 
258  fCurrentEntry = 0;
259 
260  std::ifstream filein(ConfFilePath);
261  if ( filein.is_open())
262  {
263  std::string Line;
264  TString Buffer;
265 
266  while ( filein.good() && !filein.eof() )
267  {
268  getline(filein,Line);
269  Buffer = Line;
270 
271  if(Buffer.BeginsWith("#")) continue;
272  if(Buffer.BeginsWith("b") || Buffer.BeginsWith("r")) //b /path/to PSAHits 0 //r /path/to PSAHits 0 99
273  {
274  TObjArray *TokenArray = Buffer.Tokenize(" ");
275 
276  if(Buffer.BeginsWith("b") && TokenArray->GetEntries() != 4) continue;
277  if(Buffer.BeginsWith("r") && TokenArray->GetEntries() != 5) continue;
278 
279  TString Path = TokenArray->At(1)->GetName();
280  if(!Path.EndsWith("/")) Path.Append("/");
281  TString BaseName = TokenArray->At(2)->GetName();
282  int FirstFileNbr = ((TString)TokenArray->At(3)->GetName()).Atoi();
283  int LastFileNbr = FirstFileNbr+1;
284  if(Buffer.BeginsWith("r")) LastFileNbr = ((TString)TokenArray->At(4)->GetName()).Atoi();
285 
286  TString FileName = Form("%s%s_%.4d.root",Path.Data(),BaseName.Data(),FirstFileNbr);
287 
288  int NFilesAdded = 0;
289  while(gSystem->IsFileInIncludePath(FileName) && FirstFileNbr<=LastFileNbr)
290  {
291  TFile *f = TFile::Open(FileName);
292  ListOfRootFiles->Add(f);
293  FileName = Form("%s%s_%.4d.root",Path.Data(),BaseName.Data(),++FirstFileNbr);
294 
295  if(Buffer.BeginsWith("b")) LastFileNbr++;
296  }
297  if(!NFilesAdded)
298  {
299  fBashColor->SetWarningOut();
300  cout<<"File "<<FileName<<" not found !"<<endl;
301  fBashColor->ResetColor();
302  }
303  }
304  if(Buffer.BeginsWith("l"))
305  {
306  TObjArray *TokenArray = Buffer.Tokenize(" ");
307 
308  if(TokenArray->GetEntries() != 2) continue;
309 
310  TString FileName = TokenArray->Last()->GetName();
311 
312  TFile *f = TFile::Open(FileName);
313  if(gSystem->IsFileInIncludePath(FileName)) ListOfRootFiles->Add(f);
314  else
315  {
316  fBashColor->SetWarningOut();
317  cout<<"File "<<FileName<<" not found !"<<endl;
318  fBashColor->ResetColor();
319  }
320  }
321  if(Buffer.BeginsWith("el"))
322  {
323  TObjArray *TokenArray = Buffer.Tokenize(" ");
324  int TokenSize = TokenArray->GetEntries();
325  if(TokenSize != 2 && TokenSize != 3) continue;
326 
327  TString EntryListName = "";
328  TEntryList *EntryList = 0x0;
329 
330  TString FileName = ((TString)TokenArray->At(1)->GetName());
331  TFile *f = TFile::Open(FileName);
332  if(!gSystem->IsFileInIncludePath(FileName))
333  {
334  fBashColor->SetWarningOut();
335  cout<<"File "<<FileName<<" not found !"<<endl;
336  fBashColor->ResetColor();
337  continue;
338  }
339 
340  if(TokenSize==3)
341  {
342  EntryListName = TokenArray->Last()->GetName();
343  EntryList = (TEntryList*)f->Get(EntryListName)->Clone();
344  if(EntryList==0x0)
345  {
346  fBashColor->SetWarningOut();
347  cout<<"EntryList "<<EntryListName<<" not found in file "<<FileName<<"!"<<endl;
348  fBashColor->ResetColor();
349  continue;
350  }
351  }
352  else
353  {
354  TList *ListOfKeys = f->GetListOfKeys();
355 
356  for(int i=0 ; i<ListOfKeys->GetSize() ; i++)
357  {
358  TKey *key = (TKey*)ListOfKeys->At(i);
359 
360  if((TString)key->GetClassName()=="TEntryList")
361  {
362  EntryList = (TEntryList*)f->Get(key->GetName())->Clone();
363  }
364  }
365  if(EntryList==0x0)
366  {
367  fBashColor->SetWarningOut();
368  cout<<"No EntryList found in file "<<FileName<<"!"<<endl;
369  fBashColor->ResetColor();
370  continue;
371  }
372  }
373 
374  if(fEntryList == 0x0) fEntryList = EntryList;
375  else fEntryList->Add(EntryList);
376 
377  EntryList->SetDirectory(0);
378  f->Close();
379 
380  fBashColor->SetInfoOut();
381  cout<<"EntryList "<<EntryList->GetName()<<" loaded for file "<<EntryList->GetFileName()<<endl;
382  fBashColor->ResetColor();
383  }
384  if(Buffer.BeginsWith("n"))
385  {
386  TObjArray *TokenArray = Buffer.Tokenize(" ");
387  if(TokenArray->GetEntries() != 2)
388  {
389  fBashColor->SetWarningOut();
390  std::cout<<"The TTree name has not been undestood..."<<std::endl;
391  fBashColor->ResetColor();
392  continue;
393  }
394  TreeName = TokenArray->Last()->GetName();
395  fDataChain->SetName(TreeName);
396  }
397  }
398  }
399  else
400  {
401  fBashColor->SetWarningOut();
402  std::cout<<ConfFilePath<<" not found !"<<std::endl;
403  gROOT->ProcessLine(".qqqqqqqqqqqqqqqqq");
404  fBashColor->ResetColor();
405  }
406  filein.close();
407 
408  for(int i=0 ; i<ListOfRootFiles->GetSize() ; i++)
409  {
410  TFile *f = (TFile*)ListOfRootFiles->At(i);
411 
412  if(f!= 0x0) fDataChain->Add(f->GetName());
413  }
414 
415  gErrorIgnoreLevel = kFatal;
416 
417  if(fEntryList != 0x0)
418  {
419  TObjArray *fFiles = fDataChain->GetListOfFiles();
420  Int_t ne = fFiles->GetEntries();
421  Int_t listfound=0;
422  TString treename, filename;
423 
424  TEntryList *templist = 0;
425  for (Int_t ie = 0; ie<ne; ie++){
426  treename = gSystem->BaseName( ((TChainElement*)fFiles->UncheckedAt(ie))->GetName() );
427  filename = ((TChainElement*)fFiles->UncheckedAt(ie))->GetTitle();
428  templist = fEntryList->GetEntryList(treename.Data(), filename.Data());
429  if(!templist)
430  {
431  TString BaseFilename = gSystem->BaseName(filename);
432  templist = fEntryList->GetEntryList(treename.Data(), BaseFilename.Data());
433  if(templist) templist->SetFileName(filename);
434  }
435  if (templist) listfound++;
436  }
437 
438  if(!listfound)
439  {
440  fBashColor->SetWarningOut();
441  cout<<"The selected EntryLists are not compatible with input TTrees, EntryList ignored."<<endl;
442  fBashColor->ResetColor();
443 
444  delete fEntryList;
445  fEntryList = 0x0;
446  }
447  else fDataChain->SetEntryList(fEntryList,"ne");
448 
449 
450  }
451 
452  gErrorIgnoreLevel = kPrint;
453 
454  return true;
455 }
456 
458 
460 {
461  fDataChain->SetBranchAddress("nbHits", &NbHits);
462  fDataChain->SetBranchAddress("hitE", HitE);
463  fDataChain->SetBranchAddress("hitX", HitX);
464  fDataChain->SetBranchAddress("hitY", HitY);
465  fDataChain->SetBranchAddress("hitZ", HitZ);
466 
467  fDataChain->SetBranchAddress("hitId", HitId);
468  fDataChain->SetBranchAddress("hitSg", HitSg);
469 
470 
471  fDataChain->SetBranchAddress("nbCores", &NbCores);
472  fDataChain->SetBranchAddress("coreE0", CoreE0);
473  fDataChain->SetBranchAddress("coreE1", CoreE1);
474  fDataChain->SetBranchAddress("coreT0", CoreT0);
475  fDataChain->SetBranchAddress("coreT1", CoreT1);
476  fDataChain->SetBranchAddress("hit_per_cryst", NbHitsPerCrystal);
477 
478  fDataChain->SetBranchAddress("TSHit", &fTimestamp);
479 
480  NbHits=0;
481  NbCores=0;
482  for(int i=0 ; i<MaxHits ; i++)
483  {
484  HitE[i] = 0;
485  HitX[i] = 0;
486  HitY[i] = 0;
487  HitZ[i] = 0;
488  HitId[i] = 0;
489  HitSg[i] = 0;
490 
491  if(i<MaxCore)
492  {
493  CoreE0[i]=0;
494  CoreE1[i]=0;
495  CoreT0[i]=0;
496  CoreT1[i]=0;
497  NbHitsPerCrystal[i]=0;
498  }
499 
500  }
501 }
502 
504 
506 {
507  fDataChain->SetBranchAddress("Ev.", &fStogsEv);
508 
509  fEventNumber = 0;
510 
511  fStogsEv->Clear("");
512 }
513 
515 
517 {
518 
519  int EntrySize = fDataChain->GetEntry(fCurrentEntry++);
520 
521  if(EntrySize==0)
522  {
523  fBashColor->SetWarningOut();
524  cout<<"Warning ! Entry "<<fCurrentEntry<<" Is empty, is it normal ?"<<endl;
525  fBashColor->ResetColor();
526  return false;
527  }
528 
530  frame_out->Reset();
531 
532  ((AgataKey *)frame_out->GetKey())->SetEventNumber(fCurrentEntry);
533  ((AgataKey *)frame_out->GetKey())->SetTimeStamp(fTimestamp);
534 
535  if(fdebug)
536  {
537  TString GlobMessType = AgataKeyFactory::theFactory()->GetMessage(((AgataKey*)frame_out->GetKey())->GetMessage());
538 
539  fBashColor->SetInfoOut();
540  cout<<"New Main frame: "<<GlobMessType<<endl;
541  cout<<" --timestamp = "<<((AgataKey*)frame_out->GetKey())->GetTimeStamp()<<endl;
542  cout<<" --EventNumber = "<<((AgataKey*)frame_out->GetKey())->GetEventNumber()<<endl;
543  cout<<" --Number of total hits: "<<NbHits<<" in "<<NbCores<<" core"<<endl;
544  fBashColor->ResetColor();
545  }
546 
547  int hit = 0;
548  for(int icore=0 ; icore< NbCores ; icore++)
549  {
550  Frame *PSAFrame = fSPSAFrame->GetFrame();
551  PSAFrame->Reset();
552 
553  ((AgataKey *)PSAFrame->GetKey())->SetEventNumber(fCurrentEntry);
554  ((AgataKey *)PSAFrame->GetKey())->SetTimeStamp(fTimestamp);
555 
556  PSAInterface *data_out = GetDataPointer<PSAInterface>(PSAFrame);
557  data_out->SetE(CoreE0[icore],0);
558  data_out->SetE(CoreE1[icore],1);
559  data_out->SetT(CoreT0[icore],0);
560  data_out->SetT(CoreT1[icore],1);
561  data_out->SetUID(HitId[hit]);
562 
563  if(fdebug)
564  {
565  TString MessageType = AgataKeyFactory::theFactory()->GetMessage(((AgataKey*)PSAFrame->GetKey())->GetMessage());
566  fBashColor->SetInfoOut();
567  cout<<" New subframe: "<<MessageType<<endl;
568  cout<<" --CoreID: "<<HitId[hit]<<" ==> E0 = "<<CoreE0[icore]<<", T0 = "<<CoreT0[icore]<<endl;
569  cout<<" --Number of crystal hits: "<<NbHitsPerCrystal[icore]<<endl;
570  fBashColor->ResetColor();
571  }
572 
573  ADF::PSAHit *pOut = NULL;
574  for(int iseg=0 ; iseg<NbHitsPerCrystal[icore] ; iseg++)
575  {
576  pOut = (PSAHit*)data_out->NewHit();
577  pOut->Reset();
578  pOut->SetXYZ(HitX[hit],HitY[hit],HitZ[hit]);
579  pOut->SetE(HitE[hit]);
580  pOut->SetID(HitSg[hit],0);
581  pOut->SetID(HitId[hit],1);
582 
583  if(fdebug)
584  {
585  fBashColor->SetInfoOut();
586  cout<<" hit nb: "<<iseg<<" : E = "<<HitE[hit]<<", SegID = "<<HitId[hit]<<", SegNbr = "<<HitSg[hit]<<", (X,Y,Z): ("<<HitX[hit]<<","<<HitY[hit]<<","<<HitZ[hit]<<")"<<endl;
587  fBashColor->ResetColor();
588  }
589  hit++;
590  }
591 
592  PSAFrame->Write();
593 
594  frame_out->AddSubFrame((*PSAFrame));
595  }
596 
597  if(fdebug)
598  {
599  fBashColor->SetInfoOut();
600  cout<<" ==> Number of subframes in the global frame: "<<frame_out->GetNbSubFrame()<<endl;
601  cout<<" ==> Event Lenght = "<<frame_out->GetLength()<<endl;
602  cout<<"_____________________________________________"<<endl<<endl;
603  fBashColor->ResetColor();
604  }
605 
606  return true;
607 }
608 
610 
612 {
613  fStogsEv->Clear("");
614 
615  int EntrySize;
616 
617  if(fEntryList == 0x0) EntrySize = fDataChain->GetEntry(fCurrentEntry++);
618  else if(fDataChain->InheritsFrom("TChain")) EntrySize = fDataChain->GetEntry(fEntryList->GetEntry(fCurrentEntry++));
619  else EntrySize = fDataChain->GetEntry(fDataChain->GetEntryNumber(fCurrentEntry++));
620 
621 
622  if(EntrySize==0)
623  {
624  fBashColor->SetWarningOut();
625  cout<<"Warning ! Entry "<<fCurrentEntry -1 <<" Is empty, is it normal ?"<<endl;
626  fBashColor->ResetColor();
627  TString test;
628  cin>>test;
629  if(!test.EqualTo("y",TString::kIgnoreCase)) return false;
630  }
631 
632  SBREvent *FilteredEvent = FilterEvent(fStogsEv);
633  SBREvent *PackingEvt = FilteredEvent;
634 
635  if(fProcessPacking) PackingEvt = ProcessPacking(FilteredEvent);
637  if(fApplySmearing) ApplySmearing(PackingEvt);
638 
640  frame_out->Reset();
641 
642  fTimestamp = fCurrentEntry*1e8/500.; //pour avoir un rate de 500 coups/s
643  ((AgataKey *)frame_out->GetKey())->SetEventNumber(fEventNumber);
644  ((AgataKey *)frame_out->GetKey())->SetTimeStamp(fTimestamp);
645 
646  int NbHits = PackingEvt->GetNbHits();
647 
648  int nbCores=0;
649 
650  if(fdebug)
651  {
652  fBashColor->SetInfoOut();
653  cout<<"*** New Event : NbHits = "<<NbHits<<endl;
654  fBashColor->ResetColor();
655  }
656 
657  int hit=0;
658 
659  while(hit<NbHits)
660  {
661  Frame *PSAFrame = fSPSAFrame->GetFrame();
662  PSAFrame->Reset();
663 
664  ((AgataKey *)PSAFrame->GetKey())->SetEventNumber(fEventNumber);
665  ((AgataKey *)PSAFrame->GetKey())->SetTimeStamp(fTimestamp);
666 
667  PSAInterface *data_out = GetDataPointer<PSAInterface>(PSAFrame);
668 
669  SBRHit *a_hit = PackingEvt->GetHit(hit);
670 
671  int CoreId = a_hit->fUID;
672  if(CoreId>180)
673  {
674  hit++;
675  continue;
676  }
677  double CoreE = 0.;
678 
679  int nHitsPerCrystal = 0;
680 
681  ADF::PSAHit *pOut = NULL;
682  while(( hit<NbHits ) && ( a_hit->fUID == CoreId ))
683  {
684  if(nHitsPerCrystal ==100) break;
685  pOut = (PSAHit*)data_out->NewHit();
686  pOut->Reset();
687  pOut->SetXYZ(a_hit->fX,a_hit->fY,a_hit->fZ); // Warning !!! for the moment, the space variables are given in the global frame, not in the crystal frame
688  double ahitE = a_hit->fE;
689  pOut->SetE(ahitE);
690  CoreE += ahitE;
691  pOut->SetID(0,0); // Warning, without access to the lookuptable, the seg Id is cannot be deduced from the global coordinates
692  pOut->SetID(CoreId,1);
693 
694  if(fdebug || ahitE<=0)
695  {
696  fBashColor->SetInfoOut();
697  if(ahitE<=0) fBashColor->SetWarningOut();
698  cout<<" hit nb: "<<nHitsPerCrystal<<" : E = "<<a_hit->fE<<", SegID = "<<a_hit->fUID<<", (X,Y,Z): ("<<a_hit->fX<<","<<a_hit->fY<<","<<a_hit->fZ<<")"<<endl;
699  fBashColor->ResetColor();
700  }
701 
702  hit++;
703  nHitsPerCrystal++;
704 
705  a_hit = PackingEvt->GetHit(hit);
706  }
707 
708  data_out->SetE(CoreE,0);
709  data_out->SetE(0,1);
710  data_out->SetT(0,0);
711  data_out->SetT(0,1);
712  data_out->SetUID(CoreId);
713 
714  nbCores++;
715 
716  int FrameSize = PSAFrame->Write();
717  if(FrameSize == 0)
718  {
719  fRejectedEvents++;
720  return false;
721  }
722 
723  frame_out->AddSubFrame((*PSAFrame));
724 
725  if(fdebug)
726  {
727  TString MessageType = AgataKeyFactory::theFactory()->GetMessage(((AgataKey*)PSAFrame->GetKey())->GetMessage());
728  fBashColor->SetInfoOut();
729  cout<<" New subframe: "<<MessageType<<endl;
730  cout<<" --CoreID: "<<CoreId<<" ==> E0 = "<<CoreE<<endl;
731  cout<<" --Number of crystal hits: "<<nHitsPerCrystal<<endl;
732  fBashColor->ResetColor();
733  }
734  }
735 
736  if(fProcessPacking) delete PackingEvt;
737  delete FilteredEvent;
738 
739  if(fdebug)
740  {
741  TString GlobMessType = AgataKeyFactory::theFactory()->GetMessage(((AgataKey*)frame_out->GetKey())->GetMessage());
742 
743  fBashColor->SetInfoOut();
744  cout<<"New Main frame: "<<GlobMessType<<endl;
745  cout<<" --timestamp = "<<((AgataKey*)frame_out->GetKey())->GetTimeStamp()<<endl;
746  cout<<" --EventNumber = "<<((AgataKey*)frame_out->GetKey())->GetEventNumber()<<endl;
747  cout<<" --Number of total hits: "<<NbHits<<" in "<<nbCores<<" core"<<endl;
748 
749  cout<<" ==> Number of subframes in the global frame: "<<frame_out->GetNbSubFrame()<<endl;
750  cout<<" ==> Event Lenght = "<<frame_out->GetLength()<<endl;
751  cout<<"_____________________________________________"<<endl<<endl;
752  fBashColor->ResetColor();
753  }
754 
755  if(NbHits==0) return false;
756 
757  fEventNumber++;
758 
759  return true;
760 }
761 
763 {
764  fBashColor->SetInfoOut();
765 
766  std::cout<<"******** StogsAFP producer stats: ********"<<std::endl<<std::endl;
767 
768  std::cout<<"Number of entries in the input tree : "<<fNEntries<<std::endl;
769  std::cout<<"Number of good events : "<<fNEvts<<std::endl;
770  std::cout<<"Number of empty events : "<<fNEntries-fNEvts-fRejectedEvents<<std::endl;
771  std::cout<<"Number of rejected events : "<<fRejectedEvents<<std::endl;
772  std::cout<<std::endl;
773  std::cout<<"********************************************"<<std::endl;
774 
775  fBashColor->ResetColor();
776 }
777 
779 {
780  TList *ListOfHits;
781  ListOfHits = new TList();
782  TList *ListOfPackedHits;
783  ListOfPackedHits = new TList();
784 
785  int NbHits = StogsEvt->GetNbHits();
786 
787  for(int i=0 ; i<NbHits ; i++)
788  {
789  SBRHit *a_hit = StogsEvt->GetHit(i);
790 
791  //*10 to set distances in mm
792  a_hit->fX = a_hit->fX*10.;
793  a_hit->fY = a_hit->fY*10.;
794  a_hit->fZ = a_hit->fZ*10.;
795 
796  //*1e3 to set energies in keV
797  a_hit->fE = a_hit->fE*1e3;
798 
799  fBashColor->SetWarningOut();
800  if(a_hit->fE > 1e5) cout<<"Warning !!! E = "<<a_hit->fE<<" keV, initial energies should be already in keV ?!"<<endl;
801  if(TMath::Abs(a_hit->fZ) > 1000.) cout<<"Warning !!! Z = "<<a_hit->fZ<<" mm, initial distances should be already in mm ?!"<<endl;
802  fBashColor->ResetColor();
803 
804  ListOfHits->Add(a_hit);
805  }
806 
807  while(ListOfHits->GetSize())
808  {
809  SBRHit *PackedHit = 0x0;
810 
812  for(int i=0 ; i<ListOfHits->GetSize() ; i++)
813  {
814  SBRHit *a_hit = (SBRHit*)ListOfHits->At(i);
815  if(PackedHit == 0x0) PackedHit = a_hit;
816  else if(a_hit->fE > PackedHit->fE) PackedHit = a_hit;
817  }
818 
820  ListOfHits->Remove(PackedHit);
821  ListOfPackedHits->Add(PackedHit);
822 
823  TVector3 PackedHitPosPond(PackedHit->fX*PackedHit->fE,PackedHit->fY*PackedHit->fE,PackedHit->fZ*PackedHit->fE);
824  double PondFactor = PackedHit->fE;
825 
826  TVector3 PackedHitPos(PackedHit->fX,PackedHit->fY,PackedHit->fZ);
827 
829  for(int i=0 ; i<ListOfHits->GetSize() ; i++)
830  {
831  SBRHit *a_hit = (SBRHit*)ListOfHits->At(i);
832  TVector3 HitPos(a_hit->fX,a_hit->fY,a_hit->fZ);
833 
834  TVector3 Diff = HitPos-PackedHitPos;
835 
836  double distance = Diff.Mag();
837 
838  if(distance<fSpatialResolution)
839  {
840  TVector3 HitPosPond(a_hit->fX*a_hit->fE,a_hit->fY*a_hit->fE,a_hit->fZ*a_hit->fE);
841  PackedHitPosPond += HitPosPond;
842  PackedHit->fE = PackedHit->fE + a_hit->fE;
843  PondFactor += a_hit->fE;
844 
845  ListOfHits->Remove(a_hit);
846  i--;
847  }
848  }
849 
850  PackedHitPosPond = PackedHitPosPond*(1./PondFactor);
851  PackedHit->fX = PackedHitPosPond.x();
852  PackedHit->fY = PackedHitPosPond.y();
853  PackedHit->fZ = PackedHitPosPond.z();
854  }
855 
856  SBREvent *PackedEvt;
857  PackedEvt = new SBREvent();
858 
859  for(int i=0 ; i<ListOfPackedHits->GetSize() ; i++)
860  {
861  SBRHit *a_hit = (SBRHit*)ListOfPackedHits->At(i);
862  PackedEvt->AddHit(a_hit);
863  }
864 
865  delete ListOfHits;
866  delete ListOfPackedHits;
867 
868  return PackedEvt;
869 }
870 
872 {
873  int NbHits = StogsEvt->GetNbHits();
874 
875  for(int i=0 ; i<NbHits ; i++)
876  {
877  SBRHit *a_hit = StogsEvt->GetHit(i);
878 
879  double Ener = a_hit->fE;
880 
881  double WFHM = (2.1-1.2)/(1333.-60.)*(Ener-1333.)+2.1;
882  double sigma = WFHM/sqrt(8.*log(2.));
883 
884  Ener = TMath::Abs(gRandom->Gaus(Ener,sigma));
885 
886  a_hit->fE = Ener;
887  }
888 }
889 
891 {
892  int NbHits = StogsEvt->GetNbHits();
893 
894  for(int i=0 ; i<NbHits ; i++)
895  {
896  SBRHit *a_hit = StogsEvt->GetHit(i);
897 
898  double x,y,z;
899  gRandom->Sphere(x,y,z,fSpatialResolution);
900 
901  a_hit->fX += x;
902  a_hit->fY += y;
903  a_hit->fZ += z;
904  }
905 }
906 
908 {
909  TList *ListOfHits;
910  ListOfHits = new TList();
911 
912  TList *ListOfOrderedHits;
913  ListOfOrderedHits = new TList();
914 
915  int NbHits = GlobEvt->GetNbHits();
916 
917  for(int i=0 ; i<NbHits ; i++)
918  {
919  SBRHit *a_hit = GlobEvt->GetHit(i);
920  if(a_hit->fUID < 180) ListOfHits->Add(a_hit);
921  }
922 
923 
924  while(ListOfHits->GetSize())
925  {
926  SBRHit *FirstHit = (SBRHit*)ListOfHits->First();
927  int firstUID = FirstHit->fUID;
928  ListOfOrderedHits->Add(FirstHit);
929  ListOfHits->Remove(FirstHit);
930 
931  for(int i=0 ; i<ListOfHits->GetSize() ; i++)
932  {
933  SBRHit *a_hit = (SBRHit*)ListOfHits->At(i);
934 
935  if(a_hit->fUID == firstUID)
936  {
937  ListOfOrderedHits->Add(a_hit);
938  ListOfHits->Remove(a_hit);
939  i--;
940  }
941  }
942  }
943 
944  SBREvent *FilterdEvent;
945  FilterdEvent = new SBREvent();
946 
947  for(int i=0 ; i<ListOfOrderedHits->GetSize() ; i++)
948  {
949  SBRHit *a_hit = (SBRHit*)ListOfOrderedHits->At(i);
950  FilterdEvent->AddHit(a_hit);
951  }
952 
953  delete ListOfHits;
954  delete ListOfOrderedHits;
955 
956  return FilterdEvent;
957 }
void InitDataChainADHitTree()
******************************************************************************************/// ...
Definition: StogsAFP.cpp:459
virtual UInt_t GetLength() const
total length for that frame
Definition: Frame.h:360
virtual Key * GetKey()=0
To get the Key associated to this frame.
virtual void Reset()
Reset the current frame.
virtual Frame * GetFrame() const
Definition: Frame.h:625
SBREvent * ProcessPacking(SBREvent *StogsEvt)
Definition: StogsAFP.cpp:778
bool ReadEventADHitTree()
******************************************************************************************/// ...
Definition: StogsAFP.cpp:516
It defines the interface needed to be a narval actor (producer).
header file for AgataFrameFactory.cpp
Base class for a Frame.
Definition: Frame.h:73
LogMessage & error(LogMessage &)
Double32_t fZ
Double32_t fE
Interface to AgataKey.
void InitDataChainStogs()
******************************************************************************************/// ...
Definition: StogsAFP.cpp:505
A Block of Frames.
Definition: FrameBlock.h:43
Double32_t fX
virtual Key * GetKey()
To get the Key associated to this frame.
Definition: Frame.h:344
void SetEoB(Bool_t b)
Definition: FrameBlock.h:89
virtual void SetModeIO(ConfAgent::EMode mode)
Definition: FrameBlock.h:81
void ApplyEnergyResolution(SBREvent *StogsEvt)
Definition: StogsAFP.cpp:871
void SetModel(ConfAgent::EModel model)
Definition: FrameIO.cpp:93
static ConfAgent * theGlobalAgent(std::string="Agata")
to get the global agent
Definition: ConfAgent.cpp:402
bool ReadEventStogs()
******************************************************************************************/// ...
Definition: StogsAFP.cpp:611
SBRHit * AddHit()
add a hit to the current event
void Fired(Bool_t b=true)
Fired (default) or not this trigger.
Definition: Trigger.h:106
ADF::SharedFP * fFrameEventPsa_Out
Definition: StogsAFP.h:57
SBREvent * FilterEvent(SBREvent *GlobEvt)
Definition: StogsAFP.cpp:907
virtual Bool_t Register(DFTrigger *)
To register a trigger.
Definition: FrameIO.cpp:368
ADF::SharedFP * fSPSAFrame
Definition: StogsAFP.h:58
const UInt_t aMByte
Definition: BaseBuffer.h:36
TEntryList * fEntryList
Definition: StogsAFP.h:73
virtual Bool_t AddSubFrame(const Frame &)
Add a subframe to that frame (only if composite)
unsigned int fRejectedEvents
Definition: StogsAFP.h:71
virtual void SetUID(Int_t)=0
to set the crystal ID
virtual void process_reset(unsigned int *error_code)
Destructor implementation.
Definition: StogsAFP.cpp:239
header file for PSAFrame.cpp
double fSpatialResolution
Definition: StogsAFP.h:123
virtual void SetE(Double_t, UInt_t=0u)=0
to set the energy associated to the core
const std::string & GetConfPath()
To get the algo path associated with the current actor.
Base class for a CompositeFrame.
ConfAgent.
Definition: ConfAgent.h:63
virtual SharedFP * SetOutputFrame(Frame *frame)
Once a trigger has fired, the result of the algorithm is set through this.
Definition: Trigger.h:512
header file for BufferIO.cpp
virtual void Detach(FrameBlock *in, FrameBlock *out)
Definition: FrameIO.cpp:741
UInt_t FreeSize() const
it returns the number of free bytes to the end
Definition: BufferIO.h:236
bool ReadConfFile(TString ConfFilePath)
******************************************************************************************/// ...
Definition: StogsAFP.cpp:251
virtual void SetE(Double_t e)
Definition: Hits.h:160
void SetStatus(EStatus stat)
Definition: FrameIO.h:110
virtual void SetProcessMethod(const char *)
To set the current method.
bool fProcessPacking
Definition: StogsAFP.h:122
header file for AgataKeyFactory.cpp
LogMessage & dolog(LogMessage &)
TChain * fDataChain
Definition: StogsAFP.h:72
virtual UInt_t Write()
It writes to the Frame the content of the dedicated structures.
Definition: Frame.h:206
static void process_config(const char *, unsigned int *)
to init globals (static) from a directory
Definition: StogsAFP.cpp:110
SBRHit * GetHit(Int_t)
to get a Hit
virtual BufferIO * GetBufferIO(UInt_t which=0u)=0
To get the address of the underlying buffer.
virtual void ClearMessage()
To clear the current message.
virtual void Clear(Option_t *opt)
clear the collection of hits, set H, K to 0
bool fApplySmearing
Definition: StogsAFP.h:131
virtual void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition: Hits.h:153
virtual Hit * NewHit()=0
Add a NewHit to the stack of Hits associated to this PSAFrame.
virtual Bool_t Record(DFTrigger *)
To record a frame trigger.
Definition: FrameIO.cpp:608
virtual void Reset()=0
Reset the current frame.
virtual void Reset()
Definition: PSAFrame.h:121
bool fApplyEnergyResolution
Definition: StogsAFP.h:130
ADF::AgataFrameTrigger fTrigger
Definition: StogsAFP.h:59
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual void process_initialise(unsigned int *error_code)
Constructor implementation.
Definition: StogsAFP.cpp:189
virtual void SetT(Double_t, UInt_t=0u)=0
to set the time associated to the core
virtual void SetID(Int_t id, Int_t which_id=0)
set crystal/segment id. if which_id > 0, set crystal ID.
Definition: PSAFrame.h:106
void ApplySmearing(SBREvent *StogsEvt)
Definition: StogsAFP.cpp:890
ULong64_t fCurrentEntry
Definition: StogsAFP.h:75
virtual ~StogsAFP()
******************************************************************************************/// ...
Definition: StogsAFP.cpp:102
virtual Bool_t Reserve(UInt_t, Bool_t=false, UInt_t=0u)
Reserve the given size for writing.
Definition: FrameBlock.h:137
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
Definition: FrameIO.cpp:571
StogsAFP()
*/
Definition: StogsAFP.cpp:60
virtual void Attach(FrameBlock *in, FrameBlock *out)
Attach a block to this. Rewind called if DoRewind set to true (Default)
Definition: FrameIO.cpp:724
Int_t GetNbHits() const
helper function
void Print()
Definition: StogsAFP.cpp:762
virtual SharedFP * AddUtility(const FactoryItem &key_item, const FactoryItem &frame_item, const ConfAgent *agent)
Add utilities, used to decode/encode composite frames.
Definition: Trigger.cpp:936
virtual unsigned int ProcessBlock(ADF::FrameBlock &)
Produce one block of data.
Definition: StogsAFP.cpp:121
virtual std::string & GetProcessName()
To get the Process name.
LogMessage Log
to send messages to the log server
virtual void SetName(const char *name)
Definition: FrameIO.h:102
bool IsADHitTreeModeOn
Definition: StogsAFP.h:79
ULong64_t fNEntries
Definition: StogsAFP.h:69
virtual UInt_t GetNbSubFrame() const
Returns the number of sub-frames composing this frame. Scan have to be called first.
Double32_t fY