TEST(PacBioIndexTest, CreateOnTheFly)
{
    // do this in temp directory, so we can ensure write access
    const string tempDir    = tests::GeneratedData_Dir + "/";
    const string tempBamFn  = tempDir + "temp.bam";
    const string tempPbiFn  = tempBamFn + ".pbi";

    // NOTE: new file differs in size than existing (different write parameters may yield different file sizes, even though content is same)
    const vector<int64_t> expectedNewOffsets = { 33816576, 236126208, 391315456, 469106688, 537067520, 587792384, 867303424, 1182793728, 1449787392, 1582628864 };
    vector<int64_t> observedOffsets;

    // create PBI on the fly from input BAM while we write to new file
    {
        BamFile bamFile(test2BamFn);
        BamHeader header = bamFile.Header();

        BamWriter writer(tempBamFn, header); // default compression, default thread count
        PbiBuilder builder(tempPbiFn, header.Sequences().size());

        int64_t vOffset = 0;
        EntireFileQuery entireFile(bamFile);
        for (const BamRecord& record : entireFile) {
            writer.Write(record, &vOffset);
            builder.AddRecord(record, vOffset);
            observedOffsets.push_back(vOffset);
        }
    }

    EXPECT_EQ(expectedNewOffsets, observedOffsets);

    // sanity check on original file
    {
        const vector<int64_t> originalFileOffsets = { 33816576, 33825163, 33831333, 33834264, 33836542, 33838065, 33849818, 33863499, 33874621, 1392836608 };
        BamRecord r;
        BamReader reader(test2BamFn);
        for (int i = 0; i < originalFileOffsets.size(); ++i) {
            reader.VirtualSeek(originalFileOffsets.at(i));
            EXPECT_TRUE(CanRead(reader, r, i));
        }
    }

    // attempt to seek in our new file using both expected & observed offsets
    {
        BamRecord r;
        BamReader reader(tempBamFn);
        for (int i = 0; i < expectedNewOffsets.size(); ++i) {
            reader.VirtualSeek(expectedNewOffsets.at(i));
            EXPECT_TRUE(CanRead(reader, r, i));
        }
        for (int i = 0; i < observedOffsets.size(); ++i) {
            reader.VirtualSeek(observedOffsets.at(i));
            EXPECT_TRUE(CanRead(reader, r, i));
        }
    }

    // compare data in new PBI file, to expected data
    const PbiRawData& expectedIndex = tests::Test2Bam_NewIndex();
    const PbiRawData& fromBuilt = PbiRawData(tempPbiFn);
    tests::ExpectRawIndicesEqual(expectedIndex, fromBuilt);

    // straight diff of newly-generated PBI file to existing PBI
    // TODO: Come back to this once pbindexump is in place.
    //       We can't exactly do this since file offsets may differ between 2 BAMs of differing compression levels.
    //       Should add some sort of BAM checksum based on contents, not just size, for this reason.
//    const string pbiDiffCmd = string("diff -q ") + test2BamFn + ".pbi " + tempPbiFn;
//    EXPECT_EQ(0, system(pbiDiffCmd.c_str()));

    // clean up temp file(s)
    remove(tempBamFn.c_str());
    remove(tempPbiFn.c_str());
}
void insertAttackInSemiGlobalHist(node_no attack, vector<nodes_map>& history, state q)
{
    pair<node_no,defence> pair_ (attack,success);
    history.at(q).insert(pair_);
}
void insertAttackInLocalHist(unsigned int attack_index, vector<vector<bool> >& history, state q_i)
{
    history.at(q_i).at(attack_index) = true;
}
Example #4
0
JNIEXPORT void JNICALL Java_com_jxgrabkey_JXGrabKey_listen(JNIEnv *_env,
		jobject _obj) {

	if (debug) {
		ostringstream sout;
		sout << "++ listen()";
		printToDebugCallback(_env, sout.str().c_str());
	}

	if (isListening) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - WARNING: already listening, aborting";
			printToDebugCallback(_env, sout.str().c_str());
		}
		return;
	}

	jclass cls = _env->FindClass("com/jxgrabkey/JXGrabKey");
	if (cls == NULL) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - ERROR: cannot find class jxgrabkey.JXGrabKey";
			printToDebugCallback(_env, sout.str().c_str());
		}
		errorInListen = true;
		return;
	}

	jmethodID mid = _env->GetStaticMethodID(cls, "fireKeyEvent", "(ILcom/jxgrabkey/JXEvent;)V");
	if (mid == NULL) {
		if (debug) {
			ostringstream sout;
			sout << "listen() - ERROR: cannot find method fireKeyEvent(JXEvent)";
			printToDebugCallback(_env, sout.str().c_str());
		}
		errorInListen = true;
		return;
	}

  for (int i = 0; i < displays.size(); ++i) {
    printf("register %s\n", displays[i].c_str()); fflush(stdout);
    if (debug) {
      ostringstream sout;
      sout << "listen() - registering for " << displays[i];
      printToDebugCallback(_env, sout.str().c_str());
    }
    dpy = XOpenDisplay(displays[i].c_str());
    dps.push_back(dpy);

    if (!dpy) {
      if (debug) {
        ostringstream sout;
        sout << "listen() - ERROR: cannot open display " << XDisplayName(
            NULL);
        printToDebugCallback(_env, sout.str().c_str());
      }
      errorInListen = true;
      return;
    }

    getOffendingModifiers(dpy);

    int ret = XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
    if (debug && !ret) {
      ostringstream sout;
      sout << "listen() - WARNING: XAllowEvents() returned false";
      printToDebugCallback(_env, sout.str().c_str());
    }

    for (int screen = 0; screen < ScreenCount(dpy); screen++) {
      ret = XSelectInput(dpy, RootWindow(dpy, screen), KeyPressMask);
      if (debug && !ret) {
        ostringstream sout;
        sout << "listen() - WARNING: XSelectInput() on screen " << std::dec
          << screen << " returned false";
        printToDebugCallback(_env, sout.str().c_str());
      }
    }
  }

	XSetErrorHandler((XErrorHandler) xErrorHandler);
	pthread_mutex_init(&x_lock, NULL);

	doListen = true;
	isListening = true;

	XEvent ev;
  Display* found;

	while (doListen) {

    found = NULL;
    while (found == NULL && doListen) {
      pthread_mutex_lock(&x_lock);
      for (int i = 0; i < dps.size(); ++i) {
        if (XPending(dps[i]) && doListen) { //Don't block on XNextEvent(), this breaks XGrabKey()!
          found = dps[i];
        }
      }
      pthread_mutex_unlock(&x_lock);
      if (!found && doListen) {
        usleep(SLEEP_TIME_MS * MICROSECOND_TO_MILLISECOND_MULTIPLICATOR);
      }
    }

		if (doListen) {
			XNextEvent(found, &ev);

			if (ev.type == KeyPress) {
				pthread_mutex_lock(&x_lock);
				for (int i = 0; i < keys.size(); i++) {
          KeyStruct& key = keys.at(i);
					ev.xkey.state &= ~(numlock_mask | capslock_mask
							| scrolllock_mask); //Filter offending modifiers
					if (ev.xkey.keycode == key.key && ev.xkey.state
							== key.mask) {
						if (debug) {
							ostringstream sout;
							sout << "listen() - received: id = " << std::dec
									<< key.id
									<< "; type = KeyPress; x11Keycode = '"
									<< XKeysymToString(XKeycodeToKeysym(found,
											ev.xkey.keycode, 0)) << "' (0x"
									<< std::hex << ev.xkey.keycode
									<< "); x11Mask = 0x" << std::hex
									<< ev.xkey.state << endl;
							printToDebugCallback(_env, sout.str().c_str());
						}

            CallbackStruct callback;
            callback.id = key.id;
            callback.point = getPoint(_env, found);
            callbacks.push_back(callback);
						break;
					}
				}
				pthread_mutex_unlock(&x_lock);

        printToDebugCallback(_env, "listen() - sending callbacks");
        for (int i = 0; i < callbacks.size(); ++i) {
          _env->CallStaticVoidMethod(cls, mid, callbacks[i].id, callbacks[i].point);
        }
        callbacks.clear();
        printToDebugCallback(_env, "listen() - callbacks complete");
			}
		}
	}

  printToDebugCallback(_env, "listen() - exiting!");
	isListening = false;

	pthread_mutex_destroy(&x_lock);
  for (int i = 0; i < dps.size(); ++i)
    XCloseDisplay(dps[i]);
}
void insertAttackInGlobalHist(code attack, state p, vector<vector<codes_map>>& history, state q)
{
    pair<code,defence> pair_ (attack,success);
    history.at(q).at(p).insert(pair_);
}
Example #6
0
/**
 * Main program entry point
 * @param argc
 * @param argv
 * @return EXIT_SUCCESS (0) or EXIT_FAILURE (1)
 */
