#include #include #include #include #include #include #include #include #include #include #include #include #include #include "TText.h" #include "TLatex.h" #include "TPaveStats.h" #include "TVirtualPad.h" #include "TFrame.h" #include "TGraphPainter.h" #include "TString.h" #include "TCollection.h" #include "TCanvas.h" #include "TFile.h" #include "TH1F.h" #include "TH2F.h" #include "TF1.h" #include "TGraph.h" #include "TGraphErrors.h" #include "TMinuit.h" #include "TKey.h" #include "TDatime.h" #include "TMath.h" #include "TAxis.h" #include "TLine.h" #include "TTree.h" #include "TBranch.h" #include "TH2F.h" #include "TColor.h" #include "TStyle.h" #include "TPaletteAxis.h" #include "TStopwatch.h" #include "TSystem.h" #include "TObjString.h" #include "TObjArray.h" #include "TList.h" vector ID_Vec(TList* list); vector ID_Vec_Short(); struct Base{ Base() {} Base(TString Name, TString Date) : name(Name), date(Date) {} TString name; TString type; TString date; TString absNum; double timing; double hv; double hv_set; double current; double temperature; double dynode; double mvb; double mvt; double dac; }; struct sort_Base_ASC { bool operator()(const Base &left, const Base &right) { return left.absNum < right.absNum; } }; // Convert int to TString TString int2TString(int number, int ndigits=0){ TString snumber(""); snumber += number; TString newsnumber(""); for (int i = snumber.Length(); i < ndigits; i++){ newsnumber += "0"; } newsnumber += snumber; return newsnumber; } // ndates and nlines std::pair CountDatesInFile(string fileName){ int ndates = 0; int nlines = 0; string line; ifstream myfile; myfile.open(fileName.c_str()); if (myfile.is_open()) { while( getline (myfile,line) ) { nlines++; if (line.compare(0,5,"Time:")==0){ ndates++; } } } TString t_dates = int2TString(ndates); std::pair nlines_pair; nlines_pair = std::make_pair(t_dates.Length(),nlines); return nlines_pair; } // date = Thu Jul 24 12:41:54 2014 double DateToHours(TString date){ TObjArray* tokens = date.Tokenize(" "); TString Time = ((TObjString*) tokens->At(3))->GetString().Remove(TString::kBoth, ' '); TObjArray* time_tokens = Time.Tokenize(":"); double hour = ((TObjString*) time_tokens->At(0))->GetString().Remove(TString::kBoth, ' ').Atof(); double minute = ((TObjString*) time_tokens->At(1))->GetString().Remove(TString::kBoth, ' ').Atof(); double second = ((TObjString*) time_tokens->At(2))->GetString().Remove(TString::kBoth, ' ').Atof(); delete tokens; delete time_tokens; return (hour + minute/60.0 + second/3600.0); } void GenRoot(TString inputFile){ // Count the number of lines in the text file std::pair nlines_pair = CountDatesInFile(string(inputFile)); int ndigits = nlines_pair.first; int nlines = nlines_pair.second; // vector of structs std::vector base_vec; TString date_array[2]; int counter = 0; int time_counter = 0; TString inputFileLong = gSystem->ExpandPathName(inputFile.Data()); cout << "Opening file: " << inputFileLong.Data() << endl; ifstream filestream(inputFileLong.Data(), ifstream::in); if (!filestream) { std::cout << "ERROR: Failed to open data file " << std::endl; return; } TFile* outFile = TFile::Open(inputFile.Append(".root"),"RECREATE"); TString line; TStopwatch watch; watch.Start(); while( line.ReadLine(filestream) ){ counter++; if(counter %100000 == 0){ cout << "Line: " << counter << endl; } if (line.Contains("Anagate:")) continue; if (line.Contains("Extra:")) continue; // if (line.Contains("7555535")) continue; bool new_time = false; if (counter==nlines) new_time = true; if (line.Contains("Time:")){ line.Remove(0,6); if (counter==1) date_array[0] = line; if (counter!=1) { new_time = true; date_array[1] = line; } } else{ TString base_id = ""; TString type = ""; TString data = ""; // Loop tabs in a line TObjArray* tokens = line.Tokenize("\t"); for (int i=0; iGetEntries(); i++){ TString s_value = ((TObjString*) tokens->At(i))->GetString().Remove(TString::kBoth, ' '); // Get CAN ID , ADC Type , Value if ( !line.Contains("Time:") ){ if (i==0) base_id = s_value; if (i==1) type = s_value; if (i==2) data = s_value; } } // Timeout if (base_id.Atoi()==0){ } if (base_id.Atoi()!=0){ Base my_channel(base_id, date_array[0]); my_channel.absNum = base_id + "_"+int2TString(time_counter+1,ndigits); my_channel.timing = DateToHours(my_channel.date); // Find if a crate slot channel has already been added to this list int baseSize = base_vec.size(); bool found = false; if (type!="CAT"){ for (int i=0; i 10000 || data.Atof()<0) cout << "LARGE OR NEGATIVE VALUE:" << data.Atof() << " RAW:" << data << endl; if (type=="CAT") base_vec[baseSize-i-1].hv = data.Atof(); if (type=="CUR") base_vec[baseSize-i-1].current = data.Atof()*1000; // convert Amps to milliAmps if (type=="TEM") base_vec[baseSize-i-1].temperature = data.Atof(); if (type=="DYN") base_vec[baseSize-i-1].dynode = data.Atof(); if (type=="MVB") base_vec[baseSize-i-1].mvb = data.Atof(); if (type=="MVT") base_vec[baseSize-i-1].mvt = data.Atof(); if (type=="DAC") base_vec[baseSize-i-1].dac = data.Atof(); found = true; break; } } } if (!found){ if(data.Atof() > 10000 || data.Atof()<0) cout << "LARGE OR NEGATIVE VALUE:" << data.Atof() << " RAW:" << data << endl; if (type=="CAT") my_channel.hv = data.Atof(); if (type=="CUR") my_channel.current = data.Atof()*1000; // convert Amps to milliAmps if (type=="TEM") my_channel.temperature = data.Atof(); if (type=="DYN") my_channel.dynode = data.Atof(); if (type=="MVB") my_channel.mvb = data.Atof(); if (type=="MVT") my_channel.mvt = data.Atof(); if (type=="DAC") my_channel.dac = data.Atof(); base_vec.push_back(my_channel); } } delete tokens; } if (new_time){ time_counter++; date_array[0] = date_array[1]; } } // end while loop over file filestream.close(); watch.Stop(); cout << "Reading file took:" << endl; watch.Print(); TTree* tr = 0x0; cout << "Sorting..." << endl; std::sort(base_vec.begin(),base_vec.end(),sort_Base_ASC()); int nBases = base_vec.size(); double SET_TIME = 0; for (int i=0; i0){ Base my_prev_channel = base_vec[i-1]; if (my_channel.name!=my_prev_channel.name){ create_ttree = true; } else if (my_channel.name==my_prev_channel.name){ my_time = my_channel.timing - my_prev_channel.timing; if (my_time<0) my_time += 24; SET_TIME += my_time; } } if (create_ttree){ cout << "Creating TTree: " << "CANID_" << name << endl; tr = new TTree("CANID_"+name,"CANID_"+name); // Create Branches tr->Branch("date",&date_char,"date/C"); tr->Branch("time",&empty_channel.timing,"Hours/D"); tr->Branch("vmon",&empty_channel.hv,"Volts/D"); tr->Branch("vmon_set",&empty_channel.hv_set,"Volts/D"); tr->Branch("dyn",&empty_channel.dynode,"Volts/D"); tr->Branch("imon",&empty_channel.current,"milliAmps/D"); tr->Branch("temp",&empty_channel.temperature,"Celsius/D"); tr->Branch("mvt",&empty_channel.mvt,"Volts/D"); tr->Branch("mvb",&empty_channel.mvb,"Volts/D"); tr->Branch("dac",&empty_channel.dac,"Volts/D"); SET_TIME = 0; } // Fill branches string temp = string(my_channel.date); sprintf(date_char,"%s",temp.c_str()); empty_channel.timing = SET_TIME; //cout << name << "\t" << date << "\t" << SET_TIME << endl; empty_channel.hv = my_channel.hv; empty_channel.dynode = my_channel.dynode; empty_channel.current = my_channel.current; empty_channel.temperature = my_channel.temperature; empty_channel.mvt = my_channel.mvt; empty_channel.mvb = my_channel.mvb; empty_channel.dac = my_channel.dac; tr->Fill(); // Close TTree bool close_ttree = false; if (i+1!=nBases){ Base my_next_channel = base_vec[i+1]; if (my_channel.name!=my_next_channel.name){ close_ttree = true; } } else if(i+1==nBases) close_ttree = true; if (close_ttree) tr->Write(); } cout << "Finished!!" << endl; outFile->Close(); } // This will read a root file and plot some variable vs time for all TTrees void IU_BaseToRoot_openTTree(TString inputRootFile, TString type = "vmon/imon/temp/dyn/mvb/mvt/dac"){ TFile* f = new TFile(inputRootFile); TList* list = f->GetListOfKeys(); int nList = list->GetEntries(); cout << "There are " << nList << " TTrees" << endl; for (int i=0; iAt(i))->GetString(); TTree* tr = (TTree*)f->Get(tree_name); tr->SetBranchAddress(type,&base_value); tr->SetBranchAddress("time",&timing); TGraph* gr = new TGraph(); gr->SetName(tree_name); gr->SetTitle(tree_name); int nEntries = tr->GetEntries(); for (int j=0; jGetEntry(j); gr->SetPoint(j,timing,base_value); } // Below the draw option is commented out since it will draw 2800 TGraph objects in my case // But the part that read an arbitrary ttree is useful //gr->Draw("AP"); } } void GenPlotsSingle(TString filename,TString plotvar, TString longorshort="long") { TFile *f = TFile::Open(filename); vector ID_vec; if(longorshort=="long") { TList* list = f->GetListOfKeys(); ID_vec = ID_Vec(list);} if(longorshort=="short") ID_vec = ID_Vec_Short(); TCanvas* c1D = new TCanvas("c1D","c1D",1200,800); TGraph* mygraph; TH1F* MeanHist = new TH1F("Mean", "Mean Values For Each Base",56,1570,1615); TString plotdir; if(longorshort=="long") { plotdir = "./plots/"+plotvar+"/"; } TString y_axis_title; if(plotvar=="vmon") y_axis_title="Base High Voltage"; if(plotvar=="dyn") y_axis_title="Dynode"; if(plotvar=="imon") y_axis_title="Current"; if(plotvar=="temp") y_axis_title="Temperature"; if(plotvar=="mvt") y_axis_title="Medium Voltage Top"; if(plotvar=="mvb") y_axis_title="Medium Voltage Bottom"; if(plotvar=="dac") y_axis_title="DAC value"; for(uint i = 0; i < ID_vec.size(); ++i) { if(f->GetListOfKeys()->Contains("CANID_"+ID_vec[i]) || f->GetListOfKeys()->Contains(ID_vec[i])) { if(longorshort=="short") plotdir = "./plots/"+plotvar+"/"; cout << i+1 << endl; TString treename; if(longorshort == "short") treename = "CANID_"+ID_vec[i]; if(longorshort =="long") treename = ID_vec[i]; TTree *tr = (TTree *)f->Get(treename); Long64_t nentries = tr->GetEntries(); Char_t mydate[100]; double mytime; Double_t myvmon; double mydyn; double myimon; double mytemp; double mymvt; double mymvb; double mydac; tr->SetBranchAddress("date",&mydate); tr->SetBranchAddress("time",&mytime); tr->SetBranchAddress("vmon",&myvmon); tr->SetBranchAddress("dyn",&mydyn); tr->SetBranchAddress("imon",&myimon); tr->SetBranchAddress("temp",&mytemp); tr->SetBranchAddress("mvt",&mymvt); tr->SetBranchAddress("mvb",&mymvb); tr->SetBranchAddress("dac",&mydac); Double_t y_arr[nentries]; Double_t time_arr[nentries]; for(int ii = 0; ii < nentries; ++ii) { tr->GetEntry(ii); if(plotvar=="vmon") y_arr[ii]=myvmon; if(plotvar=="dyn") y_arr[ii]=mydyn; if(plotvar=="imon") y_arr[ii]=myimon; if(plotvar=="temp") y_arr[ii]=mytemp; if(plotvar=="mvt") y_arr[ii]=mymvt; if(plotvar=="mvb") y_arr[ii]=mymvb; if(plotvar=="dac") y_arr[ii]=mydac; time_arr[ii] = mytime; } mygraph = new TGraph(nentries,time_arr,y_arr); if(longorshort=="short") mygraph->SetTitle("Can ID "+ID_vec[i]); if(longorshort=="long") mygraph->SetTitle(ID_vec[i]); mygraph->GetXaxis()->SetTitle("Time (hours)"); mygraph->GetYaxis()->SetTitle(y_axis_title); mygraph->SetMarkerStyle(7); mygraph->Draw("AP"); MeanHist->Fill(TMath::Mean(nentries,y_arr)); if(longorshort=="long") c1D->SaveAs(plotdir+filename+ID_vec[i]+plotvar+".png"); if(longorshort=="short") c1D->SaveAs(plotdir+filename+ID_vec[i]+plotvar+".png"); } } } void GenPlotsCombined(TString filename, int mode=0) { // mode 0: plot only bad bases // mode 1: plot all bases TFile *f = TFile::Open(filename); TList* list = f->GetListOfKeys(); vector ID_vec; ID_vec = ID_Vec(list); const uint numbase = ID_vec.size(); vector bad_CANs; TCanvas* cancomb[numbase]; TString plotdir = "./plots/all/"; const uint numplots = 6; TString vals[numplots] = {"vmon","dyn","imon","temp","mvt","mvb"}; TString ytitles[numplots] = {"Base High Voltage","Dynode","Current","Temperature","Medium Voltage Top","Medium Voltage Bottom"}; TGraph* graphs[numplots]; const float vmin_thresh = 900; const float vmean_thresh = 1784; const float imax_thresh = 14.9; // Loop over all the CAN IDs // for(uint ii = 0; ii < 2; ++ii) { for(uint ii = 0; ii < numbase; ++ii) { if(f->GetListOfKeys()->Contains("CANID_"+ID_vec[ii]) || f->GetListOfKeys()->Contains(ID_vec[ii])) { // cout << ii+1 << " " << ID_vec[ii] << endl; TString treename; treename = ID_vec[ii]; TTree *tr = (TTree *)f->Get(treename); const Long64_t nentries = tr->GetEntries(); Double_t gen_arr[numplots][nentries]; Double_t time_arr[nentries]; Char_t mydate[100]; double mytime; Double_t myvmon; double mydyn; double myimon; double mytemp; double mymvt; double mymvb; double mydac; tr->SetBranchAddress("date",&mydate); tr->SetBranchAddress("time",&mytime); tr->SetBranchAddress("vmon",&myvmon); tr->SetBranchAddress("dyn",&mydyn); tr->SetBranchAddress("imon",&myimon); tr->SetBranchAddress("temp",&mytemp); tr->SetBranchAddress("mvt",&mymvt); tr->SetBranchAddress("mvb",&mymvb); tr->SetBranchAddress("dac",&mydac); float sum_vmon=0; float sum_imon=0; float max_imon=0; float min_vmon=0; // make arrays for TGraphs for(int ii = 0; ii < nentries; ++ii) { tr->GetEntry(ii); gen_arr[0][ii]=myvmon; gen_arr[1][ii]=mydyn; gen_arr[2][ii]=myimon; gen_arr[3][ii]=mytemp; gen_arr[4][ii]=mymvt; gen_arr[5][ii]=mymvb; time_arr[ii] = mytime; if (max_imon < myimon) max_imon=myimon; if (vmin_thresh > myvmon) min_vmon++; sum_vmon+=myvmon; sum_imon+=myimon; } float mean_vmon = sum_vmon/nentries; float mean_imon = sum_imon/nentries; cout << ii+1 << " " << ID_vec[ii] << " mean V = " << mean_vmon << " min V = " << min_vmon << " mean I = " << mean_imon << " max I = " << max_imon << endl; bool basebad = false; if (mean_vmon < vmean_thresh || max_imon > imax_thresh) { basebad = true; bad_CANs.push_back(ID_vec[ii]); cout << endl; } if (basebad || mode==1) { cancomb[ii] = new TCanvas(ID_vec[ii],ID_vec[ii],1400,800); cancomb[ii]->Divide(3,2,0.001,0.001); for (uint jj=0; jjcd(jj+1); // TString drawstring = vals[jj]+":time"; // tr->Draw(drawstring); // graphs[jj] = (TGraph*)gPad->GetPrimitive("Graph"); graphs[jj] = new TGraph(nentries,time_arr,gen_arr[jj]); TString title = ID_vec[ii]+" "+vals[jj]; graphs[jj]->SetTitle(title); if (jj==0) graphs[jj]->SetMinimum(0); graphs[jj]->GetXaxis()->SetTitle("Time (hours)"); graphs[jj]->GetYaxis()->SetTitle(ytitles[jj]); graphs[jj]->SetMarkerStyle(7); graphs[jj]->Draw("AP"); } cancomb[ii]->SaveAs(plotdir+ID_vec[ii]+".png"); } } } for (uint ii=0; ii ID_Vec_Short() { vector temp_vec; temp_vec.push_back("7479533"); temp_vec.push_back("7515389"); temp_vec.push_back("7487360"); temp_vec.push_back("7530588"); return temp_vec; } vector ID_Vec(TList* list) { vector temp_vec; int list_size = list->GetSize(); for(int i = 0; i < list_size; ++i) { temp_vec.push_back(list->At(i)->GetTitle()); } return temp_vec; }