15#include "TTreeIndex.h"
16#include "TVirtualIndex.h"
21#include "TPolyMarker.h"
22#include "TStopwatch.h"
33 explicit TEventTime(
const std::shared_ptr<TMidasEvent>& event)
38 int banksize =
event->LocateBank(
nullptr,
"GRF2", &ptr);
42 banksize =
event->LocateBank(
nullptr,
"GRF1", &ptr);
45 uint32_t type = 0xffffffff;
46 uint32_t value = 0xffffffff;
50 for(
int x = 0; x < banksize; x++) {
51 value = *(
reinterpret_cast<int*
>(ptr) + x);
52 type = value & 0xf0000000;
64 if((value & 0xf0000000) != 0x80000000) {
67 chanadd = (value & 0x0003fff0) >> 4;
80 if((value & 0xf0000000) != 0x80000000) {
83 chanadd = (value & 0x000ffff0) >> 4;
89 default: printf(
"This bank not yet defined.\n");
break;
92 case 0xa0000000:
timelow = value & 0x0fffffff;
break;
93 case 0xb0000000:
timehigh = value & 0x00003fff;
break;
151 std::map<uint32_t, int>::iterator it;
154 it->second = index++;
195void PrintError(
const std::shared_ptr<TMidasEvent>& event,
int frags,
bool verb)
198 printf(
DRED "\n//**********************************************//" RESET_COLOR "\n");
199 printf(
DRED "\nBad things are happening. Failed on datum %i" RESET_COLOR "\n", (-1 * frags));
201 event->Print(Form(
"a%i", (-1 * frags) - 1));
203 printf(
DRED "\n//**********************************************//" RESET_COLOR "\n");
210 const int total_events = 1E6;
212 const int event_start = 1E5;
213 std::shared_ptr<TMidasEvent>
event = std::make_shared<TMidasEvent>();
214 eventQ->reserve(total_events);
221 printf(
DBLUE "Subrun 000, Starting event checker at event %d\n" RESET_COLOR, event_start);
226 unsigned int mserial = 0;
228 mserial =
static_cast<unsigned int>(
event->GetSerialNumber());
230 unsigned int mtime = 0;
232 mtime =
event->GetTimeStamp();
235 while(infile->
Read(event) > 0 && eventQ->size() < total_events) {
236 switch(event->GetEventId()) {
238 printf(
DRED "start of run\n");
243 printf(
DGREEN "end of run\n");
248 if(event->GetEventId() != 1) {
251 event->SetBankList();
253 banksize =
event->LocateBank(
nullptr,
"GRF2", &ptr);
255 banksize =
event->LocateBank(
nullptr,
"GRF1", &ptr);
268 if((subrun > 0) || (events_read > event_start)) {
278 if(events_read % 250000 == 0) {
292 auto* midvshigh =
new TList;
295 std::map<uint32_t, int>::iterator mapit;
297 auto* midvshighhist =
new TH2D(Form(
"midvshigh_0x%04x", mapit->first), Form(
"midvshigh_0x%04x", mapit->first),
298 5000, 0, 5000, 5000, 0, 5000);
299 midvshigh->Add(midvshighhist);
307 std::map<uint32_t, int> lowest_hightime;
308 std::vector<TEventTime*>::iterator it;
310 for(it = eventQ->begin(); it != eventQ->end(); it++) {
312 int hightime = (*it)->TimeStampHigh();
313 uint64_t midtime = (*it)->MidasTime() - lowmidtime;
317 if(((*it)->Digitizer() == 0) && ((*it)->DetectorType() > 1)) {
321 dynamic_cast<TH2D*
>(midvshigh->FindObject(Form(
"midvshigh_0x%04x", (*it)->Digitizer())))->Fill(
static_cast<double>(midtime), hightime);
322 if(lowest_hightime.find((*it)->Digitizer()) == lowest_hightime.end()) {
323 lowest_hightime[(*it)->Digitizer()] = hightime;
324 }
else if(hightime < lowest_hightime.find((*it)->Digitizer())->second) {
325 lowest_hightime.find((*it)->Digitizer())->second = hightime;
330 uint32_t lowest_dig = 0;
331 int lowtime = 999999;
340 lowtime = lowest_hightime.find(lowest_dig)->second;
343 printf(
"The lowest digitizer is 0x%04x\n", lowest_dig);
344 printf(
"***** High time shifts *******\n");
345 for(mapit = lowest_hightime.begin(); mapit != lowest_hightime.end(); mapit++) {
346 if(mapit->first == lowest_dig) {
349 printf(
"0x%04x:\t %d \t %lf sec\n", mapit->first, mapit->second - lowtime,
350 static_cast<double>((
static_cast<int64_t
>(mapit->second - lowtime)) * (1 << 28)) / 1.0E8);
353 ((
static_cast<uint64_t
>(mapit->second - lowtime)) * (1 << 28));
355 printf(
"********************\n");
367 auto* roughlist =
new TList;
370 std::map<uint32_t, bool> keep_filling;
371 std::map<uint32_t, int>::iterator mapit;
375 new TH1D(Form(
"rough_0x%04x", mapit->first), Form(
"rough_0x%04x", mapit->first), 3E7, -3E8, 3E8);
377 roughlist->Add(roughhist);
378 keep_filling[mapit->first] =
true;
384 TH1D* fillhist =
nullptr;
386 std::vector<TEventTime*>::iterator hit1;
387 std::vector<TEventTime*>::iterator hit2;
388 const int range = 5000;
390 for(hit1 = (eventQ->begin()); hit1 != eventQ->end(); hit1++) {
392 if(event1count % 75000 == 0) {
393 printf(
"Processing Event %d /%lu \r", event1count, eventQ->size());
398 if(((*hit1)->Digitizer() == 0) && ((*hit1)->DetectorType() > 1)) {
406 auto time1 =
static_cast<int64_t
>((*hit1)->GetTimeStamp());
408 if(event1count > range) {
411 hit2 = eventQ->begin();
415 while((hit2 != eventQ->end()) && (event2count < 2 * range)) {
417 if(((*hit2)->Digitizer() == 0) && ((*hit2)->DetectorType() > 1)) {
426 uint32_t digitizer = (*hit2)->Digitizer();
427 if(keep_filling[digitizer]) {
429 dynamic_cast<TH1D*
>(roughlist->At((*hit2)->DigIndex()));
430 int64_t time2 =
static_cast<int64_t
>((*hit2)->GetTimeStamp()) -
432 auto bin =
static_cast<Int_t
>(time2 - time1);
434 if((fillhist->FindBin(bin) > 0) && (fillhist->FindBin(bin) < fillhist->GetNbinsX())) {
446 fillhist =
dynamic_cast<TH1D*
>(roughlist->At(mapit->second));
447 std::cout << static_cast<int64_t>(fillhist->GetBinCenter(fillhist->GetMaximumBin())) << std::endl;
449 static_cast<int64_t
>(fillhist->GetBinCenter(fillhist->GetMaximumBin()));
452 printf(
"***** Rough time shifts *******\n");
453 std::map<uint32_t, int64_t>::iterator cit;
456 printf(
"0x%04x:\t BEST\n", cit->first);
459 printf(
"0x%04x:\t %lld\n", cit->first, cit->second);
461 printf(
"0x%04x:\t %ld\n", cit->first, cit->second);
465 printf(
"********************\n");
478 auto* list =
new TList;
481 std::map<uint32_t, int>::iterator mapit;
484 new TH1D(Form(
"timediff_0x%04x", mapit->first), Form(
"timediff_0x%04x", mapit->first), 1000, -500, 500);
492 TH1D* fillhist =
nullptr;
494 std::vector<TEventTime*>::iterator hit1;
495 std::vector<TEventTime*>::iterator hit2;
497 const int range = 2000;
498 for(hit1 = eventQ->begin(); hit1 != eventQ->end(); hit1++) {
500 if(event1count % 75000 == 0) {
501 printf(
"Processing Event %d / %lu \r", event1count, eventQ->size());
507 if((*hit1)->Digitizer() == 0 && (*hit1)->DetectorType() > 1) {
515 auto time1 =
static_cast<int64_t
>((*hit1)->GetTimeStamp());
517 if(event1count > range) {
520 hit2 = eventQ->begin();
524 while((hit2 != eventQ->end()) && (event2count < range * 2)) {
527 if(((*hit2)->Digitizer() == 0) && ((*hit2)->DetectorType() > 1)) {
535 dynamic_cast<TH1D*
>(list->At((*hit2)->DigIndex()));
536 int64_t time2 =
static_cast<int64_t
>((*hit2)->GetTimeStamp()) -
538 if((time2 - time1 < 2147483647) &&
539 (time2 - time1 > -2147483647)) {
540 auto bin =
static_cast<Int_t
>(time2 - time1);
551 fillhist =
dynamic_cast<TH1D*
>(list->At(mapit->second));
552 auto* spec =
new TSpectrum();
553 spec->Search(fillhist);
554 double peak = spec->GetPositionX()[0];
555 std::cout << static_cast<int64_t>(floor(peak + 0.5)) << std::endl;
560 printf(
"***** Rough time shifts *******\n");
561 std::map<uint32_t, int64_t>::iterator cit;
564 printf(
"0x%04x:\t BEST\n", cit->first);
567 printf(
"0x%04x:\t %lld\n", cit->first, cit->second);
569 printf(
"0x%04x:\t %ld\n", cit->first, cit->second);
574 printf(
"********************\n");
583 if(event->GetEventId() != 1) {
587 event->SetBankList();
593 int banksize =
event->LocateBank(
nullptr,
"GRF2", &ptr);
596 banksize =
event->LocateBank(
nullptr,
"GRF1", &ptr);
599 uint32_t type = 0xffffffff;
600 uint32_t value = 0xffffffff;
602 uint32_t dettype = 0;
603 uint32_t chanadd = 0;
605 unsigned int timelow = 0;
606 unsigned int timehigh = 0;
610 for(
int x = 0; x < banksize; x++) {
611 value = *(
reinterpret_cast<int*
>(ptr) + x);
612 type = value & 0xf0000000;
624 if((value & 0xf0000000) != 0x80000000) {
627 chanadd = (value & 0x0003fff0) >> 4;
628 dettype = (value & 0x0000000f);
640 if((value & 0xf0000000) != 0x80000000) {
643 chanadd = (value & 0x000ffff0) >> 4;
644 dettype = (value & 0x0000000f);
649 default: printf(
"This bank not yet defined.\n");
break;
652 case 0xa0000000: timelow = value & 0x0fffffff;
break;
653 case 0xb0000000: timehigh = value & 0x00003fff;
break;
659 time |= timelow & 0x0fffffff;
663 if(dettype > 1 && ((chanadd & 0xF) > 1) && ((chanadd & 0xF00) > 1) &&
SplitMezz) {
671 if(time > 0x3ffffffffff) {
672 time -= 0x3ffffffffff;
685 std::shared_ptr<TMidasEvent> copyevent = std::make_shared<TMidasEvent>(*event);
686 copyevent->SetBankList();
688 banksize = copyevent->LocateBank(
nullptr,
"GRF2", &ptr);
690 banksize = copyevent->LocateBank(
nullptr,
"GRF1", &ptr);
693 for(
int x = 0; x < banksize; x++) {
694 value = *(
reinterpret_cast<int*
>(ptr) + x);
695 type = value & 0xf0000000;
699 timelow = time & 0x0fffffff;
700 timelow += 0xa0000000;
701 *(
reinterpret_cast<int*
>(ptr) + x) = timelow;
704 timehigh = (time & 0x3fff0000000) >> 28;
705 int tempdead = value & 0xffffc000;
706 timehigh += tempdead;
707 *(
reinterpret_cast<int*
>(ptr) + x) = timehigh;
729 std::ifstream in(file->
GetFilename(), std::ifstream::in | std::ifstream::binary);
730 in.seekg(0, std::ifstream::end);
731 int64_t filesize = in.tellg();
735 int64_t bytesread = 0;
742 std::shared_ptr<TMidasEvent>
event = std::make_shared<TMidasEvent>();
745 bytes = file->
Read(event);
755 switch(event->GetEventId()) {
757 printf(
"start of run\n");
764 printf(
"end of run\n");
775 if(num_evt % 5000 == 0) {
776 gSystem->ProcessEvents();
777 std::streamsize precision = std::cout.precision();
778 std::cout.precision(2);
779 std::cout <<
HIDE_CURSOR <<
" Events " << num_evt <<
" have processed " <<
static_cast<double>(bytesread) / 1000000. <<
"MB/" <<
static_cast<double>(filesize) / 1000000. <<
" MB => " << std::setprecision(1) <<
static_cast<double>(bytesread) / 1000000 / w.RealTime() <<
" MB/s " <<
SHOW_CURSOR <<
"\r";
780 std::cout.precision(precision);
792 std::array<char, 64> filename;
793 snprintf(filename.data(), filename.size(),
"corrections%05i.root", runnumber);
794 auto* corrfile =
new TFile(filename.data(),
"RECREATE");
798 uint32_t address = 0;
799 Long64_t correction = 0;
800 auto* t =
new TTree(
"correctiontree",
"Tree with map");
801 t->Branch(
"address", &address);
802 t->Branch(
"correction", &correction);
804 std::map<uint32_t, int64_t>::iterator it;
807 correction = it->second;
819 std::array<char, 64> filename;
820 snprintf(filename.data(), filename.size(),
"corrections%05i.root", runnumber);
821 auto* corrfile =
new TFile(filename.data(),
"READ");
822 if(!(corrfile->IsOpen())) {
830 corrfile->GetObject(
"correctiontree", t);
831 TBranch* baddress =
nullptr;
832 TBranch* bcorrection =
nullptr;
833 uint32_t address = 0;
834 Long64_t correction = 0;
835 t->SetBranchAddress(
"address", &address, &baddress);
836 t->SetBranchAddress(
"correction", &correction, &bcorrection);
839 printf(
"Digitizer \t Correction\n");
841 Long64_t tentry = t->LoadTree(i++);
845 baddress->GetEntry(tentry);
846 bcorrection->GetEntry(tentry);
847 printf(
"0x%04x: \t\t %lld\n", address, correction);
851 t->ResetBranchAddresses();
863 printf(
"Usage: ./offsetfix <input.mid> <output.mid> <y/n>(read correction file)\n");
866 if(argv[1] == argv[2]) {
867 printf(
"ERROR: Cannot overwrite midas file %s\n", argv[1]);
871 midfile->
Open(argv[1]);
872 int runnumber = midfile->GetRunNumber();
873 int subrunnumber = midfile->GetSubRunNumber();
875 midfile->OutOpen(Form(
"fixrun%05d_%03d.mid", runnumber, subrunnumber));
877 midfile->OutOpen(argv[2]);
879 std::array<char, 64> filename;
880 if(subrunnumber > -1) {
881 snprintf(filename.data(), filename.size(),
"time_diffs%05i_%03i.root", runnumber, subrunnumber);
883 snprintf(filename.data(), filename.size(),
"time_diffs%05i.root", runnumber);
885 printf(
"Creating root outfile: %s\n", filename.data());
889 if(strcmp(argv[3],
"n") == 0) {
900 if(nDigitizers == 0) {
901 auto* outfile =
new TFile(filename.data(),
"RECREATE");
902 auto* eventQ =
new std::vector<TEventTime*>;
904 std::cout <<
"Number of Digitizers Found: " <<
TEventTime::digmap.size() << std::endl;
911 midfile->Open(argv[1]);
int64_t GetTimeStamp() const
static uint32_t GetBestDigitizer()
static std::map< uint32_t, int > digmap
static uint64_t GetLowestTime()
unsigned int TimeStampHigh() const
static int GetBestDigitizer()
TEventTime(const std::shared_ptr< TMidasEvent > &event)
static std::map< uint32_t, bool > digset
uint32_t Digitizer() const
uint64_t MidasTime() const
TEventTime(TEventTime &&) noexcept=default
static std::map< int, int > digmap
TEventTime(const TEventTime &)=default
static int64_t lowest_time
static std::map< uint32_t, int64_t > correctionmap
static void OrderDigitizerMap()
static uint64_t low_timemidas
static uint64_t GetLowestMidasTime()
int GetFailedWord() const
int GriffinDataToFragment(uint32_t *data, int size, EBank bank, unsigned int midasSerialNumber=0, time_t midasTime=0)
static TGRSIOptions * Get(int argc=0, char **argv=nullptr)
Do not use!
bool SuppressErrors() const
Reader for MIDAS .mid files.
bool Open(const char *filename) override
Open input file.
int Read(std::shared_ptr< TRawEvent > event) override
Read one event from the file.
int GetSubRunNumber() override
int GetLastErrno() const
Get error value for the last file error.
void FillBuffer(const std::shared_ptr< TMidasEvent > &midasEvent, Option_t *opt="")
const char * GetLastError() const
Get error text for the last file error.
virtual const char * GetFilename() const
Get the name of this file.
void WriteEvents(TMidasFile *file)
int main(int argc, char **argv)
bool ProcessEvent(const std::shared_ptr< TMidasEvent > &event, TMidasFile *outfile)
int QueueEvents(TMidasFile *infile, std::vector< TEventTime * > *eventQ)
void WriteCorrectionFile(int runnumber)
int CorrectionFile(int runnumber)
void GetRoughTimeDiff(std::vector< TEventTime * > *eventQ)
void CheckHighTimeStamp(std::vector< TEventTime * > *eventQ)
void GetTimeDiff(std::vector< TEventTime * > *eventQ)
void PrintError(const std::shared_ptr< TMidasEvent > &event, int frags, bool verb)