int main(int argc, char** argv) {

    // <editor-fold defaultstate="collapsed" desc="Init">
    HOGDescriptor hog; // Use standard parameters here
    hog.winSize = Size(64, 128); // Default training images size as used in paper
    // Get the files to train from somewhere
    static vector<string> positiveTrainingImages;
    static vector<string> negativeTrainingImages;
    static vector<string> validExtensions;
    validExtensions.push_back("jpg");
    validExtensions.push_back("png");
    validExtensions.push_back("ppm");
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Read image files">
    getFilesInDirectory(posSamplesDir, positiveTrainingImages, validExtensions);
    getFilesInDirectory(negSamplesDir, negativeTrainingImages, validExtensions);
    /// Retrieve the descriptor vectors from the samples
    unsigned long overallSamples = positiveTrainingImages.size() + negativeTrainingImages.size();
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Calculate HOG features and save to file">
    // Make sure there are actually samples to train
    if (overallSamples == 0) {
        printf("No training sample files found, nothing to do!\n");
        return EXIT_SUCCESS;
    }

    /// @WARNING: This is really important, some libraries (e.g. ROS) seems to set the system locale which takes decimal commata instead of points which causes the file input parsing to fail
    setlocale(LC_ALL, "C"); // Do not use the system locale
    setlocale(LC_NUMERIC,"C");
    setlocale(LC_ALL, "POSIX");

    printf("Reading files, generating HOG features and save them to file '%s':\n", featuresFile.c_str());
    float percent;
    /**
     * Save the calculated descriptor vectors to a file in a format that can be used by SVMlight for training
     * @NOTE: If you split these steps into separate steps: 
     * 1. calculating features into memory (e.g. into a cv::Mat or vector< vector<float> >), 
     * 2. saving features to file / directly inject from memory to machine learning algorithm,
     * the program may consume a considerable amount of main memory
     */ 
    fstream File;
    File.open(featuresFile.c_str(), ios::out);
    if (File.good() && File.is_open()) {
		// Remove following line for libsvm which does not support comments
        // File << "# Use this file to train, e.g. SVMlight by issuing $ svm_learn -i 1 -a weights.txt " << featuresFile.c_str() << endl;
        // Iterate over sample images
        for (unsigned long currentFile = 0; currentFile < overallSamples; ++currentFile) {
            storeCursor();
            vector<float> featureVector;
            // Get positive or negative sample image file path
            const string currentImageFile = (currentFile < positiveTrainingImages.size() ? positiveTrainingImages.at(currentFile) : negativeTrainingImages.at(currentFile - positiveTrainingImages.size()));
            // Output progress
            if ( (currentFile+1) % 10 == 0 || (currentFile+1) == overallSamples ) {
                percent = ((currentFile+1) * 100 / overallSamples);
                printf("%5lu (%3.0f%%):\tFile '%s'", (currentFile+1), percent, currentImageFile.c_str());
                fflush(stdout);
                resetCursor();
            }
            // Calculate feature vector from current image file
            calculateFeaturesFromInput(currentImageFile, featureVector, hog);
            if (!featureVector.empty()) {
                /* Put positive or negative sample class to file, 
                 * true=positive, false=negative, 
                 * and convert positive class to +1 and negative class to -1 for SVMlight
                 */
                File << ((currentFile < positiveTrainingImages.size()) ? "+1" : "-1");
                // Save feature vector components
                for (unsigned int feature = 0; feature < featureVector.size(); ++feature) {
                    File << " " << (feature + 1) << ":" << featureVector.at(feature);
                }
                File << endl;
            }
        }
        printf("\n");
        File.flush();
        File.close();
    } else {
        printf("Error opening file '%s'!\n", featuresFile.c_str());
        return EXIT_FAILURE;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Pass features to machine learning algorithm">
    /// Read in and train the calculated feature vectors
    printf("Calling SVMlight\n");
    SVMlight::getInstance()->read_problem(const_cast<char*> (featuresFile.c_str()));
    SVMlight::getInstance()->train(); // Call the core libsvm training procedure
    printf("Training done, saving model file!\n");
    SVMlight::getInstance()->saveModelToFile(svmModelFile);
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Generate single detecting feature vector from calculated SVM support vectors and SVM model">
    printf("Generating representative single HOG feature vector using svmlight!\n");
    vector<float> descriptorVector;
    vector<unsigned int> descriptorVectorIndices;
    // Generate a single detecting feature vector (v1 | b) from the trained support vectors, for use e.g. with the HOG algorithm
    SVMlight::getInstance()->getSingleDetectingVector(descriptorVector, descriptorVectorIndices);
    // And save the precious to file system
    saveDescriptorVectorToFile(descriptorVector, descriptorVectorIndices, descriptorVectorFile);
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Test detecting vector">
    printf("Testing custom detection using camera\n");
    hog.setSVMDetector(descriptorVector); // Set our custom detecting vector
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened()) { // check if we succeeded
        printf("Error opening camera!\n");
        return EXIT_FAILURE;
    }
    Mat testImage;
    while ((cvWaitKey(10) & 255) != 27) {
        cap >> testImage; // get a new frame from camera
//        cvtColor(testImage, testImage, CV_BGR2GRAY); // If you want to work on grayscale images
        detectTest(hog, testImage);
        imshow("HOG custom detection", testImage);
    }
    // </editor-fold>

    return EXIT_SUCCESS;
}
Example #7
0
void HandleDetector::refineHandleCandidatesWithDoorOutliers (vector<int> &handle_indices, vector<int> &outliers,
                                                             const geometry_msgs::Polygon &polygon,
                                                             const vector<double> &coeff, const geometry_msgs::Point32 &door_axis,
                                                             const Door& door_prior,
                                                             sensor_msgs::PointCloud& pointcloud) const
{
  // Split the remaining candidates into into clusters and remove solitary points (< euclidean_cluster_min_pts_)
  vector<vector<int> > clusters;
  findClusters (pointcloud, handle_indices, euclidean_cluster_distance_tolerance_, clusters, -1, -1, -1, 0, euclidean_cluster_min_pts_);
  ROS_DEBUG ("Number of clusters for the handle candidate points: %d.", (int)clusters.size ());

  // Copy the clusters back to the indices
  handle_indices.resize (0);
  for (unsigned int i = 0; i < clusters.size (); i++)
  {
    int old_size = handle_indices.size ();
    handle_indices.resize (old_size + clusters[i].size ());
    for (unsigned int j = 0; j < clusters[i].size (); j++)
      handle_indices.at (old_size + j) = clusters[i][j];
  }

  // Go over each cluster, fit vertical lines to get rid of the points near the door edges in an elegant manner,
  // then consider the rest as true handle candidate clusters
  vector<vector<int> > line_inliers (clusters.size ());
  vector<vector<int> > inliers (clusters.size ());
  for (int i = 0; i < (int)clusters.size (); i++)
  {
        // One method to prune point clusters would be to fit vertical lines (Z parallel) and remove their inliers
#if 0
    //fitSACOrientedLine (pointcloud, clusters[i], sac_distance_threshold_, &z_axis_, euclidean_cluster_angle_tolerance_, line_inliers[i]);
    //set_difference (clusters[i].begin (), clusters[i].end (), line_inliers[i].begin (), line_inliers[i].end (),
    //                inserter (remaining_clusters[i], remaining_clusters[i].begin ()));
#endif
    // Grow the current cluster using the door outliers
    growCurrentCluster (pointcloud, outliers, clusters[i], inliers[i], 2 * euclidean_cluster_distance_tolerance_);

    sensor_msgs::PointCloud tmp_cloud;
    cloud_geometry::projections::pointsToPlane  (pointcloud, inliers[i], tmp_cloud, coeff);
    // Fit the best horizontal line through each cluster
    fitSACOrientedLine (tmp_cloud, sac_distance_threshold_ * 2, door_axis, euclidean_cluster_angle_tolerance_, line_inliers[i]);
    for (unsigned int j = 0; j < line_inliers[i].size (); j++)
      line_inliers[i][j] = inliers[i][line_inliers[i][j]];
  }

  double best_score = -FLT_MAX;
  int best_i = -1;
  // Check the elongation of the clusters
  for (unsigned int i = 0; i < line_inliers.size (); i++)
  {
    if (line_inliers[i].size () == 0)
      continue;

    geometry_msgs::Point32 min_h, max_h, mid;
    cloud_geometry::statistics::getLargestXYPoints (pointcloud, line_inliers[i], min_h, max_h);
    mid.x = (min_h.x + max_h.x)/2.0;  mid.y = (min_h.y + max_h.y)/2.0;

    // score for line lengh
    double length = sqrt ( (min_h.x - max_h.x) * (min_h.x - max_h.x) + (min_h.y - max_h.y) * (min_h.y - max_h.y) );
    double fit = ((double)(line_inliers[i].size())) / ((double)(inliers[i].size()));
    double score = fit - 3.0 * fabs (length - 0.15);
    ROS_INFO ("  Handle line cluster %d with %d inliers, has fit %g and length %g  --> %g.", i, (int)line_inliers[i].size (), fit, length, score);

    // score for side of the door
    double dist_to_side = 0;
    if (door_prior.hinge == Door::HINGE_P1)
      dist_to_side = cloud_geometry::distances::pointToPointXYDistance(door_prior.door_p2, mid);
    else if (door_prior.hinge == Door::HINGE_P2)
      dist_to_side = cloud_geometry::distances::pointToPointXYDistance(door_prior.door_p1, mid);
    else
      ROS_ERROR("HandleDetector: Door hinge side not defined");
    if (dist_to_side > 0.3)
      score = 0;
    else
      score /= fmin(0.0001, dist_to_side);
    ROS_INFO ("  Handle is found at %f [m] from the door side", dist_to_side);

    if (score > best_score)
    {
      best_score = score;
      best_i = i;
    }
  }

  if (best_i == -1)
  {
    ROS_ERROR ("All clusters rejected!");
    // Copy the extra inliers to handle_indices
    for (unsigned int i = 0; i < inliers.size (); i++)
    {
      if (inliers[i].size () == 0)
        continue;
      int old_size = handle_indices.size ();
      handle_indices.resize (old_size + inliers[i].size ());
      for (unsigned int j = 0; j < inliers[i].size (); j++)
        handle_indices.at (old_size + j) = inliers[i][j];
    }
  }
  else
  {
    ROS_INFO ("Selecting cluster %d.", best_i);

    // Copy the intensity/curvature cluster
    handle_indices.resize (clusters[best_i].size ());
    for (unsigned int j = 0; j < clusters[best_i].size (); j++)
      handle_indices[j] = clusters[best_i][j];

    // Copy the inliers cluster
    int old_size = handle_indices.size ();
    handle_indices.resize (old_size + line_inliers[best_i].size ());
    for (unsigned int j = 0; j < line_inliers[best_i].size (); j++)
      handle_indices[old_size + j] = line_inliers[best_i][j];
  }
  sort (handle_indices.begin (), handle_indices.end ());
  handle_indices.erase (unique (handle_indices.begin (), handle_indices.end ()), handle_indices.end ());

}
Example #8
0
// Function to fill in prediceted signal values
int CalculateHypPredictions(
    PersistingThreadObjects  &thread_objects,
    const Alignment          &my_read,
    const InputStructures    &global_context,
    const vector<string>     &Hypotheses,
    const vector<bool>       &same_as_null_hypothesis,
    vector<vector<float> >   &predictions,
    vector<vector<float> >   &normalizedMeasurements,
    int flow_upper_bound) {

    // --- Step 1: Initialize Objects

	if (global_context.DEBUG > 2)
	  cout << "Prediction Generation for read " << my_read.alignment.Name << endl;

    predictions.resize(Hypotheses.size());
    normalizedMeasurements.resize(Hypotheses.size());
    // Careful: num_flows may be smaller than flow_order.num_flows()
    const ion::FlowOrder & flow_order = global_context.flow_order_vector.at(my_read.flow_order_index);
    const int & num_flows = global_context.num_flows_by_run_id.at(my_read.runid);
    int prefix_flow = 0;

    BasecallerRead master_read;
    master_read.SetData(my_read.measurements, flow_order.num_flows());
    InitializeBasecallers(thread_objects, my_read, global_context);

    // --- Step 2: Processing read prefix or solve beginning of the read if desired
    unsigned int prefix_size = 0;
    if (global_context.resolve_clipped_bases or my_read.prefix_flow < 0) {
      prefix_flow = GetStartOfMasterRead(thread_objects, my_read, global_context, Hypotheses, num_flows, master_read);
      prefix_size = master_read.sequence.size();
    }
    else {
      const string & read_prefix = global_context.key_by_read_group.at(my_read.read_group);
      prefix_size = read_prefix.length();
      for (unsigned int i_base=0; i_base < read_prefix.length(); i_base++)
        master_read.sequence.push_back(read_prefix.at(i_base));
      prefix_flow = my_read.prefix_flow;
    }

    // --- Step 3: creating predictions for the individual hypotheses

    // Compute an upper limit of flows to be simulated or solved
    if (global_context.DEBUG > 2)
      cout << "Prediction Generation: determining flow upper bound (flow_order.num_flows()=" << flow_order.num_flows() << ") as the minimum of:"
           << " flow_upper_bound " << flow_upper_bound
           << " measurement_length " << my_read.measurements_length
           << " num_flows " << num_flows << endl;
    flow_upper_bound = min(flow_upper_bound, min(my_read.measurements_length, num_flows));

    vector<BasecallerRead> hypothesesReads(Hypotheses.size());
    int max_last_flow  = 0;

    for (unsigned int i_hyp=0; i_hyp<hypothesesReads.size(); ++i_hyp) {

    	// No need to simulate if a hypothesis is equal to the read as called
    	// We get that info from the splicing module
    	if (same_as_null_hypothesis.at(i_hyp)) {
            predictions[i_hyp] = predictions[0];
            predictions[i_hyp].resize(flow_order.num_flows());
            normalizedMeasurements[i_hyp] = normalizedMeasurements[0];
            normalizedMeasurements[i_hyp].resize(flow_order.num_flows());
        } else {

            hypothesesReads[i_hyp] = master_read;

            // --- add hypothesis sequence to clipped prefix
            unsigned int i_base = 0;
            unsigned int max_bases = 2*(unsigned int)flow_order.num_flows()-prefix_size; // Our maximum allocated memory for the sequence vector
            int i_flow = prefix_flow;

            // Add bases to read object sequence
            // We add one more base beyond 'flow_upper_bound' (if available) to signal Treephaser to not even start the solver
            while (i_base<Hypotheses[i_hyp].length() and i_base<max_bases) {
              IncrementFlow(flow_order, Hypotheses[i_hyp][i_base], i_flow);
              hypothesesReads[i_hyp].sequence.push_back(Hypotheses[i_hyp][i_base]);
              if (i_flow >= flow_upper_bound) {
            	i_flow = flow_upper_bound;
                break;
              }
              i_base++;
            }

            // Find last main incorporating flow of all hypotheses
            max_last_flow = max(max_last_flow, i_flow);

            // Solver simulates beginning of the read and then fills in the remaining clipped bases
            // Above checks on flow_upper_bound and i_flow guarantee that i_flow <= flow_upper_bound <= num_flows
            thread_objects.SolveRead(my_read.flow_order_index, hypothesesReads[i_hyp], min(i_flow,flow_upper_bound), flow_upper_bound);

            // Store predictions and adaptively normalized measurements
            predictions[i_hyp].swap(hypothesesReads[i_hyp].prediction);
            predictions[i_hyp].resize(flow_order.num_flows(), 0);
            normalizedMeasurements[i_hyp].swap(hypothesesReads[i_hyp].normalized_measurements);
            normalizedMeasurements[i_hyp].resize(flow_order.num_flows(), 0);
        }
    }

    // --- verbose ---
    if (global_context.DEBUG>2)
      PredictionGenerationVerbose(Hypotheses, hypothesesReads, my_read, predictions, prefix_size, global_context);

    //return max_last_flow;
    return (max_last_flow);
}
Example #9
0
mainClass(int luminosity=5000){ // luminosity is in /pb unit



  cutname[0]="nocut";cutname[1]="Njet_4";cutname[2]="ht_500" ;cutname[3]="mht_200";
  cutname[4]="delphi";cutname[5]="iso";
  cutname[6]="CSVM_0";
  cutname[7]="CSVM_1";
  cutname[8]="CSVM_2";
  cutname[9]="CSVM_3";

  WJtype[0]="EventsWith_1RecoMuon_0RecoElectron";
  TTbartype[0]="EventsWith_1RecoMuon_0RecoElectron";



// .....................................................................................................................................................//
// WJ Section
// .....................................................................................................................................................//

  //build a vector of scale factors
  //first load the cross sections into a vector
  vector<double> WJ_xs_vec;

  WJ_xs_vec.push_back(1817.0); // HT 100-200
  WJ_xs_vec.push_back(471.6);  // HT 200-400
  WJ_xs_vec.push_back(55.61);  // HT 400-600
  WJ_xs_vec.push_back(18.81);  // HT 600-Inf

  const int wjnHT = (int) WJ_xs_vec.size();   // Total number of HT bin samples

  for(int i=1; i<=wjnHT ; i++){
    if(i==1)sprintf(tempname,"../Results/results_WJ_HT-100to200_.root");
    else if(i==2)sprintf(tempname,"../Results/results_WJ_HT-200to400_.root");
    else if(i==3)sprintf(tempname,"../Results/results_WJ_HT-400to600_.root");
    else if(i==4)sprintf(tempname,"../Results/results_WJ_HT-600toInf_.root");
    else{cout << " Error!! There are only 4 WJet ht binned sample " << endl;}
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/nocut/MHT_nocut_allEvents");
    tempvalue = (luminosity*WJ_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());

printf("Scale: %g, N: %g, Lum: %d, XS: %g \n ",tempvalue,((* (TH1D* ) file->Get(tempname)).GetEntries()),luminosity,WJ_xs_vec[i-1]);

    WJ_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "WJ normalization scale factor determination done \n " << std::endl;




//..........................................//
// main histograms like HT, MHT, ...
//..........................................//

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=wjnHT ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_WJ_HT-100to200_.root");
    else if(i==2)sprintf(tempname,"HadTauEstimation_WJ_HT-200to400_.root");
    else if(i==3)sprintf(tempname,"HadTauEstimation_WJ_HT-400to600_.root");
    else if(i==4)sprintf(tempname,"HadTauEstimation_WJ_HT-600toInf_.root");
    else{cout << " Error!! There are only 4 WJet ht binned sample " << endl;}
    WJ_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_WJ_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=WJtype.begin(); itt!=WJtype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<wjnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) WJ_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist->Scale(WJ_scalevec[i]);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);

        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("WJ main histograms stacked \n ");






