32 #include "BashColor.h"
43 fCurrentFileNumber(0u),
47 fEndOfFrame(
"EndOfFrame"),
50 fFrameCrystal_Out(NULL),
51 fTrigger(
"data:crystal"),
55 fLastKeyIsRead(false),
58 fBitFlipMezzLength(0),
59 fBitFlipTraceLength(0),
63 TotalEntryFileSize(0),
65 LastReadEntryFileSize(0)
67 if(fdebug) std::cout<<
"\E[31;1m"<<
"Construct in"<<
"\E[m"<<
std::endl;
74 fSourceOfFrames.
SetModeIO(ConfAgent::kRead);
80 if(fdebug) std::cout<<
"\E[31;1m"<<
"Construct out"<<
"\E[m"<<
std::endl;
88 for(
int nn = 0; nn <
kNSG; nn++ )
90 for(
int nn = 0; nn <
kNCC; nn++ )
100 if ( fConfMode == -1 )
105 ::fclose(fCurrentFile);
108 if ( fConfMode == 0 ) {
110 std::string
tmp = fPath;
111 if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) ==
'/') )
115 std::ostringstream filename;
116 filename << tmp << fBaseForName
117 << std::setfill(
'0') << std::setw(4) << fCurrentFileNumber++;
119 fCurrentFile = ::fopen(filename.str().data(),
"rb");
120 if ( fCurrentFile != 0x0 ) {
121 Log <<
"A new input file has just been open "
122 << filename.str() <<
nline;
124 (fCurrentFile,fMaxSize);
126 (BaseFrameIO::kIdle);
138 while ( fCurrentFileNumber < fListOfFile.size() ) {
140 fCurrentFile = ::fopen(fListOfFile[fCurrentFileNumber].data(),
"rb");
141 if ( fCurrentFile == 0x0 ) {
143 << fListOfFile[fCurrentFileNumber]
144 <<
" is not a valid file "
147 fCurrentFileNumber++;
150 Log <<
info <<
"A new input file has just been open "
151 << fListOfFile[fCurrentFileNumber]
153 fSourceOfFrames.
SetFile(fCurrentFile,fMaxSize);
157 std::vector<std::string> token2 = split(fListOfFile[fCurrentFileNumber],
'/');
158 fCrystalName = token2[token2.size()-2];
160 if(fCurrentFileNumber > 0)
162 std::vector<std::string> token = split(fListOfFile[fCurrentFileNumber-1],
'/');
163 std::string previouscrystal = token[token.size()-2];
165 if(previouscrystal != fCrystalName) NewCrystal =
true;
167 fCrystalName = previouscrystal;
170 fCurrentFileNumber++;
186 NarvalInterface::process_config(directory_path,error_code);
188 if ( (*error_code) == 0u ) {
194 if(fdebug) std::cout<<
"\E[31;1m"<<
"ProcessBlock in"<<
"\E[m"<<
std::endl;
207 unsigned int EventLenght = frame_out->
GetLength();
209 while(out.
Reserve(EventLenght,
true))
266 for(
int i = 0; i <
kNSG; i++) {
273 for(
int i = 0; i <
kNCC; i++) {
293 double Ratio = ((double)ReadEntryFileSize)/((double)TotalEntryFileSize)*100.;
294 double LastRatio = ((double)LastReadEntryFileSize)/((double)TotalEntryFileSize)*100.;
296 std::ostringstream toto;
297 toto<<TotalEntryFileSize;
298 int NDigits = toto.str().length();
300 if(((
int)Ratio) != ((
int)LastRatio))
301 std::cout<<
"\r"<<
"Analysis progress : "<<std::setw(3)<<(int)(Ratio+0.5)<<
" %"<<
" ("<<std::setw(NDigits)<<ReadEntryFileSize<<
"/"<<std::setw(NDigits)<<TotalEntryFileSize<<
" bytes)"<<std::flush;
304 LastReadEntryFileSize = ReadEntryFileSize;
308 if(fdebug) std::cout<<
"\E[31;1m"<<
"ProcessBlock out"<<
"\E[m"<<
std::endl;
317 if(fdebug) std::cout<<
"\E[31;1m"<<
"process_initialise in"<<
"\E[m"<<
std::endl;
319 std::string
tmp, option;
325 conffile +=
"ReadMezzAFP.conf";
327 fCurrentFileNumber = 0u;
329 std::ifstream filein(conffile.data());
330 if ( filein.is_open() == true ) {
333 while ( filein.good() && !filein.eof() ) {
334 if ( tmp[0] ==
'#' ) {
340 std::istringstream decode(tmp);
344 std::string pathforfiles, basename;
345 unsigned int starting_number;
346 decode >> option >> pathforfiles >> basename >> starting_number;
348 if ( decode.good() ) {
350 fPath = pathforfiles;
351 fBaseForName = basename;
352 fCurrentFileNumber = starting_number;
357 if( tmp[0] ==
'l' ) {
359 std::string filename;
360 decode >> option >> filename;
363 std::vector<std::string> token = split(filename,
'/');
366 replace(fPath,token[token.size()-1],
"");
368 fListOfFile.push_back(filename);
373 if( tmp[0] ==
'r' ) {
374 unsigned int starting_number, ending_number;
375 std::string pathforfiles, basename;
376 decode >> option >> pathforfiles >> basename >> starting_number >> ending_number;
377 if( decode.good() && starting_number < ending_number ){
379 fPath = pathforfiles;
380 fBaseForName = basename;
382 for(
unsigned int number = starting_number; number <= ending_number; number++ ){
383 std::ostringstream osfilename;
384 osfilename << pathforfiles << basename << std::setfill(
'0') << std::setw(4) << number;
385 fListOfFile.push_back(osfilename.str());
390 if( tmp[0] ==
'f' ) {
391 std::string pathforfilter;
392 decode >> option >> pathforfilter;
399 std::cout<<
"Key filter file not found, key filter ignored !"<<
std::endl;
405 std::cout<<
"Key filter file : "<<pathforfilter<<
" will be used !"<<
std::endl;
434 std::cout<<
"\E[31;1m"<<conffile.data()<<
" not found !"<<
"\E[m"<<
std::endl;
448 for(
unsigned int i=0 ; i<fListOfFile.size() ; i++)
450 std::ifstream afile(fListOfFile[i].data(), std::ios_base::binary);
451 afile.seekg( 0 , std::ios_base::end );
452 unsigned long size = afile.tellg() ;
454 TotalEntryFileSize +=
size;
457 Log <<
info <<
"List of file to be read is " << fListOfFile.size() <<
nline ;
465 std::string ConfFilePath = fPath;
467 std::vector<std::string> token = split(ConfFilePath,
'/');
469 replace(ConfFilePath,(std::string)
"/" + token[token.size()-3] +
"/",
"/Conf/");
471 ConfFilePath +=
"CrystalProducer.conf";
473 std::ifstream ProdConfFile(ConfFilePath.data());
474 if ( ProdConfFile.is_open() == true )
480 getline(ProdConfFile,Line);
482 std::vector<std::string> token = split(Line,
' ');
485 std::istringstream ( token[token.size()-1] ) >> val;
488 if(contains(Line,
"CrystalID"))
crystal_id = (
unsigned int) val;
493 std::cout<<
"\E[31;1m"<<ConfFilePath.data()<<
" not found !"<<
"\E[m"<<
std::endl;
518 for(
int atseg = 0; atseg <
kNSG; atseg++) {
525 for(
int atcore = 0; atcore <
kNCC; atcore++) {
534 if ( *error_code == 0u )
540 if(fdebug) std::cout<<
"\E[31;1m"<<
"process_initialise out"<<
"\E[m"<<
std::endl;
550 ::fclose(fCurrentFile);
551 fCurrentFileNumber = 0u;
559 std::vector<std::string> ReadMezzAFP::split(std::string s,
char c)
561 const char* str = s.c_str();
563 std::vector<std::string> result;
567 const char *begin = str;
569 while(*str != c && *str)
572 result.push_back(std::string(begin,str));
579 bool ReadMezzAFP::replace(std::string& str,
const std::string& from,
const std::string& to)
581 size_t start_pos = str.find(from);
582 if(start_pos == std::string::npos)
return false;
583 str.replace(start_pos,from.length(), to);
588 bool ReadMezzAFP::contains(std::string s1, std::string s2)
590 if(s1.find(s2) != std::string::npos)
return true;
594 unsigned int ReadMezzAFP::uncompress_single_trace_AB(
unsigned char* compr,
unsigned int csize,
unsigned short* trace)
596 unsigned char* compressed = compr;
597 unsigned char* c_end = compr + csize;
600 unsigned char p1 = *compressed++, p2 = *compressed++;
601 unsigned short ref = p2<<8 | p1;
602 trace[0] = ref^0x2000;
606 const unsigned char packed = *compressed++;
607 if( (packed & 0x80) == 0 ) {
609 const unsigned char p2 = *compressed++;
610 const unsigned short packed2 = packed<<8 | p2;
611 const int diff1 = ((packed2>>10) & 0x1F) - 15;
612 const int diff2 = ((packed2>> 5) & 0x1F) - 15;
613 const int diff3 = ( packed2 & 0x1F) - 15;
615 trace[midx++] = ref^0x2000;
617 trace[midx++] = ref^0x2000;
619 }
else if( (packed & 0xC0) == 0xC0 ) {
620 if( compressed>=c_end )
623 ref = (packed&0x3F)<<8 | *compressed++;
626 const int diff1 = (packed & 0x3F) +
MIN_1x6;
629 trace[midx] = ref^0x2000;
645 std::vector < std::pair < unsigned long long, unsigned short > > ListOfTS;
647 for(
int mezz=0 ; mezz<7 ; mezz++)
652 fread((
void *)BuffChannHead,
sizeof(
unsigned short), mezzanineMezzHeader, fCurrentFile);
653 ReadEntryFileSize += mezzanineMezzHeader*
sizeof(
unsigned short);
655 unsigned short SlotId = BuffChannHead[0] & 0x0F ;
656 evnumber = (
unsigned int) BuffChannHead[1] | (
unsigned int) BuffChannHead[2] << 16;
657 timestamp = (
unsigned long long)BuffChannHead[3] | (
unsigned long long)BuffChannHead[4] << 16 | (
unsigned long long)BuffChannHead[5] << 32;
659 unsigned short TraceLen = BuffChannHead[6];
660 unsigned short MezzLen= BuffChannHead[7];
661 unsigned short DomainId = BuffChannHead[8] & 0xFF;
662 unsigned int ActMezzLen = BuffChannHead[15];
665 bool alreadyseen =
false;
667 for(
unsigned int itest=0 ; itest<ListOfTS.size() ; itest++)
669 std::pair < unsigned long long, unsigned short > *a_pair = &ListOfTS[itest];
672 a_pair->second = a_pair->second+1;
679 std::pair < unsigned long long, unsigned short > a_pair;
683 ListOfTS.push_back(a_pair);
686 if(::feof(fCurrentFile))
688 if(SlotId != 0 && fdebug)
691 std::cout<<
"File stoped after reading the header of mezzanine ID : "<<SlotId<<
std::endl;
697 delete [] BuffChannHead;
701 if(SlotId<1 || SlotId>7)
703 std::cout<<
"\E[31;1m"<<
"Bad Header, SlotID = "<<SlotId<<
"\E[m"<<
std::endl;
706 if(TraceLen!=fTraceLengthRaw)
708 fBitFlipTraceLength++;
712 unsigned int NormalTraceLength = 100;
713 std::cout<<
"Normal TraceLength "<<std::setw(4)<<NormalTraceLength<<std::setw(6)<<std::hex<<NormalTraceLength<<std::dec<<std::setw(20)<<ConvertToBinary(NormalTraceLength)<<
std::endl;
714 std::cout<<
"Actual TraceLength "<<std::setw(4)<<TraceLen<<std::setw(6)<<std::hex<<TraceLen<<std::dec<<std::setw(20)<<ConvertToBinary(TraceLen)<<
std::endl;
716 std::cout<<
"\E[31;1m"<<
"Number of samples different from configuration file"<<
"\E[m"<<
std::endl;
717 std::cout<<
"TraceLen = "<<TraceLen<<
std::endl;
722 int toread = ActMezzLen;
731 unsigned short *compBuffer =
new unsigned short[toread];
732 memset(compBuffer, 0,
sizeof(
unsigned short)*toread);
736 fread(compBuffer,
sizeof(
unsigned short), toread, fCurrentFile);
737 ReadEntryFileSize += toread*
sizeof(
unsigned short);
739 if(::feof(fCurrentFile))
741 if(SlotId != 7 && fdebug)
744 std::cout<<
"File stoped after reading the traces of mezzanine ID : "<<SlotId<<
std::endl;
749 delete [] BuffChannHead;
750 delete [] compBuffer;
758 fBitFlipMezzLength++;
762 unsigned int NormalLength = 664;
763 std::cout<<
"Normal MezzLength "<<std::setw(4)<<NormalLength<<std::setw(6)<<std::hex<<NormalLength<<std::dec<<std::setw(20)<<ConvertToBinary(NormalLength)<<
std::endl;
764 std::cout<<
"Actual MezzLength "<<std::setw(4)<<MezzLen<<std::setw(6)<<std::hex<<MezzLen<<std::dec<<std::setw(20)<<ConvertToBinary(MezzLen)<<
std::endl;
767 std::cout<<
"\E[31;1m"<<
"Number of channels non equal to "<<NChan<<
"\E[m"<<
std::endl;
768 std::cout<<
"numchan = "<<numchan<<
std::endl;
773 unsigned short* dbuffer =
new unsigned short[lenMezzTotal];
774 memset(dbuffer, 0,
sizeof(
unsigned short)*lenMezzTotal);
775 unsigned int PosInBuff=0;
777 memcpy(dbuffer, BuffChannHead,
sizeof(
unsigned short)*mezzanineMezzHeader);
779 delete [] BuffChannHead;
782 unsigned short *pC = compBuffer;
787 for(
int nn = 0; nn < NChan; nn++) {
792 unsigned int csize = pD[-1];
793 unsigned int ctype = pD[-2] & 0xF;
798 memcpy(dbuffer, compBuffer,
sizeof(
unsigned short)*toread);
803 ll = uncompress_single_trace_AB((
unsigned char *)pC, 2*csize, pD);
807 std::cout<<
"WARNING : Compression type unkown : "<<ctype<<
std::endl;
809 std::cout<<
"Mezzanine Header :"<<
std::endl;
810 std::cout<<
"SlotId = "<<SlotId<<
std::endl;
813 std::cout<<
"TraceLen = "<<TraceLen<<
std::endl;
814 std::cout<<
"MezzLen = "<<MezzLen<<
std::endl;
815 std::cout<<
"DomainId = "<<DomainId<<
std::endl;
816 std::cout<<
"ActMezzLen = "<<ActMezzLen<<std::endl<<
std::endl;
819 delete [] compBuffer;
820 dbuffer -= PosInBuff;
833 std::cout<<
"Mezzanine Header :"<<
std::endl;
834 std::cout<<
"SlotId = "<<SlotId<<
std::endl;
837 std::cout<<
"TraceLen = "<<TraceLen<<
std::endl;
838 std::cout<<
"MezzLen = "<<MezzLen<<
std::endl;
839 std::cout<<
"DomainId = "<<DomainId<<
std::endl;
840 std::cout<<
"ActMezzLen = "<<ActMezzLen<<std::endl<<
std::endl;
844 for(
int i=0 ; i<NChan ; i++)
846 if(dbuffer[0] > 7) fBitFlipSegId++;
857 int ener32 = dbuffer[2];
858 ener32 = (ener32 << 16) | dbuffer[1];
860 int SegId = (SlotId-2)*6+(5-ID);
861 if((SegId<0 || SegId >37) && SlotId>1)
865 std::cout<<
"SlotId = "<<SlotId<<
std::endl;
869 float Ener = float(ener32)/(1<<16);
870 int Status = dbuffer[3];
871 int CompType = dbuffer[6];
872 int CompTraceLen = dbuffer[7];
874 if(SlotId==1 && (ID==0 || ID==1))
880 else if(SlotId==1) {;}
885 SegID[SegId] = SegId;
890 if(SlotId==1 && !(ID==0 || ID==1))
break;
897 std::cout<<
"Seg ID = "<<SegId<<
std::endl;
899 else std::cout<<
"core ID = "<<ID<<
std::endl;
901 std::cout<<
"Status = "<<Status<<
std::endl;
902 std::cout<<
"CompType = "<<CompType<<
std::endl;
903 std::cout<<
"CompTraceLen = "<<CompTraceLen<<
std::endl;
911 unsigned short tmp16;
922 if(SlotId==1 && (ID==0 || ID==1))
CoreTraces[ID][j] = tmp16;
923 else if(SlotId==1)
break;
931 delete [] compBuffer;
932 dbuffer -= PosInBuff;
937 if(ListOfTS.size() !=1 )
943 for(
unsigned int itest=0 ; itest<ListOfTS.size() ; itest++)
945 std::pair < unsigned long long, unsigned short > a_pair = ListOfTS[itest];
946 if(a_pair.second>NViews)
948 NViews = a_pair.second;
965 std::cout<<
"The last key of the key filter file has been read."<<
std::endl;
991 std::cout<<
"Crystal: "<<fCrystalName<<
std::endl;
993 std::cout<<
"Number of imput events : "<<fNEvts<<
std::endl;
999 int TH = (int)(TotalTimeInS/3600.);
1000 int TMin = (int)((TotalTimeInS/3600.-TH)*60.);
1001 int TSec = (int)(((TotalTimeInS/3600.-TH)*60.-TMin)*60.);
1003 std::cout<<
"Elapsed time (s) : "<<TotalTimeInS<<
" ( "<<TH<<
"h"<<TMin<<
"'"<<TSec<<
"'' )"<<std::endl<<
std::endl;
1007 std::cout<<
"Number of filtered events : "<<fNEvts-fNIgnoredEvts<<
" ( "<<((float)(fNEvts-fNIgnoredEvts))/((
float)fNEvts)*100.<<
"\% )"<<std::endl;
1008 std::cout<<
"Number of ignored events : "<<fNIgnoredEvts<<
" ( "<<((float)fNIgnoredEvts)/((float)fNEvts)*100.<<
"% )"<<
std::endl;
1012 if(fBitFlipMezzLength>0) std::cout<<
"Number of MezzLength flipped bits : "<<fBitFlipMezzLength<<
std::endl;
1013 if(fBitFlipTraceLength>0) std::cout<<
"Number of TraceLength flipped bits : "<<fBitFlipTraceLength<<
std::endl;
1014 if(fBitFlipSegId>0) std::cout<<
"Number of SegId flipped bits : "<<fBitFlipSegId<<
std::endl;
1015 if(fBitFlipTS>0) std::cout<<
"Number of Timestamp flipped bits : "<<fBitFlipTS<<
std::endl;
1018 std::cout<<std::endl<<
"*******************************************"<<
std::endl;
1029 fBitFlipMezzLength = 0;
1030 fBitFlipTraceLength = 0;
1038 std::string ReadMezzAFP::ConvertToBinary(
unsigned long long n)
1040 std::ostringstream *bin =
new std::ostringstream();
1042 ProcessConvToBinary(n, bin);
1044 std::string binvalue = ((std::string)(*bin).str());
1050 void ReadMezzAFP::ProcessConvToBinary(
unsigned long long n, std::ostringstream *str)
1053 ProcessConvToBinary(n / 2, str);
1055 (*str)<<(int)(n % 2);
ADF::AgataKey * fKeyToLink
virtual Key * GetKey()=0
To get the Key associated to this frame.
virtual Frame * GetFrame() const
unsigned short * CoreTraces[kNCC]
virtual void SetStatus(UShort_t s)
virtual GeCore * GetCore(UShort_t)=0
to get each core
It defines the interface needed to be a narval actor (producer).
LogMessage & error(LogMessage &)
LogMessage & warning(LogMessage &)
static const int mezzanineChanHeader
virtual GeSegment * GetSegment(UShort_t)=0
to get individual segment
virtual void process_initialise(unsigned int *error_code)
Constructor implementation.
virtual Signal * GetSignal()
unsigned short SegID[kNSG]
unsigned int fTraceLengthRaw
LogMessage & nline(LogMessage &)
virtual void SetModeIO(ConfAgent::EMode mode)
void SetModel(ConfAgent::EModel model)
Data produced fro a Crystal.
unsigned short crystal_status
Base class for a Global Object.
void Fired(Bool_t b=true)
Fired (default) or not this trigger.
static void process_config(const char *, unsigned int *)
to init globals (static) from a directory
virtual ULong64_t GetTimeStamp() const =0
To set the timestamp.
UInt_t Offset() const
it returns the current position in the buffer
virtual void SetID(UShort_t id)
unsigned short CoreStatus[kNSG]
static UInt_t kDefaultLength
virtual Bool_t Register(DFTrigger *)
To register a trigger.
header file for ReadMezzAFP.cpp
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
virtual UInt_t GetKeyLength() const
Unique number corresponding to a type of Key.
virtual UInt_t GetEventNumber() const =0
To get the event number encoded.
const std::string & GetConfPath()
To get the algo path associated with the current actor.
Base class for version numbers.
virtual SharedFP * SetOutputFrame(Frame *frame)
Once a trigger has fired, the result of the algorithm is set through this.
header file for BufferIO.cpp
virtual void Detach(FrameBlock *in, FrameBlock *out)
UInt_t FreeSize() const
it returns the number of free bytes to the end
void SetStatus(EStatus stat)
virtual void SetProcessMethod(const char *)
To set the current method.
ADF::SharedFP * fFrameCrystal_Out
static const int mezzanineMezzHeader
Trigger for the input frame.
unsigned short crystal_id
virtual void SetE(Double_t e)
virtual void Set(UShort_t, UInt_t)=0
Method to set the value for a particular bin of the signal.
LogMessage & dolog(LogMessage &)
virtual UInt_t Write()
It writes to the Frame the content of the dedicated structures.
unsigned short * SegmentTraces[kNSG]
virtual void ClearMessage()
To clear the current message.
Bool_t LinkItem(const Char_t *, const Char_t *, void *)
Only for basic type ... otherwise better to use the most general Link method.
unsigned long long timestamp
virtual UInt_t GetLength() const =0
total length for that frame
virtual Bool_t Record(DFTrigger *)
To record a frame trigger.
virtual Bool_t Link(const Char_t *, UInt_t)
Link an external buffer to this key.
virtual void Reset()=0
Reset the current frame.
ADF::BufferIO * fKeyFilterBuffer
ADF::LogMessage & endl(ADF::LogMessage &log)
virtual void SetFile(FILE *file, Long64_t max_size=kMaxUInt_t)
unsigned short CoreID[kNSG]
virtual void process_reset(unsigned int *error_code)
Destructor implementation.
UInt_t Import(const Char_t *from, UInt_t size_ext_buf)
Import the given array in this buffer.
virtual Bool_t Reserve(UInt_t, Bool_t=false, UInt_t=0u)
Reserve the given size for writing.
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
virtual unsigned int ProcessBlock(ADF::FrameBlock &)
Produce one block of data.
unsigned long long LastTimeStamp
virtual void Attach(FrameBlock *in, FrameBlock *out)
Attach a block to this. Rewind called if DoRewind set to true (Default)
unsigned short SegStatus[kNSG]
ADF::AgataFrameTrigger fTrigger
virtual std::string & GetProcessName()
To get the Process name.
unsigned long long FirstTimeStamp
virtual void SetT(Double_t t)
LogMessage Log
to send messages to the log server
virtual void SetName(const char *name)
unsigned long long fNextTS
Base class that described an item in a Factory.
UInt_t SetOffset(UInt_t off=0u) const
change the current position.