2023-06-14 15:20:15 -04:00
|
|
|
#include "ClassData.h"
|
|
|
|
#include "MultiBuilder.h"
|
|
|
|
|
|
|
|
#include "TROOT.h"
|
|
|
|
#include "TSystem.h"
|
|
|
|
#include "TClonesArray.h"
|
|
|
|
#include "TGraph.h"
|
|
|
|
#include "TFile.h"
|
|
|
|
#include "TTree.h"
|
|
|
|
|
|
|
|
#define MAX_MULTI 100
|
|
|
|
|
|
|
|
template<typename T> void swap(T * a, T *b );
|
|
|
|
int partition(int arr[], int kaka[], TString file[], int start, int end);
|
|
|
|
void quickSort(int arr[], int kaka[], TString file[], int start, int end);
|
|
|
|
|
|
|
|
//^#############################################################
|
|
|
|
//^#############################################################
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
|
|
|
printf("=========================================\n");
|
2023-08-22 14:38:37 -04:00
|
|
|
printf("=== *.fsu Events Builder ===\n");
|
2023-06-14 15:20:15 -04:00
|
|
|
printf("=========================================\n");
|
|
|
|
if (argc <= 3) {
|
|
|
|
printf("Incorrect number of arguments:\n");
|
|
|
|
printf("%s [timeWindow] [traceOn/Off] [verbose] [inFile1] [inFile2] .... \n", argv[0]);
|
|
|
|
printf(" timeWindow : number of tick, 1 tick. default = 100 \n");
|
|
|
|
printf(" traceOn/Off : is traces stored \n");
|
|
|
|
printf(" verbose : > 0 for debug \n");
|
|
|
|
printf(" Output file name is contructed from inFile1 \n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// File format must be YYY...Y_runXXX_AAA_BBB_CCC.fsu
|
|
|
|
/// YYY...Y = prefix
|
|
|
|
/// XXX = runID, 3 digits
|
|
|
|
/// AAA = board Serial Number, 3 digits
|
|
|
|
/// BBB = DPPtype, 3 digits
|
|
|
|
/// CCC = over size index, 3 digits
|
|
|
|
|
|
|
|
///============= read input
|
|
|
|
unsigned int timeWindow = atoi(argv[1]);
|
|
|
|
bool traceOn = atoi(argv[2]);
|
|
|
|
unsigned int debug = atoi(argv[3]);
|
|
|
|
int nFile = argc - 4;
|
|
|
|
TString inFileName[nFile];
|
|
|
|
for( int i = 0 ; i < nFile ; i++){
|
|
|
|
inFileName[i] = argv[i+4];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Form outFileName;
|
|
|
|
TString outFileName = inFileName[0];
|
|
|
|
int pos = outFileName.Index("_");
|
|
|
|
pos = outFileName.Index("_", pos+1);
|
|
|
|
outFileName.Remove(pos);
|
|
|
|
outFileName += ".root";
|
|
|
|
printf("-------> Out file name : %s \n", outFileName.Data());
|
|
|
|
|
|
|
|
|
|
|
|
printf(" Number of Files : %d \n", nFile);
|
|
|
|
for( int i = 0; i < nFile; i++) printf("%2d | %s \n", i, inFileName[i].Data());
|
|
|
|
printf("=====================================\n");
|
|
|
|
printf(" Time Window = %u \n", timeWindow);
|
2023-08-24 13:27:16 -04:00
|
|
|
|
|
|
|
printf("===================================== input files:\n");
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
///============= sorting file by the serial number & order
|
|
|
|
int ID[nFile]; /// serial+ order*1000;
|
|
|
|
int type[nFile];
|
2023-08-24 13:27:16 -04:00
|
|
|
unsigned int fileSize[nFile];
|
2023-06-14 15:20:15 -04:00
|
|
|
for( int i = 0; i < nFile; i++){
|
|
|
|
int snPos = inFileName[i].Index("_"); // first "_"
|
|
|
|
//snPos = inFileName[i].Index("_", snPos + 1);
|
|
|
|
int sn = atoi(&inFileName[i][snPos+5]);
|
|
|
|
TString typeStr = &inFileName[i][snPos+9];
|
|
|
|
typeStr.Resize(3);
|
|
|
|
|
2023-10-05 13:19:45 -04:00
|
|
|
if( typeStr == "PHA" ) type[i] = DPPType::DPP_PHA_CODE;
|
|
|
|
if( typeStr == "PSD" ) type[i] = DPPType::DPP_PSD_CODE;
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
int order = atoi(&inFileName[i][snPos+13]);
|
|
|
|
ID[i] = sn + order * 1000;
|
|
|
|
|
2023-08-24 13:27:16 -04:00
|
|
|
FILE * temp = fopen(inFileName[i].Data(), "rb");
|
|
|
|
if( temp == NULL ) {
|
|
|
|
fileSize[i] = 0;
|
|
|
|
}else{
|
|
|
|
fseek(temp, 0, SEEK_END);
|
|
|
|
fileSize[i] = ftell(temp);
|
|
|
|
}
|
|
|
|
fclose(temp);
|
|
|
|
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
//printf("sn:%d, type:%d (%s), order:%d \n", sn, type[i], typeStr.Data(), order);
|
|
|
|
|
|
|
|
}
|
|
|
|
quickSort(&(ID[0]), &(type[0]), &(inFileName[0]), 0, nFile-1);
|
|
|
|
for( int i = 0 ; i < nFile; i++){
|
2023-09-06 13:24:38 -04:00
|
|
|
printf("%d | %6d | %3d | %s | %u Bytes = %.2f MB\n", i, ID[i], type[i], inFileName[i].Data(), fileSize[i], fileSize[i]/1024./1024.);
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//*======================================= Sort files in to group
|
|
|
|
std::vector<int> snList; // store the serial number of the group
|
|
|
|
std::vector<int> typeList; // store the DPP type of the group
|
|
|
|
std::vector<std::vector<TString>> fileList; // store the file list of the group
|
|
|
|
for( int i = 0; i < nFile; i++){
|
|
|
|
if( ID[i] / 1000 == 0 ) {
|
|
|
|
std::vector<TString> temp = {inFileName[i]};
|
|
|
|
fileList.push_back(temp);
|
|
|
|
typeList.push_back(type[i]);
|
|
|
|
snList.push_back(ID[i]%1000);
|
|
|
|
}else{
|
|
|
|
for( int p = 0; p < (int) snList.size(); p++){
|
|
|
|
if( (ID[i] % 1000) == snList[p] ) {
|
|
|
|
fileList[p].push_back(inFileName[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nGroup = snList.size();
|
2023-08-24 13:27:16 -04:00
|
|
|
printf("===================================== number of file Group by digitizer %d.\n", nGroup);
|
2023-06-14 15:20:15 -04:00
|
|
|
for( int i = 0; i < nGroup; i++){
|
2023-09-06 13:24:38 -04:00
|
|
|
printf("............ Digi-%d \n", snList[i]);
|
2023-06-14 15:20:15 -04:00
|
|
|
for( int j = 0; j< (int) fileList[i].size(); j++){
|
|
|
|
printf("%s | %d\n", fileList[i][j].Data(), typeList[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//*======================================= open raw files
|
|
|
|
printf("##############################################\n");
|
|
|
|
/// for each detector, open it
|
|
|
|
std::vector<int> inFileIndex(nGroup); // the index of the the opened file for each group
|
|
|
|
FILE ** inFile = new FILE *[nGroup];
|
|
|
|
Data ** data = new Data *[nGroup];
|
|
|
|
for( int i = 0; i < nGroup; i++){
|
|
|
|
inFile[i] = fopen(fileList[i][0], "r");
|
|
|
|
if( inFile[i] ){
|
|
|
|
inFileIndex[i] = 0;
|
|
|
|
data[i] = new Data();
|
|
|
|
data[i]->DPPType = typeList[0];
|
|
|
|
data[i]->boardSN = snList[0];
|
|
|
|
}else{
|
|
|
|
inFileIndex[i] = -1;
|
|
|
|
data[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//*====================================== create tree
|
|
|
|
TFile * outRootFile = new TFile(outFileName, "recreate");
|
|
|
|
TTree * tree = new TTree("tree", outFileName);
|
|
|
|
|
2023-06-15 11:48:18 -04:00
|
|
|
unsigned long long evID = -1;
|
2023-06-14 15:20:15 -04:00
|
|
|
unsigned short multi = 0;
|
2023-09-06 13:24:38 -04:00
|
|
|
unsigned short sn[MAX_MULTI] = {0}; /// board SN
|
2023-06-14 15:20:15 -04:00
|
|
|
unsigned short bd[MAX_MULTI] = {0}; /// boardID
|
|
|
|
unsigned short ch[MAX_MULTI] = {0}; /// chID
|
|
|
|
unsigned short e[MAX_MULTI] = {0}; /// 15 bit
|
|
|
|
unsigned short e2[MAX_MULTI] = {0}; /// 15 bit
|
|
|
|
unsigned long long e_t[MAX_MULTI] = {0}; /// timestamp 47 bit
|
|
|
|
unsigned short e_f[MAX_MULTI] = {0}; /// fine time 10 bit
|
|
|
|
|
|
|
|
tree->Branch("evID", &evID, "event_ID/l");
|
|
|
|
tree->Branch("multi", &multi, "multi/s");
|
2023-09-06 13:24:38 -04:00
|
|
|
tree->Branch("sn", sn, "sn[multi]/s");
|
2023-06-14 15:20:15 -04:00
|
|
|
tree->Branch("bd", bd, "bd[multi]/s");
|
|
|
|
tree->Branch("ch", ch, "ch[multi]/s");
|
|
|
|
tree->Branch("e", e, "e[multi]/s");
|
|
|
|
tree->Branch("e2", e2, "e2[multi]/s");
|
|
|
|
tree->Branch("e_t", e_t, "e_timestamp[multi]/l");
|
|
|
|
tree->Branch("e_f", e_f, "e_timestamp[multi]/s");
|
|
|
|
|
|
|
|
TClonesArray * arrayTrace = nullptr;
|
|
|
|
unsigned short traceLength[MAX_MULTI] = {0};
|
|
|
|
TGraph * trace = nullptr;
|
|
|
|
|
|
|
|
if( traceOn ) {
|
|
|
|
arrayTrace = new TClonesArray("TGraph");
|
|
|
|
tree->Branch("traceLength", traceLength, "traceLength[multi]/s");
|
|
|
|
tree->Branch("trace", arrayTrace, 2560000);
|
|
|
|
arrayTrace->BypassStreamer();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//*====================================== build events
|
2023-08-24 13:27:16 -04:00
|
|
|
printf("================= Building events....\n");
|
2023-06-19 12:48:17 -04:00
|
|
|
MultiBuilder * mb = new MultiBuilder(data, typeList, snList);
|
2023-06-14 15:20:15 -04:00
|
|
|
mb->SetTimeWindow(timeWindow);
|
|
|
|
|
2023-09-06 09:59:06 -04:00
|
|
|
///------------------ read data
|
2023-06-14 15:20:15 -04:00
|
|
|
char * buffer = nullptr;
|
|
|
|
unsigned int word[1]; // 4 byte = 32 bit
|
|
|
|
|
2023-09-06 09:59:06 -04:00
|
|
|
int lastDataIndex[nGroup][MaxNChannels]={0}; // keep track of the DataIndex
|
|
|
|
int lastLoopIndex[nGroup][MaxNChannels]={0}; // keep track of the DataIndex
|
2023-08-24 13:27:16 -04:00
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
int aggCount[nGroup] = {0};
|
2023-06-14 15:20:15 -04:00
|
|
|
do{
|
|
|
|
|
|
|
|
/// fill the data class with some agg;
|
2023-09-06 09:59:06 -04:00
|
|
|
bool breakFlag = false;
|
2023-06-14 15:20:15 -04:00
|
|
|
do{
|
|
|
|
|
2023-09-06 09:59:06 -04:00
|
|
|
// Get 1 agg. from each file.
|
2023-06-14 15:20:15 -04:00
|
|
|
for ( int i = 0; i < nGroup; i++){
|
|
|
|
if( inFile[i] == nullptr ) continue;
|
|
|
|
size_t dummy = fread(word, 4, 1, inFile[i]);
|
2023-08-24 13:27:16 -04:00
|
|
|
if( dummy != 1) {
|
2023-09-06 13:24:38 -04:00
|
|
|
//printf("fread error, should read 4 bytes, but read %ld x 4 byte, file pos: %ld byte (%s)\n", dummy, ftell(inFile[i]), fileList[i][inFileIndex[i]].Data());
|
|
|
|
// go to next file in same digitizer
|
|
|
|
if( feof(inFile[i])){
|
|
|
|
fclose(inFile[i]);
|
|
|
|
inFile[i] = fopen(fileList[i][inFileIndex[i]+1], "r");
|
|
|
|
if( inFile[i] ){
|
|
|
|
inFileIndex[i]++;
|
|
|
|
printf("---- go to next file for digi-%d\n", snList[i]);
|
|
|
|
}else{
|
|
|
|
inFileIndex[i] = -1;
|
|
|
|
printf("---- no more file for digi-%d.\n", snList[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
}else{
|
|
|
|
fseek(inFile[i], -4, SEEK_CUR); // rool back
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
short header = ((word[0] >> 28 ) & 0xF);
|
|
|
|
if( header != 0xA ) break;
|
|
|
|
unsigned int aggSize = (word[0] & 0x0FFFFFFF) * 4; ///byte
|
|
|
|
|
|
|
|
buffer = new char[aggSize];
|
|
|
|
size_t dummy2 = fread(buffer, aggSize, 1, inFile[i]);
|
|
|
|
if( dummy2 != 1) {
|
|
|
|
printf("fread error, should read %d bytes, but read %ld x %d byte, file pos: %ld byte (%s)\n", aggSize, dummy, aggSize, ftell(inFile[i]), fileList[i][inFileIndex[i]].Data());
|
|
|
|
}else{
|
|
|
|
data[i]->DecodeBuffer(buffer, aggSize, false, 0);
|
|
|
|
data[i]->ClearBuffer();
|
|
|
|
|
|
|
|
aggCount[i] ++;
|
|
|
|
}
|
2023-09-06 09:59:06 -04:00
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
//if all file exhausted, break
|
|
|
|
int okFileNum = 0;
|
|
|
|
for( int i = 0; i < nGroup; i++){
|
|
|
|
if( inFileIndex[i] != -1 ) okFileNum ++;
|
|
|
|
}
|
|
|
|
if( okFileNum == 0 ) break;
|
|
|
|
|
|
|
|
//check if Data Index near MaxNData. if near by 50%, break
|
|
|
|
//printf("-----------------------------------\n");
|
|
|
|
for( int i = 0; i < nGroup; i++){
|
2023-09-06 09:59:06 -04:00
|
|
|
for( int ch = 0; ch < MaxNChannels; ch ++){
|
|
|
|
if( data[i]->LoopIndex[ch]*MaxNData + data[i]->DataIndex[ch] - lastLoopIndex[i][ch]*MaxNData - lastDataIndex[i][ch] > MaxNData * 0.5 ) {
|
|
|
|
breakFlag = true;
|
2023-09-06 13:24:38 -04:00
|
|
|
//printf("digi:%d | ch: %d DataIndex: %d (%d) \n", data[i]->boardSN, ch, data[i]->DataIndex[ch], lastDataIndex[i][ch]);
|
2023-08-24 13:27:16 -04:00
|
|
|
}
|
2023-09-06 09:59:06 -04:00
|
|
|
lastDataIndex[i][ch] = data[i]->DataIndex[ch];
|
|
|
|
lastLoopIndex[i][ch] = data[i]->LoopIndex[ch];
|
2023-08-24 13:27:16 -04:00
|
|
|
}
|
2023-09-06 13:24:38 -04:00
|
|
|
//printf("%3d | agg : %d | %s\n", snList[i], aggCount[i], breakFlag ? "Break" : "cont." );
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
2023-09-06 09:59:06 -04:00
|
|
|
}while(breakFlag);
|
2023-06-14 15:20:15 -04:00
|
|
|
|
|
|
|
mb->BuildEvents(0, 0, debug);
|
|
|
|
|
|
|
|
///----------- save to tree;
|
|
|
|
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
|
2023-09-06 13:24:38 -04:00
|
|
|
//printf("startIndex : %6ld -> %6ld, %6ld, %6ld, %ld | %llu\n", startIndex, startIndex < 0 ? startIndex + MaxNEvent : startIndex, mb->eventIndex, mb->eventBuilt, mb->totalEventBuilt, tree->GetEntries());
|
2023-09-06 09:59:06 -04:00
|
|
|
if (startIndex < 0 ) startIndex += MaxNEvent;
|
2023-09-06 13:24:38 -04:00
|
|
|
for( long p = startIndex; p < startIndex + mb->eventBuilt; p++){
|
|
|
|
int k = p % MaxNEvent;
|
|
|
|
multi = mb->events[k].size();
|
|
|
|
if( multi > MAX_MULTI) {
|
|
|
|
printf("!!!!! MAX_MULTI %d reached.\n", MAX_MULTI);
|
|
|
|
break;
|
|
|
|
}
|
2023-08-24 13:27:16 -04:00
|
|
|
evID ++;
|
2023-06-14 15:20:15 -04:00
|
|
|
for( int j = 0; j < multi; j ++){
|
2023-09-06 13:24:38 -04:00
|
|
|
bd[j] = mb->events[k][j].bd;
|
|
|
|
sn[j] = mb->events[k][j].sn;
|
|
|
|
ch[j] = mb->events[k][j].ch;
|
|
|
|
e[j] = mb->events[k][j].energy;
|
|
|
|
e2[j] = mb->events[k][j].energy2;
|
|
|
|
e_t[j] = mb->events[k][j].timestamp;
|
|
|
|
e_f[j] = mb->events[k][j].fineTime;
|
2023-06-14 15:20:15 -04:00
|
|
|
if( traceOn ){
|
2023-09-06 13:24:38 -04:00
|
|
|
traceLength[j] = mb->events[k][j].trace.size();
|
2023-06-14 15:20:15 -04:00
|
|
|
trace = (TGraph *) arrayTrace->ConstructedAt(j, "C");
|
|
|
|
trace->Clear();
|
|
|
|
for( int hh = 0; hh < traceLength[j]; hh++){
|
2023-09-06 13:24:38 -04:00
|
|
|
trace->SetPoint(hh, hh, mb->events[k][j].trace[hh]);
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outRootFile->cd();
|
|
|
|
tree->Fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
int okFileNum = 0;
|
|
|
|
for( int i = 0; i < nGroup; i++){
|
|
|
|
if( inFileIndex[i] != -1 ) okFileNum ++;
|
|
|
|
}
|
|
|
|
if( okFileNum == 0 ) break;
|
|
|
|
|
|
|
|
}while(true);
|
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
if( timeWindow > 0 ){
|
|
|
|
printf("------------------- build the last data\n");
|
|
|
|
|
|
|
|
mb->BuildEvents(1, 0, debug);
|
|
|
|
|
|
|
|
///----------- save to tree;
|
|
|
|
long startIndex = mb->eventIndex - mb->eventBuilt + 1;
|
|
|
|
//printf("startIndex : %ld -> %ld, %ld, %ld, %ld\n", startIndex, startIndex < 0 ? startIndex + MaxNEvent : startIndex, mb->eventIndex, mb->eventBuilt, mb->totalEventBuilt);
|
|
|
|
if( startIndex < 0 ) startIndex += MaxNEvent;
|
|
|
|
for( long p = startIndex; p <= startIndex + mb->eventBuilt; p++){
|
|
|
|
int k = p % MaxNEvent;
|
|
|
|
multi = mb->events[k].size();
|
|
|
|
if( multi > MAX_MULTI) {
|
|
|
|
printf("!!!!! MAX_MULTI %d reached.\n", MAX_MULTI);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
evID ++;
|
|
|
|
for( int j = 0; j < multi; j ++){
|
|
|
|
bd[j] = mb->events[k][j].bd;
|
|
|
|
sn[j] = mb->events[k][j].sn;
|
|
|
|
ch[j] = mb->events[k][j].ch;
|
|
|
|
e[j] = mb->events[k][j].energy;
|
|
|
|
e2[j] = mb->events[k][j].energy2;
|
|
|
|
e_t[j] = mb->events[k][j].timestamp;
|
|
|
|
e_f[j] = mb->events[k][j].fineTime;
|
|
|
|
if( traceOn ){
|
|
|
|
traceLength[j] = mb->events[k][j].trace.size();
|
|
|
|
trace = (TGraph *) arrayTrace->ConstructedAt(j, "C");
|
|
|
|
trace->Clear();
|
|
|
|
for( int hh = 0; hh < traceLength[j]; hh++){
|
|
|
|
trace->SetPoint(hh, hh, mb->events[k][j].trace[hh]);
|
|
|
|
}
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 13:24:38 -04:00
|
|
|
outRootFile->cd();
|
|
|
|
tree->Fill();
|
2023-06-14 15:20:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tree->Write();
|
|
|
|
|
|
|
|
printf("========================= finsihed.\n");
|
2023-09-06 13:24:38 -04:00
|
|
|
printf("total events built = %llu(%llu)\n", evID + 1, tree->GetEntriesFast());
|
2023-06-14 15:20:15 -04:00
|
|
|
printf("=======> saved to %s \n", outFileName.Data());
|
|
|
|
|
2023-09-06 13:24:38 -04:00
|
|
|
outRootFile->Close();
|
|
|
|
|
2023-06-14 15:20:15 -04:00
|
|
|
delete mb;
|
2023-09-06 09:59:06 -04:00
|
|
|
for( int i = 0 ; i < nGroup; i++) delete data[i];
|
2023-06-14 15:20:15 -04:00
|
|
|
delete [] data;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//^#############################################################
|
|
|
|
//^#############################################################
|
|
|
|
template<typename T> void swap(T * a, T *b ){
|
|
|
|
T temp = * b;
|
|
|
|
*b = *a;
|
|
|
|
*a = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int partition(int arr[], int kaka[], TString file[], int start, int end){
|
|
|
|
int pivot = arr[start];
|
|
|
|
int count = 0;
|
|
|
|
for (int i = start + 1; i <= end; i++) {
|
|
|
|
if (arr[i] <= pivot) count++;
|
|
|
|
}
|
|
|
|
/// Giving pivot element its correct position
|
|
|
|
int pivotIndex = start + count;
|
|
|
|
swap(&arr[pivotIndex], &arr[start]);
|
|
|
|
swap(&file[pivotIndex], &file[start]);
|
|
|
|
swap(&kaka[pivotIndex], &kaka[start]);
|
|
|
|
|
|
|
|
/// Sorting left and right parts of the pivot element
|
|
|
|
int i = start, j = end;
|
|
|
|
while (i < pivotIndex && j > pivotIndex) {
|
|
|
|
while (arr[i] <= pivot) {i++;}
|
|
|
|
while (arr[j] > pivot) {j--;}
|
|
|
|
if (i < pivotIndex && j > pivotIndex) {
|
|
|
|
int ip = i++;
|
|
|
|
int jm = j--;
|
|
|
|
swap( &arr[ip], &arr[jm]);
|
|
|
|
swap(&file[ip], &file[jm]);
|
|
|
|
swap(&kaka[ip], &kaka[jm]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pivotIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void quickSort(int arr[], int kaka[], TString file[], int start, int end){
|
|
|
|
/// base case
|
|
|
|
if (start >= end) return;
|
|
|
|
/// partitioning the array
|
|
|
|
int p = partition(arr, kaka, file, start, end);
|
|
|
|
/// Sorting the left part
|
|
|
|
quickSort(arr, kaka, file, start, p - 1);
|
|
|
|
/// Sorting the right part
|
|
|
|
quickSort(arr, kaka, file, p + 1, end);
|
|
|
|
}
|