// .....................................................................................................................................................//
// TTbar Section
// .....................................................................................................................................................//

  //build a vector of scale factors
  //first load the cross sections into a vector
  vector<double> TTbar_xs_vec;

  TTbar_xs_vec.push_back(806.1); // 

  const int ttbarnHT = (int) TTbar_xs_vec.size();   // Total number of HT bin samples

  for(int i=1; i<=ttbarnHT ; i++){
    if(i==1)sprintf(tempname,"../Results/results_TTbar_.root");
    else{cout << " Error!! There are only 1 TTbaret ht binned sample " << endl;}
    file = new TFile(tempname, "R");
    sprintf(tempname,"allEvents/nocut/MHT_nocut_allEvents");
    tempvalue = (luminosity*TTbar_xs_vec[i-1])/((* (TH1D* ) file->Get(tempname)).GetEntries());

printf("Scale: %g, N: %g, Lum: %d, XS: %g \n ",tempvalue,((* (TH1D* ) file->Get(tempname)).GetEntries()),luminosity,TTbar_xs_vec[i-1]);

    TTbar_scalevec.push_back(tempvalue);
  }//end of loop over HTbins 
  std::cout << "TTbar normalization scale factor determination done \n " << std::endl;
  



//..........................................//
// main histograms like HT, MHT, ...
//..........................................//

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=ttbarnHT ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_TTbar_.root");
    else{cout << " Error!! There are only 1 TTbaret ht binned sample " << endl;}
    TTbar_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_TTbar_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=TTbartype.begin(); itt!=TTbartype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();

      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<ttbarnHT ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", TTbartype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          temphist = (TH1D *) TTbar_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist->Scale(TTbar_scalevec[i]);
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("TTbar main histograms stacked \n ");



