12 #include "TSpectrum.h" 15 #include "TTreeIndex.h" 16 #include "TVirtualIndex.h" 21 #include "TPolyMarker.h" 22 #include "TStopwatch.h" 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;
147 std::map<uint32_t, int>::iterator it;
150 it->second = index++;
191 void PrintError(
const std::shared_ptr<TMidasEvent>& event,
int frags,
bool verb)
194 printf(
DRED "\n//**********************************************//" RESET_COLOR "\n");
195 printf(
DRED "\nBad things are happening. Failed on datum %i" RESET_COLOR "\n", (-1 * frags));
197 event->Print(Form(
"a%i", (-1 * frags) - 1));
199 printf(
DRED "\n//**********************************************//" RESET_COLOR "\n");
206 const int total_events = 1E6;
208 const int event_start = 1E5;
209 std::shared_ptr<TMidasEvent>
event = std::make_shared<TMidasEvent>();
210 eventQ->reserve(total_events);
217 printf(
DBLUE "Subrun 000, Starting event checker at event %d\n" RESET_COLOR, event_start);
222 unsigned int mserial = 0;
224 mserial = (
unsigned int)(event->GetSerialNumber());
226 unsigned int mtime = 0;
228 mtime =
event->GetTimeStamp();
231 while(infile->
Read(event) > 0 && eventQ->size() < total_events) {
232 switch(event->GetEventId()) {
234 printf(
DRED "start of run\n");
239 printf(
DGREEN "end of run\n");
244 if(event->GetEventId() != 1) {
247 event->SetBankList();
249 banksize =
event->LocateBank(
nullptr,
"GRF2", &ptr);
251 banksize =
event->LocateBank(
nullptr,
"GRF1", &ptr);
264 if((subrun > 0) || (events_read > event_start)) {
274 if(events_read % 250000 == 0) {
288 auto* midvshigh =
new TList;
291 std::map<uint32_t, int>::iterator mapit;
293 auto* midvshighhist =
new TH2D(Form(
"midvshigh_0x%04x", mapit->first), Form(
"midvshigh_0x%04x", mapit->first),
294 5000, 0, 5000, 5000, 0, 5000);
295 midvshigh->Add(midvshighhist);
308 std::map<uint32_t, int> lowest_hightime;
309 std::vector<TEventTime*>::iterator it;
311 for(it = eventQ->begin(); it != eventQ->end(); it++) {
313 int hightime = (*it)->TimeStampHigh();
314 unsigned long midtime = (*it)->MidasTime() - lowmidtime;
318 if(((*it)->Digitizer() == 0) && ((*it)->DetectorType() > 1)) {
322 (
dynamic_cast<TH2D*
>(midvshigh->FindObject(Form(
"midvshigh_0x%04x", (*it)->Digitizer()))))
323 ->Fill(midtime, hightime);
324 if(lowest_hightime.find((*it)->Digitizer()) == lowest_hightime.end()) {
325 lowest_hightime[(*it)->Digitizer()] = hightime;
326 }
else if(hightime < lowest_hightime.find((*it)->Digitizer())->second) {
327 lowest_hightime.find((*it)->Digitizer())->second = hightime;
332 uint32_t lowest_dig = 0;
333 int lowtime = 999999;
342 lowtime = lowest_hightime.find(lowest_dig)->second;
345 printf(
"The lowest digitizer is 0x%04x\n", lowest_dig);
346 printf(
"***** High time shifts *******\n");
347 for(mapit = lowest_hightime.begin(); mapit != lowest_hightime.end(); mapit++) {
348 if(mapit->first == lowest_dig) {
351 printf(
"0x%04x:\t %d \t %lf sec\n", mapit->first, mapit->second - lowtime,
352 static_cast<double>((static_cast<int64_t>(mapit->second - lowtime)) * (1<<28)) / 1.0E8);
355 ((
static_cast<uint64_t
>(mapit->second - lowtime)) * (1<<28));
357 printf(
"********************\n");
369 auto* roughlist =
new TList;
372 std::map<uint32_t, bool> keep_filling;
373 std::map<uint32_t, int>::iterator mapit;
377 new TH1D(Form(
"rough_0x%04x", mapit->first), Form(
"rough_0x%04x", mapit->first), 3E7, -3E8, 3E8);
379 roughlist->Add(roughhist);
380 keep_filling[mapit->first] =
true;
388 std::vector<TEventTime*>::iterator hit1;
389 std::vector<TEventTime*>::iterator hit2;
390 const int range = 5000;
392 for(hit1 = (eventQ->begin()); hit1 != eventQ->end(); hit1++) {
394 if(event1count % 75000 == 0) {
395 printf(
"Processing Event %d /%lu \r", event1count, eventQ->size());
400 if(((*hit1)->Digitizer() == 0) && ((*hit1)->DetectorType() > 1)) {
408 int64_t time1 =
static_cast<int64_t
>((*hit1)->GetTimeStamp());
410 if(event1count > range) {
413 hit2 = eventQ->begin();
417 while((hit2 != eventQ->end()) && (event2count < 2 * range)) {
419 if(((*hit2)->Digitizer() == 0) && ((*hit2)->DetectorType() > 1)) {
428 uint32_t digitizer = (*hit2)->Digitizer();
429 if(keep_filling[digitizer]) {
431 dynamic_cast<TH1D*
>(roughlist->At((*hit2)->DigIndex()));
432 int64_t time2 =
static_cast<int64_t
>((*hit2)->GetTimeStamp()) -
434 Int_t bin =
static_cast<Int_t
>(time2 - time1);
436 if((fillhist->FindBin(bin) > 0) && (fillhist->FindBin(bin) < fillhist->GetNbinsX())) {
448 fillhist =
dynamic_cast<TH1D*
>(roughlist->At(mapit->second));
449 std::cout<<static_cast<int64_t>(fillhist->GetBinCenter(fillhist->GetMaximumBin()))<<std::endl;
451 static_cast<int64_t
>(fillhist->GetBinCenter(fillhist->GetMaximumBin()));
454 printf(
"***** Rough time shifts *******\n");
455 std::map<uint32_t, int64_t>::iterator cit;
458 printf(
"0x%04x:\t BEST\n", cit->first);
461 printf(
"0x%04x:\t %lld\n", cit->first, cit->second);
463 printf(
"0x%04x:\t %ld\n", cit->first, cit->second);
467 printf(
"********************\n");
480 auto* list =
new TList;
483 std::map<uint32_t, int>::iterator mapit;
486 new TH1D(Form(
"timediff_0x%04x", mapit->first), Form(
"timediff_0x%04x", mapit->first), 1000, -500, 500);
496 std::vector<TEventTime*>::iterator hit1;
497 std::vector<TEventTime*>::iterator hit2;
499 const int range = 2000;
500 for(hit1 = eventQ->begin(); hit1 != eventQ->end(); hit1++) {
502 if(event1count % 75000 == 0) {
503 printf(
"Processing Event %d / %lu \r", event1count, eventQ->size());
509 if((*hit1)->Digitizer() == 0 && (*hit1)->DetectorType() > 1) {
517 int64_t time1 =
static_cast<int64_t
>((*hit1)->GetTimeStamp());
519 if(event1count > range) {
522 hit2 = eventQ->begin();
526 while((hit2 != eventQ->end()) && (event2count < range * 2)) {
529 if(((*hit2)->Digitizer() == 0) && ((*hit2)->DetectorType() > 1)) {
537 dynamic_cast<TH1D*
>(list->At((*hit2)->DigIndex()));
538 int64_t time2 =
static_cast<int64_t
>((*hit2)->GetTimeStamp()) -
540 if((time2 - time1 < 2147483647) &&
541 (time2 - time1 > -2147483647)) {
542 Int_t bin =
static_cast<Int_t
>(time2 - time1);
553 fillhist =
dynamic_cast<TH1D*
>(list->At(mapit->second));
554 auto* spec =
new TSpectrum();
555 spec->Search(fillhist);
556 double peak = spec->GetPositionX()[0];
557 std::cout<<static_cast<int64_t>(floor(peak + 0.5))<<std::endl;
562 printf(
"***** Rough time shifts *******\n");
563 std::map<uint32_t, int64_t>::iterator cit;
566 printf(
"0x%04x:\t BEST\n", cit->first);
569 printf(
"0x%04x:\t %lld\n", cit->first, cit->second);
571 printf(
"0x%04x:\t %ld\n", cit->first, cit->second);
576 printf(
"********************\n");
585 if(event->GetEventId() != 1) {
589 event->SetBankList();
595 int banksize =
event->LocateBank(
nullptr,
"GRF2", &ptr);
598 banksize =
event->LocateBank(
nullptr,
"GRF1", &ptr);
601 uint32_t type = 0xffffffff;
602 uint32_t value = 0xffffffff;
604 uint32_t dettype = 0;
605 uint32_t chanadd = 0;
607 unsigned int timelow = 0;
608 unsigned int timehigh = 0;
612 for(
int x = 0; x < banksize; x++) {
613 value = *(
reinterpret_cast<int*
>(ptr) + x);
614 type = value & 0xf0000000;
626 if((value & 0xf0000000) != 0x80000000) {
629 chanadd = (value & 0x0003fff0) >> 4;
630 dettype = (value & 0x0000000f);
642 if((value & 0xf0000000) != 0x80000000) {
645 chanadd = (value & 0x000ffff0) >> 4;
646 dettype = (value & 0x0000000f);
651 default: printf(
"This bank not yet defined.\n");
break;
654 case 0xa0000000: timelow = value & 0x0fffffff;
break;
655 case 0xb0000000: timehigh = value & 0x00003fff;
break;
661 time |= timelow & 0x0fffffff;
665 if(dettype > 1 && ((chanadd & 0xF) > 1) && ((chanadd & 0xF00) > 1) &&
SplitMezz) {
673 if(time > 0x3ffffffffff) {
674 time -= 0x3ffffffffff;
687 std::shared_ptr<TMidasEvent> copyevent = std::make_shared<TMidasEvent>(*event);
688 copyevent->SetBankList();
690 banksize = copyevent->LocateBank(
nullptr,
"GRF2", &ptr);
692 banksize = copyevent->LocateBank(
nullptr,
"GRF1", &ptr);
695 for(
int x = 0; x < banksize; x++) {
696 value = *(
reinterpret_cast<int*
>(ptr) + x);
697 type = value & 0xf0000000;
701 timelow = time & 0x0fffffff;
702 timelow += 0xa0000000;
703 *(
reinterpret_cast<int*
>(ptr) + x) = timelow;
706 timehigh = (time & 0x3fff0000000) >> 28;
707 int tempdead = value & 0xffffc000;
708 timehigh += tempdead;
709 *(
reinterpret_cast<int*
>(ptr) + x) = timehigh;
731 std::ifstream in(file->
GetFilename(), std::ifstream::in | std::ifstream::binary);
732 in.seekg(0, std::ifstream::end);
733 long long filesize = in.tellg();
737 long long bytesread = 0;
744 std::shared_ptr<TMidasEvent>
event = std::make_shared<TMidasEvent>();
747 bytes = file->
Read(event);
757 switch(event->GetEventId()) {
759 printf(
"start of run\n");
766 printf(
"end of run\n");
777 if(num_evt % 5000 == 0) {
778 gSystem->ProcessEvents();
781 num_evt, (bytesread / 1000000.0), (filesize / 1000000.0), (bytesread / 1000000.0) / w.RealTime());
794 sprintf(filename,
"corrections%05i.root", runnumber);
795 auto* corrfile =
new TFile(filename,
"RECREATE");
801 auto* t =
new TTree(
"correctiontree",
"Tree with map");
802 t->Branch(
"address", &address);
803 t->Branch(
"correction", &correction);
805 std::map<uint32_t, int64_t>::iterator it;
808 correction = it->second;
821 sprintf(filename,
"corrections%05i.root", runnumber);
822 auto* corrfile =
new TFile(filename,
"READ");
823 if(!(corrfile->IsOpen())) {
831 corrfile->GetObject(
"correctiontree", t);
832 TBranch* baddress =
nullptr;
833 TBranch* bcorrection =
nullptr;
836 t->SetBranchAddress(
"address", &address, &baddress);
837 t->SetBranchAddress(
"correction", &correction, &bcorrection);
840 printf(
"Digitizer \t Correction\n");
842 Long64_t tentry = t->LoadTree(i++);
846 baddress->GetEntry(tentry);
847 bcorrection->GetEntry(tentry);
848 printf(
"0x%04x: \t\t %lld\n", address, correction);
852 t->ResetBranchAddresses();
864 printf(
"Usage: ./offsetfix <input.mid> <output.mid> <y/n>(read correction file)\n");
868 printf(
"ERROR: Cannot overwrite midas file %s\n",
argv[1]);
873 int runnumber = midfile->GetRunNumber();
874 int subrunnumber = midfile->GetSubRunNumber();
876 midfile->OutOpen(Form(
"fixrun%05d_%03d.mid", runnumber, subrunnumber));
878 midfile->OutOpen(
argv[2]);
881 if(subrunnumber > -1) {
882 sprintf(filename,
"time_diffs%05i_%03i.root", runnumber, subrunnumber);
884 sprintf(filename,
"time_diffs%05i.root", runnumber);
886 printf(
"Creating root outfile: %s\n", filename);
890 if(strcmp(
argv[3],
"n") == 0) {
901 if(nDigitizers == 0) {
902 auto* outfile =
new TFile(filename,
"RECREATE");
903 auto* eventQ =
new std::vector<TEventTime*>;
912 midfile->Open(
argv[1]);
void GetRoughTimeDiff(std::vector< TEventTime *> *eventQ)
static std::map< uint32_t, bool > digset
TEventTime(std::shared_ptr< TMidasEvent > event)
unsigned long MidasTime()
const char * GetFilename() const override
Get the name of this file.
static std::map< uint32_t, int64_t > correctionmap
static void OrderDigitizerMap()
static std::map< int, int > digmap
void PrintError(const std::shared_ptr< TMidasEvent > &event, int frags, bool verb)
static std::map< uint32_t, int > digmap
int GetSubRunNumber() override
unsigned int TimeStampHigh()
static TGRSIOptions * Get(int argc=0, char **argv=nullptr)
Do not use!
static uint32_t GetBestDigitizer()
static unsigned long GetLowestMidasTime()
const char * GetLastError() const
Get error text for the last file error.
int QueueEvents(TMidasFile *infile, std::vector< TEventTime *> *eventQ)
int GriffinDataToFragment(uint32_t *data, int size, EBank bank, unsigned int midasSerialNumber=0, time_t midasTime=0)
static int64_t lowest_time
Reader for MIDAS .mid files.
bool ProcessEvent(const std::shared_ptr< TMidasEvent > &event, TMidasFile *outfile)
void WriteCorrectionFile(int runnumber)
static unsigned long low_timemidas
int main(int argc, char **argv)
static int GetBestDigitizer()
static uint64_t GetLowestTime()
void WriteEvents(TMidasFile *file)
void CheckHighTimeStamp(std::vector< TEventTime *> *eventQ)
void GetTimeDiff(std::vector< TEventTime *> *eventQ)
int Read(std::shared_ptr< TRawEvent > event) override
Read one event from the file.
void FillBuffer(const std::shared_ptr< TMidasEvent > &midasEvent, Option_t *opt="")
int CorrectionFile(int runnumber)
bool Open(const char *filename) override
Open input file.
int GetLastErrno() const
Get error value for the last file error.
bool SuppressErrors() const