// ..................................................................................................................................................... //
// Stack main histograms from TTbar and WJet
// ..................................................................................................................................................... //

  // There are two contributors 1-TTbar and 2-WJ
  int NSamples=2;

  // A vector that contains all the samples
  vector<TFile*> sample_inputfilevec;

  THStack * tempstack2 = new THStack("stack","Binned Sample Stack");

  // Load the files to a vector 
  // These are the HT, MHT, .. variables
  for(int i=1; i<=NSamples ; i++){
    if(i==1)sprintf(tempname,"HadTauEstimation_TTbar_stacked.root");
    else if(i==2)sprintf(tempname,"HadTauEstimation_WJ_stacked.root");
    else{cout << " Error!! There are only 2 contributors! " << endl;}
    sample_inputfilevec.push_back(TFile::Open(tempname,"R"));
  }//end of loop over HTbins 

  // Stack
  delete tempstack;
  tempstack = new THStack("stack","Binned Sample Stack");
  sprintf(tempname,"HadTauEstimation_stacked.root");
  file = new TFile(tempname,"RECREATE");
  histname.clear();
  histname[0]="weight";
  histname[1]="HT";
  histname[2]="MHT";
  histname[3]="NJet";
  histname[4]="NBtag";
  histname[5]="MuonPt";
  histname[6]="MtW";

  for(map<int , string >::iterator itt=WJtype.begin(); itt!=WJtype.end();itt++){        // loop over different event types

    cdtoitt = file->mkdir((itt->second).c_str());
    cdtoitt->cd();

    for(map<int , string >::iterator it=cutname.begin(); it!=cutname.end();it++){   // loop over different cutnames

      cdtoit =  cdtoitt->mkdir((it->second).c_str());
      cdtoit->cd();


      for(int j=0; j<histname.size(); j++){                                        // loop over different histograms

        for(int i=0; i<NSamples ; i++){                                                  // loop over different HT bins

          //cout << "================================" << endl;
          //cout << "HT#: " <<i << ", WJtype: " << itt->second << ", cutname: " << it->second << ", hist#: " << j << endl;  
          sprintf(tempname,"%s/%s/%s_%s_%s",(itt->second).c_str(),(it->second).c_str(),(histname[j]).c_str(),(it->second).c_str(),(itt->second).c_str());
          tempstack2 = (THStack *) sample_inputfilevec.at(i)->Get(tempname)->Clone();
          temphist = (TH1D*)tempstack2->GetStack()->Last(); 
          
          temphist->SetFillColor(i+2);
          tempstack->Add(temphist);

        }//end of loop over HTbins 1..7

        sprintf(tempname,"%s_%s_%s",histname[j].c_str(),(it->second).c_str(),(itt->second).c_str());
        tempstack->Write(tempname);
        delete tempstack;
        tempstack = new THStack("stack","Binned Sample Stack");

      }//end of loop over histograms

    }//end of loop over cutnames 

  }//end of loop over event types

  file->Close();
  printf("All samples main histograms stacked \n ");






} // End of the constructor 
Example #10
0
void feature_search(vector<vector<float>> data, int choice)
{
	vector<int> current_set_of_features; //current set of features being considered
	vector<int> high_set; //set that gives the highest accuracy
	float high_acc = 0; //highest accuracy
	//loops through the number of columns (feature#)
	if (choice == 2)
	{
		for (int i = 1; i < data.at(0).size(); i++)
		{
			current_set_of_features.push_back(i);
		}
		//cout << "Running nearest neighbor with all features, I get an accuracy of ";
		//int filler = INFINITY;
		//high_acc = leave_one_out_cross_validation(data, current_set_of_features, 0, choice, filler);
		//cout << high_acc << "%\n" << endl;
	}
	for (int x = 1; x < data.at(0).size(); x++)
	{
		int feature_to_add_at_this_level;
		float best_so_far_accuracy = 0;
		int wrong = INFINITY;
		//loops through the number of columns (feature#)
		for (int x2 = 1; x2 < data.at(0).size(); x2++)
		{
			//first check if the current feature we are looking at has already been considered
			vector<int> intersections; //stores what intersects
			vector<int> current_feature; //store current feature#
			current_feature.push_back(x2);
			sort(current_set_of_features.begin(), current_set_of_features.end());
			set_intersection(current_set_of_features.begin(),current_set_of_features.end(),
				current_feature.begin(), current_feature.end(), 
				back_inserter(intersections)); //current feature intersects set

			if ((intersections.empty() && (choice == 1 || choice == 3)) || (choice == 2 && !intersections.empty())) //Only consider adding, if not already added
			{
				cout << "\tUsing feature(s) {";
				current_set_of_features.push_back(x2);
				printset(current_set_of_features);
				current_set_of_features.pop_back();
				float accuracy = leave_one_out_cross_validation(data, current_set_of_features, x2, choice, wrong);
				cout << " accuracy is " << accuracy << "%" << endl;
				//store only the best accuracy and best feature#
				if (accuracy > best_so_far_accuracy)
				{
					best_so_far_accuracy = accuracy;
					feature_to_add_at_this_level = x2;
				}
			}

		}
		if (choice == 2)
		{
			current_set_of_features.erase(remove(current_set_of_features.begin(), current_set_of_features.end(), feature_to_add_at_this_level), current_set_of_features.end());
			cout << "Feature set {";
			printset(current_set_of_features); 
			cout << " was best, accuracy is " << best_so_far_accuracy << "%\n" << endl;
		}
		else
		{
			current_set_of_features.push_back(feature_to_add_at_this_level);
			cout << "Feature set {";
			printset(current_set_of_features); 
			cout << " was best, accuracy is " << best_so_far_accuracy << "%\n" << endl;
		}

		//after each iteration determine if the accracy increased
		high_acc = max(high_acc, best_so_far_accuracy);
		if (high_acc == best_so_far_accuracy)
		{
			high_set = current_set_of_features;
		}
	}

	cout << "Finished search!! The best feature subset is {";
	printset(high_set);
	cout << ", which has an accuracy of " << high_acc << "%" << endl;
}
Example #11
0
void formatLong( vector<char*> &v)
{
	//get total for output
	blkcnt_t sum=0;
	totalSize(v,sum);
	//total will be based off GNU convention
	cout << "total: " << sum << endl;

	
	for(unsigned i=0;i<v.size();++i)
	{
		char *temp=new char[16];
		strcpy(temp,v.at(i));

		struct stat buf;

		if(-1==(stat(temp,&buf)))
		{
			perror("stat");
		}
		if(buf.st_mode & S_IFDIR) { cout << "d"; }
		else if(buf.st_mode & S_IFREG) {cout << "-"; }
		else if(buf.st_mode & S_IFLNK) { cout << "l";}
		
		
		//permissions
		cout <<
		((S_IRUSR & buf.st_mode) ? "r" : "-") <<
		((S_IWUSR & buf.st_mode) ? "w" : "-") << 
		((S_IXUSR & buf.st_mode) ? "x" : "-") <<
		((S_IRGRP & buf.st_mode) ? "r" : "-") <<
		((S_IWGRP & buf.st_mode) ? "w" : "-") <<
		((S_IXGRP & buf.st_mode) ? "x" : "-") <<
		((S_IROTH & buf.st_mode) ? "r" : "-") <<
		((S_IWOTH & buf.st_mode) ? "w" : "-") <<
		((S_IXOTH & buf.st_mode) ? "x" : "-") << " ";
		
		//number of links
		cout << buf.st_nlink << " ";

		//uid and gid
		struct passwd *pw;
		if(NULL==(pw=getpwuid(buf.st_uid)))
		{ 
			perror("getpwuid");
		}
		cout << pw->pw_name << " " ;

		//get group id
		struct group *grp;
		if(NULL==(grp=getgrgid(buf.st_gid)))
		{
			perror("getgrgid");
		}
		cout << grp->gr_name << " ";

		//size
		cout << setw(6) << setfill(' ') << buf.st_size << " ";
		
		//time
		//time_t mtime=buf.st_mtime;
		struct tm timep;

		map<int,string> month;
		setMap(month);

		//time(&mtime);
		if(NULL==(localtime_r(&buf.st_mtime,&timep)));
		cout << setw(3) << month[timep.tm_mon] << " ";
		cout << setw(3) << setfill(' ') << timep.tm_mday << " ";
		cout << setw(2) << setfill('0') << timep.tm_hour;
		cout << ":" << setw(2) << setfill('0') << timep.tm_min << " ";
		
	
		if(color)
		{
			if(isHiddenFile(v.at(i))) { cout << "\x1b[100m" ; }
			if(isDirectory(v.at(i))) { cout << "\x1b[34m";  }
			else if(isExecutable(v.at(i))) { cout << "\x1b[32m";  }
		}

		//filename
		cout << v.at(i);
		if(color) { cout << "\x1b[0m";  }
		cout << endl;
		delete [] temp;
	}	
		
	cout << endl;
	return;
}
TEllipse CEllipseClustring::getCentroidEllipse(vector<TEllipse> ellipses)
{
  int sumScore = 0;
  
  double sumCenterX = 0.0;
  double sumCenterY = 0.0;
  double sumSizeWidth = 0.0;
  double sumSizeHeight = 0.0;
  double sumAngle = 0.0;
  
  for(int i = 0; i < (int)ellipses.size(); i++)
  {
    sumScore += ellipses.at(i).score;
    
    sumCenterX += ellipses.at(i).center.x * ellipses.at(i).score;
    sumCenterY += ellipses.at(i).center.y * ellipses.at(i).score;
    sumSizeWidth += ellipses.at(i).boundingBox.size.width * ellipses.at(i).score;
    sumSizeHeight += ellipses.at(i).boundingBox.size.height * ellipses.at(i).score;
    sumAngle += ellipses.at(i).boundingBox.angle * ellipses.at(i).score;
  }
  
  return TEllipse(
    RotatedRect(
      Point2f(sumCenterX / sumScore, sumCenterY / sumScore), 
      Size2f(sumSizeWidth / sumScore, sumSizeHeight / sumScore), sumAngle / sumScore), 
    ellipses.at(0).points.at(ellipses.at(0).points.size() / 2));
}
Example #13
0
void printLargeDigit(unsigned i,  ostream &out, int scalingFactor){



	     vector<string> number = numbers.at(i);

	     for(int a =0; a< number.size();++a){


	    		     if(a==0)
	    		     {
	    		    	 string line ;
						 line +=number.at(a).at(0);
						 for(int k= 0 ;k < scalingFactor;++k)
							line += number.at(a).at(1);
						 line+= number.at(a).at(2);
						 cout << line<<endl;

	    		     }
	    		     if(a==1)
					 {
						 string line ;
						 line +=number.at(a).at(0);
						 for(int k= 0 ;k < scalingFactor;++k)
							line += number.at(a).at(1);
						 line+= number.at(a).at(2);
						 for(int k= 0 ;k < scalingFactor;++k)
							 cout << line<<endl;


					 }
	    		     if(a==2)
					 {
						 string line ;
						 line +=number.at(a).at(0);
						 for(int k= 0 ;k < scalingFactor;++k)
							line += number.at(a).at(1);
						 line+= number.at(a).at(2);
						 cout << line<<endl;

					 }
	    		     if(a==3)
					 {
						 string line ;
						 line +=number.at(a).at(0);
						 for(int k= 0 ;k < scalingFactor;++k)
							line += number.at(a).at(1);
						 line+= number.at(a).at(2);
						 for(int k= 0 ;k < scalingFactor;++k)
						    cout << line<<endl;

					 }
	    		     if(a==4)
					 {
						 string line ;
						 line +=number.at(a).at(0);
						 for(int k= 0 ;k < scalingFactor;++k)
							line += number.at(a).at(1);
						 line+= number.at(a).at(2);
						 cout << line;

					 }



	    	   }
 };
Example #14
0
int main(int argc,char * argv[]){
    extern vector<string>values;
    char currPath[100];
    char rootPath[100];
    struct stat st_buff;
    Database * dirDb;
    getcwd(currPath,sizeof(currPath));
    getRoot(rootPath,currPath);
    string dbPath = string(rootPath) + "syncDB.db";
    if(stat(dbPath.c_str(),&st_buff) == 0)
        dirDb = new Database(dbPath);
    string path = string(rootPath) + "status.sync";
    fstream statStream(path.c_str(),fstream::in | fstream::out | fstream::app);
    if(argv[1] == NULL)
        cout<<"No command line arguments"<<endl;
    else if(strcmp(argv[1],"init") == 0){
        dirDb = new Database(dbPath);
        if(stat(dbPath.c_str(),&st_buff) != 0){
            cout<<"Initializing for the first time.."<<endl;
        }
        else
            cout<<"Already Initialized.."<<endl;

        cout<<dirDb->createTable("CREATE TABLE IF NOT EXISTS syncedDir (removable_dsk_path TEXT,local_path TEXT,PRIMARY KEY(removable_dsk_path,local_path))");

    }
    else if(strcmp(argv[1],"sync") == 0 && argv[2] != NULL && argv[3] != NULL){
        isInitialized(dbPath);
        string src = string(argv[3]);
        string target = string(currPath) + "/" + string(argv[2]);
        if(!(isDirectory(src) && isDirectory(target))){
            cout<<"These Paths are not directory"<<endl;
            return 0;
        }
        string sql = "INSERT INTO syncedDir (removable_dsk_path,local_path) VALUES ('" + target + "','" + src + "')";
        if(!dirDb->insertTable(sql))
            return 0;
        syncPaths(target,src);
        writeDIff(statStream);
        viewChanges();
    }
    else if(strcmp(argv[1],"update") == 0){
        isInitialized(dbPath);
        if(isEmpty(statStream)){
            cout<<"Run status command first"<<endl;
            return 0;
        }
        if(strcmp(argv[2],"all") == 0){
            extractFromFile(statStream);
            makeChanges();
            remove(path.c_str());
        }
        else{
            struct stat st_buff;
            if(argv[2][strlen(argv[2])-1] == '/')
                return 0;

            if(stat(argv[2],&st_buff) == 0){
                string sPath = string(argv[2]);
                string tPath = sPath.substr(0,sPath.find_last_of("/"));
                string command = "cp -aur " + sPath + " " + tPath;
                //cout<<sPath<<endl<<tPath<<endl;
                system(command.c_str());
            }
            else
                return 0;
        }
    }
    else if(strcmp(argv[1],"status") == 0){
        isInitialized(dbPath);
        if(!isEmpty(statStream)){
            extractFromFile(statStream);
            viewChanges();
        }
        else{
            dirDb->selectTable("SELECT * FROM syncedDir");
            for(int i = 0;i<values.size();i++){
                syncPaths(values.at(i),values.at(i+1));
                i++;
            }
            writeDIff(statStream);
            viewChanges();
        }
    }
    else if(strcmp(argv[1],"view")==0){
        dirDb->selectTable("SELECT * FROM syncedDir");
        cout<<"Synced Paths"<<endl;
        for(int i = 0;i<values.size();i++){
            cout<<values.at(i)<<" -- " <<values.at(i+1)<<endl;
            i++;
        }
    }
    else if(strcmp(argv[1],"remove") == 0 && argv[2] != NULL && argv[3] != NULL){
        string removablePath = string(argv[2]);
        string localPath = string(argv[3]);
        string sql = "DELETE FROM syncedDir where removable_dsk_path = '" + removablePath + "' and local_path = '" + localPath + "'";
        if(dirDb->selectTable(sql))
            cout<<"Removal complete"<<endl;
        else
            cout<<"Error Occured"<<endl;
    }
    else{
        cout<<"Invalid command"<<endl;
    }
    return 0;
}
Example #15
0
int stemRawText(vector<string> inputFiles, string inDir) {
	int INC = 50;
	int i_max = INC;
	static char * s;
	s = (char *) malloc(i_max + 1);
	char docID_ar[DOCNO_MAX_DIGITS];
	int docID = 0;
	FILE * fout = fopen("docInfo.dat","w");
	if(!fout) {
		cout << "Failed to load the docInfo.dat file...exiting now" << endl;
		return 1;
	}

	//doStemming(fileName.c_str(), "tmp");
	struct stemmer * z = create_stemmer();
	for(unsigned n = 0; n < inputFiles.size(); n++) {
	// Do not consider the deafult directories
		if((inputFiles.at(n).compare(".") != 0) && (inputFiles.at(n).compare("..") != 0)) {
			string absFileName = inDir + "/" + inputFiles.at(n);
			//cout << absFileName << endl;
			FILE * f = fopen(absFileName.c_str(),"r");
			if(!f) {
				cout << "Failed to load " << absFileName << "...exiting now" << endl;
				return 1;
			}
			int cPos = 0;
			while(true)
			{
				int ch = getc(f);
				cPos++;
				if (ch == EOF) {
					cPos--; 
					break;
				}
				if(LETTER(ch)) {
					ch = tolower(ch); /* forces lower case */
					ch = getc(f);
					cPos++;
					if(ch == 'N') {
						for(int pending = 3; pending > 0; pending--) {
							ch = getc(f);
							cPos++;
						}
						int j = 0;
						for(j = 0; j < DOCNO_MAX_DIGITS; j++) {
							ch = getc(f);
							cPos++;
							if(isdigit(ch)) {
								docID_ar[j] = ch;
							}
							else {
								break;
							}								
						}
						docID_ar[j] = '\0';
						docID = atoi(docID_ar);
						char * separator = "|";
						char * newline = "\n";
						// Write out to the file, then load to sqlite3 db
						fwrite(docID_ar, sizeof(char), strlen(docID_ar), fout);
						fwrite(separator, sizeof(char), strlen(separator), fout);
						fwrite(absFileName.c_str(), sizeof(char), strlen(absFileName.c_str()), fout);
						fwrite(newline, sizeof(char), strlen(newline), fout);
						break;
					}
				}
			}
			while(true)
			{  
				int ch = getc(f);
				cPos++;
				if (ch == EOF) {
					cPos--; 
					break;
				}
				if (LETTER(ch)) {
					int i = 0;
					 while(true) {
						if (i == i_max) {
							i_max += INC;
	                    			        s = (char*)realloc(s, i_max + 1);
		    				}
			    			ch = tolower(ch); /* forces lower case */
					        s[i] = ch; i++;
					        ch = getc(f);
						cPos++;
			    			if (!LETTER(ch)) { 
							ungetc(ch,f);
							cPos--;
							break;
						}
			 		}
			 		s[stem(z, s, i - 1) + 1] = 0;
					/* the previous line calls the stemmer and uses its result to
					    zero-terminate the string in s */
					if(stopwords.find(s) == stopwords.end()) { // If s not found in stopwords
						if(docList.find(s) == docList.end()) { // If s not found in docList
							string str(docID_ar);
							docList[s] = str;
						}
						else {
							string str_append(docID_ar);
							if(docList[s].length() >= str_append.length()) {
								if(/* *it != docID*/docList[s].substr(docList[s].length() - str_append.length()).compare(str_append) != 0) {
									docList[s].append("," + str_append);
								}
							}
							else {
								docList[s].append("," + str_append);
							}
						}
						if(docChar.find(s) == docChar.end()) { // If s not found in docChar
							int termPos = cPos - strlen(s);
							string termPos_s = boost::lexical_cast<string>( termPos );
							pair<int, string> p1(docID, termPos_s);
							docChar[s].push_back(p1);
						}
						else { // If s found in docChar
							int termPos = cPos - strlen(s);
							string termPos_s = boost::lexical_cast<string>( termPos );
							vector<pair<int, string> >::iterator it = docChar[s].end() - 1;
							if((*it).first == docID) {
								string p1((*it).second);
								p1.append("," + termPos_s);
								*it = make_pair(docID, p1);
							}
							else {
								pair<int, string> p1(docID, termPos_s);
								docChar[s].push_back(p1);
							}
						}
					}
				}
				else {
					//putchar(ch);
				}
			}
			fclose(f);
		}
	}

	FILE * findex = fopen("invertedIndex.dat","w");
	if(!findex) {
		cout << "Failed to load the invertedIndex.dat file...exiting now" << endl;
		return 1;
	}
	cout << "Generating the invertedIndex data..." << endl;
	for(map<string, vector<pair<int, string> > >::iterator it = docChar.begin(); it != docChar.end(); ++it) {
		string term = (*it).first;
		string outJson = term + "|{\"dI\":[" + docList[term] + "],\"dC\":{";
//		std::cout << (*it).first << "" term " << endl;
		for(std::vector<pair<int,string> >::iterator it1 = (*it).second.begin() ; it1 != (*it).second.end(); ++it1) {
			string docid_s = boost::lexical_cast<string>((*it1).first);
			const char* postingList = (*it1).second.c_str();
			int num = 0;
			for (unsigned int curr = 0; curr < strlen(postingList); curr++) {
				if(postingList[curr] == ',') {
					num++;
				}
			}
			string num_s = boost::lexical_cast<string>(num + 1);
			string str_append = "\"" + docid_s + "\":{\"l\":" + num_s + ",\"p\":[" + (*it1).second + "]}";
			outJson.append(str_append);
			if(it1 != (*it).second.end() - 1) {outJson.append(",");}
			//std::cout << (*it1).first << " " << (*it1).second << endl;
		}
		outJson.append("}}\n");
		fwrite(outJson.c_str(), sizeof(char), strlen(outJson.c_str()), findex);
//		cout << outJson << endl;
	}
	fclose(findex);
	//std::cout << '\n';
	fclose(fout);
	return 0;
}
Example #16
0
 void dumpData() {
     for (unsigned int i = 0; i != values.size(); ++i) {
         cout << "#" << i << ": " << types.at(i) << ", " << values.at(i) << endl;
     }
     cout << "Dump complete\n";
 }
Example #17
0
bool Tensor::ComputeJointSVD(Tensor& Umat, vector<Tensor*>& extra_tensor_list, vector<int>& mult_modes, int nonzerovals)
{
	int num_sing_vals = (int)pow((double)nonzerovals, (double)mult_modes.size());

	vector<int> free_modes;
	vector<int> mult_dims;
	vector<int> free_dims;
	vector<int> mult_offsets;
	vector<int> free_offsets;

	Tensor& temp_tensor = *(extra_tensor_list.at(0));
	VectorPlus::SetDiff(free_modes, temp_tensor.Modes(), mult_modes);  
	VectorPlus::Subset(mult_dims, temp_tensor.Dims(), mult_modes);
	VectorPlus::CSubset(free_dims, temp_tensor.Dims(), mult_modes);
	ComputeOffsets(mult_offsets, mult_dims);
	ComputeOffsets(free_offsets, free_dims);

	vector<int> usmalldims(free_modes.size(), nonzerovals);
	vector<int> udims;
	vector<int> usmall_offsets;
	ComputeOffsets(usmall_offsets, usmalldims);

	int numMultElements = VectorPlus::Product(mult_dims);
	int numFreeElements = VectorPlus::Product(free_dims);

	assert(numMultElements == numFreeElements);

    Eigen::MatrixXd matricized_tensor(numFreeElements,extra_tensor_list.size() * numMultElements);

	//cout << "copy start 1\n";
	for (int z = 0; z < extra_tensor_list.size(); ++z)
	{
		int z_offset = z * numMultElements;
		FastIndexer i_indexer(free_dims);
		for (int i = 0; i < numFreeElements; ++i)
		{
			vector<int>& free_indices = i_indexer.GetNext();
		//	ComputeIndexArray(free_indices, free_offsets, i);
			FastIndexer j_indexer(mult_dims);
			for (int j = 0; j < numMultElements; ++j)
			{
				vector<int>& mult_indices = j_indexer.GetNext();
		//		ComputeIndexArray(mult_indices, mult_offsets, j);
				vector<int> total_indices;
				VectorPlus::Concat(total_indices, free_indices, mult_indices);
				matricized_tensor(i,z_offset + j) = extra_tensor_list.at(z)->At(total_indices);
			}
		}
	}
//	cout << "copy end 1\n";	
//	MatrixXd matricized_inverse = matricized_tensor.inverse();
//	cout << matricized_inverse;
//	cout << "\n";
	//compute pseudoinverse
	//cout << "svd start 1\n";	
		Eigen::JacobiSVD<Eigen::MatrixXd> svd(matricized_tensor, Eigen::ComputeFullU);
		Eigen::MatrixXd U = svd.matrixU();
	//	cout << "svd end 1\n";	
		Eigen::MatrixXd thinU = U.leftCols(num_sing_vals);
		
	
		VectorPlus::Concat(udims, free_dims, usmalldims);
		Umat.Initialize(udims);

		

		vector<int> semi_dims;
		semi_dims.push_back(thinU.rows());
		semi_dims.push_back(thinU.cols());

	//	cout << "copy start 2 \n";	
		FastIndexer i_indexer(free_dims);
		for (int i = 0; i < thinU.rows(); ++i)
		{
			vector<int>& left_indices = i_indexer.GetNext();
		//	ComputeIndexArray(left_indices, free_offsets, i);

			FastIndexer j_indexer(usmalldims);
			for (int j = 0; j < thinU.cols(); ++j)
			{
				vector<int>& right_indices = j_indexer.GetNext();
			//	ComputeIndexArray(right_indices, usmall_offsets, j);
				vector<int> indices;
				VectorPlus::Concat(indices, left_indices, right_indices);
			//	Umat.Set(indices, rand_matrix.At(i,j));
				Umat.Set(indices, thinU(i,j));

				if (thinU.rows() == thinU.cols())
				{
					if (VectorPlus::Equals(left_indices, right_indices))
					{	
						Umat.Set(indices, 1);
					}
					else
					{
						Umat.Set(indices, 0);
					}
				}
			}
		}
	//	cout << "copy end 2 \n";	
	return true;
}
Example #18
0
 void setData(int pos, int tokenType, long double value) {
     types.at(pos) = tokenType;
     values.at(pos) = value;
 }
Example #19
0
bool
make_changable_training_sets_by_patent(const list <const Record*> & record_pointers,
                                       const vector<string >& blocking_column_names,
                                       const vector < const StringManipulator *> & pstring_oper,
                                       const unsigned int limit,
                                       const vector <string> & training_filenames) {

    if ( training_filenames.size() != 2 )
        throw cException_Other("Training: there should be 2 changeable training sets.");


    const bool is_coauthor_active = cCoauthor::static_is_comparator_activated();
    const bool is_class_active = cClass::static_is_comparator_activated();

    if ( ! is_coauthor_active )
        cCoauthor::static_activate_comparator();

    if ( ! is_class_active )
        cClass::static_activate_comparator();

    const string uid_identifier = cUnique_Record_ID::static_get_class_name();
    cBlocking_For_Training bft (record_pointers, blocking_column_names, pstring_oper, uid_identifier, limit);

    StringRemainSame donotchange;
    vector <const StringManipulator*> t_extract_equal, t_extract_nonequal, x_extract_equal, x_extract_nonequal;
    x_extract_equal.push_back(& donotchange);
    x_extract_nonequal.push_back(& donotchange);
    x_extract_nonequal.push_back(&donotchange);

    std::ofstream outfile;
    //xset01
    const string xset01_equal_name_array[] = {cApplyYear::static_get_class_name() };
    const string xset01_nonequal_name_array[] = { cAsgNum::static_get_class_name(), cCity::static_get_class_name() };
    const vector <string> xset01_equal_name_vec (xset01_equal_name_array, xset01_equal_name_array + sizeof(xset01_equal_name_array)/sizeof(string));
    const vector <string> xset01_nonequal_name_vec (xset01_nonequal_name_array, xset01_nonequal_name_array + sizeof(xset01_nonequal_name_array)/sizeof(string));



    bft.create_set(&cBlocking_For_Training::create_xset01_on_block, xset01_equal_name_vec, x_extract_equal, xset01_nonequal_name_vec, x_extract_nonequal);
    const char * current_file = training_filenames.at(0).c_str();
    outfile.open(current_file);
    if ( ! outfile.good() )
        throw cException_File_Not_Found(current_file);
    std::cout << "Creating " << current_file << " ..." << std::endl;
    bft.print(outfile, uid_identifier);
    outfile.close();
    std::cout << "Done" << std::endl;

    // tset05
    bft.reset(blocking_column_names.size());
    const string tset05_equal_name_array[] = {};
    const string tset05_nonequal_name_array[] = {};
    const vector <string> tset05_equal_name_vec (tset05_equal_name_array, tset05_equal_name_array + sizeof(tset05_equal_name_array)/sizeof(string));
    const vector <string> tset05_nonequal_name_vec (tset05_nonequal_name_array, tset05_nonequal_name_array + sizeof(tset05_nonequal_name_array)/sizeof(string));

    bft.create_set(&cBlocking_For_Training::create_tset05_on_block, tset05_equal_name_vec, t_extract_equal, tset05_nonequal_name_vec, t_extract_nonequal );

    current_file = training_filenames.at(1).c_str();
    outfile.open(current_file);
    if ( ! outfile.good() )
        throw cException_File_Not_Found(current_file);
    std::cout << "Creating " << current_file << " ..." << std::endl;
    bft.print(outfile, uid_identifier);
    outfile.close();
    std::cout << "Done" << std::endl;

    if ( ! is_coauthor_active )
        cCoauthor::static_deactivate_comparator();

    if ( ! is_class_active )
        cClass::static_deactivate_comparator();

    return true;
}
Example #20
0
void PaperUtil::foundMarker(vector< Point2f > marker_corners, vector<vector< Point2f > > found, size_t i){
    found.at(i) = (marker_corners);
}
Example #21
0
void  HandleDetector::getHandleCandidates (const vector<int> &indices, const vector<double> &coeff,
                                           const geometry_msgs::Polygon &polygon, const geometry_msgs::Polygon &polygon_tr,
                                           Eigen::Matrix4d transformation, vector<int> &handle_indices,
                                           sensor_msgs::PointCloud& pointcloud, geometry_msgs::PointStamped& viewpoint_cloud) const
{
  // Check the points in bounds for extra geometric constraints
  handle_indices.resize (indices.size ());

  // Install the basis for a viewpoint -> point line
  vector<double> viewpoint_pt_line (6);
  viewpoint_pt_line[0] = viewpoint_cloud.point.x;
  viewpoint_pt_line[1] = viewpoint_cloud.point.y;
  viewpoint_pt_line[2] = viewpoint_cloud.point.z;

  // Remove outliers around the door margin
  geometry_msgs::Point32 tmp_p;              // Used as a temporary point
  int nr_p = 0;
  geometry_msgs::Point32 pt;
  for (unsigned int i = 0; i < indices.size (); i++)
  {
    // Transform the point onto X-Y for faster checking inside the polygonal bounds
    double distance_to_plane;
    cloud_geometry::projections::pointToPlane (pointcloud.points.at (indices.at (i)), pt, coeff, distance_to_plane);
    if (distance_to_plane < 0)
      continue;
    cloud_geometry::transforms::transformPoint (pt, tmp_p, transformation);
    if (!cloud_geometry::areas::isPointIn2DPolygon (tmp_p, polygon_tr))        // Is the point's projection inside the door ?
      continue;

    // Close to the edges (3D)
    if (cloud_geometry::distances::pointToPolygonDistanceSqr (tmp_p, polygon_tr) < distance_from_door_margin_)
      continue;

    // Check whether the line viewpoint->point intersects the polygon
    viewpoint_pt_line[3] = pointcloud.points.at (indices.at (i)).x - viewpoint_cloud.point.x;
    viewpoint_pt_line[4] = pointcloud.points.at (indices.at (i)).y - viewpoint_cloud.point.y;
    viewpoint_pt_line[5] = pointcloud.points.at (indices.at (i)).z - viewpoint_cloud.point.z;
    // Normalize direction
    double n_norm = sqrt (viewpoint_pt_line[3] * viewpoint_pt_line[3] +
                          viewpoint_pt_line[4] * viewpoint_pt_line[4] +
                          viewpoint_pt_line[5] * viewpoint_pt_line[5]);
    viewpoint_pt_line[3] /= n_norm;
    viewpoint_pt_line[4] /= n_norm;
    viewpoint_pt_line[5] /= n_norm;

    // Check for the actual intersection
    geometry_msgs::Point32 viewpoint_door_intersection;
    if (!cloud_geometry::intersections::lineWithPlaneIntersection (coeff, viewpoint_pt_line, viewpoint_door_intersection))
    {
      ROS_WARN ("Line and plane are parallel (no intersections found between the line and the plane).");
          continue;
    }
    // Transform the point onto X-Y for faster checking inside the polygonal bounds
    cloud_geometry::projections::pointToPlane (viewpoint_door_intersection, pt, coeff);
    cloud_geometry::transforms::transformPoint (pt, tmp_p, transformation);
    if (!cloud_geometry::areas::isPointIn2DPolygon (tmp_p, polygon_tr))    // Is the viewpoint<->point intersection inside the door ?
      continue;

    // Save the point indices which satisfied all the geometric criteria so far
    handle_indices[nr_p++] = indices.at (i);
      } // end loop over points
  handle_indices.resize (nr_p);    // Resize to the actual value
}
bool Pkcs7SignedData::verify(bool checkSignerCert, vector<Certificate> trustedCerts, CertPathValidatorResult **cpvr, vector<ValidationFlags> vflags)
{
	BIO *p7bio;
	bool ret;
	int rc;
	int i;
	int flags = 0;	
	X509_STORE *store = NULL;
	STACK_OF(X509) *certs = NULL;

	OpenSSL_add_all_algorithms();
	ERR_load_crypto_strings();
	
	if(checkSignerCert)
	{
		 
		i=OBJ_obj2nid(this->pkcs7->type);
		switch (i)
		{
			case NID_pkcs7_signed:
				certs = this->pkcs7->d.sign->cert;
				break;
		
			case NID_pkcs7_signedAndEnveloped:
				certs = this->pkcs7->d.signed_and_enveloped->cert;
				break;
		
			default:
				throw Pkcs7Exception(Pkcs7Exception::INVALID_TYPE, "Pkcs7SignedData::verify");				
		}
						
		//instancia store de certificados
		if(!(store = X509_STORE_new()))
		{
			throw Pkcs7Exception(Pkcs7Exception::INTERNAL_ERROR, "Pkcs7SignedData::verify");
		}
		
		//define funcao de callback
		X509_STORE_set_verify_cb_func(store, Pkcs7SignedData::callback);
		
		//define certificados confiaveis
		for(unsigned int i = 0 ;  i < trustedCerts.size(); i++)
		{
			X509_STORE_add_cert(store, trustedCerts.at(i).getX509());
		}				
		
		//define flags
		for(unsigned int i = 0 ; i < vflags.size() ; i++)
		{
			switch(vflags.at(i))
			{
				case CRL_CHECK:
					X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
					break;
				
				case CRL_CHECK_ALL:
					/*precisa por CRL_CHECK tambem, caso contrario o openssl nao verifica CRL*/
					X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
					X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK_ALL);
					break;
			}
		}
		
/*		if( (sk_X509_CRL_num(this->pkcs7->d.sign->crl) > 0) || 
				(sk_X509_CRL_num(this->pkcs7->d.signed_and_enveloped->crl) > 0 ))
		{
			//X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL); //obriga a haver uma crl para cada nivel da cadeia de certificacao
			X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK);
		}
*/	
	}
	else
	{
		flags = PKCS7_NOVERIFY;
	}
	
	p7bio = PKCS7_dataInit(this->pkcs7, NULL);
	
	rc = PKCS7_verify(this->pkcs7, certs, store, p7bio, NULL, flags);
	if (rc == 1)
	{
		ret = true;
	}
	else
	{
		//this case can be a error 
		ret = false;
		
		if(cpvr)
		{
			*cpvr = new CertPathValidatorResult(Pkcs7SignedData::cpvr);
		}
	}
	
	/*desaloca estruturas*/
	BIO_free(p7bio);
	X509_STORE_free(store);
	
	return ret;
}
Example #23
0
static void unregisterHotkey(JNIEnv* _env, int i) {
  ungrabKey(_env, keys.at(i));
  keys.erase(keys.begin() + i);
}
//COMPUTE THE COMMON TANGENT GIVEN TWO UPPER HULLS
void tangentBetweenTwoUpperHulls( int n, vector<Point> &leftHull, int m, vector<Point> &rightHull, int* t1, int* t2 )
{
    int ix1=0, ix2=0;      // search indices for upper hulls 1 and 2
	int p=n-1;
	int q=n-2;
	//int leftSentinel=round_toward_infinity;
	//int rightSentinel=round_toward_infinity;
	int pp=0;
	int qq=1;

	
	
	
	
	while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))>=0 && orient(rightHull.at(pp),leftHull.at(p),leftHull.at(q))<=0)
	{
		while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))>=0)
		{
			pp=qq; //advance pp to its successor
			qq=qq+1;  //advance qq to its successor
			if(pp==m-1)  //if pp reaches the rightmost point of the hull
				break;
		}
		while(orient(rightHull.at(pp),leftHull.at(p),leftHull.at(q))<=0)
		{
			p=q;   //advance p to its predecessor
			q=q-1;   //advance q to its predecessor
			if(p==0)  //if p reaches the leftmost point of the hull
				break;
		}
		if(pp==m-1)
			break;
		if(p==0)
		{
			while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))>=0)
		{
			pp=qq;
			qq=qq+1;
			if(pp==m-1)
				break;
		}
			break;
		}
	}
		
   		ix1 = p;
		ix2 = pp;
    
	
	*t1=ix1;
    *t2=ix2;
    return;
} // End of tangentBetweenTwoUpperHulls
void insertAttackInGlobalHist(code attack, state p, vector<vector<set<code>>>& history, state q)
{
    history.at(q).at(p).insert(attack);
}
//COMPUTE THE COMMON TANGENT GIVEN TWO LOWER HULLS
void tangentBetweenTwoLowerHulls( int n, vector<Point> &leftHull, int m, vector<Point> &rightHull, int* t1, int* t2 )
{
    int ix1=0, ix2=0;      // search indices for upper hulls 1 and 2
	int p=n-1;
	int q=n-2;
	//int leftSentinel=round_toward_infinity;
	//int rightSentinel=round_toward_infinity;
	int pp=0;
	int qq=1;

	
	
	
	
	while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))<=0 && orient(rightHull.at(pp),leftHull.at(p),leftHull.at(q))>=0)
	{
		while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))<=0)
		{
			pp=qq;
			qq=qq+1;
			if(pp==m-1)
				break;
		}
		while(orient(rightHull.at(pp),leftHull.at(p),leftHull.at(q))>=0)
		{
			p=q;
			q=q-1;
			if(p==0)
				break;
		}
		if(pp==m-1)
			break;
		if(p==0)
		{
			while(orient(leftHull.at(p),rightHull.at(pp),rightHull.at(qq))<=0)
		{
			pp=qq;
			qq=qq+1;
			if(pp==m-1)
				break;
		}
			break;
		}
	}
		
   		ix1 = p;
		ix2 = pp;
    
	
	*t1=ix1;
    *t2=ix2;
    return;
} // End of tangentBetweenTwoLowerHulls
void insertBadAttackInSemiGlobalHist(node_no attack, vector<map<node_no,defence2>>& history, state q)
{
    pair<node_no,defence2> pair_ (attack,false);
    history.at(q).insert(pair_);
}
Example #28
0
string SchemaManager::toReplyTxtMerge(const vector<NotifyBodyPtr>& nbs, 
    int view, bool is_kaixin, vector<Ice::Long>& fail_nids) const {
  ctemplate::TemplateDictionary dict("table");
  int schemaid;
  for (map<string, string>::iterator i = nbs.at(0)->props.begin(); i
      != nbs.at(0)->props.end(); ++i) {
    dict.SetValue(i->first, i->second);
  }
  size_t nbsize = nbs.size();
  MCE_DEBUG("SchemaManager::toReplyTxtMergeVct --> notify body size:" 
    << nbsize << " view:" << view);
  //为uid去重
  vector<int> index;
  set<int> uniq_uids;
  for(size_t i = 0; i != nbs.size() && uniq_uids.size() <= 3; ++i) {
    int uid = nbs.at(i)->from;
    if(!uniq_uids.count(uid)) {
      uniq_uids.insert(uid);
      index.push_back(i);
    }
  }

  if (index.size() == 1) {
    ctemplate::TemplateDictionary* sub_dict = dict.AddSectionDictionary("REPLY_ONE");
    sub_dict->SetValue("from_name", nbs.at(index.at(0))->props["from_name"]);
    sub_dict->SetIntValue("from", nbs.at(index.at(0))->from);
  } else {
    ctemplate::TemplateDictionary* sub_dict;
    if(index.size() == 2){
      sub_dict = dict.AddSectionDictionary("REPLY_TWO");
    }else{
      sub_dict = dict.AddSectionDictionary("REPLY_MORE");
    }
    sub_dict->SetValue("from_name1",nbs.at(index.at(0))->props["from_name"]);
    sub_dict->SetIntValue("from1", nbs.at(index.at(0))->from);
    sub_dict->SetValue("from_name2",nbs.at(index.at(1))->props["from_name"]);
    sub_dict->SetIntValue("from2", nbs.at(index.at(1))->from);
  }

  ostringstream oss;
  Ice::Long min_nid = nbs.at(0)->id;
  for(size_t i = 0 ; i != nbs.size(); ++i){
    min_nid = min_nid < nbs.at(i)->id ? min_nid : nbs.at(i)->id;
    oss << nbs.at(i)->id;
    if(i != nbs.size()-1){
      oss << "-";
    }
  }

  dict.SetValue("nid_list", oss.str());
  dict.SetIntValue("notify_id", min_nid);
  dict.SetIntValue("source", nbs.at(0)->source);
  dict.SetIntValue("owner", nbs.at(0)->owner);
  dict.SetIntValue("from", nbs.at(0)->from);//TODO 特殊
  dict.SetIntValue("type", ((nbs.at(0)->type) & 65535));
  dict.SetIntValue("time", nbs.at(0)->time);
  //merge count only 1;
  dict.SetIntValue("unreadcount", nbs.size());
  dict.SetIntValue("unread", nbs.at(0)->unread);
  try {
    schemaid = boost::lexical_cast<int>(nbs.at(0)->props["schema_id"]);
  } catch (boost::bad_lexical_cast & e) {
    MCE_WARN("SchemaManager::toReplyTxtMergeVct --> nid:" << nbs.at(0)->id 
        << "schemaid cast exception:" << e.what());
    fail_nids.push_back(nbs.at(0)->id);
    return "";
  }
  dict.SetValue("domain", is_kaixin ? "kaixin" : "renren");
  IceUtil::RWRecMutex::RLock lock(_mutex);

  // TODO : 1) use find(). 2)leak prone?
  SchemaMap::const_iterator schema = _schema_map.find(schemaid);
  if (schema == _schema_map.end()) {
    MCE_WARN("SchemaManager::toReplyTxtMergeVct --> no schema id:" 
        << schemaid << " view:" << view);
    return "";
  }

  map<int, ctemplate::Template*>::const_iterator tpl =
      schema->second.templates.find(view);
  if (tpl == schema->second.templates.end() || tpl->second == NULL) {
    MCE_WARN("SchemaManager::toReplyTxtMergeVct --> no tpl, schemaid:" 
        << schemaid << " view:" << view);
    return "";
  }
  string notifyText;
  if(tpl->second) {
    tpl->second->Expand(&notifyText, &dict);
  } 
  if (notifyText.empty()) {
    MCE_WARN("SchemaManager::toReplyTxtMergeVct notify text empty, schemaid:" 
        << schemaid << " view:" << view << " to:" << nbs.at(0)->owner);
  }
  return notifyText;
}
bool attackIsInGlobalHist(code attack, state p, vector<vector<codes_map>>& history, state q)
{
    return (history.at(q).at(p).find(attack)) != history.at(q).at(p).end();
}
Example #30
0
int main (int argc, char** argv){

	int res = parseArgs(argc, argv);
	if (res < 0){
		printf(help());
		return res;
	}

	if (infile == "\0") {
		printf(help());
		return WRONG_INPUT_FILE;
	}

	if (outfile == "\0") {
		if (has_suffix(infile, '.' + EXT))
			outfile = split(infile, '.')[0] + '.' + (o_ext == "\0" ? "html" : o_ext);

		else if (is_markup(infile))
			outfile = split(infile, '.')[0] + '.' + EXT;

		else {
			printf(help());
			return WRONG_OUTPUT_FILE;
		}
	}
	
	vector<unsigned char> v = ftov(infile);

	if (FILE *out = fopen(outfile.c_str(), "w")) {
		string tag_name = "";
		bool quoting = false;
		for (int i = 0; i < v.size(); i++) {
			switch (v.at(i)) {
				case '(':
					mode = ARGUMENTS;
					break;
				case '{':
					if (mode != ARGUMENTS) mode = CONTENT;
					break;
				case '}':
					if (mode != ARGUMENTS) mode = NEXT_TAG;
					break;
				case ')':
					mode = EXPECT_CONTENT;
					break;
				case '"':
					if (!is_escaped(v, i) && mode != ARGUMENTS)
						mode = QUOTING_INGORE_QUOTES;
					break;
				case ' ':
				case '\n':
				case '\t':
					if (mode != ARGUMENTS)
						mode = COPY;
					break;
				default:
					if (mode != ARGUMENTS)
						mode = NEW_TAG;
			}

			switch (mode) {
				case ARGUMENTS:
					if (v.at(i) == '(' && !quoting && v.at(i + 1) != ')')
						fprintf(out, " ");
					else if (v.at(i) != '(') fprintf(out, "%c", v.at(i));
					if (v.at(i) == '"' && !is_escaped(v, i))
						quoting = !quoting;
					break;
				case CONTENT:
					mode = COPY;
					break;
				case NEXT_TAG:
					fprintf(out, "</%s>", tag_names.at(tag_names.size() - 1).c_str());
					tag_names.pop_back();
					mode = COPY;
					break;
				case EXPECT_CONTENT:
					fprintf(out, "%c", '>');
					mode = COPY;
					break;
				case NEW_TAG:
					tag_name = "";
					for (; v.at(i) != '(' && i < v.size(); i++)
						tag_name += v.at(i);

					tag_names.push_back(tag_name);
					fprintf(out, "<%s", tag_name.c_str());
					mode = COPY;
					i--;
					break;
				case QUOTING_INGORE_QUOTES:
					i++;
					while (v.at(i) != '"' || (v.at(i) == '"' && is_escaped(v, i))) {
						if (v.at(i) == '\\') {
							for (; v.at(i) == '\\'; i++);

							if (v.at(i) == '"' && is_escaped(v, i)) {
								fprintf(out, "%c", v.at(i));
								i++;
							}

							else
								for (i--; v.at(i) == '\\'; i++)
									if (is_escaped(v, i)) fprintf(out, "%c", v.at(i));
						}
						else {
							fprintf(out, "%c", v.at(i));
							i++;
						}
					}
					break;
				case COPY:
					fprintf(out, "%c", v.at(i));
					break;
			}
		}
		fclose(out);
	} else {
		printf("unable to write to output. do you have the required permissions?\n");
		return NO_PERMISSIONS;
	}

	return 0;
}